[nginx] Memcached: protect from too long responses.

Maxim Dounin mdounin at mdounin.ru
Mon Jul 6 18:00:51 UTC 2020


details:   https://hg.nginx.org/nginx/rev/7731c710796f
branches:  
changeset: 7674:7731c710796f
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Mon Jul 06 18:36:17 2020 +0300
description:
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.

diffstat:

 src/http/modules/ngx_http_memcached_module.c |  9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diffs (28 lines):

diff -r c5840ca2063d -r 7731c710796f src/http/modules/ngx_http_memcached_module.c
--- a/src/http/modules/ngx_http_memcached_module.c	Fri Jul 03 16:16:47 2020 +0300
+++ b/src/http/modules/ngx_http_memcached_module.c	Mon Jul 06 18:36:17 2020 +0300
@@ -485,10 +485,11 @@ ngx_http_memcached_filter(void *data, ss
 
     if (u->length == (ssize_t) ctx->rest) {
 
-        if (ngx_strncmp(b->last,
+        if (bytes > u->length
+            || ngx_strncmp(b->last,
                    ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest,
                    bytes)
-            != 0)
+               != 0)
         {
             ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
                           "memcached sent invalid trailer");
@@ -540,7 +541,9 @@ ngx_http_memcached_filter(void *data, ss
 
     last += (size_t) (u->length - NGX_HTTP_MEMCACHED_END);
 
-    if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) {
+    if (bytes > u->length
+        || ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0)
+    {
         ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
                       "memcached sent invalid trailer");
 


More information about the nginx-devel mailing list