Blame SOURCES/bz1652694-fix-buffer-overflow-http-status.patch

9cfb40
From f28015671a4b04785859d1b4b1327b367b6a10e9 Mon Sep 17 00:00:00 2001
9cfb40
From: Quentin Armitage <quentin@armitage.org.uk>
9cfb40
Date: Tue, 24 Jul 2018 09:28:43 +0100
9cfb40
Subject: [PATCH] Fix buffer overflow in extract_status_code()
9cfb40
9cfb40
Issue #960 identified that the buffer allocated for copying the
9cfb40
HTTP status code could overflow if the http response was corrupted.
9cfb40
9cfb40
This commit changes the way the status code is read, avoids copying
9cfb40
data, and also ensures that the status code is three digits long,
9cfb40
is non-negative and occurs on the first line of the response.
9cfb40
9cfb40
Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
9cfb40
---
9cfb40
 lib/html.c | 23 +++++++++--------------
9cfb40
 1 file changed, 9 insertions(+), 14 deletions(-)
9cfb40
9cfb40
diff --git a/lib/html.c b/lib/html.c
9cfb40
index 5a3eaeac..69d3bd2d 100644
9cfb40
--- a/lib/html.c
9cfb40
+++ b/lib/html.c
9cfb40
@@ -58,23 +58,18 @@ size_t extract_content_length(char *buffer, size_t size)
9cfb40
  */
9cfb40
 int extract_status_code(char *buffer, size_t size)
9cfb40
 {
9cfb40
-	char *buf_code;
9cfb40
-	char *begin;
9cfb40
 	char *end = buffer + size;
9cfb40
-	size_t inc = 0;
9cfb40
-	int code;
9cfb40
-
9cfb40
-	/* Allocate the room */
9cfb40
-	buf_code = (char *)MALLOC(10);
9cfb40
+	unsigned long code;
9cfb40
 
9cfb40
 	/* Status-Code extraction */
9cfb40
-	while (buffer < end && *buffer++ != ' ') ;
9cfb40
-	begin = buffer;
9cfb40
-	while (buffer < end && *buffer++ != ' ')
9cfb40
-		inc++;
9cfb40
-	strncat(buf_code, begin, inc);
9cfb40
-	code = atoi(buf_code);
9cfb40
-	FREE(buf_code);
9cfb40
+	while (buffer < end && *buffer != ' ' && *buffer != '\r')
9cfb40
+		buffer++;
9cfb40
+	buffer++;
9cfb40
+	if (buffer + 3 >= end || *buffer == ' ' || buffer[3] != ' ')
9cfb40
+		return 0;
9cfb40
+	code = strtoul(buffer, &end, 10);
9cfb40
+	if (buffer + 3 != end)
9cfb40
+		return 0;
9cfb40
 	return code;
9cfb40
 }
9cfb40
 
9cfb40
-- 
9cfb40
2.19.1
9cfb40