Repository navigation
external/webserver: Fix off-by-one stack writes in HTTP header parsing - #7670
Open
mangeshraut712 wants to merge 1 commit into
Open
mangeshraut712 wants to merge 1 commit into
mangeshraut712 wants to merge 1 commit into
Conversation
http_separate_header accepted a URL of exactly HTTP_CONF_MAX_REQUEST_HEADER_URL_LENGTH (128) characters and then wrote its NUL terminator at url[128], one byte past the 128-byte url[] buffer that http_recv_and_handle_request declares on the stack. http_separate_keyvalue had the same off-by-one on value[]: a value of exactly HTTP_CONF_MAX_VALUE_LENGTH (256) characters was accepted and the NUL was written at value[256]. Its key bound also allowed a key of 32 characters to fill key[] with no room for a NUL, and a line without ':' returned HTTP_OK with key[] never terminated, which the caller then prints with %s. When ':' was the last character of the line the value position ran past the line and the NUL was written at value[-2]. Reserve one byte for each NUL terminator, require ':' before accepting a line, and start the value right after ':' when no space follows it, so the position never runs past the line. Reported-by: OvOhao Refs: Samsung#7662, Samsung#7663 Signed-off-by: Mangesh Raut <95149253+mangeshraut712@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7662
Fixes #7663
Both functions in
external/webserver/http_string_util.caccept a field one character longer than the caller's stack buffer and then write the NUL terminator one byte past it.http_separate_header(#7662). The length check allowsurl_length == HTTP_CONF_MAX_REQUEST_HEADER_URL_LENGTH(128) and then writesurl[128].http_recv_and_handle_requestdeclareschar url[128](http_client.c:622), so a request line whose URL is exactly 128 characters writes one byte past it.http_handle_filealready sizes its own buffer+ 1for the NUL (http_client.c:807); this check now requires the same room.http_separate_keyvalue(#7663). The value loop allowsi - value_positionup toHTTP_CONF_MAX_VALUE_LENGTH(256) and then writesvalue[256]pastchar value[256](http_client.c:282). Three further problems in the same function:key[32]with no room for a NUL.:returnsHTTP_OKwithkey[]never terminated, which the caller then prints with%s(http_client.c:352).:is the last character of the line,value_positionisi + 2, past the line, so the NUL is written atvalue[-2]— two bytes before the buffer.The change. Each bound now reserves one byte for the NUL. A line is only accepted once
:is found, and the value starts right after:when no space follows it, so the position never runs past the line.Verification. The webserver has no host test target, so I compiled the two functions on the host with a harness that places a canary after each buffer (the functions are extracted from the source file, not retyped) and ran it against the code before and after this change. Before: 6 of 20 checks fail — the canary after
url[]is overwritten by a 128-character URL, the canary aftervalue[]by a 256-character value, a line without:is accepted, andHost:example.com(no space) is mis-parsed. After: 20 of 20 pass. Every input that was legal before is still accepted and parses identically (values of 255, keys of 31, URLs of 127 and 100 characters,Content-Type: application/json,GET /index.html HTTP/1.1). I can add the harness to the tree if you would like a regression test kept with the code.Credit for finding and analysing both bugs goes to @OvOhao, who reported them in #7662 and #7663 with the suggested fixes; this PR implements them.