Blame SOURCES/CVE-2019-15605-HTTP-request-smuggling.patch

1025aa
From b41d69bedcdbb8fe0cd790d0bcccbb457d6170d3 Mon Sep 17 00:00:00 2001
1025aa
From: Sergio Correia <scorreia@redhat.com>
1025aa
Date: Wed, 26 Feb 2020 17:03:26 -0300
1025aa
Subject: [PATCH] CVE-2019-15605 - HTTP request smuggling
1025aa
1025aa
Upstream: https://github.com/nodejs/http-parser/commit/7d5c99d09f6743b055d53fc3f642746d9801479b
1025aa
1025aa
Support multi-coding Transfer-Encoding
1025aa
1025aa
`Transfer-Encoding` header might have multiple codings in it. Even
1025aa
though llhttp cares only about `chunked`, it must check that `chunked`
1025aa
is the last coding (if present).
1025aa
1025aa
ABNF from RFC 7230:
1025aa
1025aa
```
1025aa
Transfer-Encoding = *( "," OWS ) transfer-coding *( OWS "," [ OWS
1025aa
    transfer-coding ] )
1025aa
transfer-coding = "chunked" / "compress" / "deflate" / "gzip" /
1025aa
    transfer-extension
1025aa
   transfer-extension = token *( OWS ";" OWS transfer-parameter )
1025aa
   transfer-parameter = token BWS "=" BWS ( token / quoted-string )
1025aa
```
1025aa
1025aa
However, if `chunked` is not last - llhttp must assume that the encoding
1025aa
and size of the body is unknown (according to 3.3.3 of RFC 7230) and
1025aa
read the response until EOF. For request - the error must be raised for
1025aa
an unknown `Transfer-Encoding`.
1025aa
1025aa
Furthermore, 3.3.3 of RFC 7230 explicitly states that presence of both
1025aa
`Transfer-Encoding` and `Content-Length` indicates the smuggling attack
1025aa
and "ought to be handled as an error".
1025aa
1025aa
For the lenient mode:
1025aa
1025aa
* Unknown `Transfer-Encoding` in requests is not an error and request
1025aa
  body is simply read until EOF (end of connection)
1025aa
* Only `Transfer-Encoding: chunked` together with `Content-Length` would
1025aa
  result an error (just like before the patch)
1025aa
1025aa
PR-URL: nodejs-private/http-parser-private#4
1025aa
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1025aa
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
1025aa
Reviewed-By: James M Snell <jasnell@gmail.com>
1025aa
---
1025aa
 http_parser.c | 110 ++++++++++++++++++++++++++++++++++++++++++++------
1025aa
 http_parser.h |   8 ++--
1025aa
 test.c        |  90 +++++++++++++++++++++++++++++++++++++++--
1025aa
 3 files changed, 189 insertions(+), 19 deletions(-)
1025aa
1025aa
diff --git a/http_parser.c b/http_parser.c
1025aa
index aef4437..cd120d8 100644
1025aa
--- a/http_parser.c
1025aa
+++ b/http_parser.c
1025aa
@@ -176,6 +176,22 @@ static const char *method_strings[] =
1025aa
 #undef XX
1025aa
   };
1025aa
 
1025aa
+/* Added for handling CVE-2019-15605. */
1025aa
+static void reset_flags(http_parser* p)
1025aa
+{
1025aa
+    p->flags = 0;
1025aa
+    p->transfer_encoding = 0;
1025aa
+}
1025aa
+
1025aa
+static void set_transfer_encoding(http_parser* p)
1025aa
+{
1025aa
+    p->transfer_encoding = 1;
1025aa
+}
1025aa
+
1025aa
+static int is_transfer_encoding(const http_parser* p)
1025aa
+{
1025aa
+    return p->transfer_encoding;
1025aa
+}
1025aa
 
1025aa
 /* Tokens as defined by rfc 2616. Also lowercases them.
1025aa
  *        token       = 1*<any CHAR except CTLs or separators>
1025aa
@@ -378,6 +394,7 @@ enum header_states
1025aa
   , h_upgrade
1025aa
 
1025aa
   , h_matching_transfer_encoding_chunked
1025aa
+
1025aa
   , h_matching_connection_token_start
1025aa
   , h_matching_connection_keep_alive
1025aa
   , h_matching_connection_close
1025aa
@@ -388,6 +405,10 @@ enum header_states
1025aa
   , h_connection_keep_alive
1025aa
   , h_connection_close
1025aa
   , h_connection_upgrade
1025aa
+
1025aa
+  /* CVE-2019-15605 */
1025aa
+  , h_matching_transfer_encoding_token_start
1025aa
+  , h_matching_transfer_encoding_token
1025aa
   };
