Write beyond bounds in TransactionObserver::OnDataAvailable()
An integer overflow in TransactionObserver::OnDataAvailable() can cause underallocation and a write beyond bounds in the chrome process heap. An attacker could potentially write attacker-controlled data beyond the allocated buffer, leading to memory corruption and a crash.
Originally disclosed on Mozilla
- Fixed in 4 months
- Paid in 5 months
- Disclosed after 4 months
Created attachment 9323764
ffbug_2106.htm
`TransactionObserver::OnDataAvailable()` (`netwerk/protocol/http/AlternateServices.cpp`) can experience an integer overflow and subsequent underallocation and write beyond bounds (WBB) under specific conditions. The data written beyond bounds is unmodified attacker-provided data, and it is written into the heap of the chrome process.
The bug is in line 824, which adds two `uint32_t`s without checking for overflow:
'''
818: NS_IMETHODIMP
819: TransactionObserver::OnDataAvailable(nsIRequest* aRequest,
820: nsIInputStream* aStream, uint64_t aOffset,
821: uint32_t aCount) {
822: MOZ_ASSERT(NS_IsMainThread());
823: uint32_t oldLen = mWKResponse.Length();
824: uint64_t newLen = aCount + oldLen;
825: if (newLen < MAX_WK) {
826: auto handleOrErr = mWKResponse.BulkWrite(newLen, oldLen, false);
827: if (handleOrErr.isErr()) {
828: return handleOrErr.unwrapErr();
829: }
830: auto handle = handleOrErr.unwrap();
831: uint32_t amtRead;
832: if (NS_SUCCEEDED(
833: aStream->Read(handle.Elements() + oldLen, aCount, &amtRead))) {
834: MOZ_ASSERT(oldLen + amtRead <= newLen);
835: handle.Finish(oldLen + amtRead, false);
836: LOG(("TransactionObserver onDataAvailable %p read %d of .wk [%zd]\n",
837: this, amtRead, mWKResponse.Length()));
838: } else {
839: LOG(("TransactionObserver onDataAvailable %p read error\n", this));
840: }
841: }
842: return NS_OK;
843: }
'''
The conditions required are (1) some nonstandard prefs; and (2) a main thread that stalls at the right time and long enough for the socket thread to accumulate enough data to trigger the overflow. These conditions are unlikely to occur in the wild, though probably there are other combinations of prefs that would elicit the same behavior, and there probably still are ways to monopolize the chrome process's main thread for long periods of time.
Attached is a POC that shows the overflow and WBB. Use the POC thusly:
1. Copy `ffbug_2106.htm` to a webserver.
2. Compile and run `gen-attack.cpp` and copy the resulting file to `/.well_known/http-opportunistic` on the same webserver as in step 1. This file can contain any attacker-provided data, but must be large enough to cause the overflow.
3. Configure the webserver to serve HTTP content on port `444` (as well as the default port `80`) and to send the header
'''
Alt-Svc: h2=":444"
'''
4. Start FF and set prefs as follows:
'''
network.http.altsvc.oe true
network.buffer.cache.size 510 // yes, really 510 and not 512
network.buffer.cache.count 8421510 // not critical, just has to be large enough
'''
5. Attach a debugger to FF and set a BP on line 824, above.
6. Load `ffbug_2106.htm` in FF.
7. When the BP fires, freeze the thread (should be main thread in the chrome process) to simulate a lengthy operation on the main thread. Continue execution.
8. FF will accumulate > 4GB of memory usage. When it stops adding memory, use the debugger to break into execution.
9. Thaw the thread that you froze in step 7. Proceed.
10. When you get a second BP, aCount probably will be >= `0x10000`, which would make the remainder of the function a no-op. Just proceed.
11. After a few seconds you'll get a third BP, this time with `aCount` == `0xfffffefe`. Now step lines 823-24 and notice that `oldLen` probably == `0x200`, and that `aCount + oldLen` on line 824 overflowed, causing `newLen` to have the value `0xfe`.
12. Step through lines 822-23 and watch them write beyond bounds. Proceed and the program probably eventually will hit a no-access page and crash.
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
The code adds two 32-bit unsigned integers without checking for overflow, leading to a small allocation and subsequent out-of-bounds write.
Impact
An attacker could write attacker-controlled data beyond the bounds of a heap buffer in the chrome process, potentially leading to memory corruption and a crash, though the required conditions are unlikely to occur in the wild.
What the attacker needed
Nonstandard prefs set; main thread stalls at right time; attacker controls webserver
How it was found
- Copy the provided HTML file to a webserver.
- Compile and run the generator program and place the resulting file at /.well_known/http-opportunistic on the same webserver.
- Configure the webserver to serve HTTP on port 444 and send an Alt-Svc header pointing to h2 on that port.
- Start Firefox and set the specified preferences.
- Attach a debugger, set a breakpoint on line 824, and load the HTML file.
- When the breakpoint fires, freeze the main thread to simulate a long operation, then continue and later thaw the thread to trigger the overflow.
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.
This bug is a memory safety issue where a calculation that adds two numbers can wrap around to a small value, causing the program to allocate too little memory and then write more data than fits. An attacker who can control the data being written might be able to corrupt memory, which could crash the browser or potentially be exploited for code execution.
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.