Commit Graph

8266 Commits

Author SHA1 Message Date
Sergey Kandaurov
9cda58178b Gzip: compatibility with recent zlib-ng 2.2.x versions.
It now uses 5/4 times more memory for the pending buffer.

Further, a single allocation is now used, which takes additional 56 bytes
for deflate_allocs in 64-bit mode aligned to 16, to store sub-allocation
pointers, and the total allocation size now padded up to 128 bytes, which
takes theoretically 200 additional bytes in total.  This fits though into
"4 * (64 + sizeof(void*))" additional space for ZALLOC used in zlib-ng
2.1.x versions.  The comment was updated to reflect this.
2025-02-05 20:40:47 +04:00
Roman Arutyunyan
977824010f QUIC: prevented BIO leak in case of error. 2025-02-05 20:40:47 +04:00
Roman Arutyunyan
5c8a92f1f0 QUIC: fixed accessing a released stream.
While trying to close a stream in ngx_quic_close_streams() by calling its
read event handler, the next stream saved prior to that could be destroyed
recursively.  This caused a segfault while trying to access the next stream.

The way the next stream could be destroyed in HTTP/3 is the following.
A request stream read event handler ngx_http_request_handler() could
end up calling ngx_http_v3_send_cancel_stream() to report a cancelled
request stream in the decoder stream.  If sending stream cancellation
decoder instruction fails for any reason, and the decoder stream is the
next in order after the request stream, the issue is triggered.

The fix is to postpone calling read event handlers for all streams being
closed to avoid closing a released stream.
2025-02-05 20:40:47 +04:00
Roman Arutyunyan
0d11f2885e QUIC: ignore version negotiation packets.
Previously, such packets were treated as long header packets with unknown
version 0, and a version negotiation packet was sent in response.  This
could be used to set up an infinite traffic reflect loop with another nginx
instance.

Now version negotiation packets are ignored.  As per RFC 9000, Section 6.1:

  An endpoint MUST NOT send a Version Negotiation packet in response to
  receiving a Version Negotiation packet.
2025-02-05 20:40:47 +04:00
Roman Arutyunyan
e9e83dbb69 Mp4: prevent chunk index underflow.
When cropping stsc atom, it's assumed that chunk index is never 0.
Based on this assumption, start_chunk and end_chunk are calculated
by subtracting 1 from it.  If chunk index is zero, start_chunk or
end_chunk may underflow, which will later trigger
"start/end time is out mp4 stco chunks" error.  The change adds an
explicit check for zero chunk index to avoid underflow and report
a proper error.

Zero chunk index is explicitly banned in ISO/IEC 14496-12, 8.7.4
Sample To Chunk Box.  It's also implicitly banned in QuickTime File
Format specification.  Description of chunk offset table references
"Chunk 1" as the first table element.
2025-02-05 20:40:47 +04:00
Roman Arutyunyan
1ebe58a02e Mp4: unordered stsc chunks error for the final chunk.
Currently an error is triggered if any of the chunk runs in stsc are
unordered.  This however does not include the final chunk run, which
ends with trak->chunks + 1.  The previous chunk index can be larger
leading to a 32-bit overflow.  This could allow to skip the validity
check "if (start_sample > n)".  This could later lead to a large
trak->start_chunk/trak->end_chunk, which would be caught later in
ngx_http_mp4_update_stco_atom() or ngx_http_mp4_update_co64_atom().

While there are no implications of the validity check being avoided,
the change still adds a check to ensure the final chunk run is ordered,
to produce a meaningful error and avoid a potential integer overflow.
2025-02-05 20:40:47 +04:00
Roman Arutyunyan
4712dee882 Mp4: fixed handling an empty run of chunks in stsc atom.
A specially crafted mp4 file with an empty run of chunks in the stsc atom
and a large value for samples per chunk for that run, combined with a
specially crafted request, allowed to store that large value in prev_samples
and later in trak->end_chunk_samples while in ngx_http_mp4_crop_stsc_data().
Later in ngx_http_mp4_update_stsz_atom() this could result in buffer
overread while calculating trak->end_chunk_samples_size.

