Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main/expat: backport fix for CVE-2022-40674
- Loading branch information
Showing
2 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
From 7802454a5548fbe3037db316adbeeabb596b9255 Mon Sep 17 00:00:00 2001 | ||
From: Rhodri James <rhodri@wildebeest.org.uk> | ||
Date: Wed, 17 Aug 2022 18:26:18 +0100 | ||
Subject: [PATCH 1/2] Ensure raw tagnames are safe exiting internalEntityParser | ||
|
||
It is possible to concoct a situation in which parsing is | ||
suspended while substituting in an internal entity, so that | ||
XML_ResumeParser directly uses internalEntityProcessor as | ||
its processor. If the subsequent parse includes some unclosed | ||
tags, this will return without calling storeRawNames to ensure | ||
that the raw versions of the tag names are stored in memory other | ||
than the parse buffer itself. If the parse buffer is then changed | ||
or reallocated (for example if processing a file line by line), | ||
badness will ensue. | ||
|
||
This patch ensures storeRawNames is always called when needed | ||
after calling doContent. The earlier call do doContent does | ||
not need the same protection; it only deals with entity | ||
substitution, which cannot leave unbalanced tags, and in any | ||
case the raw names will be pointing into the stored entity | ||
value not the parse buffer. | ||
|
||
(cherry picked from commit 4a32da87e931ba54393d465bb77c40b5c33d343b) | ||
--- | ||
expat/lib/xmlparse.c | 7 ++++++- | ||
1 file changed, 6 insertions(+), 1 deletion(-) | ||
|
||
diff --git a/expat/lib/xmlparse.c b/expat/lib/xmlparse.c | ||
index dfc316ca..d8e324e8 100644 | ||
--- a/lib/xmlparse.c | ||
+++ b/lib/xmlparse.c | ||
@@ -5277,9 +5277,14 @@ internalEntityProcessor(XML_Parser parser, const char *s, const char *end, | ||
{ | ||
parser->m_processor = contentProcessor; | ||
/* see externalEntityContentProcessor vs contentProcessor */ | ||
- return doContent(parser, parser->m_parentParser ? 1 : 0, parser->m_encoding, | ||
+ result = doContent(parser, parser->m_parentParser ? 1 : 0, parser->m_encoding, | ||
s, end, nextPtr, | ||
(XML_Bool)! parser->m_parsingStatus.finalBuffer); | ||
+ if (result == XML_ERROR_NONE) { | ||
+ if (! storeRawNames(parser)) | ||
+ return XML_ERROR_NO_MEMORY; | ||
+ } | ||
+ return result; | ||
} | ||
} | ||
|
||
-- | ||
2.37.3 | ||
|
||
|
||
From cff3c9a5e43bc929e43ccd35425c3db8cd21d4de Mon Sep 17 00:00:00 2001 | ||
From: Sebastian Pipping <sebastian@pipping.org> | ||
Date: Sun, 11 Sep 2022 19:34:33 +0200 | ||
Subject: [PATCH 2/2] tests: Cover heap use-after-free issue in doContent | ||
|
||
(cherry picked from commit a7ce80a013f2a08cb1ac4aac368f2250eea03ebf) | ||
--- | ||
expat/tests/runtests.c | 74 ++++++++++++++++++++++++++++++++++++++++++ | ||
1 file changed, 74 insertions(+) | ||
|
||
diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c | ||
index 2490d86b..70fb583a 100644 | ||
--- a/tests/runtests.c | ||
+++ b/tests/runtests.c | ||
@@ -4904,6 +4904,78 @@ START_TEST(test_suspend_resume_internal_entity) { | ||
} | ||
END_TEST | ||
|
||
+void | ||
+suspending_comment_handler(void *userData, const XML_Char *data) { | ||
+ UNUSED_P(data); | ||
+ XML_Parser parser = (XML_Parser)userData; | ||
+ XML_StopParser(parser, XML_TRUE); | ||
+} | ||
+ | ||
+START_TEST(test_suspend_resume_internal_entity_issue_629) { | ||
+ const char *const text | ||
+ = "<!DOCTYPE a [<!ENTITY e '<!--COMMENT-->a'>]><a>&e;<b>\n" | ||
+ "<" | ||
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaa" | ||
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
+ "/>" | ||
+ "</b></a>"; | ||
+ const size_t firstChunkSizeBytes = 54; | ||
+ | ||
+ XML_Parser parser = XML_ParserCreate(NULL); | ||
+ XML_SetUserData(parser, parser); | ||
+ XML_SetCommentHandler(parser, suspending_comment_handler); | ||
+ | ||
+ if (XML_Parse(parser, text, (int)firstChunkSizeBytes, XML_FALSE) | ||
+ != XML_STATUS_SUSPENDED) | ||
+ xml_failure(parser); | ||
+ if (XML_ResumeParser(parser) != XML_STATUS_OK) | ||
+ xml_failure(parser); | ||
+ if (XML_Parse(parser, text + firstChunkSizeBytes, | ||
+ (int)(strlen(text) - firstChunkSizeBytes), XML_TRUE) | ||
+ != XML_STATUS_OK) | ||
+ xml_failure(parser); | ||
+ XML_ParserFree(parser); | ||
+} | ||
+END_TEST | ||
+ | ||
/* Test syntax error is caught at parse resumption */ | ||
START_TEST(test_resume_entity_with_syntax_error) { | ||
const char *text = "<!DOCTYPE doc [\n" | ||
@@ -11387,6 +11459,8 @@ make_suite(void) { | ||
tcase_add_test(tc_basic, test_partial_char_in_epilog); | ||
tcase_add_test(tc_basic, test_hash_collision); | ||
tcase_add_test__ifdef_xml_dtd(tc_basic, test_suspend_resume_internal_entity); | ||
+ tcase_add_test__ifdef_xml_dtd(tc_basic, | ||
+ test_suspend_resume_internal_entity_issue_629); | ||
tcase_add_test__ifdef_xml_dtd(tc_basic, test_resume_entity_with_syntax_error); | ||
tcase_add_test__ifdef_xml_dtd(tc_basic, test_suspend_resume_parameter_entity); | ||
tcase_add_test(tc_basic, test_restart_on_error); | ||
-- | ||
2.37.3 | ||
|