Skip to content

Fix QUIC use-after-free during handshake and mp4 heap buffer overflow - #2020

Open
dhivadhiva wants to merge 2 commits into
nginx:masterfrom
dhivadhiva:fix/mp4-moov-refill-overflow
Open

dhivadhiva wants to merge 2 commits into
nginx:masterfrom
dhivadhiva:fix/mp4-moov-refill-overflow

Conversation

@dhivadhiva

Copy link
Copy Markdown

Problem

This PR fixes two independent memory safety issues, found by source
review and confirmed with AddressSanitizer on current master
(2e8440b). Each fix is a separate commit.

1. QUIC: the main connection can be freed during its own handshake

A QUIC main connection is marked reusable as soon as it is created
(ngx_quic_new_connection()). When a worker is low on free connections,
ngx_get_connection() drains reusable connections by closing them
through their read handlers.

During the TLS handshake of a QUIC connection, nginx code running for
that connection can itself open a connection:

  • with ssl_stapling on, the certificate status callback called from
    SSL_do_handshake() starts an OCSP staple update;
  • with ssl_ocsp on, ngx_quic_init_streams() starts OCSP validation
    of the client certificate.

If the handshaking connection is the one picked by the drain, it is
closed and freed while its handshake is still on the stack:

  • with stapling, SSL_do_handshake() continues on a freed SSL
    connection and the worker process crashes; no client certificate is
    needed;
  • with client certificate OCSP, the OCSP context is freed while
    ngx_event_connect_peer() is still using it (AddressSanitizer:
    heap-use-after-free in ngx_event_connect_peer(), freed via
    ngx_drain_connections() -> ngx_quic_close_connection() ->
    ngx_ssl_ocsp_cleanup()).

TCP connections are not affected, because ngx_http_ssl_handshake()
makes the connection non-reusable before the handshake. b5252da fixed
the same kind of issue for ngx_quic_create_stream() only.

2. Mp4: heap buffer overflow after a buffer refill while parsing moov

While the moov atom is parsed it is kept in the buffer in full, and the
atoms found are referenced by pointers into that buffer.
ngx_http_mp4_read() refills the buffer from its start whenever the
requested bytes are not in it.

ngx_http_mp4_read_atom() reads the next atom header without checking
that the header fits into the parent atom. If the last atom in moov is
followed by 1 to 3 stray bytes and moov ends exactly at the end of the
buffer, the read refills the buffer with the file data following moov,
replacing the atoms already parsed. If the four bytes read look like an
atom of zero size, parsing completes successfully.

ngx_http_mp4_update_mdhd_atom() then reads the mdhd version byte from
the buffer again. If it differs from the version checked in
ngx_http_mp4_read_mdhd_atom(), an 8-byte duration is written at the
offset valid only for the larger version 1 atom, up to 7 bytes past the
end of the heap buffer (AddressSanitizer: heap-buffer-overflow, WRITE,
0 bytes after the moov buffer).

This needs a crafted file served from an mp4; location and a request
with a start or end argument. Default buffer sizes are enough.

Solution

QUIC

Keep the connection out of the reusable queue for exactly the periods
when its own code may open connections:

  • ngx_quic_handshake(): save c->reusable, clear it before
    SSL_do_handshake(), restore it afterwards;
  • ngx_quic_init_streams(): clear the flag before
    ngx_ssl_ocsp_validate();
  • ngx_quic_do_init_streams(): once streams are initialized, make the
    connection reusable again if ngx_quic_can_shutdown() allows it.

The connection stays reusable between datagrams during the handshake,
so half-open connections can still be reclaimed under connection
pressure. Not marking the connection reusable until the handshake is
complete would be simpler, but would remove that.

Mp4

  • ngx_http_mp4_read_atom(): return an error if fewer than 4 bytes
    remain in the parent atom before reading the size, or fewer than 16
    before reading a 64-bit header, and move the existing "mp4 atom too
    large" check ahead of the 8-byte header read. Every header read is
    then inside the parent atom, so the buffer is never refilled while
    moov is being parsed.
  • Save the mdhd layout checked at read time in ngx_http_mp4_trak_t
    and use it in ngx_http_mp4_update_mdhd_atom() instead of the version
    byte from the buffer.

