Debian Codegraph @

t/48_security_oob_utf8_gh146.t

raw ◀ ▶
1 Debian patch touch this file
•1 # Security regression test for GitHub issue #146:
2 # Out-of-bounds heap read via hand-rolled UTF-8 walker on truncated sequences.
3 #
4 # The original domParseChar() read continuation bytes for multi-byte UTF-8
5 # sequences without verifying they exist or are valid. A truncated sequence
6 # (e.g., "a\xF0") caused reads past the NUL terminator into uninitialized
7 # heap memory. This affects all DOM methods that validate node names via
8 # LibXML_test_node_name(): createElement, createAttribute, setNodeName,
9 # createElementNS, createAttributeNS, etc.
10 #
11 # Impact: denial of service (crash on unmapped memory) and potential
12 # information disclosure (reading adjacent heap allocations).
13 #
14 # Fixed by replacing the hand-rolled UTF-8 walker (domParseChar) with
15 # libxml2's own xmlValidateName(), which correctly handles all UTF-8
16 # edge cases.
17 #
18 # NOTE: This test verifies that malformed UTF-8 does not crash the process
19 # (the actual security issue). Whether a given sequence is rejected depends
20 # on the linked libxml2 version — older 2.9.x builds may accept some
21 # sequences that newer versions reject.
22
23 use strict;
24 use warnings;
25
26 use Test::More;
27 use XML::LibXML;
28
29 # Truncated UTF-8 sequences that previously caused OOB heap reads.
30 # Each entry: [ bytes, description ]
31 my @truncated_sequences = (
32 [ "a\xC0", "truncated 2-byte (leader only)" ],
33 [ "a\xC2", "truncated 2-byte (valid leader, missing continuation)" ],
34 [ "a\xE0", "truncated 3-byte (leader only)" ],
35 [ "a\xE0\x80", "truncated 3-byte (leader + 1 continuation)" ],
36 [ "a\xF0", "truncated 4-byte (leader only)" ],
37 [ "a\xF0\x80", "truncated 4-byte (leader + 1 continuation)" ],
38 [ "a\xF0\x80\x80", "truncated 4-byte (leader + 2 continuations)" ],
39 );
40
41 # Invalid continuation bytes — the leader is valid but the continuations
42 # are not 10xxxxxx.
43 my @invalid_continuations = (
44 [ "a\xC2\x41", "2-byte with ASCII continuation" ],
45 [ "a\xE0\x41\x80", "3-byte with ASCII in first continuation" ],
46 [ "a\xE0\x80\x41", "3-byte with ASCII in second continuation" ],
47 [ "a\xF0\x41\x80\x80", "4-byte with ASCII in first continuation" ],
48 [ "a\xF0\x80\x41\x80", "4-byte with ASCII in second continuation" ],
49 [ "a\xF0\x80\x80\x41", "4-byte with ASCII in third continuation" ],
50 );
51
52 my @all_bad = (@truncated_sequences, @invalid_continuations);
53
54 my @methods = qw( createElement setNodeName createElementNS
55 createAttribute createAttributeNS );
56
57 # TEST:$bad_count=13
58 # TEST:$method_count=5
59 plan tests => scalar(@all_bad) * scalar(@methods);
60
61 my $doc = XML::LibXML::Document->new();
62 my $nsURI = "http://example.com/ns";
63
64 for my $case (@all_bad) {
65 my ($bytes, $desc) = @$case;
66
67 for my $method (@methods) {
68 eval {
69 if ($method eq 'createElement') {
70 $doc->createElement($bytes);
71 }
72 elsif ($method eq 'setNodeName') {
73 my $node = $doc->createElement("tmp");
74 $node->setNodeName($bytes);
75 }
76 elsif ($method eq 'createElementNS') {
77 $doc->createElementNS($nsURI, $bytes);
78 }
79 elsif ($method eq 'createAttribute') {
80 $doc->createAttribute($bytes, "value");
81 }
82 elsif ($method eq 'createAttributeNS') {
83 $doc->createAttributeNS($nsURI, $bytes, "value");
84 }
85 };
86
87 # TEST*$bad_count*$method_count
88 pass("$method survives $desc without crashing");
89 }
90 }