From 65cc1da1ef2801e4678c7ee908214a9622efba66 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Sat, 15 Aug 2026 16:38:44 -0400 Subject: [PATCH 1/3] BUG: Keep the XML skip depth at the element that started the skip afni_xml's parser holds sixteen levels of open elements in a fixed array and refuses to push a seventeenth by entering a skip block: epush() records the depth in xd->dskip and stops touching the stack until the matching pop. Two details of that bookkeeping are wrong. epush() overwrote dskip on every push past the limit, so the skip was recorded as starting at the innermost depth. epop() then cleared dskip before the stack was handled, so the pop that ends the skip fell through to "xd->stack[xd->depth-1] = NULL" and wrote a pointer eight bytes past the sixteen-entry array, which lives in the file-scope afni_xml_control. The next pop found dskip clear and dereferenced xd->stack[16]. epush() now keeps the outermost skip depth, since that is the depth whose pop ends the skip, and epop() clears dskip only after the element has been skipped. Depths within the limit are unaffected. The XML comes from the CIFTI extension of a NIfTI file, or from any file read by axml_read_file(), so the nesting is chosen by the input. Found by fuzzing axml_read_buf() with libFuzzer under AddressSanitizer. --- cifti/afni_xml.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 45b72e52..8b838546 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -734,8 +734,9 @@ static int epush(afni_xml_control * xd, const char * ename, const char ** attr) if( xd->verb > 3 ) show_attrs(xd, attr, 1); } - /* determine whether we should go into a skip block */ - if( errs ) xd->dskip = xd->depth; + /* determine whether we should go into a skip block; keep the outermost + such depth, since that is the one whose pop ends the skip */ + if( errs && ! xd->dskip ) xd->dskip = xd->depth; /* if we are in a skip block, do nothing but monitor stack */ if( xd->dskip ) { @@ -767,16 +768,16 @@ static int epop(afni_xml_control * xd, const char * ename) if( xd->wkeep ) xd->wkeep = 0; /* clear storage continuation */ if( xd->dskip ) { - if( xd->dskip == xd->depth ) xd->dskip = 0; /* clear */ - if( xd->verb > 3 ) fprintf(stderr,"-- skip=%d, depth=%d, skipping pop element '%s'\n", xd->dskip, xd->depth, ename); + + /* clear only after the element has been skipped, so that the stack + is not touched at a depth that was never pushed onto it */ + if( xd->dskip == xd->depth ) xd->dskip = 0; } else { process_popped_element(xd, ename); - } - if( ! xd->dskip ) { xd->stack[xd->depth-1] = NULL; /* should be irrelevant */ if( xd->verb > 4 ) { From 3d620acfa577166acc9583e841b14ac669eb9675 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Devenyi" Date: Fri, 18 Sep 2026 21:59:26 -0400 Subject: [PATCH 2/3] BUG: Do not assume a popped element filled its stack slot process_popped_element() read xd->stack[xd->depth-1] and dereferenced it without a test. A slot is filled by the matching epush(), and an element that was skipped never fills one, so the read is only safe because the preceding commit keeps the skip running to the element that started it. Guard it rather than rely on that: check the depth is inside the stack, and that the slot holds a struct with a name, before the strcmp(). --- cifti/afni_xml.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cifti/afni_xml.c b/cifti/afni_xml.c index 8b838546..1c9a5956 100644 --- a/cifti/afni_xml.c +++ b/cifti/afni_xml.c @@ -794,7 +794,21 @@ static int epop(afni_xml_control * xd, const char * ename) static int process_popped_element(afni_xml_control * xd, const char * ename) { afni_xml_t * ax; + + /* a stack slot is filled by the matching epush(). An element that was + skipped never fills one, so do not assume this slot holds a struct + with a name. */ + if( xd->depth <= 0 || xd->depth > AXML_MAX_DEPTH ) { + if( gAXD.verb ) fprintf(stderr,"** pop at depth %d!\n", xd->depth); + return 1; + } + ax = xd->stack[xd->depth-1]; + if( ! ax || ! ax->name ) { + if( gAXD.verb ) fprintf(stderr,"** pop of unfilled element '%s'!\n", + ename ? ename : "NULL"); + return 1; + } if( strcmp(ename, ax->name) ) { if( gAXD.verb ) fprintf(stderr,"** pop mismatch!\n"); From c81291f8ef51fc9e3022c4b5610d8c2234d76651 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Tue, 22 Sep 2026 08:44:18 -0500 Subject: [PATCH 3/3] ENH: Cover XML nesting past the afni_xml stack limit deep_nesting.xml nests twenty elements inside a CIFTI extension whose stack holds sixteen, which is the shape that walked off the end of xd->stack. The test drives it through cifti_tool and pins that the BrainModel sibling is still summarized, so a fix that refused the whole document would not pass. --- cifti/CMakeLists.txt | 9 +++++++++ cifti/testdata/deep_nesting.xml | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100644 cifti/testdata/deep_nesting.xml diff --git a/cifti/CMakeLists.txt b/cifti/CMakeLists.txt index ec8a4307..e13e7bdf 100644 --- a/cifti/CMakeLists.txt +++ b/cifti/CMakeLists.txt @@ -50,4 +50,13 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) -eval_cext -eval_type show_summary ) set_tests_properties( ${TEST_PREFIX}_tool_unterminated_cext PROPERTIES PASS_REGULAR_EXPRESSION "MapName : demo" ) + + # Nesting past AXML_MAX_DEPTH must stay inside the skip block rather than + # walk off the fixed stack; the regex pins that the rest still parses. + add_test( NAME ${TEST_PREFIX}_deep_nesting + COMMAND $ + -as_cext -eval_cext -eval_type show_summary + -input ${CMAKE_CURRENT_LIST_DIR}/testdata/deep_nesting.xml ) + set_tests_properties( ${TEST_PREFIX}_deep_nesting + PROPERTIES PASS_REGULAR_EXPRESSION "BrainModel" ) endif() diff --git a/cifti/testdata/deep_nesting.xml b/cifti/testdata/deep_nesting.xml new file mode 100644 index 00000000..cf26ee24 --- /dev/null +++ b/cifti/testdata/deep_nesting.xml @@ -0,0 +1,8 @@ + + + + + + + +