Setting up a test environment
A bug in the H2O web server's QUIC implementation allowed an attacker to leak uninitialized memory from the server by sending a crafted sequence of QUIC frames. The leaked data could include other users' requests and responses, cookies, and internal statistics.
Published on medium.com
This post will go through a QUIC (HTTP/3) implementation bug in the H2O webserver. The bug is pretty interesting as it affected Fastly in a way that it allowed stealing random requests and responses from uninitialized memory of its’ nodes, somewhat similar to CloudBleed (but unlike CloudBleed, this vulnerability…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This is a story about a QUIC (HTTP/3) implementation bug in the H2O web server. The bug is interesting because of how it affected Fastly: it allowed stealing random requests and responses from uninitialized memory of its nodes, somewhat similar to CloudBleed, but unlike CloudBleed this vulnerability required specific actions from an attacker.
Fastly is a CDN and anti-DDoS service that acts as a reverse proxy: it accepts users' requests, processes them according to a set of complex rules, and either serves them from its cache or forwards them to an upstream, a real server that stays behind Fastly.
The research started as a hunt for HTTP Request Smuggling that could arise from HTTP/3 termination. The author had previously done similar research for HTTP/2, and one of the issues found then was related to Cloudflare, a well-known CDN and anti-DDoS service that serves as a reverse proxy behind its clients. As QUIC, the underlying protocol for HTTP/3, recently became RFC9000, the author decided to look through its implementations, and chose another target with a similar operational model: Fastly.
Setup
Setting up a Fastly service is simple. One registers an account, buys a domain, and sets up DNS records pointing to Fastly servers. After that, one needs to buy a TLS certificate, because HTTP/2 and HTTP/3 require encryption for all data.
Enabling HTTP/3 support is manual: one writes a support ticket and waits for a reply. However, it turned out that HTTP/3 does not need to be enabled for the particular service in order to test something: it is enough to find any Fastly server supporting QUIC and send requests to it. A real browser will not do that because of the missing Alt-Svc header, but a request sent directly to the QUIC port is processed regardless of the settings in the control panel, which is enough for testing.
As www.fastly.com itself is HTTP/3 enabled, resolving this domain yields some HTTP/3-supporting IPs:
$ host -t A www.fastly.comwww.fastly.com is an alias for prod.www-fastly-com.map.fastly.net.prod.www-fastly-com.map.fastly.net has address [redacted]
Requests can then be sent to it with the author's tool http2smugl, by simply spoofing the :authority pseudo-header. The tool supports HTTP/3 despite its name:
$ http2smugl request https+h3://[redacted]/ ":authority:a-domain-behind-fastly.com":status: 200content-length: 0...
Running netcat instead at the HTTP port on the upstream shows a connection with the request Fastly proxied:
$ nc -l 80GET / HTTP/1.1host: a-domain-behind-fastly.comcontent-length: 0user-agent: Mozilla/5.0Fastly-SSL: 1Fastly-Client-IP: <snip>X-Forwarded-For: <snip>X-Forwarded-Server: cache-fjr7923-FJRX-Forwarded-Host: a-domain-behind-fastly.comX-Timer: S1637693479.183877,VS0X-Varnish: 2313353937Fastly-FF: PPtav1cHmKdVa+PX0PZLG1dkeRjY/RpDKLvKU7LtCKo=!FJR!cache-fjr7923-FJRCDN-Loop: Fastly...
Finding it
Identifying the HTTP/3 terminator
Fastly discloses that it uses Varnish for caching and complex request processing. Varnish does not support HTTP/3, so there must be another piece of software that terminates HTTP/3: it accepts the HTTP/3 connection from the user's browser, decodes it, and forwards to Varnish.
Putting a newline into a request header name or value made Fastly respond with an error message, which destroyed the hope of getting HTTP Request Smuggling:
$ http2smugl request https+h3://[redacted]/ ":authority:a-domain-behind-fastly.com" "eldushechka:\n":status: 400content-length: 42content-type: text/plain; charset=utf-8found an invalid character in header value
The good news is that googling that error reveals it comes from H2O, a small HTTP/{2,3} web server that had recently implemented QUIC support. That identifies the target, and the source code can be studied in depth. H2O can also be built and set up as a reverse proxy locally, since all the necessary config examples are provided in the distribution.
A crash as a hint
Testing became interesting quickly. Running a request with the CONNECT method and a non-zero content-length value crashed the server:
$ http2smugl request https+h3://[redacted]:8443/ ":method:CONNECT" "content-length:10"...at another tab...h2o: ../lib/http3/server.c:462: void shutdown_stream(struct st_h2o_http3_server_stream_t *, int, int, int): Assertion `stream->state < [redacted]’ failed.received fatal signal 6./h2o(backtrace+0x5b)[0x47bacb]./h2o[0x932766]...
This is a simple assert with no security impact beyond DoS, but it means the QUIC code is not fuzzed to hell and can still contain bugs. The assert message mentioned an object of the type recvstate, so the author decided to look for something more exploitable around it.
QUIC streams and the frames that matter
HTTP/3 is the first HTTP version that has nothing to do with TCP. It uses a new transport protocol, QUIC, which uses UDP to transport its data. UDP provides no reliability, no order-preserving guarantees and no congestion control, so a QUIC implementation must handle all of that itself. This means a QUIC implementation has to take care of a lot more than an HTTP/{1,2} web server.
H2O uses a specially designed QUIC implementation separated into the Quicly library. The library handles everything related to QUIC: crypto handshakes for connections, retransmissions, flow control, and so on.
The logical abstraction built over a QUIC connection for data transfer is called a stream. A QUIC connection is usually used for transferring several streams. A single stream is somewhat similar to a TCP connection: data can be sent in both directions of a stream, delivery is guaranteed, and data order is preserved. When QUIC is used for HTTP/3, each stream carries a single HTTP request: the client sends request headers and body to a new stream, the server sends the response, and the stream is closed. Some streams can be initiated by the server (pushes), but those are not needed here.
On the wire, QUIC data is encoded in frames. RFC9000 defines 20 frame types; leaving aside connection establishment, flow control, path probing, delivery confirmation and crypto, only three frame types are related directly to data transfer:
- STREAM frames, which carry stream data.
- RESET_STREAM, which indicates that no more stream data will be sent; a client can send this frame to inform that a request it previously started is not needed anymore.
- STOP_SENDING, which is sent by the receiver that does not want to get more data in a stream.
Only the first two types affect the state of the receiving half of the stream, which is stored in the recvstream structure in Quicly.
Data transfer semantics
The data transferred over a QUIC stream does not have its length known before the transfer, unlike the required Content-Length header in HTTP/1.0. It is always transmitted in chunks, similar to the chunked transfer encoding of HTTP/1.1. In the QUIC world, the chunks are STREAM frames.
Each STREAM frame contains several fields:
- the stream ID;
- the offset of the frame, which indicates the offset of this particular chunk in the whole half-stream of this transfer direction (client to server or server to client);
- the length of this frame;
- the FIN flag, indicating if this frame is final in the half-stream of this stream direction. If the flag is set, the frame additionally contains the total size of this transfer;
- the stream data itself.
The offset field is needed because QUIC frames are transmitted over UDP, which does not guarantee that packets arrive in the order they were sent. Unlike TCP, where the receiver can discard out-of-order data, a QUIC receiver must hold a buffer with data that came out of order and use it when the missing prefix arrives.
The RESET_STREAM frame is sent when a side of the stream (typically the client) decides not to complete the transfer. Semantically it means that no more data will be sent in this direction. It may be sent if, for example, a browser user clicks the stop button to cancel sending a large body. However, the server may still process the request even if it received RESET_STREAM: for example, the server may have started processing before the RESET_STREAM arrived. In this case the protocol allows sending the response in the stream.
The RESET_STREAM frame contains three meaningful fields:
- stream ID;
- error code, which is not of interest here;
- the total number of bytes sent in the stream up to this moment.
The receiver should use the latter field to check if RESET_STREAM was sent after all stream data had been sent. If it indeed has all the data, it may interpret RESET_STREAM as a zero-sized STREAM frame with the FIN flag set; that is, it might act as if the stream was sent entirely and not aborted.
Exploitation
The information about H2O in this section is valid for commit d1f0f65269 and earlier. That commit is not a fix to the described vulnerability but the previous one; it is referenced only to indicate which version was tested. The actual fix landed at a68cabaeb1.
The three flaws
Quicly, unlike other implementations, does not store a buffer for out-of-order data inside it. Instead it triggers a callback function from its user, which receives the data offset as a parameter, along with the data size and the data itself. Thus the application using Quicly (H2O itself) is responsible for holding the bytes that are not usable yet because they arrived earlier than some preceding ones in the stream order.
Quicly does store the positions (start and end offsets) of already received bytes and provides an interface to access these ranges. In particular, it defines the quicly_recvstate_bytes_available function that returns the total size of the continuous prefix. From the application's point of view, this equals the number of bytes that can already be used, under the assumption that it correctly stored all data previously sent via the callback.
H2O, which is responsible for storing already arrived data, does not keep it in chunks. Instead it uses a single continuous buffer (st_h2o_http3_server_stream_t->recvbuf) and stores all fragments there, resizing the buffer if necessary. Thus the bytes corresponding to not yet arrived data are uninitialized in this buffer.
H2O of course does not store data it has already processed. Instead it moves the offset corresponding to the beginning of the buffer and subtracts the same value from the offset of the received STREAM frames. This detail does not affect the exploit and is given for clarity: an implementation that stores all the stream bytes, including ones it no longer needs, would be insane.
This behaviour gives a way to allocate a buffer that contains a lot of uninitialized data: send a STREAM frame with such an offset that there is a gap between it and the last previously sent byte. The next step is to make H2O use it, and that is where RESET_STREAM frames come in handy.
Quicly processes RESET_STREAM in the following way:
- If it already knows the total size of the stream (either from a STREAM frame with the FIN flag or from a previously received RESET_STREAM frame), it checks that the value of the total size field written in the frame matches the one it already has.
- It drops all the information about previously received byte ranges.
The latter means that after a RESET_STREAM was processed, H2O has no way to distinguish which bytes of stream->recvbuf were initialized by the client's data and which were left as-is. Even more interesting, a call to quicly_recvstate_bytes_available would return the total stream size, as if all the data left in the buffer had previously been set by STREAM frames.
The only thing left before a working exploit is to trigger sending the recvbuf contents to an upstream after the RESET_STREAM frame was sent. This relies on how H2O proxies requests to an upstream, in particular on how the request body is buffered.
When H2O receives an HTTP/3 request whose headers indicate it contains a body, it does not start processing it right away. Instead it buffers the body until it reaches a particular limit, 10240 bytes by default. After receiving the 10240th byte of the body, it switches to streaming mode (Transfer-Encoding: chunked will be sent to an HTTP/1.1 upstream in this mode) and goes on with request processing.
Any reasonable proxy implementation must handle the case when an upstream reads data more slowly than the client sends it. One way is to check if the client's buffer contains more bytes every time after something was sent to the upstream, and that is exactly what H2O does. So if the RESET_STREAM frame can be made to be processed after the request started being sent to the upstream but before all the available data has been sent, H2O will check if stream->recvbuf has more data. Due to the incorrect quicly_recvstate_bytes_available return value, the check will succeed, and H2O will proceed with forwarding data to the upstream, unaware that this data is uninitialized. The code path does not check whether the stream was cancelled via RESET_STREAM either.
To sum up, the three flaws of the Quicly/H2O combination that are chained together:
- When a RESET_STREAM frame is received,
quicly_recvstate_resetatdeps/quicly/lib/quicly.cjust clears the received ranges of thest_quicly_recvstate_tstructure. Subsequent calls toquicly_recvstate_bytes_availablereturn the total number of bytes still left in the stream, whether they were indeed received or not. - The function
handle_buffered_inputatlib/http3/server.cdoes not check if a stream has been cancelled (quicly_stop_requestedchecks if the sending part was cancelled, not the receiving one). Thus it will process uninitialized ranges ofstream->recvbufas if the client sent them. - The
proceed_request_streamingfunction atlib/http3/server.ccallshandle_buffered_inputand does not check if the stream was cancelled. This function is set as a callback that is called when the reverse proxy requests more data to be sent in streaming mode.
The exploit plan
-
The exploit establishes a connection to an H2O instance and performs a QUIC handshake.
-
The exploit sends the request headers to the instance, followed by precisely 10239 bytes of the request body. H2O receives and acknowledges them.
-
The exploit sends a specially crafted datagram that contains three QUIC frames:
- a STREAM frame with the 10240th byte of the body (that is, the offset field is set to the length of headers + 10239, and the data size is 1);
- a STREAM frame with the 30000th byte of the body, which also has the FIN flag set (that is, the offset field is set to the headers length + 29999, data size is 1, FIN flag set, and the final size field is set to the headers length + 30000);
- a RESET_STREAM frame with the final size field equal to the headers length + 30000.
The value 30000 is chosen arbitrarily. It leaks 30000 - 10240 = 19760 bytes of uninitialized data. During the actual exploitation several different values of this parameter were tried, leaking different types of data.
-
H2O processes the datagram, performing the following steps:
- after processing the first STREAM frame, it switches the HTTP request to streaming mode and initiates a connection to the upstream;
- after processing the frame with the 30000th byte of the body, it enlarges
stream->recvbufto the size 30000 and writes the last byte of it, leaving the rest uninitialized. It also sets theeosvalue to 30000 as the FIN flag is set in the frame; - after receiving the RESET_STREAM frame it calls
quicly_recvstate_reset, effectively forgetting which bytes ofstream->recvbufwere initialized and which were not.
-
The H2O reverse proxy module sends the first 10240 bytes to the upstream and requests more data by calling
proceed_request_streaming. The latter function forwards the remaining 19760 bytes to the upstream, only the last one of which was ever set; the rest leak uninitialized data.
HTTP/3 level framing was omitted from this description. In reality the data transmitted over QUIC streams is packed into HTTP/3 frames, which are different from QUIC frames and similar to HTTP/2 ones. HTTP/3 level framing does not add any difficulty to the exploitation beyond the need to adjust the calculations of the offsets in the underlying QUIC stream.
Crafting the frames
To execute the plan, specially designed QUIC frames are needed. After several tries to make a simple QUIC implementation from scratch, the author gave up and patched quic-go. All that is needed is to rename the internal directory so the frames can be crafted directly, and to add a function for the sendStream struct that adds a frame directly to the queue for sending.
After several rounds of debugging it worked against the local H2O setup. The local webserver received something that looked like uninitialized memory: it contained memory addresses, parts of readable strings, and so on. Running the exploit against Fastly worked on the first try. At the upstream, the author received a request from Fastly whose body contained an HTTP response that was obviously meant to be sent to another user (related to another website using Fastly), mixed up with binary data. Several runs returned different things: images, dumps of Fastly internal stats, cookies, and other things.
Root cause
The mistake is the interaction between two components that each behave reasonably in isolation. Quicly keeps only the received byte ranges and delegates storage of out-of-order data to its user, exposing quicly_recvstate_bytes_available as the size of the continuous prefix. H2O stores fragments in one continuous recvbuf and relies on that function to know how much of the buffer is usable. When quicly_recvstate_reset clears the received ranges on RESET_STREAM, the function starts reporting the whole remaining stream as available, while the buffer still contains uninitialized bytes for the gaps. H2O's handle_buffered_input and proceed_request_streaming do not check whether the receiving side of the stream was cancelled, so the uninitialized bytes are forwarded to the upstream as request body data.
The cancellation check that exists (quicly_stop_requested) covers the sending part, not the receiving one, and the streaming callback path performs no cancellation check at all.
Impact
On Fastly, the bug allowed stealing random requests and responses from uninitialized memory of its nodes. In practice, the upstream received request bodies containing HTTP responses meant for other users of other websites behind Fastly, mixed with binary data, and repeated runs returned images, dumps of Fastly internal stats, cookies and other data. Unlike CloudBleed, this vulnerability required specific actions from an attacker. The impact depends on being able to reach a QUIC-capable node directly, which does not require HTTP/3 to be enabled for that particular service.
Fix
The actual fix landed in H2O at commit a68cabaeb1. The bug in H2O itself was assigned CVE-2021-43848. On the Fastly side, the issue was hot-fixed in production within several days and then coordinated for disclosure. The timeline was:
- November 23 (2021): the bug was reported
- December 1: the hotfix was deployed at one instance
- December 8: the issue was fully resolved
- January 31: public disclosure
Variants to test
- Other QUIC/HTTP/3 terminators that delegate out-of-order buffering to the application and expose a bytes-available style API: send a STREAM frame with a gap, then a RESET_STREAM, and check whether the gap bytes reach the upstream. A positive result is upstream request body content that does not match what was sent.
- Proxies that switch from buffering to streaming at a fixed threshold: try to land the RESET_STREAM exactly at the threshold crossing, as done here with 10239 bytes plus one. A positive result is a body that continues past the threshold with bytes the client never sent.
- Cancellation checks that only cover the sending direction: look for code paths that test a stop-requested flag before forwarding received data. A positive result is data forwarded after a RESET_STREAM for the receiving direction.
- Different gap sizes and offsets: vary the arbitrary final size (30000 here) and observe what kind of memory is leaked. A positive result is readable strings, addresses, cookies or responses belonging to other requests.
- HTTP/3 framing adjustments: account for HTTP/3 frame headers when computing QUIC stream offsets, and verify that the leaked bytes still appear in the upstream body. A positive result confirms the bug is reachable through normal HTTP/3 framing, not only raw QUIC frames.
What happened, in plain words
A researcher found a bug in a piece of software called H2O, which is a web server. A web server is a program that sits on a computer somewhere and answers requests from browsers: you type an address, your browser asks the server for a page, and the server sends it back. H2O is special because it can speak a newer version of the web's language, HTTP/3.
The bug let an attacker make H2O send out pieces of its own memory that it had never properly filled in. Memory is the working space a program uses while it runs; when a program reserves a chunk of memory and does not write anything into it, whatever was there before (left over from other work) is still sitting in it. Sending that leftover content to someone who should not see it is called an information disclosure, or a memory leak.
The company affected was Fastly. Fastly is a CDN, which stands for content delivery network, and also an anti-DDoS service. A DDoS attack is when someone floods a service with so much traffic that it stops working; an anti-DDoS service tries to absorb or filter that flood. Fastly works as a reverse proxy: websites put Fastly in front of their real servers, and visitors talk to Fastly instead. Fastly looks at each request, applies a set of rules, and either answers from its cache (a store of saved copies of content) or passes the request on to the real server. That real server is called the upstream.
If a bug makes Fastly send out raw memory, the leftovers can include requests and responses belonging to completely different customers. The researcher compared this to CloudBleed, a famous earlier incident where a CDN leaked memory. The difference is that this bug needed an attacker to do specific things on purpose.
The research did not start as a memory-leak hunt. It started as a hunt for HTTP Request Smuggling. Request smuggling is a class of bug where an attacker crafts a request that two pieces of software in a chain (say, a proxy and the server behind it) disagree about, so that part of one request gets treated as the start of another. The researcher had looked for the same kind of bug in HTTP/2 before, and one earlier finding was in Cloudflare, another well-known CDN and anti-DDoS service that also acts as a reverse proxy. QUIC, the transport protocol underneath HTTP/3, had just been published as a standard document called RFC9000, so the researcher decided to read through implementations of it. Fastly was chosen as the target because it works in the same way as Cloudflare.
Setting up a test environment
To test Fastly, the researcher first had to become a Fastly customer. The steps are simple:
- Register an account.
- Buy a domain (a web address you own, like example.com).
- Set up DNS records that point that domain at Fastly's servers. DNS is the internet's address book: it translates a name like example.com into the numeric address of a machine.
- Buy a TLS certificate. TLS is the encryption layer behind the padlock in your browser; HTTP/2 and HTTP/3 require all data to be encrypted, so a certificate is mandatory.
One detail matters a lot: turning on HTTP/3 support for a service is manual. You write a support ticket and wait for a reply. But it turned out you do not need HTTP/3 enabled on your own service to test something. It is enough to find any Fastly server that supports QUIC and send requests straight to it. A normal browser will not do that, because the browser only learns that a server speaks HTTP/3 from a response header called Alt-Svc. Without that header the browser sticks to older versions. But a request sent directly to the QUIC port is processed anyway, regardless of what the control panel says. That is enough for testing.
Since www.fastly.com itself has HTTP/3 enabled, looking up that domain gives you some addresses of machines that speak HTTP/3:
$ host -t A www.fastly.comwww.fastly.com is an alias for prod.www-fastly-com.map.fastly.net.prod.www-fastly-com.map.fastly.net has address [redacted]
Line by line:
host -t A www.fastly.comis a command that asks DNS for theArecords of the namewww.fastly.com. AnArecord maps a name to an IPv4 address.- The output says
www.fastly.comis an alias forprod.www-fastly-com.map.fastly.net, meaning the name is a nickname that points at another name. - That second name has the address
[redacted], which is a Fastly machine.
With an address in hand, requests can be sent to it using the researcher's own tool, http2smugl. Despite the name, the tool supports HTTP/3. The trick is to spoof the :authority pseudo-header. In HTTP/2 and HTTP/3, the request is split into headers, and :authority is the header that carries the host name the request is meant for. By setting it to a domain that sits behind Fastly, you make Fastly treat the request as if it were addressed to that customer, even though you connected to a generic Fastly address:
$ http2smugl request https+h3://[redacted]/ ":authority:a-domain-behind-fastly.com":status: 200content-length: 0...
Line by line:
http2smugl requesttells the tool to send a single request.https+h3://[redacted]/is the target: the Fastly address, over HTTP/3.":authority:a-domain-behind-fastly.com"sets the host header to a customer domain, which is the spoofing step.:status: 200,content-length: 0and the trailing...are the start of the response the tool printed back: status 200 means success, and the response had no body.
To confirm that Fastly really passed the request on, the researcher ran netcat on the upstream machine, listening on port 80 (the standard unencrypted web port). Netcat is a small tool that opens a network connection and prints whatever arrives. The output shows the request as Fastly delivered it:
$ nc -l 80GET / HTTP/1.1host: a-domain-behind-fastly.comcontent-length: 0user-agent: Mozilla/5.0Fastly-SSL: 1Fastly-Client-IP: <snip>X-Forwarded-For: <snip>X-Forwarded-Server: cache-fjr7923-FJRX-Forwarded-Host: a-domain-behind-fastly.comX-Timer: S1637693479.183877,VS0X-Varnish: 2313353937Fastly-FF: PPtav1cHmKdVa+PX0PZLG1dkeRjY/RpDKLvKU7LtCKo=!FJR!cache-fjr7923-FJRCDN-Loop: Fastly...
Line by line:
nc -l 80starts netcat in listening mode on port 80.GET / HTTP/1.1is the request line: the method isGET, the path is/, and the protocol is HTTP/1.1. Fastly translated the HTTP/3 request into an HTTP/1.1 request for the upstream.host: a-domain-behind-fastly.comis the host header, matching the spoofed:authority.content-length: 0says the request has no body.user-agent: Mozilla/5.0is the browser identification string that was sent.Fastly-SSL: 1is a header Fastly adds to say the original connection was encrypted.Fastly-Client-IPandX-Forwarded-Forcarry the visitor's address; the report redacts them as<snip>.X-Forwarded-Server: cache-fjr7923-FJRnames the Fastly machine that handled the request.X-Forwarded-Host: a-domain-behind-fastly.comrepeats the original host.X-Timer: S1637693479.183877,VS0is a timing header.X-Varnish: 2313353937is an identifier from Varnish, the caching software Fastly uses.Fastly-FF: ...is a Fastly-specific header.CDN-Loop: Fastlymarks the request as having passed through Fastly, so it is not looped back again.
So the setup works: a request sent to a generic Fastly address, with a spoofed host, reaches the right customer's upstream.
Finding it
Identifying the HTTP/3 terminator
Fastly says publicly that it uses Varnish for caching and for its complex request processing. Varnish does not support HTTP/3. So some other piece of software must be terminating HTTP/3: accepting the HTTP/3 connection from the browser, decoding it, and handing it to Varnish. That piece of software is the interesting target.
To probe it, the researcher put a newline character into a request header name or value. Newlines are special in HTTP because they separate headers from each other; if a proxy and a server disagree about where a header ends, that is the raw material for request smuggling. Fastly answered with an error, which ruled out that particular smuggling idea:
$ http2smugl request https+h3://[redacted]/ ":authority:a-domain-behind-fastly.com" "eldushechka:\n":status: 400content-length: 42content-type: text/plain; charset=utf-8found an invalid character in header value
Line by line:
- The command sends a request to the Fastly address, again with a spoofed
:authority. "eldushechka:\n"is a header whose name iseldushechkaand whose value contains a newline (\n).:status: 400is the response status: 400 means bad request, the server refused it.content-length: 42says the error message is 42 bytes long.content-type: text/plain; charset=utf-8says the error is plain text.found an invalid character in header valueis the error message itself.
The good news is that googling that exact error message shows it comes from H2O, a small web server that supports HTTP/2 and HTTP/3 and had recently added QUIC support. Now the target is identified, and its source code can be read in depth. H2O can also be built and run locally as a reverse proxy, because the distribution includes example configurations. Running it locally means the researcher can experiment without touching Fastly.
A crash as a hint
Testing got interesting quickly. Sending a request with the CONNECT method and a non-zero content-length crashed the server. CONNECT is an HTTP method normally used to ask a proxy to open a tunnel to somewhere else; it is not meant to carry a body, so a content-length on it is unusual:
$ http2smugl request https+h3://[redacted]:8443/ ":method:CONNECT" "content-length:10"...at another tab...h2o: ../lib/http3/server.c:462: void shutdown_stream(struct st_h2o_http3_server_stream_t *, int, int, int): Assertion `stream->state < [redacted]’ failed.received fatal signal 6./h2o(backtrace+0x5b)[0x47bacb]./h2o[0x932766]...
Line by line:
- The command sends a request to
[redacted]:8443, which is the local machine (localhost) on port 8443, where the researcher's own copy of H2O is listening. ":method:CONNECT"sets the HTTP method toCONNECT."content-length:10"claims the request has a 10-byte body....at another tab...marks a switch to the other terminal window, where the server's output appeared.h2o: ../lib/http3/server.c:462: void shutdown_stream(...): Assertion 'stream->state < [redacted]' failed.is the crash message. An assertion is a check a program makes about its own state; when the check fails, the program stops on purpose. Here the check was about the state of a stream, and the state was not what the code expected.received fatal signal 6means the process was killed by the operating system (signal 6 is the abort signal).- The following lines are a backtrace, a list of function calls showing where the crash happened.
This is a simple assertion failure. On its own it only lets someone crash the server, which is a denial of service (DoS), not a way to steal data. But it shows that the QUIC code had not been hammered by fuzzing. Fuzzing is automated testing that feeds a program huge amounts of random or semi-random input looking for crashes. If a simple request can crash it, there may be worse bugs nearby. The crash message mentioned an object of type recvstate, so the researcher went looking around that object for something more serious.
QUIC streams and the frames that matter
HTTP/3 is the first version of HTTP that has nothing to do with TCP. TCP is the traditional transport protocol of the internet: it delivers a stream of bytes reliably and in order, and it is what HTTP/1.1 and HTTP/2 run on. HTTP/3 instead uses QUIC, a brand new transport protocol that runs over UDP. UDP is a much simpler protocol: it just fires packets off and does not promise that they arrive, that they arrive in order, or that the sender slows down when the network is congested. Congestion control is the mechanism that stops a sender from overwhelming the network. Because UDP offers none of these guarantees, a QUIC implementation has to provide reliability, ordering and congestion control itself. That means a QUIC implementation has far more work to do than an HTTP/1.1 or HTTP/2 server, and more code means more places for bugs.
H2O uses a separate library for QUIC, called Quicly. The library handles everything QUIC-related: the cryptographic handshake that sets up a connection, retransmissions of lost data, flow control (making sure one side does not send faster than the other can take), and so on.
The logical unit for moving data over a QUIC connection is called a stream. One QUIC connection usually carries several streams at once. A single stream is a bit like a TCP connection: data can flow in both directions, delivery is guaranteed, and the order is preserved. When QUIC is used for HTTP/3, each stream carries exactly one HTTP request: the client sends the request headers and body on a new stream, the server sends the response, and the stream is closed. Some streams can be started by the server (these are called pushes), but they are not needed here.
On the wire, QUIC data is packed into frames. A frame is a small structured chunk with a type and some fields. RFC9000 defines 20 frame types. Leaving aside connection setup, flow control, path probing, delivery confirmation and cryptography, only three frame types deal directly with moving data:
- STREAM frames, which carry the actual stream data.
- RESET_STREAM, which says that no more stream data will be sent. A client can send it to say that a request it started is no longer needed.
- STOP_SENDING, which is sent by the receiver when it does not want any more data on a stream.
Only the first two affect the state of the receiving half of a stream. That state is stored in a structure called recvstream inside Quicly. A structure, in programming, is a bundle of related fields kept together under one name.
Data transfer semantics
The data sent over a QUIC stream does not have a known length before the transfer starts. That is different from HTTP/1.0, where a Content-Length header was required up front. Instead, data is always sent in chunks, similar to the chunked transfer encoding of HTTP/1.1. In QUIC, the chunks are STREAM frames.
Each STREAM frame contains several fields:
- the stream ID, which says which stream the chunk belongs to;
- the offset, which says where this chunk sits in the whole flow of bytes in this direction (client to server, or server to client);
- the length of the frame;
- the FIN flag, which marks this frame as the last one in this direction. If the flag is set, the frame also carries the total size of the transfer;
- the stream data itself.
The offset field exists because QUIC frames travel over UDP, and UDP does not guarantee that packets arrive in the order they were sent. TCP lets the receiver simply throw away data that arrives out of order, because TCP will resend it. QUIC cannot do that: the receiver has to keep a buffer of out-of-order data and use it once the missing earlier bytes arrive. A buffer is just a reserved area of memory used to hold data temporarily.
The RESET_STREAM frame is sent when one side of a stream (usually the client) decides not to finish the transfer. It means no more data will be sent in that direction. A browser user clicking the stop button while uploading a large body is a typical reason. But the server may still process the request even after receiving RESET_STREAM, because it may have started processing before the reset arrived. The protocol allows the server to send its response on the stream in that case.
The RESET_STREAM frame has three meaningful fields:
- the stream ID;
- an error code, which does not matter here;
- the total number of bytes sent in the stream so far.
The receiver is supposed to use that last field to check whether RESET_STREAM arrived after all the stream data had been sent. If all the data really is there, the receiver may treat RESET_STREAM as if it were a zero-length STREAM frame with the FIN flag set. In other words, it may act as if the stream was completed normally rather than aborted.
Exploitation
The details about H2O in this section apply to commit d1f0f65269 and earlier. A commit is a saved snapshot of a code repository. That commit is not the fix for this bug; it is simply the version that was tested. The actual fix landed at commit a68cabaeb1.
The three flaws
Quicly, unlike some other QUIC implementations, does not keep a buffer of out-of-order data inside itself. Instead it calls a callback function belonging to its user. A callback is a function you hand to a library so the library can call it later. The callback receives the data offset, the data size and the data itself. So the application using Quicly, which here is H2O, is responsible for holding bytes that cannot be used yet because they arrived before some earlier bytes in the stream.
Quicly does keep track of the positions (start and end offsets) of the bytes it has already received, and it offers a way to query those ranges. In particular, it has a function called quicly_recvstate_bytes_available that returns the size of the continuous prefix, meaning the run of bytes from the start with no gaps. From the application's point of view, that number equals how many bytes are ready to use, assuming the application correctly stored everything the callback handed it.
H2O, which is the one storing the arrived data, does not keep it in separate chunks. It uses one continuous buffer, st_h2o_http3_server_stream_t->recvbuf, and copies every fragment into it, growing the buffer when needed. So the bytes that correspond to data not yet received are uninitialized in that buffer: nothing has been written there, and whatever was in memory before is still there.
H2O naturally does not keep data it has already processed. It moves the start of the buffer forward and subtracts the same amount from the offsets of incoming STREAM frames. This detail does not affect the exploit; it is mentioned only for clarity, because an implementation that kept every byte forever would be absurd.
This behaviour gives a way to allocate a buffer full of uninitialized data: send a STREAM frame with an offset so far ahead that there is a gap between it and the last byte sent before. Now the buffer has a hole in it. The next step is to make H2O actually use that hole, and that is where RESET_STREAM comes in.
Quicly handles RESET_STREAM like this:
- If it already knows the total size of the stream (from a STREAM frame with the FIN flag, or from an earlier RESET_STREAM), it checks that the total size in this frame matches the one it already has.
- It throws away all the information about which byte ranges it had received.
The second point is the key. After a RESET_STREAM is processed, H2O can no longer tell which bytes of stream->recvbuf were filled in by the client and which were left untouched. Even better for the attacker, a call to quicly_recvstate_bytes_available now returns the whole stream size, as if every byte left in the buffer had been delivered by STREAM frames.
One thing remains before this becomes a working exploit: making H2O send the contents of recvbuf to an upstream after the RESET_STREAM. That depends on how H2O proxies requests, and specifically on how it buffers the request body.
When H2O receives an HTTP/3 request whose headers say it has a body, it does not start processing immediately. It buffers the body until it reaches a limit, 10240 bytes by default. Once the 10240th byte of the body arrives, it switches to streaming mode. In streaming mode, an HTTP/1.1 upstream is sent Transfer-Encoding: chunked, which means the body will be delivered in pieces of unknown total length, and processing continues.
Any sensible proxy has to cope with an upstream that reads data more slowly than the client sends it. One way is to check, every time something has been sent to the upstream, whether the client's buffer holds more bytes. That is exactly what H2O does. So if the RESET_STREAM can be made to arrive after the request has started being sent to the upstream but before all the available data has gone out, H2O will check whether stream->recvbuf has more data. Because quicly_recvstate_bytes_available now returns a wrong, too-large value, the check succeeds, and H2O forwards the data to the upstream without knowing that the data is uninitialized. That code path does not check whether the stream was cancelled by RESET_STREAM either.
To sum up, three flaws in the Quicly and H2O combination are chained together:
- When a RESET_STREAM frame arrives,
quicly_recvstate_resetindeps/quicly/lib/quicly.csimply clears the received ranges in thest_quicly_recvstate_tstructure. Later calls toquicly_recvstate_bytes_availablereturn the total number of bytes still left in the stream, whether they were really received or not. - The function
handle_buffered_inputinlib/http3/server.cdoes not check whether a stream has been cancelled. The check that exists,quicly_stop_requested, looks at the sending part of the stream, not the receiving part. Sohandle_buffered_inputprocesses uninitialized ranges ofstream->recvbufas if the client had sent them. - The function
proceed_request_streaminginlib/http3/server.ccallshandle_buffered_inputand also does not check whether the stream was cancelled. This function is registered as a callback that the reverse proxy calls when it wants more data in streaming mode.
The exploit plan
-
The exploit connects to an H2O instance and performs a QUIC handshake. The handshake is the opening exchange where the two sides agree on encryption keys and parameters.
-
The exploit sends the request headers, followed by exactly 10239 bytes of request body. H2O receives them and acknowledges them. Acknowledging means confirming to the sender that the data arrived.
-
The exploit sends one specially crafted datagram containing three QUIC frames. A datagram is a single UDP packet.
- a STREAM frame carrying the 10240th byte of the body. Its offset field is the length of the headers plus 10239, and its data size is 1;
- a STREAM frame carrying the 30000th byte of the body, with the FIN flag set. Its offset field is the headers length plus 29999, its data size is 1, the FIN flag is set, and the final size field is the headers length plus 30000;
- a RESET_STREAM frame whose final size field is the headers length plus 30000.
The value 30000 is arbitrary. It leaks 30000 minus 10240, which is 19760 bytes of uninitialized data. During the actual exploitation the researcher tried several different values and leaked different kinds of data.
-
H2O processes the datagram in these steps:
- after the first STREAM frame, it switches the HTTP request to streaming mode and opens a connection to the upstream;
- after the frame carrying the 30000th byte, it grows
stream->recvbufto 30000 bytes and writes the last byte, leaving the rest uninitialized. It also sets theeosvalue to 30000 because the FIN flag is set; - after the RESET_STREAM frame, it calls
quicly_recvstate_reset, which makes it forget which bytes ofstream->recvbufwere initialized and which were not.
-
The H2O reverse proxy module sends the first 10240 bytes to the upstream and asks for more by calling
proceed_request_streaming. That function forwards the remaining 19760 bytes, of which only the last one was ever written; the rest leak uninitialized memory.
One thing left out of this description is HTTP/3 level framing. In reality, the data carried over QUIC streams is packed into HTTP/3 frames, which are different from QUIC frames and similar to HTTP/2 frames. This does not make exploitation any harder; it only means the offset calculations in the underlying QUIC stream have to be adjusted.
Crafting the frames
To carry out the plan, specially built QUIC frames are needed. After several attempts to write a simple QUIC implementation from scratch, the researcher gave up and patched quic-go instead. quic-go is an existing QUIC library written in the Go programming language. All that was needed was to rename the internal directory so the frames could be built directly, and to add a function to the sendStream structure that puts a frame straight into the send queue.
After several rounds of debugging, it worked against the local H2O setup. The local web server received something that looked like uninitialized memory: it contained memory addresses, fragments of readable strings, and so on. Running the exploit against Fastly worked perfectly on the first try. At the upstream, the researcher received a request from Fastly whose body contained an HTTP response that was clearly meant for another user, on another website behind Fastly, mixed with binary data. Several runs returned different things: images, dumps of Fastly internal statistics, cookies, and other data. A cookie is a small piece of data a website stores in your browser to recognise you, and it is often sensitive.
Root cause
The mistake is in how two components interact, even though each behaves reasonably on its own. Quicly keeps only the received byte ranges and leaves storage of out-of-order data to its user, exposing quicly_recvstate_bytes_available as the size of the continuous prefix. H2O stores fragments in one continuous recvbuf and relies on that function to know how much of the buffer is usable. When quicly_recvstate_reset clears the received ranges on RESET_STREAM, the function starts reporting the whole remaining stream as available, while the buffer still holds uninitialized bytes in the gaps. H2O's handle_buffered_input and proceed_request_streaming do not check whether the receiving side of the stream was cancelled, so the uninitialized bytes are forwarded to the upstream as request body data.
The cancellation check that exists, quicly_stop_requested, covers the sending part, not the receiving part, and the streaming callback path has no cancellation check at all.
Impact
On Fastly, the bug allowed stealing random requests and responses from uninitialized memory of its nodes. In practice, the upstream received request bodies containing HTTP responses meant for other users of other websites behind Fastly, mixed with binary data, and repeated runs returned images, dumps of Fastly internal statistics, cookies and other data. Unlike CloudBleed, this vulnerability required specific actions from an attacker. The impact depends on being able to reach a QUIC-capable node directly, which does not require HTTP/3 to be enabled for that particular service.
Fix
The actual fix landed in H2O at commit a68cabaeb1. The bug in H2O itself was assigned CVE-2021-43848. A CVE is a public identifier for a known vulnerability, so people can refer to it unambiguously. On the Fastly side, the issue was hot-fixed in production within several days, and then disclosure was coordinated. A hotfix is an urgent patch applied to a live system. The timeline was:
- November 23 (2021): the bug was reported
- December 1: the hotfix was deployed at one instance
- December 8: the issue was fully resolved
- January 31: public disclosure
Variants to test
- Other QUIC/HTTP/3 terminators that leave out-of-order buffering to the application and expose a bytes-available style function: send a STREAM frame with a gap, then a RESET_STREAM, and check whether the gap bytes reach the upstream. A positive result is upstream request body content that does not match what was sent.
- Proxies that switch from buffering to streaming at a fixed threshold: try to land the RESET_STREAM exactly at the threshold crossing, as done here with 10239 bytes plus one. A positive result is a body that continues past the threshold with bytes the client never sent.
- Cancellation checks that only cover the sending direction: look for code paths that test a stop-requested flag before forwarding received data. A positive result is data forwarded after a RESET_STREAM for the receiving direction.
- Different gap sizes and offsets: vary the arbitrary final size (30000 here) and observe what kind of memory is leaked. A positive result is readable strings, addresses, cookies or responses belonging to other requests.
- HTTP/3 framing adjustments: account for HTTP/3 frame headers when computing QUIC stream offsets, and verify that the leaked bytes still appear in the upstream body. A positive result confirms the bug is reachable through normal HTTP/3 framing, not only raw QUIC frames.
Glossary
- HTTP: the language browsers and web servers use to exchange requests and responses.
- HTTP/3: the newest version of HTTP, which runs over QUIC instead of TCP.
- QUIC: a transport protocol that runs over UDP and provides reliability, ordering and congestion control itself.
- UDP: a simple transport protocol that sends packets without guarantees of delivery or order.
- TCP: the traditional reliable, ordered transport protocol used by HTTP/1.1 and HTTP/2.
- RFC9000: the standard document that defines QUIC.
- Web server: a program that answers requests from browsers.
- H2O: a small web server that supports HTTP/2 and HTTP/3.
- Quicly: the QUIC library used by H2O.
- Fastly: a CDN and anti-DDoS service that acts as a reverse proxy.
- CDN: content delivery network, a service that sits in front of websites and serves or forwards their traffic.
- Anti-DDoS: protection against distributed denial of service attacks, where floods of traffic try to knock a service offline.
- Reverse proxy: a server that sits in front of real servers and forwards requests to them.
- Upstream: the real server behind a proxy.
- Cache: a store of saved copies of content, used to answer requests faster.
- Varnish: caching software that Fastly uses.
- DNS: the internet's address book, translating names into numeric addresses.
- A record: a DNS record that maps a name to an IPv4 address.
- TLS: the encryption layer behind the padlock in browsers.
- Certificate: a document that proves a server's identity and enables TLS.
- Alt-Svc: a response header that tells a browser a server also speaks HTTP/3.
- Header: a named field in an HTTP request or response.
- Pseudo-header: a header in HTTP/2 and HTTP/3 whose name starts with a colon, such as
:authorityor:method. :authority: the pseudo-header that carries the host name a request is meant for.:method: the pseudo-header that carries the HTTP method.- HTTP method: the verb of a request, such as GET or CONNECT.
- CONNECT: an HTTP method used to ask a proxy to open a tunnel.
- Content-Length: a header stating the size of a request or response body.
- Transfer-Encoding: chunked: a way of sending a body in pieces without stating its total length up front.
- Status code: a number in a response saying what happened, such as 200 for success or 400 for a bad request.
- Netcat: a small tool that opens network connections and prints what arrives.
- Port: a numbered channel on a machine that a service listens on.
- Localhost: the local machine itself, address 127.0.0.1.
- HTTP Request Smuggling: a bug class where two pieces of software disagree about where one request ends and the next begins.
- CloudBleed: an earlier incident where a CDN leaked memory containing other customers' data.
- Cloudflare: a well-known CDN and anti-DDoS service.
- Information disclosure: a bug that lets someone see data they should not see.
- Memory leak: here, sending out leftover contents of memory that were never properly initialised.
- Uninitialized memory: memory that was reserved but never written to, so it still holds whatever was there before.
- Buffer: a reserved area of memory used to hold data temporarily.
- Stream: the logical unit for moving data over a QUIC connection.
- Frame: a small structured chunk of protocol data with a type and fields.
- STREAM frame: a QUIC frame that carries stream data.
- RESET_STREAM: a QUIC frame saying no more data will be sent in a direction.
- STOP_SENDING: a QUIC frame sent by a receiver that wants no more data.
- Offset: the position of a chunk within the whole flow of bytes.
- FIN flag: a flag marking the last frame in a direction, carrying the total size.
- Datagram: a single UDP packet.
- Handshake: the opening exchange that sets up encryption and parameters for a connection.
- Acknowledge: to confirm to the sender that data arrived.
- Callback: a function handed to a library so the library can call it later.
- Structure: a bundle of related fields kept together under one name in a program.
- Commit: a saved snapshot of a code repository.
- Assertion: a check a program makes about its own state, stopping if it fails.
- Backtrace: a list of function calls showing where a crash happened.
- Signal 6: the abort signal, which kills a process.
- Fuzzing: automated testing with large amounts of random or semi-random input to find crashes.
- Denial of service (DoS): making a service unavailable, for example by crashing it.
- Congestion control: the mechanism that stops a sender from overwhelming the network.
- Flow control: making sure one side does not send faster than the other can take.
- quic-go: an existing QUIC library written in Go, which the researcher patched.
- Go: a programming language.
- CVE: a public identifier for a known vulnerability.
- Hotfix: an urgent patch applied to a live system.
- Cookie: a small piece of data a website stores in your browser to recognise you.
- Binary data: raw bytes that are not readable text.