Commit Graph

7733 Commits

Author SHA1 Message Date
Sergey Kandaurov
b5e4f1f4f0 QUIC: consistent use of 5-byte buffers for header protection.
The output buffer is now also of 5 bytes.  Header protection uses
stream ciphers, which don't produce extra output nor PKCS padding.
2021-06-16 17:53:18 +03:00
Sergey Kandaurov
ae58d87c01 QUIC: updated specification references.
This includes updating citations and further clarification.
2021-06-16 11:55:12 +03:00
Roman Arutyunyan
96e1db1c34 HTTP/3: client GOAWAY support. 2021-06-11 13:24:24 +03:00
Roman Arutyunyan
80a5227617 HTTP/3: generate more H3_FRAME_UNEXPECTED.
As per quic-http-34, these are the cases when this error should be generated:

   If an endpoint receives a second SETTINGS frame
   on the control stream, the endpoint MUST respond with a connection
   error of type H3_FRAME_UNEXPECTED

   SETTINGS frames MUST NOT be sent on any stream other than the control
   stream.  If an endpoint receives a SETTINGS frame on a different
   stream, the endpoint MUST respond with a connection error of type
   H3_FRAME_UNEXPECTED.

   A client MUST NOT send a PUSH_PROMISE frame.  A server MUST treat the
   receipt of a PUSH_PROMISE frame as a connection error of type
   H3_FRAME_UNEXPECTED; see Section 8.

   The MAX_PUSH_ID frame is always sent on the control stream.  Receipt
   of a MAX_PUSH_ID frame on any other stream MUST be treated as a
   connection error of type H3_FRAME_UNEXPECTED.

   Receipt of an invalid sequence of frames MUST be treated as a
   connection error of type H3_FRAME_UNEXPECTED; see Section 8.  In
   particular, a DATA frame before any HEADERS frame, or a HEADERS or
   DATA frame after the trailing HEADERS frame, is considered invalid.

   A CANCEL_PUSH frame is sent on the control stream.  Receiving a
   CANCEL_PUSH frame on a stream other than the control stream MUST be
   treated as a connection error of type H3_FRAME_UNEXPECTED.

   The GOAWAY frame is always sent on the control stream.
2021-06-11 12:11:08 +03:00
Roman Arutyunyan
9cf6426f6a HTTP/3: reordered H3_MISSING_SETTINGS and H3_FRAME_UNEXPECTED.
The quic-http-34 is ambiguous as to what error should be generated for the
first frame in control stream:

   Each side MUST initiate a single control stream at the beginning of
   the connection and send its SETTINGS frame as the first frame on this
   stream.  If the first frame of the control stream is any other frame
   type, this MUST be treated as a connection error of type
   H3_MISSING_SETTINGS.

   If a DATA frame is received on a control stream, the recipient MUST
   respond with a connection error of type H3_FRAME_UNEXPECTED.

   If a HEADERS frame is received on a control stream, the recipient MUST
   respond with a connection error of type H3_FRAME_UNEXPECTED.

Previously, H3_FRAME_UNEXPECTED had priority, but now H3_MISSING_SETTINGS has.
The arguments in the spec sound more compelling for H3_MISSING_SETTINGS.
2021-06-11 10:56:51 +03:00
Vladimir Homutov
bf7b32e1b6 QUIC: improved errors readability. 2021-06-10 23:17:51 +03:00
Vladimir Homutov
0c77dc9c0b QUIC: persistent congestion calculation.
According to RFC 9002 (quic-recovery) 7.6.
2021-06-09 15:11:43 +03:00
Roman Arutyunyan
64586eaa36 QUIC: stream flow control refactored.
- Function ngx_quic_control_flow() is introduced.  This functions does
both MAX_DATA and MAX_STREAM_DATA flow controls.  The function is called
from STREAM and RESET_STREAM frame handlers.  Previously, flow control
was only accounted for STREAM.  Also, MAX_DATA flow control was not accounted
at all.

