fix char-signedness out-of-bounds read in url and header lookup tables#3376
Open
ubeddulla wants to merge 1 commit into
Open
fix char-signedness out-of-bounds read in url and header lookup tables#3376ubeddulla wants to merge 1 commit into
ubeddulla wants to merge 1 commit into
Conversation
Contributor
|
@ubeddulla Thank you for your PR! Could you add some test cases? |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes out-of-bounds reads caused by char signedness when indexing +128-biased 256-entry lookup tables used in URL parsing and ASCII lowercasing (affecting unsigned-char platforms like aarch64/riscv64).
Changes:
- Casts URL parsing table indices to
(signed char)to keep indexing within[-128, 127]inURI::SetHttpURLandParseURL. - Casts
ascii_tolower’s index to(signed char)to prevent OOB reads when header-name bytes are>= 0x80. - Adds clarifying comments explaining the signedness/bias assumption for the tolower table.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/butil/containers/case_ignored_flat_map.h | Prevents OOB reads in ascii_tolower by forcing signed-char indexing into the biased table. |
| src/brpc/uri.cpp | Prevents OOB reads in URL parsing by forcing signed-char indexing into the biased action table. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+32
to
+36
| // g_tolower_map is biased by +128 and sized for a signed-char index | ||
| // ([-128,127]). Callers pass a `char`; on platforms where `char` is | ||
| // unsigned (aarch64, riscv64) a byte >= 0x80 arrives here as 128..255 and | ||
| // indexes past the 256-entry table. Fold back into signed-char range. | ||
| return g_tolower_map[(signed char)c]; |
Comment on lines
168
to
172
| bool need_scheme = true; | ||
| bool need_user_info = true; | ||
| for (; true; ++p) { | ||
| const char action = g_url_parsing_fast_action_map[(int)*p]; | ||
| const char action = g_url_parsing_fast_action_map[(signed char)*p]; | ||
| if (action == URI_PARSE_CONTINUE) { |
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.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
Two +128-biased 256-entry lookup tables are read out of bounds when an input byte has the high bit set, on builds where
charis unsigned (aarch64, riscv64).g_url_parsing_fast_action_map(uri.cpp, used byURI::SetHttpURLandParseURL) is indexed with(int)*p, where*pwalks the untrusted request-URI. On an unsigned-charplatform a byte in0x80..0xFFgives index128..255, soraw[256..383]is read, up to 127 bytes past the table.g_tolower_map(ascii_tolowerincase_ignored_flat_map.h) has the same +128 bias and is indexed by the promotedcharargument. It is reached fromCaseIgnoredHasherover HTTP header names and from HPACK, so the same high-bit byte over-reads the 256-entry table. The note already there records an aarch64 char-signedness crash; making the parameterintdid not fix it, because acharargument still promotes to0..255.On x86_64 (
charsigned) both indices fall in[-128,127]and stay in bounds, which is why the current CI does not surface it.What is changed and the side effects?
Changed:
Fold the index back into signed-char range at the three lookup sites with a
(signed char)cast. The table layout is untouched and behavior on signed-charplatforms is unchanged. Verified with an ASan build under-funsigned-char: a URL host byte0xC3and a header-name byte>= 0x80each trip a buffer-overflow before the change and run clean after, andascii_toloweroutput matches the expected value for all 256 byte values.Side effects:
Performance effects: none (a single cast).
Breaking backward compatibility: no.
Check List: