CVE-2018-25103 (published 2024-06-17T18:15Z)

There exists use-after-free vulnerabilities in lighttpd <= 1.4.50 request parsing which might read from invalid pointers to memory used in the same request, not from other requests.


Weakness information:
cwe_id: NVD-CWE-noinfo
cwe_name: Insufficient Information
description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
extended_description: Insufficient Information

Fix information:
repo_url: https://github.com/lighttpd/lighttpd1.4
commit_details: repo_url: https://github.com/lighttpd/lighttpd1.4
author: Stefan Bühler
author_date: 2018-08-25 13:53:34+02:00
msg: [core,security] process headers after combining folded headers

- this fixes various use-after-free scenarios (reported by Or Peles of
  VDOO): when parse_single_header stores pointers to header values in
  con->request, those pointers are not updated if the header value is
  reallocated when folded header lines are appended.
- also remove trailing white-space from folded lines
repository: repo_url: https://github.com/lighttpd/lighttpd1.4
repo_name: lighttpd/lighttpd1.4
description: lighttpd1.4 on github for easier collaboration - main repo still on lighttpd.net
repo_language: C
owner: lighttpd
file_changes: filename: request.c
change_type: ModificationType.MODIFY
diff: @@ -444,6 +444,11 @@ static void init_parse_header_state(parse_header_state* state) {
 static int parse_single_header(server *srv, connection *con, parse_header_state *state, data_string *ds) {
 	int cmp = 0;
 
+	/* empty header-fields are not allowed by HTTP-RFC, we just ignore them */
+	if (buffer_string_is_empty(ds->value)) {
+		goto drop_header;
+	}
+
 	/* retreive values
 	 *
 	 *
@@ -575,6 +580,7 @@ int http_request_parse(server *srv, connection *con) {
 	char *uri = NULL, *proto = NULL, *method = NULL;
 	int is_key = 1, key_len = 0, is_ws_after_key = 0, in_folding;
 	char *value = NULL, *key = NULL;
+	data_string *current_header = NULL;
 
 	int line = 0;
 
@@ -1024,7 +1030,8 @@ int http_request_parse(server *srv, connection *con) {
 			case '\r':
 			case '\n':
 				if (*cur == '\n' || con->parse_request->ptr[i+1] == '\n') {
-					data_string *ds = NULL;
+					int value_len;
+
 					if (*cur == '\n') {
 						if (http_header_strict) {
 							http_request_missing_CR_before_LF(srv, con);
@@ -1038,17 +1045,15 @@ int http_request_parse(server *srv, connection *con) {
 					/* End of Headerline */
 					con->parse_request->ptr[i] = '\0';
 
+					value_len = cur - value;
+
+					/* strip trailing white-spaces */
+					while (value_len > 0 && (value[value_len - 1] == ' ' || value[value_len - 1] == '\t')) {
+						--value_len;
+					}
+
 					if (in_folding) {
-						/**
-						 * we use a evil hack to handle the line-folding
-						 * 
-						 * As array_insert_unique() deletes 'ds' in the case of a duplicate
-						 * ds points somewhere and we get a evil crash. As a solution we keep the old
-						 * "key" and get the current value from the hash and append us
-						 *
-						 * */
-
-						if (!key || !key_len) {
+						if (!current_header) {
 							/* 400 */
 
 							if (srv->srvconf.log_request_header_on_error) {
@@ -1062,47 +1067,32 @@ int http_request_parse(server *srv, connection *con) {
 							goto failure;
 						}
 
-						if (NULL != (ds = (data_string *)array_get_element_klen(con->request.headers, key, key_len))) {
-							buffer_append_string(ds->value, value);
-						}
+						buffer_append_string_len(current_header->value, value, value_len);
 					} else {
-						int s_len;
-						key = con->parse_request->ptr + first;
-
-						s_len = cur - value;
-
-						/* strip trailing white-spaces */
-						for (; s_len > 0 && 
-								(value[s_len - 1] == ' ' || 
-									value[s_len - 1] == '\t'); s_len--);
-
-						value[s_len] = '\0';
-
-						if (s_len > 0) {
-							if (NULL == (ds = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
-								ds = data_string_init();
-							}
-							buffer_copy_string_len(ds->key, key, key_len);
-							buffer_copy_string_len(ds->value, value, s_len);
-
+						/* process previous header */
+						if (current_header) {
+							data_string *ds = current_header;
+							current_header = NULL;
 							if (!parse_single_header(srv, con, &state, ds)) {
 								/* parse_single_header should already have logged it */
 								goto failure;
 							}
-						} else {
-							/* empty header-fields are not allowed by HTTP-RFC, we just ignore them */
 						}
+
+						key = con->parse_request->ptr + first;
+
+						if (NULL == (current_header = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
+							current_header = data_string_init();
+						}
+
+						buffer_copy_string_len(current_header->key, key, key_len);
+						buffer_copy_string_len(current_header->value, value, value_len);
 					}
 
 					first = i+1;
 					is_key = 1;
 					value = NULL;
-#if 0
-					/**
-					 * for Bug 1230 keep the key_len a live
-					 */
 					key_len = 0; 
-#endif
 					in_folding = 0;
 				} else {
 					if (srv->srvconf.log_request_header_on_error) {
@@ -1132,6 +1122,16 @@ int http_request_parse(server *srv, connection *con) {
 		}
 	}
 
+	/* process last header */
+	if (current_header) {
+		data_string* ds = current_header;
+		current_header = NULL;
+		if (!parse_single_header(srv, con, &state, ds)) {
+			/* parse_single_header should already have logged it */
+			goto failure;
+		}
+	}
+
 	con->header_len = i;
 
 	/* do some post-processing */
@@ -1252,6 +1252,8 @@ int http_request_parse(server *srv, connection *con) {
 	return 0;
 
 failure:
+	if (current_header) current_header->free((data_unset *)current_header);
+
 	con->keep_alive = 0;
 	con->response.keep_alive = 0;
 	if (!con->http_status) con->http_status = 400;

programming_language: C
repo_url: https://github.com/lighttpd/lighttpd1.4
commit_details: repo_url: https://github.com/lighttpd/lighttpd1.4
author: Glenn Strauss
author_date: 2018-07-23 22:25:23-04:00
msg: [core] security: use-after-free invalid Range req

(thx Marcus Wengelin)
repository: repo_url: https://github.com/lighttpd/lighttpd1.4
repo_name: lighttpd/lighttpd1.4
description: lighttpd1.4 on github for easier collaboration - main repo still on lighttpd.net
repo_language: C
owner: lighttpd
file_changes: filename: base.h
change_type: ModificationType.MODIFY
diff: @@ -47,7 +47,6 @@ typedef struct {
 
 	/* strings to the header */
 	buffer *http_host; /* not alloced */
-	const char   *http_range;
 	const char   *http_content_type;
 	const char   *http_if_modified_since;
 	const char   *http_if_none_match;
@@ -58,9 +57,6 @@ typedef struct {
 	off_t content_length; /* returned by strtoll() */
 	off_t te_chunked;
 
-	/* internal representation */
-	int     accept_encoding;
-
 	/* internal */
 	buffer *pathinfo;
 } request;

programming_language: C
filename: connections.c
change_type: ModificationType.MODIFY
diff: @@ -685,7 +685,6 @@ int connection_reset(server *srv, connection *con) {
 		con->request.x = NULL;
 
 	CLEAN(http_host);
-	CLEAN(http_range);
 	CLEAN(http_content_type);
 #undef CLEAN
 	con->request.content_length = 0;

programming_language: C
filename: http-header-glue.c
change_type: ModificationType.MODIFY
diff: @@ -229,7 +229,7 @@ int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
 }
 
 
-static int http_response_parse_range(server *srv, connection *con, buffer *path, stat_cache_entry *sce) {
+static int http_response_parse_range(server *srv, connection *con, buffer *path, stat_cache_entry *sce, const char *range) {
 	int multipart = 0;
 	int error;
 	off_t start, end;
@@ -247,11 +247,26 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
 		content_type = ds->value;
 	}
 
-	for (s = con->request.http_range, error = 0;
+	for (s = range, error = 0;
 	     !error && *s && NULL != (minus = strchr(s, '-')); ) {
 		char *err;
 		off_t la, le;
 
+		if (s != minus) {
+			la = strtoll(s, &err, 10);
+			if (err != minus) {
+				/* should not have multiple range-unit in Range, but
+				 * handle just in case multiple Range headers merged */
+				while (*s == ' ' || *s == '\t') ++s;
+				if (0 != strncmp(s, "bytes=", 6)) return -1;
+				s += 6;
+				if (s != minus) {
+					la = strtoll(s, &err, 10);
+					if (err != minus) return -1;
+				}
+			}
+		}
+
 		if (s == minus) {
 			/* -<stop> */
 
@@ -281,9 +296,6 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
 		} else if (*(minus+1) == '\0' || *(minus+1) == ',') {
 			/* <start>- */
 
-			la = strtoll(s, &err, 10);
-
-			if (err == minus) {
 				/* ok */
 
 				if (*(err + 1) == '\0') {
@@ -301,16 +313,9 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
 				} else {
 					error = 1;
 				}
-			} else {
-				/* error */
-				error = 1;
-			}
 		} else {
 			/* <start>-<stop> */
 
-			la = strtoll(s, &err, 10);
-
-			if (err == minus) {
 				le = strtoll(minus+1, &err, 10);
 
 				/* RFC 2616 - 14.35.1 */
@@ -335,11 +340,6 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
 
 					error = 1;
 				}
-			} else {
-				/* error */
-
-				error = 1;
-			}
 		}
 
 		if (!error) {
@@ -516,9 +516,11 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
 		}
 	}
 
-	if (con->request.http_range && con->conf.range_requests
+	if (con->conf.range_requests
 	    && (200 == con->http_status || 0 == con->http_status)
+	    && NULL != (ds = (data_string *)array_get_element(con->request.headers, "Range"))
 	    && NULL == array_get_element(con->response.headers, "Content-Encoding")) {
+		buffer *range = ds->value;
 		int do_range_request = 1;
 		/* check if we have a conditional GET */
 
@@ -547,11 +549,14 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
 			}
 		}
 
-		if (do_range_request) {
+		if (do_range_request
+		    && !buffer_string_is_empty(range)
+		    && 0 == strncmp(range->ptr, "bytes=", 6)) {
+			/* support only "bytes" byte-unit */
 			/* content prepared, I'm done */
 			con->file_finished = 1;
 
-			if (0 == http_response_parse_range(srv, con, path, sce)) {
+			if (0 == http_response_parse_range(srv, con, path, sce, range->ptr+6)) {
 				con->http_status = 206;
 			}
 			return;

programming_language: C
filename: request.c
change_type: ModificationType.MODIFY
diff: @@ -1118,30 +1118,6 @@ int http_request_parse(server *srv, connection *con) {
 									ds->free((data_unset*) ds);
 									ds = NULL;
 								}
-							} else if (cmp > 0 && 0 == (cmp = buffer_caseless_compare(CONST_BUF_LEN(ds->key), CONST_STR_LEN("Range")))) {
-								if (!con->request.http_range) {
-									/* bytes=.*-.* */
-
-									if (0 == strncasecmp(ds->value->ptr, "bytes=", 6) &&
-									    NULL != strchr(ds->value->ptr+6, '-')) {
-
-										/* if dup, only the first one will survive */
-										con->request.http_range = ds->value->ptr + 6;
-									}
-								} else {
-									con->http_status = 400;
-									con->keep_alive = 0;
-
-									if (srv->srvconf.log_request_header_on_error) {
-										log_error_write(srv, __FILE__, __LINE__, "s",
-												"duplicate Range-header -> 400");
-										log_error_write(srv, __FILE__, __LINE__, "Sb",
-												"request-header:\n",
-												con->request.request);
-									}
-									array_insert_unique(con->request.headers, (data_unset *)ds);
-									return 0;
-								}
 							}
 
 							if (ds) array_insert_unique(con->request.headers, (data_unset *)ds);

programming_language: C
filename: test_request.c
change_type: ModificationType.MODIFY
diff: @@ -13,7 +13,6 @@ static void test_request_connection_reset(connection *con)
     con->request.http_method = HTTP_METHOD_UNSET;
     con->request.http_version = HTTP_VERSION_UNSET;
     con->request.http_host = NULL;
-    con->request.http_range = NULL;
     con->request.http_content_type = NULL;
     con->request.http_if_modified_since = NULL;
     con->request.http_if_none_match = NULL;
@@ -372,13 +371,40 @@ static void test_request_http_request_parse(server *srv, connection *con)
                     "Content-Type: 4\r\n"
                     "\r\n"));
 
-    run_http_request_parse(srv, con, __LINE__, 400,
-      "Duplicate Range headers",
+    /* (not actually testing Range here anymore; parsing deferred until use) */
+
+    run_http_request_parse(srv, con, __LINE__, 0,
+      "Duplicate Range headers (get appended)",
       CONST_STR_LEN("GET / HTTP/1.0\r\n"
                     "Range: bytes=5-6\r\n"
                     "Range: bytes=5-9\r\n"
                     "\r\n"));
 
+    run_http_request_parse(srv, con, __LINE__, 0,
+      "Duplicate Range headers with invalid range (a)",
+      CONST_STR_LEN("GET / HTTP/1.0\r\n"
+                    "Range: bytes=0\r\n"
+                    "Range: bytes=5-9\r\n"
+                    "\r\n"));
+    run_http_request_parse(srv, con, __LINE__, 0,
+      "Duplicate Range headers with invalid range (b)",
+      CONST_STR_LEN("GET / HTTP/1.0\r\n"
+                    "Range: bytes=5-9\r\n"
+                    "Range: bytes=0\r\n"
+                    "\r\n"));
+    run_http_request_parse(srv, con, __LINE__, 0,
+      "Duplicate Range headers with invalid range (c)",
+      CONST_STR_LEN("GET / HTTP/1.0\r\n"
+                    "Range: 0\r\n"
+                    "Range: bytes=5-9\r\n"
+                    "\r\n"));
+    run_http_request_parse(srv, con, __LINE__, 0,
+      "Duplicate Range headers with invalid range (d)",
+      CONST_STR_LEN("GET / HTTP/1.0\r\n"
+                    "Range: bytes=5-9\r\n"
+                    "Range: 0\r\n"
+                    "\r\n"));
+
     run_http_request_parse(srv, con, __LINE__, 0,
       "Duplicate If-None-Match headers",
       CONST_STR_LEN("GET / HTTP/1.0\r\n"

programming_language: C
filename: request.t
change_type: ModificationType.MODIFY
diff: @@ -8,7 +8,7 @@ BEGIN {
 
 use strict;
 use IO::Socket;
-use Test::More tests => 50;
+use Test::More tests => 52;
 use LightyTest;
 
 my $tf = LightyTest->new();
@@ -391,6 +391,26 @@ EOF
 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
 ok($tf->handle_http($t) == 0, 'GET, Range with range-requests-disabled');
 
+$t->{REQUEST}  = ( <<EOF
+GET /12345.txt HTTP/1.0
+Host: 123.example.org
+Range: 0
+Range: bytes=0-3
+EOF
+ );
+$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => "12345\n" } ];
+ok($tf->handle_http($t) == 0, 'GET, Range invalid range-unit (first)');
+
+$t->{REQUEST}  = ( <<EOF
+GET /12345.txt HTTP/1.0
+Host: 123.example.org
+Range: bytes=0-3
+Range: 0
+EOF
+ );
+$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 206 } ];
+ok($tf->handle_http($t) == 0, 'GET, Range ignore invalid range (second)');
+
 $t->{REQUEST}  = ( <<EOF
 OPTIONS / HTTP/1.0
 Content-Length: 4

programming_language: Perl