- Function ngx_quic_update_flow() is introduced.  This function advances flow
control windows and sends MAX_DATA/MAX_STREAM_DATA.  The function is called
from RESET_STREAM frame handler, stream cleanup handler and stream recv()
handler.
2021-06-07 10:12:46 +03:00
Maxim Dounin
5eadaf69e3 Fixed SSL logging with lingering close.
Recent fixes to SSL shutdown with lingering close (554c6ae25ffc, 1.19.5)
broke logging of SSL variables.  To make sure logging of SSL variables
works properly, avoid freeing c->ssl when doing an SSL shutdown before
lingering close.

Reported by Reinis Rozitis
(http://mailman.nginx.org/pipermail/nginx/2021-May/060670.html).
2021-06-01 17:37:51 +03:00
Maxim Dounin
235d2df1de SSL: ngx_ssl_shutdown() rework.
Instead of calling SSL_free() with each return point, introduced a single
place where cleanup happens.  As a positive side effect, this fixes two
potential memory leaks on ngx_handle_read_event() and ngx_handle_write_event()
errors where there were no SSL_free() calls (though unlikely practical,
as errors there are only expected to happen due to bugs or kernel issues).
2021-06-01 17:37:49 +03:00
Sergey Kandaurov
dcdf62549f HTTP/3: undo 5a92523e50d3 after parser refactoring (e1eb7f4ca9f1).
This is no longer needed after HTTP/3 request processing has moved
into its own function ngx_http_v3_process_header().
2021-06-01 12:02:08 +03:00
Sergey Kandaurov
1f85c660cb HTTP/3: fixed parsing encoder insertions with empty header value.
When starting processing a new encoder instruction, the header state is not
memzero'ed because generally it's burdensome.  If the header value is empty,
this resulted in inserting a stale value left from the previous instruction.

Based on a patch by Zhiyong Sun.
2021-06-01 11:41:38 +03:00
Sergey Kandaurov
e8a7625269 HTTP/3: removed $http3 that served its purpose.
To specify final protocol version by hand:

    add_header Alt-Svc h3=":443";
2021-05-31 11:54:47 +03:00
Gena Makhomed
df1da673f7 Contrib: vim syntax, update core and 3rd party module directives. 2021-05-30 12:26:00 +03:00
Maxim Dounin
52cde89586 Core: disabled SO_REUSEADDR on UDP sockets while testing config.
On Linux, SO_REUSEADDR allows completely duplicate UDP sockets, so using
SO_REUSEADDR when testing configuration results in packets being dropped
if there is an existing traffic on the sockets being tested (ticket #2187).
While dropped packets are expected with UDP, it is better to avoid this
when possible.

With this change, SO_REUSEADDR is no longer set on datagram sockets when
testing configuration.
2021-05-31 16:36:51 +03:00
Maxim Dounin
85a104aa4e Core: disabled cloning sockets when testing config (ticket #2188).
Since we anyway do not set SO_REUSEPORT when testing configuration
(see ecb5cd305b06), trying to open additional sockets does not make much
sense, as all these additional sockets are expected to result in EADDRINUSE
errors from bind().  On the other hand, there are reports that trying
to open these sockets takes significant time under load: total configuration
testing time greater than 15s was observed in ticket #2188, compared to less
than 1s without load.

With this change, no additional sockets are opened during testing
configuration.
2021-05-31 16:36:37 +03:00
Maxim Dounin
427cfff79b Version bump. 2021-05-31 16:36:12 +03:00
Sergey Kandaurov
de1ce76199 README: updated after QUIC RFC publication, nginx 1.21 rebase. 2021-05-28 13:45:09 +03:00
Sergey Kandaurov
b2b8637f98 Merged with the default branch. 2021-05-28 13:33:08 +03:00
Sergey Kandaurov
03fcff287d HTTP/3: fixed Insert With Name Reference index processing.
Based on a patch by Zhiyong Sun.
2021-05-27 13:29:00 +03:00
Roman Arutyunyan
27a24f4c6b QUIC: call stream read handler on new data arrival.
This was broken in b3f6ad181df4.
2021-05-26 13:07:06 +03:00
Roman Arutyunyan
1677503f98 QUIC: make sure stream data size is lower than final size.
As per quic-transport 34, FINAL_SIZE_ERROR is generated if an endpoint received
a STREAM frame or a RESET_STREAM frame containing a final size that was lower
than the size of stream data that was already received.
2021-05-25 16:41:59 +03:00
Maxim Dounin
798813e96b release-1.21.0 tag 2021-05-25 15:28:56 +03:00
Maxim Dounin
ffcf93a6bf nginx-1.21.0-RELEASE 2021-05-25 15:28:55 +03:00
Maxim Dounin
e860ecce82 Resolver: explicit check for compression pointers in question.
Since nginx always uses exactly one entry in the question section of
a DNS query, and never uses compression pointers in this entry, parsing
of a DNS response in ngx_resolver_process_response() does not expect
compression pointers to appear in the question section of the DNS
response.  Indeed, compression pointers in the first name of a DNS response
hardly make sense, do not seem to be allowed by RFC 1035 (which says
"a pointer to a prior occurance of the same name", note "prior"), and
were never observed in practice.

Added an explicit check to ngx_resolver_process_response()'s parsing
of the question section to properly report an error if compression pointers
nevertheless appear in the question section.
2021-05-25 15:17:50 +03:00
Maxim Dounin
f85d701694 Resolver: simplified ngx_resolver_copy().
Instead of checking on each label if we need to place a dot or not,
now it always adds a dot after a label, and reduces the resulting
length afterwards.
2021-05-25 15:17:45 +03:00
Maxim Dounin
f1dd1d50e0 Resolver: reworked ngx_resolver_copy() copy loop.
To make the code easier to read, reworked the ngx_resolver_copy()
copy loop to match the one used to calculate length.  No functional
changes.
2021-05-25 15:17:43 +03:00
Maxim Dounin
bbd403a7ab Resolver: fixed label types handling in ngx_resolver_copy().
Previously, anything with any of the two high bits set were interpreted
as compression pointers.  This is incorrect, as RFC 1035 clearly states
that "The 10 and 01 combinations are reserved for future use".  Further,
the 01 combination is actually allocated for EDNS extended label type
(see RFC 2671 and RFC 6891), not really used though.

Fix is to reject unrecognized label types rather than misinterpreting
them as compression pointers.
2021-05-25 15:17:41 +03:00
Maxim Dounin
077a890a76 Resolver: fixed off-by-one read in ngx_resolver_copy().
It is believed to be harmless, and in the worst case it uses some
uninitialized memory as a part of the compression pointer length,
eventually leading to the "name is out of DNS response" error.
2021-05-25 15:17:38 +03:00
Maxim Dounin
9f1dcb0c04 Resolver: fixed off-by-one write in ngx_resolver_copy().
Reported by Luis Merino, Markus Vervier, Eric Sesterhenn, X41 D-Sec GmbH.
2021-05-25 15:17:36 +03:00
Roman Arutyunyan
cd86cf34db QUIC: refactored CRYPTO and STREAM buffer ordering.
Generic function ngx_quic_order_bufs() is introduced.  This function creates
and maintains a chain of buffers with holes.  Holes are marked with b->sync
flag.  Several buffers and holes in this chain may share the same underlying
memory buffer.

When processing STREAM frames with this function, frame data is copied only
once to the right place in the stream input chain.  Previously data could
be copied twice.  First when buffering an out-of-order frame data, and then
when filling stream buffer from ordered frame queue.  Now there's only one
data chain for both tasks.
2021-05-25 13:55:12 +03:00
Ruslan Ermilov
41a241b3ef Location header escaping in redirects (ticket #882).
The header is escaped in redirects based on request URI or
location name (auto redirect).
2021-05-24 21:55:20 +03:00
Maxim Dounin
52d0ec7d17 Fixed log action when using SSL certificates with variables.
When variables are used in ssl_certificate or ssl_certificate_key, a request
is created in the certificate callback to evaluate the variables, and then
freed.  Freeing it, however, updates c->log->action to "closing request",
resulting in confusing error messages like "client timed out ... while
closing request" when a client times out during the SSL handshake.

Fix is to restore c->log->action after calling ngx_http_free_request().
2021-05-24 18:23:42 +03:00
Sergey Kandaurov
f137860101 QUIC: unroll and inline ngx_quic_varint_len()/ngx_quic_build_int().
According to profiling, those two are among most frequently called,
so inlining is generally useful, and unrolling should help with it.
Further, this fixes undefined behaviour seen with invalid values.

Inspired by Yu Liu.
2021-05-22 18:40:45 +03:00
Ruslan Ermilov
ecbe06b9fe Stream: the "fastopen" parameter of the "listen" directive.
Based on a patch by Anbang Wen.
2021-05-20 19:59:16 +03:00
Ruslan Ermilov
6029e211c6 Core: fixed comment about msie_refresh escaping.
After 12a656452ad1, the "%" character is no longer escaped by
ngx_escape_uri(NGX_ESCAPE_REFRESH).
2021-05-19 16:24:13 +03:00
Maxim Dounin
173f16f736 Mail: max_errors directive.
Similarly to smtpd_hard_error_limit in Postfix and smtp_max_unknown_commands
in Exim, specifies the number of errors after which the connection is closed.
2021-05-19 03:13:31 +03:00
Maxim Dounin
5015209054 Mail: IMAP pipelining support.
The change is mostly the same as the SMTP one (04e43d03e153 and 3f5d0af4e40a),
and ensures that nginx is able to properly handle or reject multiple IMAP
commands.  The s->cmd field is not really used and set for consistency.

Non-synchronizing literals handling in invalid/unknown commands is limited,
so when a non-synchronizing literal is detected at the end of a discarded
line, the connection is closed.
2021-05-19 03:13:28 +03:00
Maxim Dounin
4617dd64b8 Mail: stricter checking of IMAP tags.
Only "A-Za-z0-9-._" characters now allowed (which is stricter than what
RFC 3501 requires, but expected to be enough for all known clients),
and tags shouldn't be longer than 32 characters.
2021-05-19 03:13:26 +03:00
Maxim Dounin
82840d1651 Mail: fixed backslash handling in IMAP literals.
Previously, s->backslash was set if any of the arguments was a quoted
string with a backslash character.  After successful command parsing
this resulted in all arguments being filtered to remove backslashes.
This is, however, incorrect, as backslashes should not be removed from
IMAP literals.  For example:

   S: * OK IMAP4 ready
   C: a01 login {9}
   S: + OK
   C: user\name "pass\"word"
   S: * BAD internal server error

resulted in "Auth-User: username" instead of "Auth-User: user\name"
as it should.

Fix is to apply backslash filtering on per-argument basis during parsing.
2021-05-19 03:13:23 +03:00
Maxim Dounin
fabe28259f Mail: removed dead s->arg_start handling.
As discussed in the previous change, s->arg_start handling in the "done"
labels of ngx_mail_pop3_parse_command(), ngx_mail_imap_parse_command(),
and ngx_mail_smtp_parse_command() is wrong: s->arg_start cannot be
set there, as it is handled and cleared on all code paths where the
"done" labels are reached.  The relevant code is dead and now removed.
2021-05-19 03:13:22 +03:00
Maxim Dounin
3c660ef59b Mail: fixed s->arg_start clearing on invalid IMAP commands.
Previously, s->arg_start was left intact after invalid IMAP commands,
and this might result in an argument incorrectly added to the following
command.  Similarly, s->backslash was left intact as well, leading
to unneeded backslash removal.

For example (LFs from the client are explicitly shown as "<LF>"):

  S: * OK IMAP4 ready
  C: a01 login "\<LF>
  S: a01 BAD invalid command
  C: a0000000000\2 authenticate <LF>
  S: a00000000002 aBAD invalid command

The backslash followed by LF generates invalid command with s->arg_start
and s->backslash set, the following command incorrectly treats anything
from the old s->arg_start to the space after the command as an argument,
and removes the backslash from the tag.  If there is no space, s->arg_end
will be NULL.

Both things seem to be harmless though.  In particular:

- This can be used to provide an incorrect argument to a command without
  arguments.  The only command which seems to look at the single argument
  is AUTHENTICATE, and it checks the argument length before trying to
  access it.

- Backslash removal uses the "end" pointer, and stops due to "src < end"
  condition instead of scanning all the process memory if s->arg_end is
  NULL (and arg[0].len is huge).

- There should be no backslashes in unquoted strings.

An obvious fix is to clear s->arg_start and s->backslash on invalid commands,
similarly to how it is done in POP3 parsing (added in 810:e3aa8f305d21) and
SMTP parsing.

This, however, makes it clear that s->arg_start handling in the "done"
label is wrong: s->arg_start cannot be legitimately set there, as it
is expected to be cleared in all possible cases when the "done" label is
reached.  The relevant code is dead and will be removed by the following
change.
2021-05-19 03:13:20 +03:00
Maxim Dounin
d96d60d2e0 Mail: POP3 pipelining support.
The change is mostly the same as the SMTP one (04e43d03e153 and 3f5d0af4e40a),
and ensures that nginx is able to properly handle or reject multiple POP3
commands, as required by the PIPELINING capability (RFC 2449).  The s->cmd
field is not really used and set for consistency.
2021-05-19 03:13:18 +03:00
Maxim Dounin
317223cb56 Mail: optimized discarding invalid SMTP commands.
There is no need to scan buffer from s->buffer->pos, as we already scanned
the buffer till "p" and wasn't able to find an LF.

There is no real need for this change in SMTP, since it is at most a
microoptimization of a non-common code path.  Similar code in IMAP, however,
will have to start scanning from "p" to be correct, since there can be
newlines in IMAP literals.
2021-05-19 03:13:17 +03:00
Maxim Dounin
204f944add Mail: fixed handling of invalid SMTP commands split between reads.
Previously, if an invalid SMTP command was split between reads, nginx failed
to wait for LF before returning an error, and interpreted the rest of the
command received later as a separate command.

The sw_invalid state in ngx_mail_smtp_parse_command(), introduced in
04e43d03e153, did not work, since ngx_mail_smtp_auth_state() clears
s->state when returning an error due to NGX_MAIL_PARSE_INVALID_COMMAND.
And not clearing s->state will introduce another problem: the rest
of the command would trigger duplicate error when rest of the command is
received.

Fix is to return NGX_AGAIN from ngx_mail_smtp_parse_command() until full
command is received.
2021-05-19 03:13:15 +03:00
Maxim Dounin
60a5a6f0d3 Mail: fixed SMTP pipelining to send the response immediately.
Previously, if there were some pipelined SMTP data in the buffer when
a proxied connection with the backend was established, nginx called
ngx_mail_proxy_handler() to send these data, and not tried to send the
response to the last command.  In most cases, this response was later sent
along with the response to the pipelined command, but if for some reason
client decides to wait for the response before finishing the next command
this might result in a connection hang.

Fix is to always call ngx_mail_proxy_handler() to send the response, and
additionally post an event to send the pipelined data if needed.
2021-05-19 03:13:12 +03:00
Roman Arutyunyan
66f736391e HTTP/3: fixed server push after 9ec3e71f8a61.
When using server push, a segfault occured because
ngx_http_v3_create_push_request() accessed ngx_http_v3_session_t object the old
way.  Prior to 9ec3e71f8a61, HTTP/3 session was stored directly in c->data.
Now it's referenced by the v3_session field of ngx_http_connection_t.
2021-05-18 18:17:25 +03:00
Maxim Dounin
130a3ec501 MIME: added application/wasm type (ticket #1606). 2021-05-08 20:31:03 +03:00
Maxim Dounin
c7de65228f Upstream: variables support in certificates. 2021-05-06 02:22:09 +03:00
Maxim Dounin
a6bce8c227 Auth basic: changed alcf->user_file to be a pointer.
This saves some memory in typical case when auth_basic_user_file is not
explicitly set, and unifies the code with alcf->realm.
2021-05-06 02:22:07 +03:00