Now the value of samples per chunk specified for an empty run is ignored.
2025-02-05 20:40:47 +04:00
Thierry Bastian
cfd68334d8 Configure: MSVC compatibility with PCRE2 10.43. 2025-02-05 20:40:47 +04:00
Roman Arutyunyan
35a1420560 Moved LICENSE and README to root. 2025-02-05 20:40:47 +04:00
Roman Arutyunyan
bfe0a1fd6e Switched GNUmakefile from hg to git. 2025-02-05 20:40:47 +04:00
Sergey Kandaurov
11b890d66d Version bump. 2025-02-05 20:40:47 +04:00
Sergey Kandaurov
99a5842241 release-1.26.2 tag 2024-08-12 18:28:31 +04:00
Sergey Kandaurov
37fe983554 nginx-1.26.2-RELEASE 2024-08-12 18:25:45 +04:00
Sergey Kandaurov
6da478eacd Updated OpenSSL used for win32 builds. 2024-08-12 18:20:49 +04:00
Roman Arutyunyan
2262362fd3 Mp4: rejecting unordered chunks in stsc atom.
Unordered chunks could result in trak->end_chunk smaller than trak->start_chunk
in ngx_http_mp4_crop_stsc_data().  Later in ngx_http_mp4_update_stco_atom()
this caused buffer overread while trying to calculate trak->end_offset.
2024-08-12 18:20:45 +04:00
Roman Arutyunyan
3dc0fba5ad Mp4: fixed buffer underread while updating stsz atom.
While cropping an stsc atom in ngx_http_mp4_crop_stsc_data(), a 32-bit integer
overflow could happen, which could result in incorrect seeking and a very large
value stored in "samples".  This resulted in a large invalid value of
trak->end_chunk_samples.  This value is further used to calculate the value of
trak->end_chunk_samples_size in ngx_http_mp4_update_stsz_atom().  While doing
this, a large invalid value of trak->end_chunk_samples could result in reading
memory before stsz atom start.  This could potentially result in a segfault.
2024-08-12 18:20:43 +04:00
Sergey Kandaurov
e1daadc388 Typo fixed. 2024-08-09 19:12:23 +04:00
Sergey Kandaurov
ddd5b9c531 Version bump. 2024-08-12 18:21:52 +04:00
Sergey Kandaurov
46222c0ab3 release-1.26.1 tag 2024-05-28 17:28:07 +04:00
Sergey Kandaurov
02725ce722 nginx-1.26.1-RELEASE 2024-05-28 17:26:54 +04:00
Sergey Kandaurov
ffed470390 HTTP/3: fixed handling of zero-length literal field line.
Previously, st->value was passed with NULL data pointer to header handlers.
2024-05-28 17:20:45 +04:00
Roman Arutyunyan
0e7702e066 QUIC: ngx_quic_buffer_t use-after-free protection.
Previously the last chain field of ngx_quic_buffer_t could still reference freed
chains and buffers after calling ngx_quic_free_buffer().  While normally an
ngx_quic_buffer_t object should not be used after freeing, resetting last_chain
field would prevent a potential use-after-free.
2024-05-28 17:19:21 +04:00
Roman Arutyunyan
376f12e40a QUIC: ignore CRYPTO frames after handshake completion.
Sending handshake-level CRYPTO frames after the client's Finished message could
lead to memory disclosure and a potential segfault, if those frames are sent in
one packet with the Finished frame.
2024-05-28 17:19:08 +04:00
Roman Arutyunyan
3f2d8cb8f9 HTTP/3: fixed dynamic table overflow.
While inserting a new entry into the dynamic table, first the entry is added,
and then older entries are evicted until table size is within capacity.  After
the first step, the number of entries may temporarily exceed the maximum
calculated from capacity by one entry, which previously caused table overflow.