Behaviour change: a container with 1 to 3 stray bytes after its last
atom is now rejected ("mp4 atom is truncated"). Previously such a file
was accepted only if the following bytes happened to read as zero. A
4-byte zero terminator is still accepted.

Testing

AddressSanitizer build (--with-debug, --with-http_v3_module,
--with-http_mp4_module, -DNGX_DEBUG_PALLOC=1), OpenSSL 3.5.5,
Linux x86-64.

QUIC: with a worker close to its worker_connections limit, a single
QUIC handshake crashed the worker (stapling) or produced a
heap-use-after-free report (client certificate OCSP) before the change.
After the change the handshake completes in both configurations at
every connection count tried, with no sanitizer report. The debug log
shows the same reusable state after stream initialization as before.

Mp4: four crafted files (1, 2 and 3 stray bytes with a moov larger than
mp4_buffer_size, and a smaller moov ending at the end of the first
buffer load) produced a heap-buffer-overflow report before the change
and are rejected with 500 after it. Five well-formed files (version 0
and 1 mdhd, moov before and after mdat, small and large moov, 4-byte
zero terminator) requested with five different start/end arguments give
byte-identical responses before and after.

nginx-tests was not run.

Checklist

Before submitting this PR, please confirm:

  • I have read the CONTRIBUTING guidelines
  • I have added tests (if applicable) to validate my changes
  • All existing tests pass
  • I have updated documentation where necessary
  • My branch is rebased on the latest master
  • This PR targets the master branch from my fork
  • My commit message follows NGINX standards (imperative mood, clear
    subject, references related issue if applicable, and contains only
    relevant changes)

Release Notes

  • Use-after-free might occur in a worker process when using HTTP/3 with
    OCSP stapling or client certificate OCSP validation if
    worker_connections were not enough.
  • A heap buffer overflow might occur in a worker process when processing
    a specially crafted mp4 file with the ngx_http_mp4_module.

A QUIC connection is marked reusable as soon as it is created.  SSL
callbacks called during the handshake may open connections, notably
when the certificate status callback starts an OCSP staple update.
OCSP validation of a client certificate, started from
ngx_quic_init_streams(), opens connections as well.  If the worker
process was low on free connections, ngx_get_connection() could drain
the very connection being handshaked and free it while it was still
in use.

With "ssl_stapling on", this could be triggered by a client without
a certificate and resulted in SSL_do_handshake() using the freed SSL
connection.  With "ssl_ocsp on", the OCSP context was freed while
ngx_event_connect_peer() was still using it.

The fix is to make the connection non-reusable while SSL_do_handshake()
is running, and from the start of OCSP validation until streams are
initialized.

A similar issue was fixed in b5252da.
While the moov atom is parsed, it is kept in the buffer in full, and
the atoms found are referenced by pointers into this buffer.  However,
ngx_http_mp4_read_atom() read the next atom header without checking
that the header fits into the parent atom.  If the last atom in moov
was followed by 1 to 3 stray bytes and the moov atom ended exactly at
the end of the buffer, the read refilled the buffer with the data
following the moov atom, replacing the atoms already parsed.

If the bytes read happened to look like an atom of zero size, parsing
completed successfully.  The mdhd atom version, read from the buffer
once again in ngx_http_mp4_update_mdhd_atom(), could then differ from
the version checked earlier, and an 8-byte duration was written at the
offset valid only for the larger version 1 atom, up to 7 bytes past the
end of the buffer.

The fix is to check that an atom header fits into the parent atom
before reading it.  Additionally, the mdhd atom layout checked while
reading the atom is now saved and used when updating the atom, instead
of the version byte from the buffer.
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅ All required contributors have signed the F5 CLA for this PR. Thank you!
Posted by the CLA Assistant Lite bot.

@dhivadhiva

Copy link
Copy Markdown
Author

I have hereby read the F5 CLA and agree to its terms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

1 participant