1025aa
 
1025aa
 enum http_host_state
1025aa
@@ -722,7 +743,7 @@ reexecute:
1025aa
       {
1025aa
         if (ch == CR || ch == LF)
1025aa
           break;
1025aa
-        parser->flags = 0;
1025aa
+        reset_flags(parser);
1025aa
         parser->content_length = ULLONG_MAX;
1025aa
 
1025aa
         if (ch == 'H') {
1025aa
@@ -757,7 +778,7 @@ reexecute:
1025aa
 
1025aa
       case s_start_res:
1025aa
       {
1025aa
-        parser->flags = 0;
1025aa
+        reset_flags(parser);
1025aa
         parser->content_length = ULLONG_MAX;
1025aa
 
1025aa
         switch (ch) {
1025aa
@@ -921,7 +942,7 @@ reexecute:
1025aa
       {
1025aa
         if (ch == CR || ch == LF)
1025aa
           break;
1025aa
-        parser->flags = 0;
1025aa
+        reset_flags(parser);
1025aa
         parser->content_length = ULLONG_MAX;
1025aa
 
1025aa
         if (UNLIKELY(!IS_ALPHA(ch))) {
1025aa
@@ -1313,6 +1334,7 @@ reexecute:
1025aa
                 parser->header_state = h_general;
1025aa
               } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) {
1025aa
                 parser->header_state = h_transfer_encoding;
1025aa
+                set_transfer_encoding(parser);
1025aa
               }
1025aa
               break;
1025aa
 
1025aa
@@ -1393,10 +1415,14 @@ reexecute:
1025aa
             if ('c' == c) {
1025aa
               parser->header_state = h_matching_transfer_encoding_chunked;
1025aa
             } else {
1025aa
-              parser->header_state = h_general;
1025aa
+              parser->header_state = h_matching_transfer_encoding_token;
1025aa
             }
1025aa
             break;
1025aa
 
1025aa
+          /* Multi-value `Transfer-Encoding` header */
1025aa
+          case h_matching_transfer_encoding_token_start:
1025aa
+            break;
1025aa
+
1025aa
           case h_content_length:
1025aa
             if (UNLIKELY(!IS_NUM(ch))) {
1025aa
               SET_ERRNO(HPE_INVALID_CONTENT_LENGTH);
1025aa
@@ -1539,16 +1565,41 @@ reexecute:
1025aa
               goto error;
1025aa
 
1025aa
             /* Transfer-Encoding: chunked */
1025aa
+            case h_matching_transfer_encoding_token_start:
1025aa
+              /* looking for 'Transfer-Encoding: chunked' */
1025aa
+              if ('c' == c) {
1025aa
+                h_state = h_matching_transfer_encoding_chunked;
1025aa
+              } else if (STRICT_TOKEN(c)) {
1025aa
+                /* TODO(indutny): similar code below does this, but why?
1025aa
+                 * At the very least it seems to be inconsistent given that
1025aa
+                 * h_matching_transfer_encoding_token does not check for
1025aa
+                 * `STRICT_TOKEN`
1025aa
+                 */
1025aa
+                h_state = h_matching_transfer_encoding_token;
1025aa
+              } else if (c == ' ' || c == '\t') {
1025aa
+                /* Skip lws */
1025aa
+              } else {
1025aa
+                h_state = h_general;
1025aa
+              }
1025aa
+              break;
1025aa
+
1025aa
             case h_matching_transfer_encoding_chunked:
1025aa
               parser->index++;
1025aa
               if (parser->index > sizeof(CHUNKED)-1
1025aa
                   || c != CHUNKED[parser->index]) {
1025aa
-                h_state = h_general;
1025aa
+                h_state = h_matching_transfer_encoding_token;
1025aa
               } else if (parser->index == sizeof(CHUNKED)-2) {
1025aa
                 h_state = h_transfer_encoding_chunked;
1025aa
               }
1025aa
               break;
1025aa
 
1025aa
+            case h_matching_transfer_encoding_token:
1025aa
+              if (ch == ',') {
1025aa
+                h_state = h_matching_transfer_encoding_token_start;
1025aa
+                parser->index = 0;
1025aa
+              }
1025aa
+              break;
1025aa
+
1025aa
             case h_matching_connection_token_start:
1025aa
               /* looking for 'Connection: keep-alive' */
1025aa
               if (c == 'k') {
1025aa
@@ -1607,7 +1658,7 @@ reexecute:
1025aa
               break;
1025aa
 
1025aa
             case h_transfer_encoding_chunked:
1025aa
-              if (ch != ' ') h_state = h_general;
1025aa
+              if (ch != ' ') h_state = h_matching_transfer_encoding_token;
1025aa
               break;
1025aa
 
1025aa
             case h_connection_keep_alive:
1025aa
@@ -1732,12 +1783,17 @@ reexecute:
1025aa
           REEXECUTE();
1025aa
         }
1025aa
 
1025aa
-        /* Cannot use chunked encoding and a content-length header together
1025aa
-           per the HTTP specification. */
1025aa
-        if ((parser->flags & F_CHUNKED) &&
1025aa
+        /* Cannot use transfer-encoding and a content-length header together
1025aa
+           per the HTTP specification. (RFC 7230 Section 3.3.3) */
1025aa
+         if ((is_transfer_encoding(parser)) &&
1025aa
             (parser->flags & F_CONTENTLENGTH)) {
1025aa
-          SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
1025aa
-          goto error;
1025aa
+          /* Allow it for lenient parsing as long as `Transfer-Encoding` is
1025aa
+           * not `chunked`
1025aa
+           */
1025aa
+          if (!lenient || (parser->flags & F_CHUNKED)) {
1025aa
+            SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
1025aa
+            goto error;
1025aa
+          }
1025aa
         }
1025aa
 
1025aa
         UPDATE_STATE(s_headers_done);
1025aa
@@ -1811,8 +1867,31 @@ reexecute:
1025aa
           UPDATE_STATE(NEW_MESSAGE());
1025aa
           CALLBACK_NOTIFY(message_complete);
1025aa
         } else if (parser->flags & F_CHUNKED) {
1025aa
-          /* chunked encoding - ignore Content-Length header */
1025aa
+          /* chunked encoding - ignore Content-Length header,
1025aa
+           * prepare for a chunk */
1025aa
           UPDATE_STATE(s_chunk_size_start);
1025aa
+        } else if (is_transfer_encoding(parser)) {
1025aa
+          if (parser->type == HTTP_REQUEST && !lenient) {
1025aa
+            /* RFC 7230 3.3.3 */
1025aa
+
1025aa
+            /* If a Transfer-Encoding header field
1025aa
+             * is present in a request and the chunked transfer coding is not
1025aa
+             * the final encoding, the message body length cannot be determined
1025aa
+             * reliably; the server MUST respond with the 400 (Bad Request)
1025aa
+             * status code and then close the connection.
1025aa
+             */
1025aa
+            SET_ERRNO(HPE_INVALID_TRANSFER_ENCODING);
1025aa
+            RETURN(p - data); /* Error */
1025aa
+          } else {
1025aa
+            /* RFC 7230 3.3.3 */
1025aa
+
1025aa
+            /* If a Transfer-Encoding header field is present in a response and
1025aa
+             * the chunked transfer coding is not the final encoding, the
1025aa
+             * message body length is determined by reading the connection until
1025aa
+             * it is closed by the server.
1025aa
+             */
1025aa
+            UPDATE_STATE(s_body_identity_eof);
1025aa
+          }
1025aa
         } else {
1025aa
           if (parser->content_length == 0) {
1025aa
             /* Content-Length header given but zero: Content-Length: 0\r\n */
1025aa
@@ -2064,6 +2143,12 @@ http_message_needs_eof (const http_parser *parser)
1025aa
     return 0;
1025aa
   }
1025aa
 
1025aa
+  /* RFC 7230 3.3.3, see `s_headers_almost_done` */
1025aa
+  if ((is_transfer_encoding(parser)) &&
1025aa
+      (parser->flags & F_CHUNKED) == 0) {
1025aa
+    return 1;
1025aa
+  }
1025aa
+
1025aa
   if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) {
1025aa
     return 0;
1025aa
   }
1025aa
@@ -2107,6 +2192,7 @@ http_parser_init (http_parser *parser, enum http_parser_type t)
1025aa
   parser->type = t;
1025aa
   parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res));
1025aa
   parser->http_errno = HPE_OK;
1025aa
+  reset_flags(parser);
1025aa
 }
1025aa
 
1025aa
 void
1025aa
diff --git a/http_parser.h b/http_parser.h
1025aa
index ea7bafe..a4841be 100644
1025aa
--- a/http_parser.h
1025aa
+++ b/http_parser.h
1025aa
@@ -275,8 +275,9 @@ enum flags
1025aa
   XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
1025aa
   XX(STRICT, "strict mode assertion failed")                         \
1025aa
   XX(PAUSED, "parser is paused")                                     \
1025aa
-  XX(UNKNOWN, "an unknown error occurred")
1025aa
-
1025aa
+  XX(UNKNOWN, "an unknown error occurred") \
1025aa
+  XX(INVALID_TRANSFER_ENCODING,                                      \
1025aa
+     "request has invalid transfer-encoding")
1025aa
 
1025aa
 /* Define HPE_* values for each errno value above */
1025aa
 #define HTTP_ERRNO_GEN(n, s) HPE_##n,
1025aa
@@ -293,7 +294,7 @@ enum http_errno {
1025aa
 struct http_parser {
1025aa
   /** PRIVATE **/
1025aa
   unsigned int type : 2;         /* enum http_parser_type */
1025aa
-  unsigned int flags : 8;        /* F_* values from 'flags' enum; semi-public */
1025aa
+  unsigned int flags : 8;       /* F_* values from 'flags' enum; semi-public */
1025aa
   unsigned int state : 7;        /* enum state from http_parser.c */
1025aa
   unsigned int header_state : 7; /* enum header_state from http_parser.c */
1025aa
   unsigned int index : 7;        /* index into current matcher */
1025aa
@@ -318,6 +319,7 @@ struct http_parser {
1025aa
 
1025aa
   /** PUBLIC **/
1025aa
   void *data; /* A pointer to get hook to the "connection" or "socket" object */
1025aa
+  unsigned int transfer_encoding : 8; /* CVE-2019-15605 */
1025aa
 };
1025aa
 
1025aa
 
1025aa
diff --git a/test.c b/test.c
1025aa
index a1fa0d3..bb83d14 100644
1025aa
--- a/test.c
1025aa
+++ b/test.c
1025aa
@@ -260,7 +260,6 @@ const struct message requests[] =
1025aa
   ,.type= HTTP_REQUEST
1025aa
   ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"
1025aa
          "Accept: */*\r\n"
1025aa
-         "Transfer-Encoding: identity\r\n"
1025aa
          "Content-Length: 5\r\n"
1025aa
          "\r\n"
1025aa
          "World"
1025aa
@@ -273,10 +272,9 @@ const struct message requests[] =
1025aa
   ,.fragment= "hey"
1025aa
   ,.request_path= "/post_identity_body_world"
1025aa
   ,.request_url= "/post_identity_body_world?q=search#hey"
1025aa
-  ,.num_headers= 3
1025aa
+  ,.num_headers= 2
1025aa
   ,.headers=
1025aa
     { { "Accept", "*/*" }
1025aa
-    , { "Transfer-Encoding", "identity" }
1025aa
     , { "Content-Length", "5" }
1025aa
     }
1025aa
   ,.body= "World"
1025aa
@@ -1172,6 +1170,61 @@ const struct message requests[] =
1025aa
   ,.body= ""
1025aa
   }
1025aa
 
1025aa
+#define POST_MULTI_TE_LAST_CHUNKED 43
1025aa
+, {.name= "post - multi coding transfer-encoding chunked body"
1025aa
+  ,.type= HTTP_REQUEST
1025aa
+  ,.raw= "POST / HTTP/1.1\r\n"
1025aa
+         "Transfer-Encoding: deflate, chunked\r\n"
1025aa
+         "\r\n"
1025aa
+         "1e\r\nall your base are belong to us\r\n"
1025aa
+         "0\r\n"
1025aa
+         "\r\n"
1025aa
+  ,.should_keep_alive= TRUE
1025aa
+  ,.message_complete_on_eof= FALSE
1025aa
+  ,.http_major= 1
1025aa
+  ,.http_minor= 1
1025aa
+  ,.method= HTTP_POST
1025aa
+  ,.query_string= ""
1025aa
+  ,.fragment= ""
1025aa
+  ,.request_path= "/"
1025aa
+  ,.request_url= "/"
1025aa
+  ,.num_headers= 1
1025aa
+  ,.headers=
1025aa
+    { { "Transfer-Encoding" , "deflate, chunked" }
1025aa
+    }
1025aa
+  ,.body= "all your base are belong to us"
1025aa
+  ,.num_chunks_complete= 2
1025aa
+  ,.chunk_lengths= { 0x1e }
1025aa
+  }
1025aa
+
1025aa
+#define POST_MULTI_LINE_TE_LAST_CHUNKED 43
1025aa
+, {.name= "post - multi coding transfer-encoding chunked body"
1025aa
+  ,.type= HTTP_REQUEST
1025aa
+  ,.raw= "POST / HTTP/1.1\r\n"
1025aa
+         "Transfer-Encoding: deflate,\r\n"
1025aa
+         " chunked\r\n"
1025aa
+         "\r\n"
1025aa
+         "1e\r\nall your base are belong to us\r\n"
1025aa
+         "0\r\n"
1025aa
+         "\r\n"
1025aa
+  ,.should_keep_alive= TRUE
1025aa
+  ,.message_complete_on_eof= FALSE
1025aa
+  ,.http_major= 1
1025aa
+  ,.http_minor= 1
1025aa
+  ,.method= HTTP_POST
1025aa
+  ,.query_string= ""
1025aa
+  ,.fragment= ""
1025aa
+  ,.request_path= "/"
1025aa
+  ,.request_url= "/"
1025aa
+  ,.num_headers= 1
1025aa
+  ,.headers=
1025aa
+    { { "Transfer-Encoding" , "deflate, chunked" }
1025aa
+    }
1025aa
+  ,.body= "all your base are belong to us"
1025aa
+  ,.num_chunks_complete= 2
1025aa
+  ,.chunk_lengths= { 0x1e }
1025aa
+  }
1025aa
+
1025aa
 , {.name= NULL } /* sentinel */
1025aa
 };
1025aa
 
1025aa
@@ -1951,6 +2004,29 @@ const struct message responses[] =
1025aa
   ,.chunk_lengths= { 2, 2 }
1025aa
   }
1025aa
 
1025aa
+#define HTTP_200_MULTI_TE_NOT_LAST_CHUNKED 28
1025aa
+, {.name= "HTTP 200 response with `chunked` being *not last* Transfer-Encoding"
1025aa
+  ,.type= HTTP_RESPONSE
1025aa
+  ,.raw= "HTTP/1.1 200 OK\r\n"
1025aa
+         "Transfer-Encoding: chunked, identity\r\n"
1025aa
+         "\r\n"
1025aa
+         "2\r\n"
1025aa
+         "OK\r\n"
1025aa
+         "0\r\n"
1025aa
+         "\r\n"
1025aa
+  ,.should_keep_alive= FALSE
1025aa
+  ,.message_complete_on_eof= TRUE
1025aa
+  ,.http_major= 1
1025aa
+  ,.http_minor= 1
1025aa
+  ,.status_code= 200
1025aa
+  ,.response_status= "OK"
1025aa
+  ,.num_headers= 1
1025aa
+  ,.headers= { { "Transfer-Encoding", "chunked, identity" }
1025aa
+             }
1025aa
+  ,.body= "2\r\nOK\r\n0\r\n\r\n"
1025aa
+  ,.num_chunks_complete= 0
1025aa
+  }
1025aa
+
1025aa
 , {.name= NULL } /* sentinel */
1025aa
 };
1025aa
 
1025aa
@@ -3629,7 +3705,7 @@ test_chunked_content_length_error (int req)
1025aa
   parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
1025aa
   assert(parsed == strlen(buf));
1025aa
 
1025aa
-  buf = "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n";
1025aa
+  buf = "Transfer-Encoding: anything\r\nContent-Length: 1\r\n\r\n";
1025aa
   size_t buflen = strlen(buf);
1025aa
 
1025aa
   parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
1025aa
@@ -4277,6 +4353,12 @@ main (void)
1025aa
               "fooba",
1025aa
               HPE_OK);
1025aa
 
1025aa
+  // Unknown Transfer-Encoding in request
1025aa
+  test_simple("GET / HTTP/1.1\r\n"
1025aa
+              "Transfer-Encoding: unknown\r\n"
1025aa
+              "\r\n",
1025aa
+              HPE_INVALID_TRANSFER_ENCODING);
1025aa
+
1025aa
   static const char *all_methods[] = {
1025aa
     "DELETE",
1025aa
     "GET",
1025aa
-- 
1025aa
2.18.2
1025aa