|
|
8ad338 |
commit 22fe2da8e2bc0625d3c492f42d6b716adb36d5c2
|
|
|
8ad338 |
Author: Tomas Korbar <tkorbar@redhat.com>
|
|
|
8ad338 |
Date: Mon Feb 14 12:09:42 2022 +0100
|
|
|
8ad338 |
|
|
|
8ad338 |
CVE-2022-23852
|
|
|
8ad338 |
|
|
|
8ad338 |
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
|
|
|
8ad338 |
index 85ee0a8..4552680 100644
|
|
|
8ad338 |
--- a/lib/xmlparse.c
|
|
|
8ad338 |
+++ b/lib/xmlparse.c
|
|
|
8ad338 |
@@ -161,6 +161,9 @@ typedef char ICHAR;
|
|
|
8ad338 |
/* Round up n to be a multiple of sz, where sz is a power of 2. */
|
|
|
8ad338 |
#define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
|
|
|
8ad338 |
|
|
|
8ad338 |
+/* Do safe (NULL-aware) pointer arithmetic */
|
|
|
8ad338 |
+#define EXPAT_SAFE_PTR_DIFF(p, q) (((p) && (q)) ? ((p) - (q)) : 0)
|
|
|
8ad338 |
+
|
|
|
8ad338 |
/* Handle the case where memmove() doesn't exist. */
|
|
|
8ad338 |
#ifndef HAVE_MEMMOVE
|
|
|
8ad338 |
#ifdef HAVE_BCOPY
|
|
|
8ad338 |
@@ -2026,39 +2029,54 @@ XML_GetBuffer(XML_Parser parser, int len)
|
|
|
8ad338 |
default: ;
|
|
|
8ad338 |
}
|
|
|
8ad338 |
|
|
|
8ad338 |
- if (len > parser->m_bufferLim - parser->m_bufferEnd) {
|
|
|
8ad338 |
-#ifdef XML_CONTEXT_BYTES
|
|
|
8ad338 |
+ if (len > EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd)) {
|
|
|
8ad338 |
int keep;
|
|
|
8ad338 |
-#endif /* defined XML_CONTEXT_BYTES */
|
|
|
8ad338 |
/* Do not invoke signed arithmetic overflow: */
|
|
|
8ad338 |
- int neededSize = (int) ((unsigned)len + (unsigned)(parser->m_bufferEnd - parser->m_bufferPtr));
|
|
|
8ad338 |
+ int neededSize = (int)((unsigned)len
|
|
|
8ad338 |
+ + (unsigned)EXPAT_SAFE_PTR_DIFF(
|
|
|
8ad338 |
+ parser->m_bufferEnd, parser->m_bufferPtr));
|
|
|
8ad338 |
if (neededSize < 0) {
|
|
|
8ad338 |
parser->m_errorCode = XML_ERROR_NO_MEMORY;
|
|
|
8ad338 |
return NULL;
|
|
|
8ad338 |
}
|
|
|
8ad338 |
-#ifdef XML_CONTEXT_BYTES
|
|
|
8ad338 |
- keep = (int)(parser->m_bufferPtr - parser->m_buffer);
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+ keep = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer);
|
|
|
8ad338 |
if (keep > XML_CONTEXT_BYTES)
|
|
|
8ad338 |
keep = XML_CONTEXT_BYTES;
|
|
|
8ad338 |
+ /* Detect and prevent integer overflow */
|
|
|
8ad338 |
+ if (keep > INT_MAX - neededSize) {
|
|
|
8ad338 |
+ parser->m_errorCode = XML_ERROR_NO_MEMORY;
|
|
|
8ad338 |
+ return NULL;
|
|
|
8ad338 |
+ }
|
|
|
8ad338 |
neededSize += keep;
|
|
|
8ad338 |
-#endif /* defined XML_CONTEXT_BYTES */
|
|
|
8ad338 |
- if (neededSize <= parser->m_bufferLim - parser->m_buffer) {
|
|
|
8ad338 |
+ if (neededSize
|
|
|
8ad338 |
+ <= EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_buffer)) {
|
|
|
8ad338 |
#ifdef XML_CONTEXT_BYTES
|
|
|
8ad338 |
- if (keep < parser->m_bufferPtr - parser->m_buffer) {
|
|
|
8ad338 |
- int offset = (int)(parser->m_bufferPtr - parser->m_buffer) - keep;
|
|
|
8ad338 |
- memmove(parser->m_buffer, &parser->m_buffer[offset], parser->m_bufferEnd - parser->m_bufferPtr + keep);
|
|
|
8ad338 |
+ if (keep < EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer)) {
|
|
|
8ad338 |
+ int offset
|
|
|
8ad338 |
+ = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer)
|
|
|
8ad338 |
+ - keep;
|
|
|
8ad338 |
+ /* The buffer pointers cannot be NULL here; we have at least some bytes
|
|
|
8ad338 |
+ * in the buffer */
|
|
|
8ad338 |
+ memmove(parser->m_buffer, &parser->m_buffer[offset],
|
|
|
8ad338 |
+ parser->m_bufferEnd - parser->m_bufferPtr + keep);
|
|
|
8ad338 |
parser->m_bufferEnd -= offset;
|
|
|
8ad338 |
parser->m_bufferPtr -= offset;
|
|
|
8ad338 |
}
|
|
|
8ad338 |
#else
|
|
|
8ad338 |
- memmove(parser->m_buffer, parser->m_bufferPtr, parser->m_bufferEnd - parser->m_bufferPtr);
|
|
|
8ad338 |
- parser->m_bufferEnd = parser->m_buffer + (parser->m_bufferEnd - parser->m_bufferPtr);
|
|
|
8ad338 |
- parser->m_bufferPtr = parser->m_buffer;
|
|
|
8ad338 |
-#endif /* not defined XML_CONTEXT_BYTES */
|
|
|
8ad338 |
- }
|
|
|
8ad338 |
- else {
|
|
|
8ad338 |
+ if (parser->m_buffer && parser->m_bufferPtr) {
|
|
|
8ad338 |
+ memmove(parser->m_buffer, parser->m_bufferPtr,
|
|
|
8ad338 |
+ EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr));
|
|
|
8ad338 |
+ parser->m_bufferEnd
|
|
|
8ad338 |
+ = parser->m_buffer
|
|
|
8ad338 |
+ + EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr);
|
|
|
8ad338 |
+ parser->m_bufferPtr = parser->m_buffer;
|
|
|
8ad338 |
+ }
|
|
|
8ad338 |
+#endif /* not defined XML_CONTEXT_BYTES */
|
|
|
8ad338 |
+ } else {
|
|
|
8ad338 |
char *newBuf;
|
|
|
8ad338 |
- int bufferSize = (int)(parser->m_bufferLim - parser->m_bufferPtr);
|
|
|
8ad338 |
+ int bufferSize
|
|
|
8ad338 |
+ = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferPtr);
|
|
|
8ad338 |
if (bufferSize == 0)
|
|
|
8ad338 |
bufferSize = INIT_BUFFER_SIZE;
|
|
|
8ad338 |
do {
|
|
|
8ad338 |
@@ -2077,25 +2095,33 @@ XML_GetBuffer(XML_Parser parser, int len)
|
|
|
8ad338 |
parser->m_bufferLim = newBuf + bufferSize;
|
|
|
8ad338 |
#ifdef XML_CONTEXT_BYTES
|
|
|
8ad338 |
if (parser->m_bufferPtr) {
|
|
|
8ad338 |
- int keep = (int)(parser->m_bufferPtr - parser->m_buffer);
|
|
|
8ad338 |
- if (keep > XML_CONTEXT_BYTES)
|
|
|
8ad338 |
- keep = XML_CONTEXT_BYTES;
|
|
|
8ad338 |
- memcpy(newBuf, &parser->m_bufferPtr[-keep], parser->m_bufferEnd - parser->m_bufferPtr + keep);
|
|
|
8ad338 |
+ memcpy(newBuf, &parser->m_bufferPtr[-keep],
|
|
|
8ad338 |
+ EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr)
|
|
|
8ad338 |
+ + keep);
|
|
|
8ad338 |
FREE(parser, parser->m_buffer);
|
|
|
8ad338 |
parser->m_buffer = newBuf;
|
|
|
8ad338 |
- parser->m_bufferEnd = parser->m_buffer + (parser->m_bufferEnd - parser->m_bufferPtr) + keep;
|
|
|
8ad338 |
+ parser->m_bufferEnd
|
|
|
8ad338 |
+ = parser->m_buffer
|
|
|
8ad338 |
+ + EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr)
|
|
|
8ad338 |
+ + keep;
|
|
|
8ad338 |
parser->m_bufferPtr = parser->m_buffer + keep;
|
|
|
8ad338 |
- }
|
|
|
8ad338 |
- else {
|
|
|
8ad338 |
- parser->m_bufferEnd = newBuf + (parser->m_bufferEnd - parser->m_bufferPtr);
|
|
|
8ad338 |
+ } else {
|
|
|
8ad338 |
+ /* This must be a brand new buffer with no data in it yet */
|
|
|
8ad338 |
+ parser->m_bufferEnd = newBuf;
|
|
|
8ad338 |
parser->m_bufferPtr = parser->m_buffer = newBuf;
|
|
|
8ad338 |
}
|
|
|
8ad338 |
#else
|
|
|
8ad338 |
if (parser->m_bufferPtr) {
|
|
|
8ad338 |
- memcpy(newBuf, parser->m_bufferPtr, parser->m_bufferEnd - parser->m_bufferPtr);
|
|
|
8ad338 |
+ memcpy(newBuf, parser->m_bufferPtr,
|
|
|
8ad338 |
+ EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr));
|
|
|
8ad338 |
FREE(parser, parser->m_buffer);
|
|
|
8ad338 |
+ parser->m_bufferEnd
|
|
|
8ad338 |
+ = newBuf
|
|
|
8ad338 |
+ + EXPAT_SAFE_PTR_DIFF(parser->m_bufferEnd, parser->m_bufferPtr);
|
|
|
8ad338 |
+ } else {
|
|
|
8ad338 |
+ /* This must be a brand new buffer with no data in it yet */
|
|
|
8ad338 |
+ parser->m_bufferEnd = newBuf;
|
|
|
8ad338 |
}
|
|
|
8ad338 |
- parser->m_bufferEnd = newBuf + (parser->m_bufferEnd - parser->m_bufferPtr);
|
|
|
8ad338 |
parser->m_bufferPtr = parser->m_buffer = newBuf;
|
|
|
8ad338 |
#endif /* not defined XML_CONTEXT_BYTES */
|
|
|
8ad338 |
}
|
|
|
8ad338 |
diff --git a/tests/runtests.c b/tests/runtests.c
|
|
|
8ad338 |
index e1f1ad1..ecc6f47 100644
|
|
|
8ad338 |
--- a/tests/runtests.c
|
|
|
8ad338 |
+++ b/tests/runtests.c
|
|
|
8ad338 |
@@ -4116,6 +4116,31 @@ START_TEST(test_get_buffer_2)
|
|
|
8ad338 |
}
|
|
|
8ad338 |
END_TEST
|
|
|
8ad338 |
|
|
|
8ad338 |
+/* Test for signed integer overflow CVE-2022-23852 */
|
|
|
8ad338 |
+#if defined(XML_CONTEXT_BYTES)
|
|
|
8ad338 |
+START_TEST(test_get_buffer_3_overflow) {
|
|
|
8ad338 |
+ XML_Parser parser = XML_ParserCreate(NULL);
|
|
|
8ad338 |
+ assert(parser != NULL);
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+ const char *const text = "\n";
|
|
|
8ad338 |
+ const int expectedKeepValue = (int)strlen(text);
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+ // After this call, variable "keep" in XML_GetBuffer will
|
|
|
8ad338 |
+ // have value expectedKeepValue
|
|
|
8ad338 |
+ if (XML_Parse(parser, text, (int)strlen(text), XML_FALSE /* isFinal */)
|
|
|
8ad338 |
+ == XML_STATUS_ERROR)
|
|
|
8ad338 |
+ xml_failure(parser);
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+ assert(expectedKeepValue > 0);
|
|
|
8ad338 |
+ if (XML_GetBuffer(parser, INT_MAX - expectedKeepValue + 1) != NULL)
|
|
|
8ad338 |
+ fail("enlarging buffer not failed");
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+ XML_ParserFree(parser);
|
|
|
8ad338 |
+}
|
|
|
8ad338 |
+END_TEST
|
|
|
8ad338 |
+#endif // defined(XML_CONTEXT_BYTES)
|
|
|
8ad338 |
+
|
|
|
8ad338 |
+
|
|
|
8ad338 |
/* Test position information macros */
|
|
|
8ad338 |
START_TEST(test_byte_info_at_end)
|
|
|
8ad338 |
{
|
|
|
8ad338 |
@@ -12117,6 +12142,9 @@ make_suite(void)
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_empty_parse);
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_get_buffer_1);
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_get_buffer_2);
|
|
|
8ad338 |
+#if defined(XML_CONTEXT_BYTES)
|
|
|
8ad338 |
+ tcase_add_test(tc_basic, test_get_buffer_3_overflow);
|
|
|
8ad338 |
+#endif
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_byte_info_at_end);
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_byte_info_at_error);
|
|
|
8ad338 |
tcase_add_test(tc_basic, test_byte_info_at_cdata);
|