comparison src/http/modules/ngx_http_memcached_module.c @ 7674:7731c710796f

Memcached: protect from too long responses. If a memcached response was followed by a correct trailer, and then the NUL character followed by some extra data - this was accepted by the trailer checking code. This in turn resulted in ctx->rest underflow and caused negative size buffer on the next reading from the upstream, followed by the "negative size buf in writer" alert. Fix is to always check for too long responses, so a correct trailer cannot be followed by extra data.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 06 Jul 2020 18:36:17 +0300
parents 8b68d50090e4
children d26db4f82d7d
comparison
equal deleted inserted replaced
7673:c5840ca2063d 7674:7731c710796f
483 u = ctx->request->upstream; 483 u = ctx->request->upstream;
484 b = &u->buffer; 484 b = &u->buffer;
485 485
486 if (u->length == (ssize_t) ctx->rest) { 486 if (u->length == (ssize_t) ctx->rest) {
487 487
488 if (ngx_strncmp(b->last, 488 if (bytes > u->length
489 || ngx_strncmp(b->last,
489 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest, 490 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest,
490 bytes) 491 bytes)
491 != 0) 492 != 0)
492 { 493 {
493 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, 494 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
494 "memcached sent invalid trailer"); 495 "memcached sent invalid trailer");
495 496
496 u->length = 0; 497 u->length = 0;
538 return NGX_OK; 539 return NGX_OK;
539 } 540 }
540 541
541 last += (size_t) (u->length - NGX_HTTP_MEMCACHED_END); 542 last += (size_t) (u->length - NGX_HTTP_MEMCACHED_END);
542 543
543 if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) { 544 if (bytes > u->length
545 || ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0)
546 {
544 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0, 547 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
545 "memcached sent invalid trailer"); 548 "memcached sent invalid trailer");
546 549
547 b->last = last; 550 b->last = last;
548 cl->buf->last = last; 551 cl->buf->last = last;