Klaus Demo nginx / 7ca4b67
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. Maxim Dounin 5 years ago
3 changed file(s) with 21 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
245245 if (aligned <= cl->buf->file_last) {
246246 size = aligned - cl->buf->file_pos;
247247 }
248
249 total += size;
250 break;
248251 }
249252
250253 total += size;
9797
9898 send += file_size;
9999
100 if (header.count == 0) {
100 if (header.count == 0 && send < limit) {
101101
102102 /*
103103 * create the trailer iovec and coalesce the neighbouring bufs
113113
114114 send += file_size;
115115
116 /* create the trailer iovec and coalesce the neighbouring bufs */
117
118 cl = ngx_output_chain_to_iovec(&trailer, cl, limit - send, c->log);
119
120 if (cl == NGX_CHAIN_ERROR) {
121 return NGX_CHAIN_ERROR;
122 }
123
124 send += trailer.size;
116 if (send < limit) {
117
118 /*
119 * create the trailer iovec and coalesce the neighbouring bufs
120 */
121
122 cl = ngx_output_chain_to_iovec(&trailer, cl, limit - send,
123 c->log);
124 if (cl == NGX_CHAIN_ERROR) {
125 return NGX_CHAIN_ERROR;
126 }
127
128 send += trailer.size;
129
130 } else {
131 trailer.count = 0;
132 }
125133
126134 if (ngx_freebsd_use_tcp_nopush
127135 && c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)