Skip to content

BUG: Fix an out-of-bounds indirect call in axio_show_mim_summary - #109

Open
hjmjohnson wants to merge 3 commits into
masterfrom
pr/fix-oob-indirect-call
Open

hjmjohnson wants to merge 3 commits into
masterfrom
pr/fix-oob-indirect-call

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Fixes an out-of-bounds indirect call in axio_show_mim_summary (the guard tested the loop counter instead of the map index), frees what cifti_tool parses, and adds the first cifti tests.

Re-submission of #40, reverted from master on 2026-09-24 so it can be reviewed before merging. Base: master. Independent: nothing has to land before it.

Commits
  • BUG: Fix an out-of-bounds indirect call in axio_show_mim_summary
  • BUG: Free the XML tree and image that cifti_tool parses (now before the tests, so the Linux LeakSanitizer job is green at every commit; also frees the image on the early return)
  • ENH: Cover the MatrixIndicesMap dispatch in axio_show_mim_summary
Tests and red-green
  • cifti_mim_summary_unknown_child: fails with the guard reverted (sanitizer abort).
  • cifti_mim_summary_known_child: requires the BrainStructure attribute that only disp_brainmodel_child prints; fails when every child is skipped.
  • Both fail on any AddressSanitizer, LeakSanitizer, or UBSan report, even with exit status 0, and both fail under LeakSanitizer with the leak fix reverted.

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 pr/fix-oob-indirect-call branch from 4a52c54 to ec5ea2f Compare October 6, 2026 14:35
@hjmjohnson
hjmjohnson force-pushed the pr/fix-oob-indirect-call branch from ec5ea2f to 336afd8 Compare October 14, 2026 19:18
Comment thread cifti/cifti_tool.c Resolved
@hjmjohnson
hjmjohnson force-pushed the pr/fix-oob-indirect-call branch 3 times, most recently from 51f7943 to 64050a6 Compare November 6, 2026 15:13
@hjmjohnson

Copy link
Copy Markdown
Member Author

@greptileai review

@hjmjohnson
hjmjohnson requested a review from gdevenyi November 11, 2026 18:43

@gdevenyi gdevenyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Google Carbon

Verdict: Request Changes (minor — restore accidentally dropped review fix from 51f7943)

The bug fixes and tests themselves are spot-on:

  • cifti/afni_xml_io.c:267: if( mind >= 0 ) fixes an out-of-bounds function-pointer call (MIM_disp_funcs[-1]) whenever a valid CIFTI-2 <MatrixIndicesMap> contains <MetaData> or another element not in g_cifti_map_names.
  • cifti/cifti_tool.c:190, 197-198: Properly frees nim and ax on both error and normal return paths.
  • cifti/CMakeLists.txt:30-47: Great tests including FAIL_REGULAR_EXPRESSION for ASan/LSan/UBSan!

Why requesting changes: When 64050a6 was force-pushed to add FAIL_REGULAR_EXPRESSION, it accidentally dropped commit 51f7943 (the axml_free_xml_t caller-ownership doc comments on axio_read_buf and axio_read_file in cifti/afni_xml_io.c that @seanm requested in #discussion_r4316079036 and that were previously confirmed added). Restoring those ownership comments makes this ready to merge immediately!

Comment thread cifti/afni_xml_io.c Resolved
@hjmjohnson
hjmjohnson force-pushed the pr/fix-oob-indirect-call branch from 64050a6 to 809d82b Compare November 17, 2026 22:26
gdevenyi and others added 3 commits November 17, 2026 16:27
    mind = get_map_index(xt->xchild[kid]);
    if( kid >= 0 ) MIM_disp_funcs[mind](ofp, xt->xchild[kid], verb);

The guard tests kid, the loop counter, which is never negative, so it is
always true.  The index actually used is mind, and get_map_index()
returns -1 for any element name not in MIM_kids[].  A CIFTI file
containing an unrecognized element under MatrixIndicesMap therefore reads
a function pointer from before the start of MIM_disp_funcs and calls it.

Reported by the clang static analyzer as security.ArrayBound, "Out of
bound access to memory preceding 'MIM_disp_funcs'".
process() returned without releasing either the afni_xml_t it parsed or
the nifti_image the non-cext path fills in, so every run leaked the whole
tree. The non-cext path also leaked the image when the NIfTI part was
read but held no CIFTI extension. Both free routines already accept NULL,
so neither path needs a guard.

LeakSanitizer is on by default under AddressSanitizer on Linux but not on
Apple, so the leak ended the process with a non-zero status on the Linux
sanitizer job alone. With this commit reverted, the tests added in the
next commit, cifti_mim_summary_unknown_child and
cifti_mim_summary_known_child, both fail there with a LeakSanitizer
report.
Drives cifti_tool with -eval_type show_summary over two XML fixtures.
The first holds a MatrixIndicesMap child whose name get_map_index()
does not resolve, and fails without the fix to axio_show_mim_summary:
the Linux sanitizer job reports a global-buffer-overflow, an 8-byte read
8 bytes before MIM_disp_funcs.

The second holds a BrainModel and requires the BrainStructure attribute
that only disp_brainmodel_child prints, so that skipping the unresolved
name cannot pass by skipping every name.

Both tests fail on a sanitizer report even when the process exits with
status 0, since a pass regular expression overrides the exit status.

These are the first tests for the cifti library. The fixtures are small
enough to keep in tree, so neither needs the external testing data.
@hjmjohnson

Copy link
Copy Markdown
Member Author

First force-push (809d82b) = review fix only: the ownership comments restored; second (fdf7e14) = plain rebase on master.

@hjmjohnson
hjmjohnson force-pushed the pr/fix-oob-indirect-call branch from 809d82b to fdf7e14 Compare November 17, 2026 22:27

@seanm seanm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C code changes and comments lgtm now. I don't have much comment on the cmake changes, as I don't know cmake.

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