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