Skip to content

external/webserver: Fix off-by-one stack writes in HTTP header parsing - #7670

Open
mangeshraut712 wants to merge 1 commit into
Samsung:masterfrom
mangeshraut712:fix/webserver-http-string-util-bounds
Open

mangeshraut712 wants to merge 1 commit into
Samsung:masterfrom
mangeshraut712:fix/webserver-http-string-util-bounds

Conversation

@mangeshraut712

Copy link
Copy Markdown

Fixes #7662
Fixes #7663

Both functions in external/webserver/http_string_util.c accept 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 allows url_length == HTTP_CONF_MAX_REQUEST_HEADER_URL_LENGTH (128) and then writes url[128]. http_recv_and_handle_request declares char url[128] (http_client.c:622), so a request line whose URL is exactly 128 characters writes one byte past it. http_handle_file already sizes its own buffer + 1 for the NUL (http_client.c:807); this check now requires the same room.

http_separate_keyvalue (#7663). The value loop allows i - value_position up to HTTP_CONF_MAX_VALUE_LENGTH (256) and then writes value[256] past char value[256] (http_client.c:282). Three further problems in the same function:

  • The key bound allows a key of 32 characters, which fills key[32] with no room for a NUL.
  • A line with no : returns HTTP_OK with key[] never terminated, which the caller then prints with %s (http_client.c:352).
  • When : is the last character of the line, value_position is i + 2, past the line, so the NUL is written at value[-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 after value[] by a 256-character value, a line without : is accepted, and Host: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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant