Skip to content

BUG: Free the previous filename when an ASCII header repeats the attribute - #100

Open
hjmjohnson wants to merge 2 commits into
masterfrom
fix/ascii-dup-key-leak
Open

hjmjohnson wants to merge 2 commits into
masterfrom
fix/ascii-dup-key-leak

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Re-submission of #69, reverted from master on 2026-09-24 so it can be
reviewed before merging. The fix is the original one, minus a source
comment, and now carries its regression test (moved here from #123).

Base: master. Independent: nothing has to land before it.

Commits
  • BUG: Free the previous filename when an ASCII header repeats the attribute
  • ENH: Cover the repeated-attribute path in the ASCII header reader
Test

nifti1_tool_ascii_dup_attr and nifti_tool_ascii_dup_attr read one
fixture (niftilib/testdata/dup_attr.nia) that names header_filename
and image_filename twice. The pass regex checks that the last value
wins; a fail regex on LeakSanitizer|leaked in makes the
sanitize-clang-linux leg fail when the earlier copy leaks, since a pass
regex alone makes ctest ignore the exit status.

Checked locally with an ASan build (Homebrew clang, macOS,
ASAN_OPTIONS=detect_leaks=1): with the four free() lines removed both
tests fail ("12 byte(s) leaked in 2 allocation(s)"); with them restored
both pass, and the full suite passes 346/346.

Ordering for all the re-submitted work is tracked in #84.

@hjmjohnson
hjmjohnson marked this pull request as ready for review September 24, 2026 12:17
@hjmjohnson
hjmjohnson force-pushed the fix/ascii-dup-key-leak branch from 61bed64 to 5ad36e8 Compare October 6, 2026 14:35
gdevenyi and others added 2 commits November 6, 2026 10:51
…ibute

nifti_image_from_ascii() walks the attributes of an ASCII header and
assigns the two filename fields with

    nim->fname = nifti_strdup(rhs) ;
    nim->iname = nifti_strdup(rhs) ;

Nothing stops a header from carrying header_filename or image_filename
twice, and nothing rejects the repeat, so the second assignment drops
the first string.  The input comes from a file, so the leak is
attacker-controlled in size and count: one copy per repetition.

    Direct leak of 5 byte(s) in 1 object(s) allocated from:
        #0 malloc
        #1 nifti_strdup
        #2 nifti_image_from_ascii

Freeing before the assignment costs one call and keeps the last value,
which is what the function already documented by overwriting.  The
fields are NULL until the first assignment, and free(NULL) is defined,
so no other path changes.

The same two lines exist in nifti1_io.c and are fixed there too.

Found by fuzzing nifti_image_from_ascii() with clang's libFuzzer under
AddressSanitizer.
nifti_image_from_ascii overwrote fname and iname without releasing what
they already held, so an ASCII header naming either attribute twice leaked
the earlier copy. One fixture, kept under niftilib/testdata, drives both
libraries.

The pass regex asserts the last value survives. A pass regex makes ctest
ignore the exit status, so the leak report itself is matched by a fail
regex; the sanitize-clang-linux leg, whose LeakSanitizer reports the lost
copies, is the one that turns red without the fix.
@hjmjohnson
hjmjohnson force-pushed the fix/ascii-dup-key-leak branch from 5ad36e8 to 0b852c8 Compare November 6, 2026 16:53
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants