| 1 |
From: Toddr Bot <[email protected]> |
| 2 |
Date: Tue, 19 May 2026 19:32:08 +0000 |
| 3 |
Subject: fix: replace domParseChar with xmlValidateName to prevent OOB UTF-8 |
| 4 |
read |
| 5 |
Origin: https://github.com/cpan-authors/XML-LibXML/commit/059abf5f9336e2213794b5b545c707394cca3ac7 |
| 6 |
Bug-Debian: https://bugs.debian.org/1136300 |
| 7 |
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2026-8177 |
| 8 |
|
| 9 |
domParseChar() read continuation bytes for multi-byte UTF-8 sequences without |
| 10 |
validating they exist or have required 10xxxxxx form. Truncated sequences like |
| 11 |
"a\xF0" caused heap reads past NUL terminator. Replace with libxml2's |
| 12 |
xmlValidateName(), which correctly handles all UTF-8 edge cases. |
| 13 |
|
| 14 |
Adds 65 regression tests covering truncation points, invalid continuations, and |
| 15 |
5 affected DOM entry points (createElement, createAttribute, setNodeName, etc). |
| 16 |
|
| 17 |
Fixes https://github.com/cpan-authors/XML-LibXML/issues/146 |
| 18 |
--- |
| 19 |
.gitignore | 1 + |
| 20 |
LibXML.xs | 33 +------------ |
| 21 |
MANIFEST | 1 + |
| 22 |
dom.c | 88 --------------------------------- |
| 23 |
dom.h | 32 ------------ |
| 24 |
t/48_security_oob_utf8_gh146.t | 90 ++++++++++++++++++++++++++++++++++ |
| 25 |
6 files changed, 94 insertions(+), 151 deletions(-) |
| 26 |
create mode 100644 t/48_security_oob_utf8_gh146.t |
| 27 |
|
| 28 |
diff --git a/LibXML.xs b/LibXML.xs |
| 29 |
index df23bc7d292e..6f18e89c83e8 100644 |
| 30 |
--- a/LibXML.xs |
| 31 |
+++ b/LibXML.xs |
| 32 |
@@ -1025,40 +1025,11 @@ LibXML_cleanup_parser() { |
| 33 |
int |
| 34 |
LibXML_test_node_name( xmlChar * name ) |
| 35 |
{ |
| 36 |
- xmlChar * cur = name; |
| 37 |
- int tc = 0; |
| 38 |
- int len = 0; |
| 39 |
- |
| 40 |
- if ( cur == NULL || *cur == 0 ) { |
| 41 |
- /* warn("name is empty" ); */ |
| 42 |
- return(0); |
| 43 |
- } |
| 44 |
- |
| 45 |
- tc = domParseChar( cur, &len ); |
| 46 |
- |
| 47 |
- if ( !( IS_LETTER( tc ) || (tc == '_') || (tc == ':')) ) { |
| 48 |
- /* warn( "is not a letter\n" ); */ |
| 49 |
+ if ( name == NULL || *name == 0 ) { |
| 50 |
return(0); |
| 51 |
} |
| 52 |
|
| 53 |
- tc = 0; |
| 54 |
- cur += len; |
| 55 |
- |
| 56 |
- while (*cur != 0 ) { |
| 57 |
- tc = domParseChar( cur, &len ); |
| 58 |
- |
| 59 |
- if (!(IS_LETTER(tc) || IS_DIGIT(tc) || (tc == '_') || |
| 60 |
- (tc == '-') || (tc == ':') || (tc == '.') || |
| 61 |
- IS_COMBINING(tc) || IS_EXTENDER(tc)) ) { |
| 62 |
- /* warn( "is not a letter\n" ); */ |
| 63 |
- return(0); |
| 64 |
- } |
| 65 |
- tc = 0; |
| 66 |
- cur += len; |
| 67 |
- } |
| 68 |
- |
| 69 |
- /* warn("name is ok"); */ |
| 70 |
- return(1); |
| 71 |
+ return xmlValidateName( name, 0 ) == 0; |
| 72 |
} |
| 73 |
|
| 74 |
/* Assumes that the node has a proxy. */ |
| 75 |
diff --git a/MANIFEST b/MANIFEST |
| 76 |
index 55e093298936..9427babae6fe 100644 |
| 77 |
--- a/MANIFEST |
| 78 |
+++ b/MANIFEST |
| 79 |
@@ -170,6 +170,7 @@ t/48_replaceNode_DTD_nodes_rT_80521.t |
| 80 |
t/48_rt123379_setNamespace.t |
| 81 |
t/48_rt55000.t |
| 82 |
t/48_rt93429_recover_2_in_html_parsing.t |
| 83 |
+t/48_security_oob_utf8_gh146.t |
| 84 |
t/48importing_nodes_IDs_rt_69520.t |
| 85 |
t/49_load_html.t |
| 86 |
t/49callbacks_returning_undef.t |
| 87 |
diff --git a/dom.c b/dom.c |
| 88 |
index 94518b0aea29..a93cce30d940 100644 |
| 89 |
--- a/dom.c |
| 90 |
+++ b/dom.c |
| 91 |
@@ -237,94 +237,6 @@ domReconcileNs(xmlNodePtr tree) |
| 92 |
xmlFreeNsList(unused); |
| 93 |
} |
| 94 |
|
| 95 |
-/** |
| 96 |
- * NAME domParseChar |
| 97 |
- * TYPE function |
| 98 |
- * SYNOPSIS |
| 99 |
- * int utf8char = domParseChar( curchar, &len ); |
| 100 |
- * |
| 101 |
- * The current char value, if using UTF-8 this may actually span |
| 102 |
- * multiple bytes in the given string. This function parses an utf8 |
| 103 |
- * character from a string into a UTF8 character (an integer). It uses |
| 104 |
- * a slightly modified version of libxml2's character parser. libxml2 |
| 105 |
- * itself does not provide any function to parse characters dircetly |
| 106 |
- * from a string and test if they are valid utf8 characters. |
| 107 |
- * |
| 108 |
- * XML::LibXML uses this function rather than perls native UTF8 |
| 109 |
- * support for two reasons: |
| 110 |
- * 1) perls UTF8 handling functions often lead to encoding errors, |
| 111 |
- * which partly comes, that they are badly documented. |
| 112 |
- * 2) not all perl versions XML::LibXML intends to run with have native |
| 113 |
- * UTF8 support. |
| 114 |
- * |
| 115 |
- * domParseChar() allows to use the very same code with all versions |
| 116 |
- * of perl :) |
| 117 |
- * |
| 118 |
- * Returns the current char value and its length |
| 119 |
- * |
| 120 |
- * NOTE: If the character passed to this function is not a UTF |
| 121 |
- * character, the return value will be 0 and the length of the |
| 122 |
- * character is -1! |
| 123 |
- */ |
| 124 |
-int |
| 125 |
-domParseChar( xmlChar *cur, int *len ) |
| 126 |
-{ |
| 127 |
- unsigned char c; |
| 128 |
- unsigned int val; |
| 129 |
- |
| 130 |
- /* |
| 131 |
- * We are supposed to handle UTF8, check it's valid |
| 132 |
- * From rfc2044: encoding of the Unicode values on UTF-8: |
| 133 |
- * |
| 134 |
- * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 135 |
- * 0000 0000-0000 007F 0xxxxxxx |
| 136 |
- * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 137 |
- * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 138 |
- * |
| 139 |
- * Check for the 0x110000 limit too |
| 140 |
- */ |
| 141 |
- |
| 142 |
- if ( cur == NULL || *cur == 0 ) { |
| 143 |
- *len = 0; |
| 144 |
- return(0); |
| 145 |
- } |
| 146 |
- |
| 147 |
- c = *cur; |
| 148 |
- if ( c & 0x80 ) { |
| 149 |
- if ((c & 0xe0) == 0xe0) { |
| 150 |
- if ((c & 0xf0) == 0xf0) { |
| 151 |
- /* 4-byte code */ |
| 152 |
- *len = 4; |
| 153 |
- val = (cur[0] & 0x7) << 18; |
| 154 |
- val |= (cur[1] & 0x3f) << 12; |
| 155 |
- val |= (cur[2] & 0x3f) << 6; |
| 156 |
- val |= cur[3] & 0x3f; |
| 157 |
- } else { |
| 158 |
- /* 3-byte code */ |
| 159 |
- *len = 3; |
| 160 |
- val = (cur[0] & 0xf) << 12; |
| 161 |
- val |= (cur[1] & 0x3f) << 6; |
| 162 |
- val |= cur[2] & 0x3f; |
| 163 |
- } |
| 164 |
- } else { |
| 165 |
- /* 2-byte code */ |
| 166 |
- *len = 2; |
| 167 |
- val = (cur[0] & 0x1f) << 6; |
| 168 |
- val |= cur[1] & 0x3f; |
| 169 |
- } |
| 170 |
- if ( !IS_CHAR(val) ) { |
| 171 |
- *len = -1; |
| 172 |
- return(0); |
| 173 |
- } |
| 174 |
- return(val); |
| 175 |
- } |
| 176 |
- else { |
| 177 |
- /* 1-byte code */ |
| 178 |
- *len = 1; |
| 179 |
- return((int)c); |
| 180 |
- } |
| 181 |
-} |
| 182 |
- |
| 183 |
/** |
| 184 |
* Name: domReadWellBalancedString |
| 185 |
* Synopsis: xmlNodePtr domReadWellBalancedString( xmlDocPtr doc, xmlChar *string ) |
| 186 |
diff --git a/dom.h b/dom.h |
| 187 |
index 428b685e1e7a..32936ee3ab3a 100644 |
| 188 |
--- a/dom.h |
| 189 |
+++ b/dom.h |
| 190 |
@@ -53,38 +53,6 @@ extern "C" { |
| 191 |
void |
| 192 |
domReconcileNs(xmlNodePtr tree); |
| 193 |
|
| 194 |
-/** |
| 195 |
- * NAME domParseChar |
| 196 |
- * TYPE function |
| 197 |
- * SYNOPSIS |
| 198 |
- * int utf8char = domParseChar( curchar, &len ); |
| 199 |
- * |
| 200 |
- * The current char value, if using UTF-8 this may actually span |
| 201 |
- * multiple bytes in the given string. This function parses an utf8 |
| 202 |
- * character from a string into a UTF8 character (an integer). It uses |
| 203 |
- * a slightly modified version of libxml2's character parser. libxml2 |
| 204 |
- * itself does not provide any function to parse characters dircetly |
| 205 |
- * from a string and test if they are valid utf8 characters. |
| 206 |
- * |
| 207 |
- * XML::LibXML uses this function rather than perls native UTF8 |
| 208 |
- * support for two reasons: |
| 209 |
- * 1) perls UTF8 handling functions often lead to encoding errors, |
| 210 |
- * which partly comes, that they are badly documented. |
| 211 |
- * 2) not all perl versions XML::LibXML intends to run with have native |
| 212 |
- * UTF8 support. |
| 213 |
- * |
| 214 |
- * domParseChar() allows to use the very same code with all versions |
| 215 |
- * of perl :) |
| 216 |
- * |
| 217 |
- * Returns the current char value and its length |
| 218 |
- * |
| 219 |
- * NOTE: If the character passed to this function is not a UTF |
| 220 |
- * character, the return value will be 0 and the length of the |
| 221 |
- * character is -1! |
| 222 |
- */ |
| 223 |
-int |
| 224 |
-domParseChar( xmlChar *characters, int *len ); |
| 225 |
- |
| 226 |
xmlNodePtr |
| 227 |
domReadWellBalancedString( xmlDocPtr doc, xmlChar* string, int repair ); |
| 228 |
|
| 229 |
diff --git a/t/48_security_oob_utf8_gh146.t b/t/48_security_oob_utf8_gh146.t |
| 230 |
new file mode 100644 |
| 231 |
index 000000000000..74fb6d45697d |
| 232 |
--- /dev/null |
| 233 |
+++ b/t/48_security_oob_utf8_gh146.t |
| 234 |
@@ -0,0 +1,90 @@ |
| 235 |
+# Security regression test for GitHub issue #146: |
| 236 |
+# Out-of-bounds heap read via hand-rolled UTF-8 walker on truncated sequences. |
| 237 |
+# |
| 238 |
+# The original domParseChar() read continuation bytes for multi-byte UTF-8 |
| 239 |
+# sequences without verifying they exist or are valid. A truncated sequence |
| 240 |
+# (e.g., "a\xF0") caused reads past the NUL terminator into uninitialized |
| 241 |
+# heap memory. This affects all DOM methods that validate node names via |
| 242 |
+# LibXML_test_node_name(): createElement, createAttribute, setNodeName, |
| 243 |
+# createElementNS, createAttributeNS, etc. |
| 244 |
+# |
| 245 |
+# Impact: denial of service (crash on unmapped memory) and potential |
| 246 |
+# information disclosure (reading adjacent heap allocations). |
| 247 |
+# |
| 248 |
+# Fixed by replacing the hand-rolled UTF-8 walker (domParseChar) with |
| 249 |
+# libxml2's own xmlValidateName(), which correctly handles all UTF-8 |
| 250 |
+# edge cases. |
| 251 |
+# |
| 252 |
+# NOTE: This test verifies that malformed UTF-8 does not crash the process |
| 253 |
+# (the actual security issue). Whether a given sequence is rejected depends |
| 254 |
+# on the linked libxml2 version — older 2.9.x builds may accept some |
| 255 |
+# sequences that newer versions reject. |
| 256 |
+ |
| 257 |
+use strict; |
| 258 |
+use warnings; |
| 259 |
+ |
| 260 |
+use Test::More; |
| 261 |
+use XML::LibXML; |
| 262 |
+ |
| 263 |
+# Truncated UTF-8 sequences that previously caused OOB heap reads. |
| 264 |
+# Each entry: [ bytes, description ] |
| 265 |
+my @truncated_sequences = ( |
| 266 |
+ [ "a\xC0", "truncated 2-byte (leader only)" ], |
| 267 |
+ [ "a\xC2", "truncated 2-byte (valid leader, missing continuation)" ], |
| 268 |
+ [ "a\xE0", "truncated 3-byte (leader only)" ], |
| 269 |
+ [ "a\xE0\x80", "truncated 3-byte (leader + 1 continuation)" ], |
| 270 |
+ [ "a\xF0", "truncated 4-byte (leader only)" ], |
| 271 |
+ [ "a\xF0\x80", "truncated 4-byte (leader + 1 continuation)" ], |
| 272 |
+ [ "a\xF0\x80\x80", "truncated 4-byte (leader + 2 continuations)" ], |
| 273 |
+); |
| 274 |
+ |
| 275 |
+# Invalid continuation bytes — the leader is valid but the continuations |
| 276 |
+# are not 10xxxxxx. |
| 277 |
+my @invalid_continuations = ( |
| 278 |
+ [ "a\xC2\x41", "2-byte with ASCII continuation" ], |
| 279 |
+ [ "a\xE0\x41\x80", "3-byte with ASCII in first continuation" ], |
| 280 |
+ [ "a\xE0\x80\x41", "3-byte with ASCII in second continuation" ], |
| 281 |
+ [ "a\xF0\x41\x80\x80", "4-byte with ASCII in first continuation" ], |
| 282 |
+ [ "a\xF0\x80\x41\x80", "4-byte with ASCII in second continuation" ], |
| 283 |
+ [ "a\xF0\x80\x80\x41", "4-byte with ASCII in third continuation" ], |
| 284 |
+); |
| 285 |
+ |
| 286 |
+my @all_bad = (@truncated_sequences, @invalid_continuations); |
| 287 |
+ |
| 288 |
+my @methods = qw( createElement setNodeName createElementNS |
| 289 |
+ createAttribute createAttributeNS ); |
| 290 |
+ |
| 291 |
+# TEST:$bad_count=13 |
| 292 |
+# TEST:$method_count=5 |
| 293 |
+plan tests => scalar(@all_bad) * scalar(@methods); |
| 294 |
+ |
| 295 |
+my $doc = XML::LibXML::Document->new(); |
| 296 |
+my $nsURI = "http://example.com/ns"; |
| 297 |
+ |
| 298 |
+for my $case (@all_bad) { |
| 299 |
+ my ($bytes, $desc) = @$case; |
| 300 |
+ |
| 301 |
+ for my $method (@methods) { |
| 302 |
+ eval { |
| 303 |
+ if ($method eq 'createElement') { |
| 304 |
+ $doc->createElement($bytes); |
| 305 |
+ } |
| 306 |
+ elsif ($method eq 'setNodeName') { |
| 307 |
+ my $node = $doc->createElement("tmp"); |
| 308 |
+ $node->setNodeName($bytes); |
| 309 |
+ } |
| 310 |
+ elsif ($method eq 'createElementNS') { |
| 311 |
+ $doc->createElementNS($nsURI, $bytes); |
| 312 |
+ } |
| 313 |
+ elsif ($method eq 'createAttribute') { |
| 314 |
+ $doc->createAttribute($bytes, "value"); |
| 315 |
+ } |
| 316 |
+ elsif ($method eq 'createAttributeNS') { |
| 317 |
+ $doc->createAttributeNS($nsURI, $bytes, "value"); |
| 318 |
+ } |
| 319 |
+ }; |
| 320 |
+ |
| 321 |
+ # TEST*$bad_count*$method_count |
| 322 |
+ pass("$method survives $desc without crashing"); |
| 323 |
+ } |
| 324 |
+} |
| 325 |
-- |
| 326 |
2.53.0 |
| 327 |
|