Skip to content

Commit 268a2e5

Browse files
committed
Fix GH-25305: NULL dereference in lxb_dom_document_destroy on lexbor document init failure
lxb_dom_document_init() set owner_document only after all of the document's memory pools were created. When one of these allocations failed, which the fallible lexbor allocators make possible once memory_limit is reached, the error path of lxb_html_document_interface_create() called lxb_dom_document_destroy() with a NULL owner_document and dereferenced it. The failure path also left dangling pointers to the destroyed pools. Set owner_document before allocating and reset the pool pointers on failure, carried as a lexbor patch until lexbor/lexbor#448 is merged. Only the source/lexbor part of the upstream change is carried. Close GH-25320
1 parent 79a4c60 commit 268a2e5

27 files changed

Lines changed: 156 additions & 30 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ PHP NEWS
3737
- DOM:
3838
. Fixed bug GH-25306 (Assertion status == LXB_STATUS_OK in dom_parse_selector
3939
when lexbor allocation fails). (ndossche)
40+
. Fixed bug GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor
41+
document init failure). (David Carlier)
4042

4143
- Filter:
4244
. Fixed stack overflow in filter_var() and filter_var_array() with deeply

‎ext/dom/tests/gh25305.phpt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor document init failure)
3+
--CREDITS--
4+
YuanchengJiang
5+
--EXTENSIONS--
6+
dom
7+
--SKIPIF--
8+
<?php
9+
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
10+
?>
11+
--INI--
12+
memory_limit=2M
13+
--FILE--
14+
<?php
15+
$str = str_repeat("a", (int)(1024 * 1024 * 1.25));
16+
Dom\HTMLDocument::createFromString("<p>x</p>");
17+
?>
18+
--EXPECTF--
19+
Fatal error: Allowed memory size of 2097152 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d%A

‎ext/lexbor/lexbor/dom/interfaces/document.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,
124124

125125
document->css = NULL;
126126

127+
/*
128+
* Set before the allocations: lxb_dom_document_destroy() frees a document
129+
* that owns itself, and dereferences the owner of any other.
130+
*/
131+
node->owner_document = document;
132+
127133
/* For nodes */
128134
document->mraw = lexbor_mraw_create();
129135
status = lexbor_mraw_init(document->mraw, (4096 * 8));
@@ -166,18 +172,17 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,
166172
goto failed;
167173
}
168174

169-
node->owner_document = document;
170-
171175
return LXB_STATUS_OK;
172176

173177
failed:
174178

175-
lexbor_mraw_destroy(document->mraw, true);
176-
lexbor_mraw_destroy(document->text, true);
177-
lexbor_hash_destroy(document->tags, true);
178-
lexbor_hash_destroy(document->ns, true);
179-
lexbor_hash_destroy(document->attrs, true);
180-
lexbor_hash_destroy(document->prefix, true);
179+
/* Reset, so that lxb_dom_document_destroy() does not free them again. */
180+
document->mraw = lexbor_mraw_destroy(document->mraw, true);
181+
document->text = lexbor_mraw_destroy(document->text, true);
182+
document->tags = lexbor_hash_destroy(document->tags, true);
183+
document->ns = lexbor_hash_destroy(document->ns, true);
184+
document->attrs = lexbor_hash_destroy(document->attrs, true);
185+
document->prefix = lexbor_hash_destroy(document->prefix, true);
181186

182187
return LXB_STATUS_ERROR;
183188
}

‎ext/lexbor/lexbor/html/interfaces/document.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ lxb_html_document_parser_prepare(lxb_html_document_t *document)
278278
status = lxb_html_parser_init(doc->parser);
279279

280280
if (status != LXB_STATUS_OK) {
281-
lxb_html_parser_destroy(doc->parser);
281+
doc->parser = lxb_html_parser_destroy(doc->parser);
282282
return status;
283283
}
284284

‎ext/lexbor/lexbor/html/parser.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
4040
parser->tkz = lxb_html_tokenizer_create();
4141
lxb_status_t status = lxb_html_tokenizer_init(parser->tkz);
4242

43+
/*
44+
* A tokenizer or a tree that failed to initialize has no reference yet,
45+
* so lxb_html_parser_destroy() could not release it: destroy it here.
46+
*/
4347
if (status != LXB_STATUS_OK) {
48+
parser->tkz = lxb_html_tokenizer_destroy(parser->tkz);
4449
return status;
4550
}
4651

@@ -51,6 +56,7 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
5156
status = lxb_html_tree_init(parser->tree, parser->tkz);
5257

5358
if (status != LXB_STATUS_OK) {
59+
parser->tree = lxb_html_tree_destroy(parser->tree);
5460
return status;
5561
}
5662

‎ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Sat, 26 Aug 2023 15:08:59 +0200
4-
Subject: [PATCH 01/21] Expose line and column information for use in PHP
4+
Subject: [PATCH 01/22] Expose line and column information for use in PHP
55

66
---
77
source/lexbor/dom/interfaces/node.h | 2 ++

‎ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Mon, 14 Aug 2023 20:18:51 +0200
4-
Subject: [PATCH 02/21] Track implied added nodes for options use in PHP
4+
Subject: [PATCH 02/22] Track implied added nodes for options use in PHP
55

66
---
77
source/lexbor/html/tree.h | 3 +++

‎ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Thu, 24 Aug 2023 22:57:48 +0200
4-
Subject: [PATCH 03/21] Patch utilities and data structure to be able to
4+
Subject: [PATCH 03/22] Patch utilities and data structure to be able to
55
generate smaller lookup tables
66

77
Changed the generation script to check if everything fits in 32-bits.

‎ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Wed, 29 Nov 2023 21:26:47 +0100
4-
Subject: [PATCH 04/21] Remove unused upper case tag static data
4+
Subject: [PATCH 04/22] Remove unused upper case tag static data
55

66
---
77
source/lexbor/tag/res.h | 2 ++

‎ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Wed, 29 Nov 2023 21:29:31 +0100
4-
Subject: [PATCH 05/21] Shrink size of static binary search tree
4+
Subject: [PATCH 05/22] Shrink size of static binary search tree
55

66
This also makes it more efficient on the data cache.
77
---

0 commit comments

Comments
 (0)