[nginx] Fixed trailer construction with limit on FreeBSD and macOS.
Maxim Dounin
mdounin at mdounin.ru
Tue Jan 31 13:41:04 UTC 2017
details: http://hg.nginx.org/nginx/rev/a3e6d660b179
branches: stable-1.10
changeset: 6893:a3e6d660b179
user: Maxim Dounin <mdounin at mdounin.ru>
date: Fri Jan 20 21:12:48 2017 +0300
description:
Fixed trailer construction with limit on FreeBSD and macOS.
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.
diffstat:
src/core/ngx_buf.c | 3 +++
src/os/unix/ngx_darwin_sendfile_chain.c | 2 +-
src/os/unix/ngx_freebsd_sendfile_chain.c | 22 +++++++++++++++-------
3 files changed, 19 insertions(+), 8 deletions(-)
diffs (60 lines):
diff --git a/src/core/ngx_buf.c b/src/core/ngx_buf.c
--- a/src/core/ngx_buf.c
+++ b/src/core/ngx_buf.c
@@ -244,6 +244,9 @@ ngx_chain_coalesce_file(ngx_chain_t **in
if (aligned <= cl->buf->file_last) {
size = aligned - cl->buf->file_pos;
}
+
+ total += size;
+ break;
}
total += size;
diff --git a/src/os/unix/ngx_darwin_sendfile_chain.c b/src/os/unix/ngx_darwin_sendfile_chain.c
--- a/src/os/unix/ngx_darwin_sendfile_chain.c
+++ b/src/os/unix/ngx_darwin_sendfile_chain.c
@@ -98,7 +98,7 @@ ngx_darwin_sendfile_chain(ngx_connection
send += file_size;
- if (header.count == 0) {
+ if (header.count == 0 && send < limit) {
/*
* create the trailer iovec and coalesce the neighbouring bufs
diff --git a/src/os/unix/ngx_freebsd_sendfile_chain.c b/src/os/unix/ngx_freebsd_sendfile_chain.c
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -114,16 +114,24 @@ ngx_freebsd_sendfile_chain(ngx_connectio
send += file_size;
- /* create the trailer iovec and coalesce the neighbouring bufs */
-
- cl = ngx_output_chain_to_iovec(&trailer, cl, limit - send, c->log);
+ if (send < limit) {
- if (cl == NGX_CHAIN_ERROR) {
- return NGX_CHAIN_ERROR;
+ /*
+ * create the trailer iovec and coalesce the neighbouring bufs
+ */
+
+ cl = ngx_output_chain_to_iovec(&trailer, cl, limit - send,
+ c->log);
+ if (cl == NGX_CHAIN_ERROR) {
+ return NGX_CHAIN_ERROR;
+ }
+
+ send += trailer.size;
+
+ } else {
+ trailer.count = 0;
}
- send += trailer.size;
-
if (ngx_freebsd_use_tcp_nopush
&& c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
{
More information about the nginx-devel
mailing list