From 4eafd2c8623614d0dd7a027a0e2829a374b6a8e4 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:55:29 -0400 Subject: [PATCH 1/2] BUG: Refuse a header whose dimensions overflow the voxel count Both header converters multiply the dimensions into nim->nvox without checking the product: for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) nim->nvox *= nhdr.dim[ii]; Seven NIFTI-1 dimensions of 32767 are enough to overflow int64_t, and a NIFTI-2 header needs only two dimensions to do it: nifti2_io.c:4838: runtime error: signed integer overflow: 1152780773560811521 * 32767 cannot be represented in type 'int64_t' Signed overflow is undefined, and what the compiler does produce is a voxel count that no longer describes the file. nvox then goes on to nifti_get_volsize(), which multiplies it by nbyper for another unchecked product, and that result is used as an allocation size and a read length. Both products are now checked before they are made, in the way the surrounding code already reports a bad header. A header that overflows is rejected with a message instead of producing a silently wrong image. No valid image is affected: an int64_t voxel count is more than any file can hold. Found by fuzzing the header converters with clang's libFuzzer under UndefinedBehaviorSanitizer. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_014m231RPDZPDbYDVxawxpjG --- nifti2/nifti2_io.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/nifti2/nifti2_io.c b/nifti2/nifti2_io.c index 43a26f71..12cd59ab 100644 --- a/nifti2/nifti2_io.c +++ b/nifti2/nifti2_io.c @@ -4807,8 +4807,14 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -4817,6 +4823,11 @@ nifti_image* nifti_convert_n1hdr2nim(nifti_1_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; @@ -5077,8 +5088,14 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > INT64_MAX / nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -5087,6 +5104,11 @@ nifti_image* nifti_convert_n2hdr2nim(nifti_2_header nhdr, const char * fname) nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > INT64_MAX / nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ; From 191f84fd7ce05825aec3f5f1772b50859e57b2fd Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:50:37 -0400 Subject: [PATCH 2/2] BUG: Guard the voxel count in the NIFTI-1 library too The first commit guards both converters in nifti2/nifti2_io.c. niftilib/nifti1_io.c has the same loop in nifti_convert_nhdr2nim(), on the same untrusted path, and was left unguarded. $ nifti1_tool -disp_nim -infiles huge.nii # dim[0]=7, dim[1..7]=32767 dim 32 8 7 32767 32767 32767 32767 32767 32767 32767 nvox 64 1 -1073512449 The wrapped count then becomes the data allocation size and the result of nifti_get_volsize(). With the guard the header is refused: ** ERROR: nifti_convert_nhdr2nim: dim[] overflows the voxel count nifti_image.nvox is a size_t in this library and an int64_t in the NIFTI-2 one, so the bound here is SIZE_MAX rather than INT64_MAX. nifti_get_volsize() is size_t * size_t to match. dim[0] is already bounded by need_nhdr_swap(), and the loop that raises every dim to at least 1 still runs first, so the dim[ii] > 0 test never has to reject. Two more nvox loops remain in each library, in nifti_update_dims_from_array() and update_nifti_image_for_brick_list(). Both take dims that a caller has already set rather than dims read from a file, and reach them from nifti_tool's command line, so they are left for a separate change. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0176ZLorDY784us6dFAbE1ZJ --- niftilib/nifti1_io.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index 0580c629..f25d3df8 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -3766,8 +3766,15 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nim->nv = nim->dim[6] = nhdr.dim[6]; nim->nw = nim->dim[7] = nhdr.dim[7]; - for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ) + /* the product of the dimensions becomes an allocation size, so refuse + the header rather than let it wrap. nvox is a size_t here, where the + NIFTI-2 library uses int64_t, so the bound is SIZE_MAX. */ + for( ii=1, nim->nvox=1; ii <= nhdr.dim[0]; ii++ ){ + if( nhdr.dim[ii] > 0 && nim->nvox > SIZE_MAX / (size_t)nhdr.dim[ii] ){ + free(nim); ERREX("dim[] overflows the voxel count"); + } nim->nvox *= nhdr.dim[ii]; + } /**- set the type of data in voxels and how many bytes per voxel */ @@ -3776,6 +3783,11 @@ nifti_image* nifti_convert_nhdr2nim(struct nifti_1_header nhdr, nifti_datatype_sizes( nim->datatype , &(nim->nbyper) , &(nim->swapsize) ) ; if( nim->nbyper == 0 ){ free(nim); ERREX("bad datatype"); } + /* nifti_get_volsize() multiplies these two */ + if( nim->nvox > SIZE_MAX / (size_t)nim->nbyper ){ + free(nim); ERREX("dim[] and datatype overflow the volume size"); + } + /**- set the grid spacings */ nim->dx = nim->pixdim[1] = nhdr.pixdim[1] ;