Previously, if try_files was used with alias, and the script produced
a string smaller than the predicted size, then an integer underflow
could happen which resulted in buffer overrun.
Fix is to recalculate the length after running the script.
A similar length recalculation exists in ngx_http_index_module.
It was added by 25f920eca when fixing script overruns.
Reported by Marc Luchian & Liviu-Alexandru Bud.
If PROXY protocol v2 was enabled with dynamic certificate loading,
notably in the stream module, a segmentation fault might happen.
The reason is that during an abbreviated handshake the certificate
callback is not called, leading to NULL being returned from
SSL_get_certificate().
The fix is similar to 5784889fb.
Reported by ruokeqx.
Previously, ngx_quic_create_stream() cleared the main connection's
reusable flag and then called ngx_get_connection() before inserting the
new stream into the tree. If the worker was low on free connections,
the drain inside ngx_get_connection() could close an idle stream of the
same main connection. Since the new stream was not in the tree yet,
that close found no remaining streams and marked the main connection
reusable again. Once reusable, the connection could later be drained
while processing one of its streams, for example when connecting to an
upstream, freeing it while still in use.
Fix is to insert the new stream into the tree before calling
ngx_get_connection(), so the main connection is not considered idle.
Reported by Xint.io.
While the SMTP client address and name are being resolved, the client
read handler is ngx_mail_smtp_block_reading(), but the client write
handler was ngx_mail_send(). If ngx_handle_write_event() failed in it,
the client connection was closed, but the resolver context was not
cancelled, and the resolver handler accessed the freed session when the
resolving completed or timed out.
The client write handler is now ngx_mail_smtp_block_writing(), which
cancels the resolver context on errors the same way
ngx_mail_smtp_block_reading() does. ngx_mail_send() is restored in
ngx_mail_smtp_greeting(), which is also needed with smtp_greeting_delay,
where the error is sent without s->quit, and in
ngx_mail_smtp_resolve_addr_handler() before the posted name resolving,
where s->resolver_ctx still points to a context which is already done.
While connecting to the upstream and reading its greeting, the client
read handler is ngx_mail_proxy_block_read(), but the client write
handler was left as it was. If ngx_handle_write_event() failed in it,
the client connection was closed without the upstream one, and the next
upstream event accessed the freed session.
The client write handler is now ngx_mail_proxy_block_write(), which
closes both connections on errors the same way
ngx_mail_proxy_block_read() does.
While waiting for the auth_http response, the client read handler is
ngx_mail_auth_http_block_read(), but the write handler was left as
ngx_mail_send(), so a client write event during the wait was handled
by a function which can close the client connection, destroying the
session while the auth_http connection still referenced it.
The client write handler is now blocked while waiting for the auth_http
response, with proper cleanup on errors, and restored right after the
auth_http connection is closed.
When a client command starts an auth_http request, the default reply
set by the POP3, IMAP and SMTP auth state handlers remains in s->out,
though it is never used: the actual reply depends on the auth_http
response. With ngx_mail_send() as the client write handler, the reply
could be sent before the authentication result was known, and if the
client reset the connection, the failed send destroyed the session
while the auth_http connection still referenced it.
The queued reply is now discarded when the auth_http request starts,
similarly to ngx_mail_proxy_init().
Reported by Xint.io.
Most notably, this fixes potential issues when a fatal error message is
sent with ngx_mail_send() with s->quit set, but the read handler is not
updated. Despite s->quit, ngx_mail_send() might not be able to send the
error message immediately, and the read handler might be called on read
events. In particular, in ngx_mail_auth_http_block_read() and in
ngx_mail_proxy_block_read() this could potentially cause accesses to
freed memory or already closed connections in ngx_handle_read_event()
error handling code paths.
Reported by Evan Hellman,
https://github.com/freenginx/nginx/issues/23https://github.com/freenginx/nginx/issues/24
Signed-off-by: Vadim Zhestikov <v.zhestikov@f5.com>
Origin: <https://freenginx.org/hg/nginx/rev/87725983f346>
When a stream is terminated, its unsent payload length is refunded back
to the connection flow control. Previously, this could increase the
flow control beyond NGX_HTTP_V2_MAX_WINDOW, if it was increased by the
client after the stream frames were queued and before the stream was
terminated. This eventually resulted in broken buffer size control in
ngx_http_v2_send_chain() followed by memory corruption and a possible
segmentation fault.
The fix is to account for the queued frames when validating a client
WINDOW_UPDATE. From the client's point of view, the queued frames
still belong to the flow control window.
Reported by Xint.io.
Previously, when a resolve handler called ngx_resolve_name_done() or
ngx_resolve_addr_done() for another resolve of the same name/address,
a use-after-free could happen while reporting the finalized resolve.
This may happen with upstream resolve when at least one subrequest is
resolving the same name, and the request is terminated from the
upstream resolve handler.
The problem occurs when finalizing a non-current resolve that is
already detached from rn->waiting and is about to be reported. Such
cases produced "could not cancel" alerts. The resolve may also be
detached with its node reset, while following a CNAME, in which case
no alert was produced.
Now the alerts are converted to debug messages and a finalizer is
installed into ctx->handler to release the context memory later.
Reported by Xint.io.
Previously, if restarting a resolve with a CNAME name failed due to a
memory allocation error, no error was reported. The waiting requests
were left detached from any node until their timeouts expired.
Now the error is reported to the waiting requests immediately.
Request body reading indirectly uses the "do { c->recv() } while
(c->read->ready)" form, which is not really correct, as for example with
SSL c->read->ready may be still set when c->recv() returns NGX_AGAIN due
to SSL_ERROR_WANT_WRITE (see 7351:2b5528023f6b), and therefore this form
might be an infinite loop.
Added explicit NGX_AGAIN handling for the sake of correctness.
Signed-off-by: Elijah Zupancic <e.zupancic@f5.com>
Origin: <https://freenginx.org/hg/nginx/rev/392e8e2fd22a8461e75a67d5d33d85c07974e1e3>
If the SSL handshake completed in the first call of
ngx_stream_ssl_handler(), the client certificate was verified using the
configuration of the default server, rather than the virtual server
selected by SNI during the handshake.
The problem appeared in d21675228 (1.25.5). It is similar to the one
fixed in 620c9a4c4.
The fix is to re-read the server configuration before verifying the
client certificate.
Reported by CERT.PL.
As stated in the TLS Extensions specification (RFC 3546) and all
subsequent TLS specification updates,
: There MUST NOT be more than one extension of the same type.
Such a ClientHello is now treated as malformed, as with other parsing
errors.
As required explicitly in RFC 9846 Section 4.3 and implicitly in all the
previous versions of TLS extensions specification.
Reported by Vivek Parikh, BreachX Zero Day Labs using Typhon AI Mil v2.
Previously, an HTTP/2 connection could be reusable while having streams.
This could result in an attempt to drain the connection while processing
one if its streams, for example when connecting to an upstream. After
recognizing that the connection has streams, an attempt to send the
GOAWAY frame was made. On send failure, the connection was immediately
closed and its pool released. This triggered a use-after-free up the
stack.
Having an HTTP/2 connection with streams registered as reusable became
possible after 76672e650. A connection with no streams and a partial
frame read from client was registered as reusable to avoid DoS attacks.
So if a client sends only the first HEADERS frame byte, the connection
becomes reusable. Later, when the remaining HEADERS bytes are received
and a new stream is created, the reusable status is not reset until the
connection becomes idle.
Fix is to mark the connection non-reusable upon stream creation.
Reported by Albert @ Vexera AI.
The regex pattern was referenced from the temporary pool used for
map block parsing, which is destroyed once the block is parsed. The
pattern is later used for logging if regex matching fails, e.g., due
to PCRE2 match limit, JIT stack limit, or invalid UTF-8 with (*UTF),
resulting in released memory being logged.
The fix is to copy the pattern into the configuration pool.
Previously, "e.flushed = 1;" was incorrectly used during initial length
calculations, and as a result non-cacheable variables were not flushed.
The fix is to only set "e.flushed" when evaluating values.
A similar issue in try_files was fixed in 2424:46d11bff21ef (0.7.29).
Origin: <https://freenginx.org/hg/nginx/rev/511b31fbc525>
Signed-off-by: Roman Arutyunyan <arut@nginx.com>
As OpenSSL does not store the sign in the ASN1_INTEGER string data,
it is not possible to distinguish positive and negative serial numbers.
While negative serials are forbidden in RFC 5280 4.1.2.2, it also says
that we SHOULD expect these from non-conforming CAs.
The solution is to use full DER-encoding of a signed integer obtained
with i2d_ASN1_INTEGER.
The change also addresses a correctness issue, as ASN1_STRING accessors
should not be used on ASN1_INTEGER.
Right-padding the serials makes it impossible to distinguish between n,
n * 256 and so on. This has no practical impact, as small, predictable
serial numbers have been discouraged for the last two decades.
While the change has a potential of introducing memory fragmentation,
current allocations stay within a single slab bucket on all supported
architectures.
Reported by Gyejin Lee, QED Audit.
The $geoip_latitude and $geoip_longitude values are formatted with
ngx_sprintf(buf, "%.4f", value) into a buffer sized for a coordinate,
but the DOUBLE/FLOAT value is taken from the database with no range
check. A crafted or corrupted MMDB can store a huge magnitude (e.g.
1e30 or an infinity), and ngx_sprintf, which is unbounded, then writes
past the buffer in the request or connection pool.
Coordinates outside [-180, 180] are now rejected, so the variables
report "not found" as they do for a missing entry. The comparison also
rejects a NaN.
Closes: https://github.com/nginx/nginx/issues/1907
Previously, the "control API unavailable in single process mode"
warning was logged on every start in single process mode, even
without the "-l" option.
Chunk sizes (including leading zeros), chunk extensions and trailers
of a chunked request body were not accounted, allowing a client to send
an unbounded amount of framing with little or no body data.
Now all received bytes are counted in rb->received and compared to
the body data, using the same flood detection as in HTTP/2 and HTTP/3.
Reported by jskoetsier.
Unlike response headers, trailers are queued along with the stream data.
Blocking the queue on them would hold back every frame queued later,
including higher priority streams.
Also, this way unsent trailers are discarded along with the stream data
when the stream is reset.
With priorities from RFC 9218, streams of the same urgency are ordered
by their identifiers, so queuing a frame of an older stream traverses
the frames of all newer ones.
Frames queued in ngx_http_v2_send_chain() belong to a single stream, and
hence to a single position in the output queue, which does not change
while they are queued. Now the position is searched once per call.
A request line larger than the receive buffer was answered with no
response at all, while every other malformed request gets a status
code. The buffer-full check also ran at the start of the next read
event, which may never arrive, since SIGIO is raised by new data.
The check is now made after parsing, so 400 is returned right away.
A cookie without a name, such as "a" in "Cookie: a; a=1", was skipped
together with the separator that follows it, so the next cookie was
skipped as well and $cookie_a was not found. The userid module was
affected in the same way.
User agents send a cookie with an empty name as a bare value (RFC
6265bis, section 5.8.3), so such a token is a valid part of the Cookie
header.
The fix is to not consume the character that fails the "=" check.
The problem appeared in 899b44eab (0.1.29).
Header names not followed by a colon and a value in an OCSP responder
reply were accepted as headers with an empty value, similarly to
ngx_http_parse_header_line(). With this change, such headers are
rejected with the "OCSP responder sent invalid response" error, and the
response is not used.
The problem appeared in 74ad4494a (1.3.7).
Header names not followed by a colon and a value in an auth_http server
response were accepted as headers with an empty value, similarly to
ngx_http_parse_header_line(). With this change, such headers are
rejected with the "sent invalid header in response" error, and the
client gets the internal server error reply.
The problem appeared in 5192b3651 (0.1.38).
Starting with nginx 0.1.29 (509:9b8c906f6e63), header names not followed
by a colon and a value were allowed. Such headers were interpreted as
headers with an empty value. With this change, such headers are
unconditionally rejected.
Requested by Maksim Yevmenkin.
Closes: https://github.com/nginx/nginx/issues/1499
Signed-off-by: Vadim Zhestikov <v.zhestikov@f5.com>
Origin: <https://freenginx.org/hg/nginx/rev/9a5e2296c1be>
Five values of the static table lacked the space after a semicolon
compared to RFC 9204, Appendix A: indexes 52, 57, 58, 62 and 85. A
field sent by a client as an indexed static entry was thus decoded
without the space, for example "text/html;charset=utf-8" instead of
"text/html; charset=utf-8" in the Content-Type header. The encoder
is not affected, it does not use these indexes.
Since 3d7304b52, sessions are serialized into the static buffer
ngx_ssl_session_buffer, so the last cached, resumed, or proxied session
remains in memory, master secret included, until overwritten. Clear the
buffer on all exits that write to it.
While here, the session id length check is moved before serialization,
which removes an exit path with a populated buffer and a needless
i2d_SSL_SESSION() call for sessions that are not cached.
Note that sessions are still kept in shared memory until evicted.
Assisted-by: opencode:claude-opus-5
Closes: https://github.com/nginx/nginx/issues/1736
Previously, an initgroups() failure was only logged, and the worker
process went on with the supplementary groups inherited from the master
process, since setgid() does not change them. Anything relying on the
configured user's groups then failed at run time, far from the log
message, and the outcome depended on which worker hit the error.
Now such a failure is fatal, similarly to setgid() and setuid(). The
exception is EPERM, which is logged and ignored: it is returned when
supplementary groups cannot be changed at all, for example, in a user
namespace with setgroups denied, see user_namespaces(7), or under
seccomp.
This dates back to 42b12b34f (0.1.11).
Previously, r->headers_in.content_length_n was used, which is not 0 till
the request body is fully discarded, and the request might hang.
Adapted for nginx: the same change is applied to the HTTP/2 proxy
module (proxy_http_version 2), which is not present in freenginx and
used r->headers_in.content_length_n in the same way. With an HTTP/2
upstream, the stale length resulted in the request being rejected
by the upstream, since the stream was closed without the body.
Co-authored-by: Elijah Zupancic <e.zupancic@f5.com>
Signed-off-by: Elijah Zupancic <e.zupancic@f5.com>
Origin: <https://freenginx.org/hg/nginx/rev/51e0dc7137843feda75b497773f598d3836c60a1>
Previously, if ngx_pool_cleanup_add() failed, already incremented
connection counter was left as is, eventually resulting in unexpected
connection limiting or shared memory exhaustion. Fix is to decrement
the counter by calling the cleanup handler with data on stack.
Signed-off-by: Elijah Zupancic <e.zupancic@f5.com>
Origin: <https://freenginx.org/hg/nginx/rev/722e252fcabe6413eb63555890273d7b0e7fc132>
Previously, an invalid working_directory was only detected on worker
process startup: if chdir() failed, worker processes exited and were
not respawned, while "nginx -t" reported success.
Now the directory is checked during configuration parsing. Note that
this doesn't cover insufficient permissions for worker processes.
Previously, a relative path was resolved against the current working
directory of the master process, which depends on how nginx is started.
Now it is resolved relative to prefix, similar to pid and error_log.
The encoder wrote accumulated bits by casting the destination to
uint64_t *, or uint32_t * on 32-bit platforms, and assigning through
it, which requires the destination to be suitably aligned.
In HTTP/2 the destination is a temporary buffer from ngx_palloc() and
is always aligned. In HTTP/3 it points past a variable number of
prefix integer bytes, so its alignment is arbitrary and the store is
undefined behaviour.
Byte order conversion is now separated from the store, which is done
with ngx_memcpy().
The parser now emits NGX_JSON_KEY_ESCAPED and
NGX_JSON_VALUE_STRING_ESCAPED for strings holding escape sequences, so
consumers no longer rescan tokens.