Possible use after free caused by WebSocket::Send()
WebSocket::Send() adds the message length to a 32-bit buffered-amount counter without checking for overflow, so a very large send can wrap the counter to zero. That makes the keep-alive logic believe nothing is buffered and release the implementation object early, which the reporter suspects could lead to a use-after-free, though the proof of concept did not actually trigger one.
- Fixed in 10 days
- Paid in 17 days
- Disclosed after 10 days
Created attachment 8874049
bug_840_poc_1.htm
WebSocket::Send() (dom\base\WebSocket.cpp) can cause an integer overflow. If it does, it spuriously can cause its |mOutgoingBufferedAmount| variable to become 0. If that occurs, WebSocket::UpdateMustKeepAlive() spuriously calls |mImpl->ReleaseObject()|. It seems likely that this can cause |mImpl| to be deleted too soon, thus causing a use after free.
I have attached a POC that causes the overflow and subsequent spurious call to |mImpl->ReleaseObject()|. However, the POC does not appear to cause a use after free. I suspect I am missing some small change that would cause the problem, but I don't know what it is.
The bug is in line 2504, which does not check for overflow of the |uint32_t| variable |mOutgoingBufferedAmount|:
2488: void
2489: WebSocket::Send(nsIInputStream* aMsgStream,
2490: const nsACString& aMsgString,
2491: uint32_t aMsgLength,
2492: bool aIsBinary,
2493: ErrorResult& aRv)
2494: {
2495: AssertIsOnTargetThread();
2496:
2497: int64_t readyState = ReadyState();
2498: if (readyState == CONNECTING) {
2499: aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
2500: return;
2501: }
2502:
2503: // Always increment outgoing buffer len, even if closed
2504: mOutgoingBufferedAmount += aMsgLength;
2505:
2506: if (readyState == CLOSING ||
2507: readyState == CLOSED) {
2508: return;
2509: }
2510:
2511: // We must have mImpl when connected.
2512: MOZ_ASSERT(mImpl);
2513: MOZ_ASSERT(readyState == OPEN, "Unknown state in WebSocket::Send");
2514:
2515: nsresult rv;
2516: if (aMsgStream) {
2517: rv = mImpl->mChannel->SendBinaryStream(aMsgStream, aMsgLength);
2518: } else {
2519: if (aIsBinary) {
2520: rv = mImpl->mChannel->SendBinaryMsg(aMsgString);
2521: } else {
2522: rv = mImpl->mChannel->SendMsg(aMsgString);
2523: }
2524: }
2525:
2526: if (NS_FAILED(rv)) {
2527: aRv.Throw(rv);
2528: return;
2529: }
2530:
2531: UpdateMustKeepAlive();
2532: }
Use the POC by starting FF and attaching a debugger to it. Use an x64 build on a machine with at least 16 GB of memory. Set BPs on lines 246 (~WebsocketImpl()) and 2504. Make the BP on line 2504 conditional on |mOutgoingBufferedAmount >= 0xf0000000|. Setup a WebSocket server that discards incoming messages, and that can receive messages >= 0x10000000 bytes long, and have it listen at ws://127.0.0.1/websock . [1] Put the POC files on your webserver and load bug_840_poc_1.htm in FF. Wait for the BP on line 2504, then step that line and watch the overflow. Now run to line 2531 and trace into UpdateMustKeepAlive(). Notice how it spuriously leaves |shouldKeepAlive| as |false| because line 2189 sees that |mOutgoingBufferedAmount == 0|, then spuriously calls |mImpl->ReleaseObject()| on line 2204. Notice, however, that ~WebsocketImpl() doesn't appear to get called early.
[1] I use the WebSocket++ library and its echo_server sample (https://github.com/zaphoyd/websocketpp). I modified echo_server.cpp to not echo the message (just empty on_message() of everything but the block beginning
if (msg->get_payload() ...
). Then |modify max_message_size| in core.hpp to
...max_message_size = 0x10000000;
and build and run.
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 code adds the message length to a 32-bit unsigned counter without checking for overflow, so the counter can wrap to zero and mislead the keep-alive logic.
Impact
The reporter suspects the early release could cause a use-after-free, but the proof of concept did not actually trigger one, so the exploitable impact is unconfirmed.
What the attacker needed
64-bit Firefox build; at least 16 GB of memory; attacker-controlled page; local WebSocket server accepting huge messages
How it was found
- Start Firefox with a debugger attached on a 64-bit build with at least 16 GB of memory.
- Set breakpoints on the WebSocket implementation destructor and on the send line, making the latter conditional on the buffered amount being near its maximum.
- Run a WebSocket server that discards incoming messages and accepts messages of at least 0x10000000 bytes, listening locally.
- Load the proof-of-concept page from a web server in Firefox.
- When the breakpoint hits, step over the line and observe the counter overflow.
- Continue to the keep-alive update and observe it wrongly deciding not to keep the object alive and releasing it.
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.
A WebSocket is a two-way connection between a browser page and a server. The browser keeps a running count of how many bytes are waiting to be sent, and that count has a fixed maximum size. If you send more than the maximum, the count wraps around to a small number (or zero), so the browser wrongly thinks nothing is pending and may free memory it still needs.
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.