From 6ab81d3532ff3723f0df183509fd0593b326d8ce Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:26:11 -0400 Subject: [PATCH 1/2] BUG: Swap a nifti_1_header as a NIFTI-1 header, not as whatever its magic claims swap_nifti_header() takes a void pointer and a version number, and picks the struct to swap from the version: if ( ni_ver == 0 ) nifti_swap_as_analyze((nifti_analyze75 *)hdr); else if( ni_ver == 1 ) nifti_swap_as_nifti1((nifti_1_header *)hdr); else if( ni_ver == 2 ) nifti_swap_as_nifti2((nifti_2_header *)hdr); Two callers pass it a nifti_1_header, 348 bytes, together with the version taken from that header's own magic string by NIFTI_VERSION(). A file whose magic is "n+2" therefore has 540 bytes swapped in place: 192 bytes of read and write past the end of the caller's stack object. nifti_read_n1_hdr() reads the header straight from a file, so the bytes that decide this come from the file being read. A 348 byte file with sizeof_hdr = 348, dim[0] byte-swapped, magic = "n+2" is enough; need_nhdr_swap() then reports that swapping is needed and the overflow happens before any validity check. AddressSanitizer: ERROR: AddressSanitizer: stack-buffer-overflow READ of size 1 ... #0 nifti_swap_4bytes nifti2_io.c:3051 #1 nifti_swap_as_nifti2 nifti2_io.c:3194 #2 nifti_read_n1_hdr nifti2_io.c:5394 Address ... is located in stack of thread T0 at offset 412 in frame [64, 412) 'nhdr' (line 5339) <== Memory access ... overflows nifti_convert_n1hdr2nim() has the same line and takes its header from the caller, so any application that fills a nifti_1_header itself can reach it too. Both call sites know the struct they hold, so both now ask for the swap that struct supports: analyze when the magic is absent, NIFTI-1 otherwise. Headers claiming versions 3 to 9, which swap_nifti_header() previously refused with a message and left unswapped, are now swapped as NIFTI-1 as well, which is the only interpretation 348 bytes allow. nifti1_io.c is not affected: its swap_nifti_header() takes a nifti_1_header * and a flag, so it cannot choose a wider struct. Found by fuzzing nifti_convert_n1hdr2nim() with clang's libFuzzer under AddressSanitizer. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_014m231RPDZPDbYDVxawxpjG --- nifti2/nifti2_io.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..2602a42f 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4744,7 +4744,10 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) } if( doswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d ni1 pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , ni_ver ) ; + /* nhdr is a nifti_1_header, so swap it as one. ni_ver comes from the + magic string, and a magic of "n+2" would otherwise have + swap_nifti_header() treat these 348 bytes as a 540 byte header. */ + swap_nifti_header( &nhdr , ni_ver ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr2nim : ", &nhdr); @@ -5367,7 +5370,9 @@ nifti_1_header * nifti_read_n1_hdr(const char * hname, int *swapped, int check) if( lswap ) { if ( g_opts.debug > 3 ) disp_nifti_1_header("-d nhdr pre-swap: ", &nhdr); - swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ) ; + /* only sizeof(nifti_1_header) bytes were read, so swap as that; see + the same guard in nifti_convert_n1hdr2nim() */ + swap_nifti_header( &nhdr , NIFTI_VERSION(nhdr) ? 1 : 0 ) ; } if ( g_opts.debug > 2 ) disp_nifti_1_header("-d nhdr post-swap: ", &nhdr); From bd353ead8cb2e1b188f1ae4318ecc199e1689702 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:49:03 -0400 Subject: [PATCH 2/2] BUG: Swap the tool's headers at their own width too nifti_tool.c has the same defect this branch fixes in the library: it passes NIFTI_VERSION(*nhdr), read from the magic string, to swap_nifti_header(), which selects the struct to swap by that number. act_mod_hdrs() and act_swap_hdrs() hold a nifti_1_header of 348 bytes. A magic of "n+2" makes NIFTI_VERSION() return 2, so swap_nifti_header() treats the allocation as a 540 byte nifti_2_header and reads and writes past its end. Both functions refuse a header that is valid NIFTI-2, but a header that is valid as neither version reaches the call. $ printf ... > evil.nii # 348 bytes, sizeof_hdr byte swapped, # magic "n+2" $ nifti_tool -mod_hdr -mod_field descrip hello -overwrite \ -infiles evil.nii ==2747364==ERROR: AddressSanitizer: heap-buffer-overflow #0 nifti_swap_4bytes nifti2_io.c:3029 #1 nifti_swap_as_nifti2 nifti2_io.c:3172 #2 swap_nifti_header nifti2_io.c:3131 #3 act_mod_hdrs nifti_tool.c:3389 0 bytes after 348-byte region allocated in nifti_read_n1_hdr() Both now pass 1, or 0 for ANALYZE, which are the two 348 byte layouts. act_mod_hdr2s() holds a nifti_2_header and gets the explicit 2, as act_swap_hdrs() already does for its own NIFTI-2 display path. old_swap_nifti_header() takes a nifti_1_header and a boolean, so the calls beside these need no change. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0176ZLorDY784us6dFAbE1ZJ --- nifti2/nifti_tool.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/nifti2/nifti_tool.c b/nifti2/nifti_tool.c index 7dfa3272..85bc3f54 100644 --- a/nifti2/nifti_tool.c +++ b/nifti2/nifti_tool.c @@ -3391,7 +3391,11 @@ int act_mod_hdrs( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_1_header, so swap it as one. NIFTI_VERSION() + reads the magic string, and a magic of "n+2" would otherwise + have swap_nifti_header() treat these 348 bytes as a 540 byte + header. ni_ver 0 and 1 are both 348 byte layouts. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); /* if all is well, overwrite header in fname dataset */ (void)write_hdr_to_file(nhdr, fname); /* errors printed in function */ @@ -3512,7 +3516,9 @@ int act_mod_hdr2s( nt_opts * opts ) nifti_image_free(nim); } else if ( swap ) - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* nhdr is a nifti_2_header; use the explicit version rather than + the magic, which could claim "n+1" and swap 540 bytes as 348 */ + swap_nifti_header(nhdr, 2); /* if all is well, overwrite header in fname dataset */ (void)write_hdr2_to_file(nhdr, fname); /* errors printed in function */ @@ -3613,8 +3619,12 @@ int act_swap_hdrs( nt_opts * opts ) swap_nifti_header(nhdr, 0); /* undo ANALYZE */ swap_nifti_header(nhdr, 1); /* swap NIFTI */ } else if ( opts->swap_old ) { - /* undo whichever was done and apply the old way */ - swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); + /* undo whichever was done and apply the old way. As above, + nhdr is a nifti_1_header, so it must not be swapped as a + 540 byte NIFTI-2 header just because its magic says "n+2". + old_swap_nifti_header() takes a nifti_1_header and a + boolean, so it needs no such guard. */ + swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr) ? 1 : 0); old_swap_nifti_header(nhdr, NIFTI_VERSION(*nhdr)); }