Reads beyond bounds and overflow/underflow in decode()
The decode() function in NSSBridge.cpp copies more bytes than the base64 decoder actually produced, reading past the end of the decoded buffer. It also computes the output length with an expression that can overflow or underflow, which can make callers such as doCrypto() read beyond the bounds of the result buffer.
- Fixed in 15 days
- Paid in 21 days
- Disclosed after 15 days
decode() (mozglue\android\NSSBridge.cpp) will read beyond bounds in two cases, and overflow/underflow in another, probably in the latter causing its caller(s) to read beyond bounds.
-
In the first beyond-bounds case, decode() copies approximately |len/4| bytes of beyond-bounds memory into its output buffer |result|. This occurs because line 289 copies |len| bytes of data from the decoded output produced by f_PL_Base64Decode() [1], but f_PL_Base64Decode() produces only ~|3/4len| bytes of data. Presumably the caller will not read this data, because the output buffer length |length| is semi-correctly computed by line 281 (but see below).
-
The overflow/underflow case will occur if |len >= 0x55555556|. In this case, line 281's expression
length = (len3)/4 - adjust;
overflows on |len*3|. If |len == 0x55555556|, this produces a result of 0 on the subtraction's LHS. If |adjust| is 1 or 2 (see lines 267-70), the subtraction will then underflow, returning |length| of 0xffffffff or 0xfffffffe to the caller, which (at least in the case of doCrypto(), lines 205-) then read that beyond-bounds data from |*result|. (Note that f_PL_Base64Decode() does not protected decode() from this case, because f_PL_Base64Decode() computes the output length using an expression that cannot overflow, so f_PL_Base64Decode() does not fail, and the early exits on lines 274-279 don't occur.). Overflows with |length| having other values will produce truncated output, with unknown effects. [2]
- In the second beyond-bounds case, decode() reads one byte of data from somewhere far beyond bounds if |data| consists of the single character "=". This occurs because the unprotected computation |len-2| on line 269 underflows to |0xffffffff|. In this case, decode() doesn't propagate the improperly-read byte further, and f_PL_Base64Decode() correctly fails when attempting to decode the string "=", so the worst thing that should occur is incorrect truncation of the output if the character at the out-of-bounds location is accessible and has the value '='.
I don't have a POC because I don't have any Android devices on which to test these bugs.
259: SECStatus 260: decode(const char *data, unsigned char **result, int32_t length) 261: { 262: SECStatus rv = SECSuccess; 263: uint32_t len = strlen(data); 264: int adjust = 0; 265: 266: / Compute length adjustment */ 267: if (len > 0 && data[len-1] == '=') { 268: adjust++; 269: if (data[len-2] == '=') adjust++; 270: } 271: 272: char *decoded; 273: decoded = f_PL_Base64Decode(data, len, nullptr); 274: if (!decoded) { 275: return SECFailure; 276: } 277: if (!*decoded) { 278: return SECFailure; 279: } 280: 281: length = (len3)/4 - adjust; 282: LOG("Decoded %i chars into %i chars\n", len, *length); 283: 284: result = (unsigned char)malloc((size_t)len); 285: 286: if (!result) { 287: rv = SECFailure; 288: } else { 289: memcpy((char)*result, decoded, len); 290: } 291: f_PR_Free(decoded); 292: return rv; 293: }
[1] f_PL_Base64Decode() resolves to PL_Base64Decode().
[2] Line 281 also appears to have another bug: why should the output length |length| be adjusted based upon the presence of undecodable data in the input string of length |adjust|? Doesn't this incorrectly truncate the output?
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the output length is computed with an unchecked arithmetic expression and the copy uses the input length rather than the actual decoded length, so integer overflow/underflow and a mismatched copy size lead to out-of-bounds reads.
Impact
An attacker who can supply crafted base64 input could cause out-of-bounds reads of memory, though the author notes no proof of concept was tested and some cases only cause incorrect truncation.
What the attacker needed
Crafted base64 input to decode(); Android build using NSSBridge
The fix
The length calculation should be done with overflow-safe arithmetic and the copy should use the actual decoded length rather than the input length.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Base64 is a way of writing binary data as plain text. A function that turns it back into binary must work out how many bytes the result will be, and here that calculation is wrong in a few ways. As a result the code copies or reports more data than really exists, so it can read memory it should not touch.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.