|
| 1 | +From a53914fd5ca23af40d48ef248cdb42fb22ae7dd1 Mon Sep 17 00:00:00 2001 |
| 2 | +From: laserbear <10689391+Laserbear@users.noreply.github.com> |
| 3 | +Date: Sun, 8 Mar 2026 17:28:06 -0700 |
| 4 | +Subject: [PATCH 1/2] copy prefix name to pool before lookup |
| 5 | + |
| 6 | +.. so that we cannot end up with a zombie PREFIX in the pool |
| 7 | +that has NULL for a name. |
| 8 | + |
| 9 | +Co-authored-by: Sebastian Pipping <sebastian@pipping.org> |
| 10 | +--- |
| 11 | + lib/xmlparse.c | 43 +++++++++++++++++++++++++++++++++++-------- |
| 12 | + 1 file changed, 35 insertions(+), 8 deletions(-) |
| 13 | + |
| 14 | +diff --git a/lib/xmlparse.c b/lib/xmlparse.c |
| 15 | +index 88361a7..69027f6 100644 |
| 16 | +--- a/lib/xmlparse.c |
| 17 | ++++ b/lib/xmlparse.c |
| 18 | +@@ -590,6 +590,8 @@ static XML_Char *poolStoreString(STRING_POOL *pool, const ENCODING *enc, |
| 19 | + static XML_Bool FASTCALL poolGrow(STRING_POOL *pool); |
| 20 | + static const XML_Char *FASTCALL poolCopyString(STRING_POOL *pool, |
| 21 | + const XML_Char *s); |
| 22 | ++static const XML_Char *FASTCALL poolCopyStringNoFinish(STRING_POOL *pool, |
| 23 | ++ const XML_Char *s); |
| 24 | + static const XML_Char *poolCopyStringN(STRING_POOL *pool, const XML_Char *s, |
| 25 | + int n); |
| 26 | + static const XML_Char *FASTCALL poolAppendString(STRING_POOL *pool, |
| 27 | +@@ -7431,16 +7433,24 @@ setContext(XML_Parser parser, const XML_Char *context) { |
| 28 | + else { |
| 29 | + if (! poolAppendChar(&parser->m_tempPool, XML_T('\0'))) |
| 30 | + return XML_FALSE; |
| 31 | +- prefix |
| 32 | +- = (PREFIX *)lookup(parser, &dtd->prefixes, |
| 33 | +- poolStart(&parser->m_tempPool), sizeof(PREFIX)); |
| 34 | +- if (! prefix) |
| 35 | ++ const XML_Char *const prefixName = poolCopyStringNoFinish( |
| 36 | ++ &dtd->pool, poolStart(&parser->m_tempPool)); |
| 37 | ++ if (! prefixName) { |
| 38 | + return XML_FALSE; |
| 39 | +- if (prefix->name == poolStart(&parser->m_tempPool)) { |
| 40 | +- prefix->name = poolCopyString(&dtd->pool, prefix->name); |
| 41 | +- if (! prefix->name) |
| 42 | +- return XML_FALSE; |
| 43 | + } |
| 44 | ++ |
| 45 | ++ prefix = (PREFIX *)lookup(parser, &dtd->prefixes, prefixName, |
| 46 | ++ sizeof(PREFIX)); |
| 47 | ++ |
| 48 | ++ const bool prefixNameUsed = prefix && prefix->name == prefixName; |
| 49 | ++ if (prefixNameUsed) |
| 50 | ++ poolFinish(&dtd->pool); |
| 51 | ++ else |
| 52 | ++ poolDiscard(&dtd->pool); |
| 53 | ++ |
| 54 | ++ if (! prefix) |
| 55 | ++ return XML_FALSE; |
| 56 | ++ |
| 57 | + poolDiscard(&parser->m_tempPool); |
| 58 | + } |
| 59 | + for (context = s + 1; *context != CONTEXT_SEP && *context != XML_T('\0'); |
| 60 | +@@ -8029,6 +8039,23 @@ poolCopyString(STRING_POOL *pool, const XML_Char *s) { |
| 61 | + return s; |
| 62 | + } |
| 63 | + |
| 64 | ++// A version of `poolCopyString` that does not call `poolFinish` |
| 65 | ++// and reverts any partial advancement upon failure. |
| 66 | ++static const XML_Char *FASTCALL |
| 67 | ++poolCopyStringNoFinish(STRING_POOL *pool, const XML_Char *s) { |
| 68 | ++ const XML_Char *const original = s; |
| 69 | ++ do { |
| 70 | ++ if (! poolAppendChar(pool, *s)) { |
| 71 | ++ // Revert any previously successful advancement |
| 72 | ++ const ptrdiff_t advancedBy = s - original; |
| 73 | ++ if (advancedBy > 0) |
| 74 | ++ pool->ptr -= advancedBy; |
| 75 | ++ return NULL; |
| 76 | ++ } |
| 77 | ++ } while (*s++); |
| 78 | ++ return pool->start; |
| 79 | ++} |
| 80 | ++ |
| 81 | + static const XML_Char * |
| 82 | + poolCopyStringN(STRING_POOL *pool, const XML_Char *s, int n) { |
| 83 | + if (! pool->ptr && ! poolGrow(pool)) { |
| 84 | +-- |
| 85 | +2.45.4 |
| 86 | + |
| 87 | + |
| 88 | +From 777bd1a1b2196ac14cbc5efb71a3078583c8dfb2 Mon Sep 17 00:00:00 2001 |
| 89 | +From: laserbear <10689391+Laserbear@users.noreply.github.com> |
| 90 | +Date: Sun, 8 Mar 2026 17:28:06 -0700 |
| 91 | +Subject: [PATCH 2/2] test that we do not end up with a zombie PREFIX in the |
| 92 | + pool |
| 93 | + |
| 94 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 95 | +Upstream-reference: https://github.com/libexpat/libexpat/pull/1163.patch |
| 96 | +--- |
| 97 | + tests/nsalloc_tests.c | 27 +++++++++++++++++++++++++++ |
| 98 | + 1 file changed, 27 insertions(+) |
| 99 | + |
| 100 | +diff --git a/tests/nsalloc_tests.c b/tests/nsalloc_tests.c |
| 101 | +index a8f5718..d284a58 100644 |
| 102 | +--- a/tests/nsalloc_tests.c |
| 103 | ++++ b/tests/nsalloc_tests.c |
| 104 | +@@ -1505,6 +1505,32 @@ START_TEST(test_nsalloc_prefixed_element) { |
| 105 | + } |
| 106 | + END_TEST |
| 107 | + |
| 108 | ++/* Verify that retry after OOM in setContext() does not crash. |
| 109 | ++ */ |
| 110 | ++START_TEST(test_nsalloc_setContext_zombie) { |
| 111 | ++ const char *text = "<doc>Hello</doc>"; |
| 112 | ++ unsigned int i; |
| 113 | ++ const unsigned int max_alloc_count = 30; |
| 114 | ++ |
| 115 | ++ for (i = 0; i < max_alloc_count; i++) { |
| 116 | ++ g_allocation_count = (int)i; |
| 117 | ++ if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE) |
| 118 | ++ != XML_STATUS_ERROR) |
| 119 | ++ break; |
| 120 | ++ /* Retry on the same parser — must not crash */ |
| 121 | ++ g_allocation_count = ALLOC_ALWAYS_SUCCEED; |
| 122 | ++ XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE); |
| 123 | ++ |
| 124 | ++ nsalloc_teardown(); |
| 125 | ++ nsalloc_setup(); |
| 126 | ++ } |
| 127 | ++ if (i == 0) |
| 128 | ++ fail("Parsing worked despite failing allocations"); |
| 129 | ++ else if (i == max_alloc_count) |
| 130 | ++ fail("Parsing failed even at maximum allocation count"); |
| 131 | ++} |
| 132 | ++END_TEST |
| 133 | ++ |
| 134 | + void |
| 135 | + make_nsalloc_test_case(Suite *s) { |
| 136 | + TCase *tc_nsalloc = tcase_create("namespace allocation tests"); |
| 137 | +@@ -1539,4 +1565,5 @@ make_nsalloc_test_case(Suite *s) { |
| 138 | + tcase_add_test__if_xml_ge(tc_nsalloc, test_nsalloc_long_default_in_ext); |
| 139 | + tcase_add_test(tc_nsalloc, test_nsalloc_long_systemid_in_ext); |
| 140 | + tcase_add_test(tc_nsalloc, test_nsalloc_prefixed_element); |
| 141 | ++ tcase_add_test(tc_nsalloc, test_nsalloc_setContext_zombie); |
| 142 | + } |
| 143 | +-- |
| 144 | +2.45.4 |
| 145 | + |
0 commit comments