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

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