Skip to content

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

Open
hjmjohnson wants to merge 1 commit into
masterfrom
fix/ascii-dup-key-leak
Open

hjmjohnson wants to merge 1 commit into
masterfrom
fix/ascii-dup-key-leak

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Re-submission of #69, reverted from master on 2026-09-24 so it can be
reviewed before merging. Content is unchanged from the original.

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

Commits
  • BUG: Free the previous filename when an ASCII header repeats the attribute

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

…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 nifti2_io.c:1301
        #2 nifti_image_from_ascii nifti2_io.c:8874

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.

(cherry picked from commit b4c6894)
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