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

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