summaryrefslogtreecommitdiffhomepage
path: root/src (follow)
AgeCommit message (Collapse)AuthorFilesLines
2017-01-20Upstream: fixed cache corruption and socket leaks with aio_write.Maxim Dounin2-0/+28
The ngx_event_pipe() function wasn't called on write events with wev->delayed set. As a result, threaded writing results weren't properly collected in ngx_event_pipe_write_to_downstream() when a write event was triggered for a completed write. Further, this wasn't detected, as p->aio was reset by a thread completion handler, and results were later collected in ngx_event_pipe_read_upstream() instead of scheduling a new write of additional data. If this happened on the last reading from an upstream, last part of the response was never written to the cache file. Similar problems might also happen in case of timeouts when writing to client, as this also results in ngx_event_pipe() not being called on write events. In this scenario socket leaks were observed. Fix is to check if p->writing is set in ngx_event_pipe_read_upstream(), and therefore collect results of previous write operations in case of read events as well, similar to how we do so in ngx_event_pipe_write_downstream(). This is enough to fix the wev->delayed case. Additionally, we now call ngx_event_pipe() from ngx_http_upstream_process_request() if there are uncollected write operations (p->writing and !p->aio). This also fixes the wev->timedout case.
2017-01-20Fixed trailer construction with limit on FreeBSD and macOS.Maxim Dounin3-7/+18
The ngx_chain_coalesce_file() function may produce more bytes to send then requested in the limit passed, as it aligns the last file position to send to memory page boundary. As a result, (limit - send) may become negative. This resulted in big positive number when converted to size_t while calling ngx_output_chain_to_iovec(). Another part of the problem is in ngx_chain_coalesce_file(): it changes cl to the next chain link even if the current buffer is only partially sent due to limit. Therefore, if a file buffer was not expected to be fully sent due to limit, and was followed by a memory buffer, nginx called sendfile() with a part of the file buffer, and the memory buffer in trailer. If there were enough room in the socket buffer, this resulted in a part of the file buffer being skipped, and corresponding part of the memory buffer sent instead. The bug was introduced in 8e903522c17a (1.7.8). Configurations affected are ones using limits, that is, limit_rate and/or sendfile_max_chunk, and memory buffers after file ones (may happen when using subrequests or with proxying with disk buffering). Fix is to explicitly check if (send < limit) before constructing trailer with ngx_output_chain_to_iovec(). Additionally, ngx_chain_coalesce_file() was modified to preserve unfinished file buffers in cl.
2016-12-10HTTP/2: prevented creating temp files for requests without body.Valentin Bartenev1-2/+4
The problem was introduced by 52bd8cc17f34.
2016-11-28HTTP/2: fixed posted streams handling.Valentin Bartenev3-38/+42
A bug was introduced by 82efcedb310b that could lead to timing out of responses or segmentation fault, when accept_mutex was enabled. The output queue in HTTP/2 can contain frames from different streams. When the queue is sent, all related write handlers need to be called. In order to do so, the streams were added to the h2c->posted queue after handling sent frames. Then this queue was processed in ngx_http_v2_write_handler(). If accept_mutex is enabled, the event's "ready" flag is set but its handler is not called immediately. Instead, the event is added to the ngx_posted_events queue. At the same time in this queue can be events from upstream connections. Such events can result in sending output queue before ngx_http_v2_write_handler() is triggered. And at the time ngx_http_v2_write_handler() is called, the output queue can be already empty with some streams added to h2c->posted. But after 82efcedb310b, these streams weren't processed if all frames have already been sent and the output queue was empty. This might lead to a situation when a number of streams were get stuck in h2c->posted queue for a long time. Eventually these streams might get closed by the send timeout. In the worst case this might also lead to a segmentation fault, if already freed stream was left in the h2c->posted queue. This could happen if one of the streams was terminated but wasn't closed, due to the HEADERS frame or a partially sent DATA frame left in the output queue. If this happened the ngx_http_v2_filter_cleanup() handler removed the stream from the h2c->waiting or h2c->posted queue on termination stage, before the frame has been sent, and the stream was again added to the h2c->posted queue after the frame was sent. In order to fix all these problems and simplify the code, write events of fake stream connections are now added to ngx_posted_events instead of using a custom h2c->posted queue.
2016-11-28HTTP/2: fixed saving preread buffer to temp file (ticket #1143).Valentin Bartenev1-0/+3
Previously, a request body bigger than "client_body_buffer_size" wasn't written into a temporary file if it has been pre-read entirely. The preread buffer is freed after processing, thus subsequent use of it might result in sending corrupted body or cause a segfault.
2016-10-20HTTP/2: graceful shutdown of active connections (closes #1106).Valentin Bartenev2-9/+39
Previously, while shutting down gracefully, the HTTP/2 connections were closed in transition to idle state after all active streams have been processed. That might never happen if the client continued opening new streams. Now, nginx sends GOAWAY to all HTTP/2 connections and ignores further attempts to open new streams. A worker process will quit as soon as processing of already opened streams is finished.
2016-10-10Core: sockaddr lengths now respected by ngx_cmp_sockaddr().Maxim Dounin1-6/+13
Linux can return AF_UNIX sockaddrs with partially filled sun_path, resulting in spurious comparison failures and failed binary upgrades. Added proper checking of the lengths provided. Reported by Jan Seda, http://mailman.nginx.org/pipermail/nginx-devel/2016-September/008832.html.
2016-10-03Addition filter: set last_in_chain flag when clearing last_buf.Roman Arutyunyan1-0/+1
When the last_buf flag is cleared for add_after_body to append more data from a subrequest, other filters may still have buffered data, which should be flushed at this point. For example, the sub_filter may have a partial match buffered, which will only be flushed after the subrequest is done, ending up with interleaved data in output. Setting last_in_chain instead of last_buf flushes the data and fixes the order of output buffers.
2017-01-31Version bump.Maxim Dounin1-2/+2
2016-10-18SSL: default DH parameters compatible with OpenSSL 1.1.0.Maxim Dounin1-0/+19
This is a direct commit to stable as there is no corresponding code in mainline, default DH parameters were removed in 1aa9650a8154.
2016-09-01Event pipe: do not set file's thread_handler if not needed.Maxim Dounin1-4/+6
This fixes a problem with aio threads and sendfile with aio_write switched off, as observed with range requests after fc72784b1f52 (1.9.13). Potential problems with sendfile in threads were previously described in 9fd738b85fad, and this seems to be one of them. The problem occurred as file's thread_handler was set to NULL by event pipe code after a sendfile thread task was scheduled. As a result, no sendfile completion code was executed, and the same buffer was additionally sent using non-threaded sendfile. Fix is to avoid modifying file's thread_handler if aio_write is switched off. Note that with "aio_write on" it is still possible that sendfile will use thread_handler as set by event pipe. This is believed to be safe though, as handlers used are compatible.
2016-08-22SSL: adopted session ticket handling for OpenSSL 1.1.0.Sergey Kandaurov1-1/+1
Return 1 in the SSL_CTX_set_tlsext_ticket_key_cb() callback function to indicate that a new session ticket is created, as per documentation. Until 1.1.0, OpenSSL didn't make a distinction between non-negative return values. See https://git.openssl.org/?p=openssl.git;a=commitdiff;h=5c753de for details.
2016-08-08SSL: guarded SSL_R_NO_CIPHERS_PASSED not present in OpenSSL 1.1.0.Sergey Kandaurov1-0/+2
It was removed in OpenSSL 1.1.0 Beta 3 (pre-release 6). It was not used since OpenSSL 1.0.1n and 1.0.2b.
2016-07-19HTTP/2: flushing of the SSL buffer in transition to the idle state.Valentin Bartenev1-2/+18
It fixes potential connection leak if some unsent data was left in the SSL buffer. Particularly, that could happen when a client canceled the stream after the HEADERS frame has already been created. In this case no other frames might be produced and the HEADERS frame alone didn't flush the buffer.
2016-07-19HTTP/2: refactored ngx_http_v2_send_output_queue().Valentin Bartenev1-10/+10
Now it returns NGX_AGAIN if there's still data to be sent.
2016-07-19HTTP/2: fixed send timer handling.Valentin Bartenev1-1/+1
Checking for return value of c->send_chain() isn't sufficient since there are data can be left in the SSL buffer. Now the wew->ready flag is used instead. In particular, this fixed a connection leak in cases when all streams were closed, but there's still some data to be sent in the SSL buffer and the client forgot about the connection.
2016-07-19HTTP/2: avoid sending output queue if there's nothing to send.Valentin Bartenev1-0/+10
Particularly this fixes alerts on OS X and NetBSD systems when HTTP/2 is configured over plain TCP sockets. On these systems calling writev() with no data leads to EINVAL errors being logged as "writev() failed (22: Invalid argument) while processing HTTP/2 connection".
2016-07-19HTTP/2: always handle streams in error state.Valentin Bartenev1-6/+8
Previously, a stream could be closed by timeout if it was canceled while its send window was exhausted.
2016-07-19HTTP/2: prevented output of the HEADERS frame for canceled streams.Valentin Bartenev1-2/+6
It's useless to generate HEADERS if the stream has been canceled already.
2016-07-19HTTP/2: always send GOAWAY while worker is shutting down.Valentin Bartenev1-1/+1
Previously, if the worker process exited, GOAWAY was sent to connections in idle state, but connections with active streams were closed without GOAWAY.
2016-07-02Sub filter: eliminate unnecessary buffering.Roman Arutyunyan1-4/+37
Previously, when a buffer was processed by the sub filter, its final bytes could be buffered by the filter even if they don't match any pattern. This happened because the Boyer-Moore algorithm, employed by the sub filter since b9447fc457b4 (1.9.4), matches the last characters of patterns prior to checking other characters. If the last character is out of scope, initial bytes of a potential match are buffered until the last character is available. Now, after receiving a flush or recycled buffer, the filter performs additional checks to reduce the number of buffered bytes. The potential match is checked against the initial parts of all patterns. Non-matching bytes are not buffered. This improves processing of a chunked response from upstream by sending the entire chunks without buffering unless a partial match is found at the end of a chunk.
2016-07-02Sub filter: introduced the ngx_http_sub_match() function.Roman Arutyunyan1-31/+52
No functional changes.
2016-06-16HTTP/2: fixed the "http request count is zero" alert.Valentin Bartenev1-0/+1
When the stream is terminated the HEADERS frame can still wait in the output queue. This frame can't be removed and must be sent to the client anyway, since HTTP/2 uses stateful compression for headers. So in order to postpone closing and freeing memory of such stream the special close stream handler is set to the write event. After the HEADERS frame is sent the write event is called and the stream will be finally closed. Some events like receiving a RST_STREAM can trigger the read handler of such stream in closing state and cause unexpected processing that can result in another attempt to finalize the request. To prevent it the read handler is now set to ngx_http_empty_handler. Thanks to Amazon.
2016-06-16HTTP/2: avoid adding Content-Length for requests without body.Valentin Bartenev1-2/+4
There is no reason to add the "Content-Length: 0" header to a proxied request without body if the header isn't presented in the original request. Thanks to Amazon.
2016-06-16HTTP/2: prevented double termination of a stream.Valentin Bartenev1-0/+5
According to RFC 7540, an endpoint should not send more than one RST_STREAM frame for any stream. Also, now all the data frames will be skipped while termination.
2016-06-16HTTP/2: fixed a segfault while processing unbuffered upload.Valentin Bartenev1-4/+0
The ngx_http_v2_finalize_connection() closes current stream, but that is an invalid operation while processing unbuffered upload. This results in access to already freed memory, since the upstream module sets a cleanup handler that also finalizes the request.
2016-05-24HTTP/2: unbreak build on MSVC.Valentin Bartenev1-1/+1
2016-05-24HTTP/2: implemented preread buffer for request body (closes #959).Valentin Bartenev4-43/+146
Previously, the stream's window was kept zero in order to prevent a client from sending the request body before it was requested (see 887cca40ba6a for details). Until such initial window was acknowledged all requests with data were rejected (see 0aa07850922f for details). That approach revealed a number of problems: 1. Some clients (notably MS IE/Edge, Safari, iOS applications) show an error or even crash if a stream is rejected; 2. This requires at least one RTT for every request with body before the client receives window update and able to send data. To overcome these problems the new directive "http2_body_preread_size" is introduced. It sets the initial window and configures a special per stream preread buffer that is used to save all incoming data before the body is requested and processed. If the directive's value is lower than the default initial window (65535), as previously, all streams with data will be rejected until the new window is acknowledged. Otherwise, no special processing is used and all requests with data are welcome right from the connection start. The default value is chosen to be 64k, which is bigger than the default initial window. Setting it to zero is fully complaint to the previous behavior.
2016-05-20HTTP/2: the "421 Misdirected Request" response (closes #848).Valentin Bartenev4-9/+23
Since 4fbef397c753 nginx rejects with the 400 error any attempts of requesting different host over the same connection, if the relevant virtual server requires verification of a client certificate. While requesting hosts other than negotiated isn't something legal in HTTP/1.x, the HTTP/2 specification explicitly permits such requests for connection reuse and has introduced a special response code 421. According to RFC 7540 Section 9.1.2 this code can be sent by a server that is not configured to produce responses for the combination of scheme and authority that are included in the request URI. And the client may retry the request over a different connection. Now this code is used for requests that aren't authorized in current connection. After receiving the 421 response a client will be able to open a new connection, provide the required certificate and retry the request. Unfortunately, not all clients currently are able to handle it well. Notably Chrome just shows an error, while at least the latest version of Firefox retries the request over a new connection.
2016-10-18Version bump.Maxim Dounin1-2/+2
2016-05-31Core: skip special buffers on writing (ticket #981).Maxim Dounin1-0/+5
A special last buffer with cl->buf->pos set to NULL can be present in a chain when writing request body if chunked encoding was used. This resulted in a NULL pointer dereference if it happened to be the only buffer left after a do...while loop iteration in ngx_write_chain_to_file(). The problem originally appeared in nginx 1.3.9 with chunked encoding support. Additionally, rev. 3832b608dc8d (nginx 1.9.13) changed the minimum number of buffers to trigger this from IOV_MAX (typically 1024) to NGX_IOVS_PREALLOCATE (typically 64). Fix is to skip such buffers in ngx_chain_to_iovec(), much like it is done in other places.
2016-05-31Version bump.Maxim Dounin1-2/+2
2016-04-26Stable branch.Maxim Dounin1-2/+2
2016-04-19HTTP/2: send the output queue after emitting WINDOW_UPDATE.Valentin Bartenev1-0/+10
The WINDOW_UPDATE frame could be left in the output queue for an indefinite period of time resulting in the request timeout. This might happen if reading of the body was triggered by an event unrelated to client connection, e.g. by the limit_req timer.
2016-04-19HTTP/2: skip data frames in case of internal errors.Valentin Bartenev1-0/+2
This prevents possible processing of such frames and triggering rb->post_handler if an error occurred during r->request_body initialization.
2016-04-19HTTP/2: don't send WINDOW_UPDATE for an empty request body.Valentin Bartenev1-12/+13
Particularly this prevents sending WINDOW_UPDATE with zero delta which can result in PROTOCOL_ERROR. Also removed surplus setting of no_flow_control to 0.
2016-04-19Thread pools: memory barriers in task completion notifications.Maxim Dounin1-0/+4
The ngx_thread_pool_done object isn't volatile, and at least some compilers assume that it is permitted to reorder modifications of volatile and non-volatile objects. Added appropriate ngx_memory_barrier() calls to make sure all modifications will happen before the lock is released. Reported by Mindaugas Rasiukevicius, http://mailman.nginx.org/pipermail/nginx-devel/2016-April/008160.html.
2016-04-18HTTP/2: write logs when refusing streams with data.Maxim Dounin1-0/+4
Refusing streams is known to be incorrectly handled at least by IE, Edge and Safari. Make sure to provide appropriate logging to simplify fixing this in the affected browsers.
2016-04-14HTTP/2: send WINDOW_UPDATE instead of RST_STREAM with NO_ERROR.Valentin Bartenev1-0/+22
After the 92464ebace8e change, it has been discovered that not all clients follow the RFC and handle RST_STREAM with NO_ERROR properly. Notably, Chrome currently interprets it as INTERNAL_ERROR and discards the response. As a workaround, instead of RST_STREAM the maximum stream window update will be sent, which will let client to send up to 2 GB of a request body data before getting stuck on flow control. All the received data will be silently discarded. See for details: http://mailman.nginx.org/pipermail/nginx-devel/2016-April/008143.html https://bugs.chromium.org/p/chromium/issues/detail?id=603182
2016-04-14HTTP/2: refuse streams with data until SETTINGS is acknowledged.Valentin Bartenev2-1/+8
A client is allowed to send requests before receiving and acknowledging the SETTINGS frame. Such a client having a wrong idea about the stream's could send the request body that nginx isn't ready to process. The previous behavior was to send RST_STREAM with FLOW_CONTROL_ERROR in such case, but it didn't allow retrying requests that have been rejected.
2016-04-14HTTP/2: deduplicated some code in ngx_http_v2_state_headers().Valentin Bartenev1-18/+13
No functional changes.
2016-04-11FastCGI: skip special bufs in buffered request body chain.Valentin Bartenev1-0/+5
This prevents forming empty records out of such buffers. Particularly it fixes double end-of-stream records with chunked transfer encoding, or when HTTP/2 is used and the END_STREAM flag has been sent without data. In both cases there is an empty buffer at the end of the request body chain with the "last_buf" flag set. The canonical libfcgi, as well as php implementation, tolerates such records, while the HHVM parser is more strict and drops the connection (ticket #950).
2016-04-12Fixed NGX_CONF_TAKE1/NGX_CONF_FLAG misuse (as in e444e8f6538b).Ruslan Ermilov1-1/+1
2016-04-11Fixed typos.Alessandro Ghedini1-2/+2
2016-04-08Removed redundant "u" format specifier.Ruslan Ermilov5-6/+6
It is implied for "x" and "X".
2016-04-08Simplified ngx_unix_recv() and ngx_readv_chain().Ruslan Ermilov2-6/+2
This makes ngx_unix_recv() and ngx_udp_unix_recv() differ minimally.
2016-04-08Merged implementations of ngx_unix_recv().Valentin Bartenev1-59/+11
There's no real need in two separate implementations, with and without kqueue support.
2016-04-08Fixed small inconsistency in handling EOF among receive functions.Valentin Bartenev2-42/+41
Now all functions always drop the ready flag in this case.
2016-04-08Merged implementations of ngx_udp_unix_recv().Valentin Bartenev1-47/+4
There's no real need in two separate implementations, with and without kqueue support.
2016-04-07Fixed spelling.Josh Soref4-7/+7