CVE-2018-19198 (published 2018-11-12T15:29Z)

An issue was discovered in uriparser before 0.9.0. UriQuery.c allows an out-of-bounds write via a uriComposeQuery* or uriComposeQueryEx* function because the '&' character is mishandled in certain contexts.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-787
cwe_name: Out-of-bounds Write
description: The product writes data past the end, or before the beginning, of the intended buffer.
extended_description: 
            
            
             
			 
         
         

Fix information:
repo_url: https://github.com/uriparser/uriparser
commit_details: repo_url: https://github.com/uriparser/uriparser
author: Sebastian Pipping
author_date: 2018-09-23 20:07:25+02:00
msg: UriQuery.c: Fix out-of-bounds-write in ComposeQuery and ...Ex

Reported by Google Autofuzz team
repository: repo_url: https://github.com/uriparser/uriparser
repo_name: uriparser/uriparser
description:  :hocho: Strictly RFC 3986 compliant URI parsing and handling library written in C89; moved from SourceForge to GitHub
repo_language: C
owner: uriparser
file_changes: filename: UriQuery.c
change_type: ModificationType.MODIFY
diff: @@ -223,6 +223,7 @@ int URI_FUNC(ComposeQueryEngine)(URI_CHAR * dest,
 
 			/* Copy key */
 			if (firstItem == URI_TRUE) {
+				ampersandLen = 1;
 				firstItem = URI_FALSE;
 			} else {
 				write[0] = _UT('&');

programming_language: C
filename: test.cpp
change_type: ModificationType.MODIFY
diff: @@ -104,6 +104,7 @@ public:
 		TEST_ADD(UriSuite::testQueryList)
 		TEST_ADD(UriSuite::testQueryListPair)
 		TEST_ADD(UriSuite::testQueryDissection_Bug3590761)
+		TEST_ADD(UriSuite::testQueryCompositionMathWrite_GoogleAutofuzz113244572)
 		TEST_ADD(UriSuite::testFreeCrash_Bug20080827)
 		TEST_ADD(UriSuite::testParseInvalid_Bug16)
 		TEST_ADD(UriSuite::testRangeComparison)
@@ -1749,6 +1750,37 @@ Rule                                | Example | hostSet | absPath | emptySeg
 		uriFreeQueryListA(queryList);
 	}
 
+	void testQueryCompositionMathWrite_GoogleAutofuzz113244572() {
+		UriQueryListA second = { .key = "\x11", .value = NULL, .next = NULL };
+		UriQueryListA first = { .key = "\x01", .value = "\x02", .next = &second };
+
+		const UriBool spaceToPlus = URI_TRUE;
+		const UriBool normalizeBreaks = URI_FALSE;  /* for factor 3 but 6 */
+
+		const int charsRequired = (3 + 1 + 3) + 1 + (3);
+
+		{
+			// Minimum space to hold everything fine
+			const char * const expected = "%01=%02" "&" "%11";
+			char dest[charsRequired + 1];
+			int charsWritten;
+			TEST_ASSERT(uriComposeQueryExA(dest, &first, sizeof(dest),
+					&charsWritten, spaceToPlus, normalizeBreaks)
+				== URI_SUCCESS);
+			TEST_ASSERT(! strcmp(dest, expected));
+			TEST_ASSERT(charsWritten == strlen(expected) + 1);
+		}
+
+		{
+			// Previous math failed to take ampersand into account
+			char dest[charsRequired + 1 - 1];
+			int charsWritten;
+			TEST_ASSERT(uriComposeQueryExA(dest, &first, sizeof(dest),
+					&charsWritten, spaceToPlus, normalizeBreaks)
+				== URI_ERROR_OUTPUT_TOO_LARGE);
+		}
+	}
+
 	void testFreeCrash_Bug20080827() {
 		char const * const sourceUri = "abc";
 		char const * const baseUri = "http://www.example.org/";

programming_language: C++