The easiest way to trigger the issue is to keep adding entries with empty names
and values until first eviction.

The issue was introduced by 987bee4363d1.
2024-05-28 17:18:50 +04:00
Roman Arutyunyan
326150b82d HTTP/3: decoder stream pre-creation.
Previously a decoder stream was created on demand for sending Section
Acknowledgement, Stream Cancellation and Insert Count Increment.  If conditions
for sending any of these instructions never happen, a decoder stream is not
created at all.  These conditions include client not using the dynamic table and
no streams abandoned by server (RFC 9204, Section 2.2.2.2).  However RFC 9204,
Section 4.2 defines only one condition for not creating a decoder stream:

   An endpoint MAY avoid creating a decoder stream if its decoder sets
   the maximum capacity of the dynamic table to zero.

The change enables pre-creation of the decoder stream at HTTP/3 session
initialization if maximum dynamic table capacity is not zero.  Note that this
value is currently hardcoded to 4096 bytes and is not configurable, so the
stream is now always created.

Also, the change fixes a potential stack overflow when creating a decoder
stream in ngx_http_v3_send_cancel_stream() while draining a request stream by
ngx_drain_connections().  Creating a decoder stream involves calling
ngx_get_connection(), which calls ngx_drain_connections(), which will drain the
same request stream again.  If client's MAX_STREAMS for uni stream is high
enough, these recursive calls will continue until we run out of stack.
Otherwise, decoder stream creation will fail at some point and the request
stream connection will be drained.  This may result in use-after-free, since
this connection could still be referenced up the stack.
2024-05-28 17:18:28 +04:00
Sergey Kandaurov
eaa6daa5f5 QUIC: client transport parameter data length checking. 2024-05-28 17:17:19 +04:00
Roman Arutyunyan
75e3004902 Optimized chain link usage (ticket #2614).
Previously chain links could sometimes be dropped instead of being reused,
which could result in increased memory consumption during long requests.

A similar chain link issue in ngx_http_gzip_filter_module was fixed in
da46bfc484ef (1.11.10).

Based on a patch by Sangmin Lee.
2024-05-23 19:15:38 +04:00
Edgar Bonet
a728869cd1 Configure: fixed building libatomic test.
Using "long *" instead of "AO_t *" leads either to -Wincompatible-pointer-types
or -Wpointer-sign warnings, depending on whether long and size_t are compatible
types (e.g., ILP32 versus LP64 data models).  Notably, -Wpointer-sign warnings
are enabled by default in Clang only, and -Wincompatible-pointer-types is an
error starting from GCC 14.

Signed-off-by: Edgar Bonet <bonet@grenoble.cnrs.fr>
2024-05-16 11:15:10 +02:00
Sergey Kandaurov
ee561abfdf Version bump. 2024-05-28 17:14:08 +04:00
Roman Arutyunyan
ea3f44e012 release-1.26.0 tag 2024-04-23 18:04:32 +04:00
Roman Arutyunyan
361f6bf4b1 nginx-1.26.0-RELEASE 2024-04-23 17:40:08 +04:00
Roman Arutyunyan
ee19cf9800 Stable branch. 2024-04-23 17:31:41 +04:00
Roman Arutyunyan
d8a849ae3c release-1.25.5 tag 2024-04-16 18:29:59 +04:00
Roman Arutyunyan
14f8190ce7 nginx-1.25.5-RELEASE 2024-04-16 18:27:50 +04:00
Roman Arutyunyan
bf3e6538b9 Stream pass: limited the number of passes per connection.
Previously a cycle in pass configuration resulted in stack overflow.
2024-04-11 11:37:30 +04:00
Vladimir Khomutov
92f9968571 QUIC: fixed close timer processing with early data.
The ngx_quic_run() function uses qc->close timer to limit the handshake
duration.  Normally it is removed by ngx_quic_do_init_streams() which is
called once when we are done with initial SSL processing.

The problem happens when the client sends early data and streams are
initialized in the ngx_quic_run() -> ngx_quic_handle_datagram() call.
The order of set/remove timer calls is now reversed; the close timer is
set up and the timer fires when assigned, starting the unexpected connection
close process.

The fix is to skip setting the timer if streams were initialized during
handling of the initial datagram.  The idle timer for quic is set anyway,
and stream-related timeouts are managed by application layer.
2024-04-10 09:38:10 +03:00
Piotr Sikora
e3207a17f0 Configure: allow cross-compiling to Windows using Clang.
Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:48 +00:00
Piotr Sikora
b595a68df9 Configure: fixed "make install" when cross-compiling to Windows.
Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:46 +00:00
Piotr Sikora
2deded362e Configure: added support for Homebrew on Apple Silicon.
Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:43 +00:00
Sergey Kandaurov
6b1bb998c9 Configure: set cache line size for more architectures.
Based on a patch by Piotr Sikora.
2024-03-27 19:36:51 +04:00
Piotr Sikora
1bc19fe2db Detect cache line size at runtime on macOS.
Notably, Apple Silicon CPUs have 128 byte cache line size,
which is twice the default configured for generic aarch64.

Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:40 +00:00
Sergey Kandaurov
818f98da1c Configure: fixed Linux crypt_r() test to add libcrypt.
Previously, the resulting binary was successfully linked
because libcrypt was added in a separate test for crypt().

Patch by Piotr Sikora.
2024-02-26 20:00:38 +00:00
Piotr Sikora
eff2ea1d69 Win32: fixed unique file index calculations.
The old code was breaking strict aliasing rules.

Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:35 +00:00
Piotr Sikora
2f9e8431e6 Rewrite: fixed "return" directive without response text.
Previously, the response text wasn't initialized and the rewrite module
was sending response body set to NULL.

Found with UndefinedBehaviorSanitizer (pointer-overflow).

Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-02-26 20:00:28 +00:00
Sergey Kandaurov
3d5a356abb Fixed undefined behaviour with IPv4-mapped IPv6 addresses.
Previously, it could result when left-shifting signed integer due to implicit
integer promotion, such that the most significant bit appeared on the sign bit.

In practice, though, this results in the same left value as with an explicit
cast, at least on known compilers, such as GCC and Clang.  The reason is that
in_addr_t, which is equivalent to uint32_t and same as "unsigned int" in ILP32
and LP64 data type models, has the same type width as the intermediate after
integer promotion, so there's no side effects such as sign-extension.  This
explains why adding an explicit cast does not change object files in practice.

Found with UndefinedBehaviorSanitizer (shift).

Based on a patch by Piotr Sikora.
2024-03-18 17:14:30 +04:00
Piotr Sikora
d3d64cacb3 Geo: fixed uninitialized memory access.
While copying ngx_http_variable_value_t structures to geo binary base
in ngx_http_geo_copy_values(), and similarly in the stream module,
uninitialized parts of these structures are copied as well.  These
include the "escape" field and possible holes.  Calculating crc32 of
this data triggers uninitialized memory access.

Found with MemorySanitizer.

Signed-off-by: Piotr Sikora <piotr@aviatrix.com>
2024-03-14 18:37:20 +04:00
Sergey Kandaurov
5e79d98a59 Stream: $server_name. 2024-03-22 18:39:50 +04:00
Roman Arutyunyan
45e166b4a4 Stream: moved fastopen compatibility check.
The move makes the code look similar to the corresponding code in http module.
2024-01-18 19:12:38 +04:00
Sergey Kandaurov
bd190d825c Stream: the "setfib" parameter of the "listen" directive.
The FreeBSD SO_SETFIB support.
2024-03-22 14:53:19 +04:00
Sergey Kandaurov
04b9bfe55d Stream: the "accept_filter" parameter of the "listen" directive.
The FreeBSD accept filters support.
2024-03-22 14:53:19 +04:00