Repository navigation
BUG: Refuse a header whose dimensions overflow the voxel count - #106
Open
hjmjohnson wants to merge 8 commits into
Open
hjmjohnson wants to merge 8 commits into
hjmjohnson wants to merge 8 commits into
Conversation
hjmjohnson
marked this pull request as ready for review
September 24, 2026 12:17
hjmjohnson
force-pushed
the
fix/nvox-overflow
branch
from
October 6, 2026 14:35
f67b8fb to
da45bb3
Compare
hjmjohnson
force-pushed
the
fix/nvox-overflow
branch
from
November 6, 2026 17:00
da45bb3 to
465ced1
Compare
gdevenyi
requested changes
Nov 12, 2026
gdevenyi
left a comment
Collaborator
There was a problem hiding this comment.
Review by Google Carbon
Verdict: Request Changes
Guarding dim[] -> nvox and nvox * nbyper -> volume-size against signed/unsigned overflow in binary and .nia ASCII headers (and fixing nifti_read_ascii_image's 32-bit vsize truncation) closes a critical class of heap-overflow / OOM / bogus-extension bugs!
Four issues need to be addressed before merging:
- Merge conflict with
master: Commit 1 (5f637af, moving#ifdef REJECT_COMPLEXbelowif (nim == NULL)) already landed onmasteras75de25f. Drop5f637afwhen rebasing ontomaster. - 4 new CTests fail on 32-bit platforms (
ILP32,SIZE_MAX == 2^32 - 1):nia_volsize_trunc.nia(nx=65536, ny=65536, datatype=UINT8) hasnvox = 2^32 > UINT32_MAX, so on 32-bit bothnifti2_tool_nia_volsize_no_extsandnifti1_tool_nia_volsize_no_extsreject the header innifti_image_from_ascii(nvox > SIZE_MAX) instead of printingnum_ext = 0! Fix: Use e.g.nx='65536' ny='32768' datatype='2'(vsize = 2^31 = 2147483648), which fits in 32-bitsize_t(<= UINT32_MAX) while still overflowing signed 32-bitint(INT32_MIN) in the old buggyint vsizecode!n1_volsize.niiandnia_volsize.nia(nvox = 4 * 32767^4 > 2^62 > UINT32_MAX) trip"dim[] overflows the voxel count"on 32-bitnifti1_tool(nim->nvoxissize_t) instead of"dim[] and datatype overflow the volume size". Using e.g.nx=32767, ny=32767, nz=2, datatype=DT_FLOAT64 (8B)overflowedSIZE_MAXon 32-bit only at thenbyperstep, or accept either overflow regex on 32-bitnifti1_tool.
- Unvalidated
nim->ndiminnifti_image_from_ascii(nifti2_io.c:8941,nifti1_io.c:6809): Checksnim->dim[ii] <= 0forii = 1..7, whereasnim->ndim(nim->dim[0]) is only checked for<= 0at line 8932 — an ASCII header withndim = '99'passes right into downstream loopsfor (i = 1; i <= nim->ndim; i++)and reads pastnim->dim[8]! Checkif (nim->ndim <= 0 || nim->ndim > 7)as well. - Add Sanitizer
FAIL_REGULAR_EXPRESSIONto thePASS_REGULAR_EXPRESSIONCTests: In CTest, settingPASS_REGULAR_EXPRESSIONignores non-zero exit codes (including ASan/LSan/UBSan exit codes!) whenever stdout/stderr matches the pass regex.
hjmjohnson
force-pushed
the
fix/nvox-overflow
branch
from
November 14, 2026 15:28
465ced1 to
91fac29
Compare
Member
Author
Member
Author
|
@gdevenyi all four review points are addressed: rebased (first force-push, rebase only), then two new commits (second push, content only): aa5d71a refuses an ASCII Checks run locally
|
Both NIFTI-2 header converters multiplied the dimensions into nvox
unchecked. Seven NIFTI-1 dims of 32767 overflow int64_t, a NIFTI-2
header needs two (found with libFuzzer under UBSan):
nifti2_io.c:4838: runtime error: signed integer overflow
The wrong nvox then feeds nifti_get_volsize(), an allocation size and
a read length. Both products, and the volume size against SIZE_MAX for
32-bit targets, are now checked first; an overflowing header is refused
with a message and nifti_image_read() returns NULL. No valid image is
affected.
nifti_convert_nhdr2nim() in niftilib has the same unchecked loop on the
same untrusted path: seven dims of 32767 give nvox -1073512449, which
becomes the allocation size. It now refuses the header:
** ERROR: nifti_convert_nhdr2nim: dim[] overflows the voxel count
nvox is a size_t here, so the bound is SIZE_MAX, and nifti_get_volsize()
is size_t * size_t to match. Every dim is raised to at least 1 before
the loop, so the dim[ii] > 0 test never rejects; it guards the division.
nifti_update_dims_from_array() and update_nifti_image_for_brick_list()
compute nvox from caller-set dims, not a file header, and are left as
they are.
nifti_image_from_ascii() multiplied nx..nw from a .nia file into nvox unchecked (nifti2_io.c:8937: signed integer overflow; NIFTI-1 wraps silently). Each dim must now be positive, each step and nvox * nbyper are checked as in the binary converters, and the header is refused with a message. nifti_image_to_ascii() writes only the dims up to ndim, the rest default to 1, so nothing it writes is refused. nifti_read_ascii_image() cast the volume size to int to find the bytes left for extensions, so a 2^32 byte volume truncated to 0 and the image was read as extensions. The size is now compared in its own type, and extensions are read only when the volume fits in the file.
Four fixtures, because the guards are reached separately: a NIFTI-1 and a NIFTI-2 header whose dimensions wrap nvox, and a NIFTI-1 and a NIFTI-2 header whose nvox fits while nvox * nbyper does not. Without the guards the tool accepts all four; it reports a negative nvox for the two that wrap the voxel count. The two NIFTI-1 fixtures live in niftilib/testdata, where the NIFTI-1 library tests use them too. The regex pins which refusal fired; an exit-code test would not tell this rejection apart from any other error.
nifti_convert_nhdr2nim carries its own copy of the guards, so it needs its own tests; reverting the NIFTI-1 half alone leaves the nifti2 tests green and turns exactly these two red.
Four .nia fixtures, read by both nifti1_tool and nifti_tool: seven dimensions of 32767 (the voxel count overflows), a dimension of 0, a float64 volume whose nvox fits while nvox * nbyper does not, and a 65536 x 65536 uint8 volume followed by a valid extension block. That volume is 2^32 bytes, which an int cast truncates to 0, so without the guard the tool reads the image bytes as an extension and reports num_ext = 1. The fixtures sit in niftilib/testdata, which is always built; the NIFTI-2 tests point there.
nifti_image_from_ascii() rejected ndim <= 0 but not ndim > 7, so a .nia
header with ndim = '99' was accepted, and callers that loop to ndim
index past dim[8]:
nifti_tool.c:6913: runtime error: index 8 out of bounds
for type 'int64_t[8]'
The binary header converters already refuse that range; the ASCII
reader now does too, with a message, in both libraries. Both tools
run the new fixture.
These tests pass on a regular expression, which makes CTest ignore the exit code, so a sanitizer report printed beside the expected message would pass. Each now also fails on "runtime error", "Sanitizer", or "leaked in". With a 32-bit size_t the voxel count (NIFTI-1) or the volume size (NIFTI-2) overflows before the check a test is aimed at, so the header is refused with that earlier message. On 32-bit only, the four affected tests also accept it; 64-bit expectations are unchanged.
hjmjohnson
force-pushed
the
fix/nvox-overflow
branch
from
November 14, 2026 15:38
4d64760 to
d29fb47
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Re-submission of #68. A NIFTI-1, NIFTI-2, or ASCII (
.nia) header whosedimensions overflow the voxel count or the volume size is now refused, so
callers such as ITK get NULL from
nifti_image_read()for it instead of animage with a wrapped
nvox. Regression tests are included.Base:
master. Independent: nothing has to land before it. The twotest commits taken from #123 now live here, so #123 should drop them.
The NULL check that the
-DFSLSTYLE=ONrejection tests need landed on master separately (75de25f).Commits
library, both binary converters; the volume size is also checked
against
SIZE_MAXfor 32-bit targets)(
nifti_image_from_ascii()in both libraries; also stops the(int)cast of
nifti_get_volsize()innifti_read_ascii_image()fromreading image bytes as extensions)
ndimof99 indexed past
dim[8]in the tools)(on a 32-bit
size_tthe earlier overflow message is also accepted)Deferred
Two more
nvoxloops remain in each library, and neither reads dimensionsfrom a file header:
nifti_update_dims_from_array()(dims the caller has set; reached fromnifti_tool -cci)update_nifti_image_for_brick_list()(brick count of a caller's bricklist)
Two ASCII-reader problems that predate this change, found while reviewing it:
int(nx = '99999999999') is undefinedbehavior in the parser, before the new guard runs;
num_extis taken from the header text unchecked, and a mismatchreaches undefined behavior in
nifti_tool.Testing
Every guard was neutralized in turn; each mutant turns exactly its own
test red (12 mutants, 14 tests); the ndim tests fail with their fix
reverted. All 16 tests pass under ASan + UBSan and with gcc-16
-Wall -Wextra. The 32-bit expectations were checked by simulating theCMake branch, not by a 32-bit build.
Ordering for all the re-submitted work is tracked in #84.