Blame SOURCES/nghttp2-1.33.0-CVE-2020-11080.patch

9a19bc
From 34670cfbc56f1c63ec046c38b9ad518010b5c84d Mon Sep 17 00:00:00 2001
9a19bc
From: James M Snell <jasnell@gmail.com>
9a19bc
Date: Fri, 17 Apr 2020 16:53:51 -0700
9a19bc
Subject: [PATCH 1/2] Implement max settings option
9a19bc
9a19bc
Upstream-commit: 336a98feb0d56b9ac54e12736b18785c27f75090
9a19bc
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
9a19bc
---
9a19bc
 doc/CMakeLists.txt             |  1 +
9a19bc
 doc/Makefile.am                |  1 +
9a19bc
 lib/includes/nghttp2/nghttp2.h | 23 +++++++++++++
9a19bc
 lib/nghttp2_helper.c           |  2 ++
9a19bc
 lib/nghttp2_option.c           |  5 +++
9a19bc
 lib/nghttp2_option.h           |  5 +++
9a19bc
 lib/nghttp2_session.c          | 21 ++++++++++++
9a19bc
 lib/nghttp2_session.h          |  2 ++
9a19bc
 tests/main.c                   |  2 ++
9a19bc
 tests/nghttp2_session_test.c   | 61 ++++++++++++++++++++++++++++++++++
9a19bc
 tests/nghttp2_session_test.h   |  1 +
9a19bc
 11 files changed, 124 insertions(+)
9a19bc
9a19bc
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
9a19bc
index 34c0279..f3aec84 100644
9a19bc
--- a/doc/CMakeLists.txt
9a19bc
+++ b/doc/CMakeLists.txt
9a19bc
@@ -42,6 +42,7 @@ set(APIDOCS
9a19bc
   nghttp2_option_set_no_recv_client_magic.rst
9a19bc
   nghttp2_option_set_peer_max_concurrent_streams.rst
9a19bc
   nghttp2_option_set_user_recv_extension_type.rst
9a19bc
+  nghttp2_option_set_max_settings.rst
9a19bc
   nghttp2_pack_settings_payload.rst
9a19bc
   nghttp2_priority_spec_check_default.rst
9a19bc
   nghttp2_priority_spec_default_init.rst
9a19bc
diff --git a/doc/Makefile.am b/doc/Makefile.am
9a19bc
index c17d933..5a58f8e 100644
9a19bc
--- a/doc/Makefile.am
9a19bc
+++ b/doc/Makefile.am
9a19bc
@@ -68,6 +68,7 @@ APIDOCS= \
9a19bc
 	nghttp2_option_set_peer_max_concurrent_streams.rst \
9a19bc
 	nghttp2_option_set_user_recv_extension_type.rst \
9a19bc
 	nghttp2_option_set_max_outbound_ack.rst \
9a19bc
+	nghttp2_option_set_max_settings.rst \
9a19bc
 	nghttp2_pack_settings_payload.rst \
9a19bc
 	nghttp2_priority_spec_check_default.rst \
9a19bc
 	nghttp2_priority_spec_default_init.rst \
9a19bc
diff --git a/lib/includes/nghttp2/nghttp2.h b/lib/includes/nghttp2/nghttp2.h
9a19bc
index d79bf48..b0ff6c5 100644
9a19bc
--- a/lib/includes/nghttp2/nghttp2.h
9a19bc
+++ b/lib/includes/nghttp2/nghttp2.h
9a19bc
@@ -222,6 +222,13 @@ typedef struct {
9a19bc
  */
9a19bc
 #define NGHTTP2_CLIENT_MAGIC_LEN 24
9a19bc
 
9a19bc
+/**
9a19bc
+ * @macro
9a19bc
+ *
9a19bc
+ * The default max number of settings per SETTINGS frame
9a19bc
+ */
9a19bc
+#define NGHTTP2_DEFAULT_MAX_SETTINGS 32
9a19bc
+
9a19bc
 /**
9a19bc
  * @enum
9a19bc
  *
9a19bc
@@ -392,6 +399,11 @@ typedef enum {
9a19bc
    * receives an other type of frame.
9a19bc
    */
9a19bc
   NGHTTP2_ERR_SETTINGS_EXPECTED = -536,
9a19bc
+  /**
9a19bc
+   * When a local endpoint receives too many settings entries
9a19bc
+   * in a single SETTINGS frame.
9a19bc
+   */
9a19bc
+  NGHTTP2_ERR_TOO_MANY_SETTINGS = -537,
9a19bc
   /**
9a19bc
    * The errors < :enum:`NGHTTP2_ERR_FATAL` mean that the library is
9a19bc
    * under unexpected condition and processing was terminated (e.g.,
9a19bc
@@ -2648,6 +2660,17 @@ NGHTTP2_EXTERN void nghttp2_option_set_no_closed_streams(nghttp2_option *option,
9a19bc
 NGHTTP2_EXTERN void nghttp2_option_set_max_outbound_ack(nghttp2_option *option,
9a19bc
                                                         size_t val);
9a19bc
 
9a19bc
+/**
9a19bc
+ * @function
9a19bc
+ *
9a19bc
+ * This function sets the maximum number of SETTINGS entries per
9a19bc
+ * SETTINGS frame that will be accepted. If more than those entries
9a19bc
+ * are received, the peer is considered to be misbehaving and session
9a19bc
+ * will be closed. The default value is 32.
9a19bc
+ */
9a19bc
+NGHTTP2_EXTERN void nghttp2_option_set_max_settings(nghttp2_option *option,
9a19bc
+                                                    size_t val);
9a19bc
+
9a19bc
 /**
9a19bc
  * @function
9a19bc
  *
9a19bc
diff --git a/lib/nghttp2_helper.c b/lib/nghttp2_helper.c
9a19bc
index 3b282c7..49bbf07 100644
9a19bc
--- a/lib/nghttp2_helper.c
9a19bc
+++ b/lib/nghttp2_helper.c
9a19bc
@@ -334,6 +334,8 @@ const char *nghttp2_strerror(int error_code) {
9a19bc
   case NGHTTP2_ERR_FLOODED:
9a19bc
     return "Flooding was detected in this HTTP/2 session, and it must be "
9a19bc
            "closed";
9a19bc
+  case NGHTTP2_ERR_TOO_MANY_SETTINGS:
9a19bc
+    return "SETTINGS frame contained more than the maximum allowed entries";
9a19bc
   default:
9a19bc
     return "Unknown error code";
9a19bc
   }
9a19bc
diff --git a/lib/nghttp2_option.c b/lib/nghttp2_option.c
9a19bc
index e53f22d..34348e6 100644
9a19bc
--- a/lib/nghttp2_option.c
9a19bc
+++ b/lib/nghttp2_option.c
9a19bc
@@ -121,3 +121,8 @@ void nghttp2_option_set_max_outbound_ack(nghttp2_option *option, size_t val) {
9a19bc
   option->opt_set_mask |= NGHTTP2_OPT_MAX_OUTBOUND_ACK;
9a19bc
   option->max_outbound_ack = val;
9a19bc
 }
9a19bc
+
9a19bc
+void nghttp2_option_set_max_settings(nghttp2_option *option, size_t val) {
9a19bc
+  option->opt_set_mask |= NGHTTP2_OPT_MAX_SETTINGS;
9a19bc
+  option->max_settings = val;
9a19bc
+}
9a19bc
diff --git a/lib/nghttp2_option.h b/lib/nghttp2_option.h
9a19bc
index 1f740aa..939729f 100644
9a19bc
--- a/lib/nghttp2_option.h
9a19bc
+++ b/lib/nghttp2_option.h
9a19bc
@@ -67,6 +67,7 @@ typedef enum {
9a19bc
   NGHTTP2_OPT_MAX_DEFLATE_DYNAMIC_TABLE_SIZE = 1 << 9,
9a19bc
   NGHTTP2_OPT_NO_CLOSED_STREAMS = 1 << 10,
9a19bc
   NGHTTP2_OPT_MAX_OUTBOUND_ACK = 1 << 11,
9a19bc
+  NGHTTP2_OPT_MAX_SETTINGS = 1 << 12,
9a19bc
 } nghttp2_option_flag;
9a19bc
 
9a19bc
 /**
9a19bc
@@ -85,6 +86,10 @@ struct nghttp2_option {
9a19bc
    * NGHTTP2_OPT_MAX_OUTBOUND_ACK
9a19bc
    */
9a19bc
   size_t max_outbound_ack;
9a19bc
+  /**
9a19bc
+   * NGHTTP2_OPT_MAX_SETTINGS
9a19bc
+   */
9a19bc
+  size_t max_settings;
9a19bc
   /**
9a19bc
    * Bitwise OR of nghttp2_option_flag to determine that which fields
9a19bc
    * are specified.
9a19bc
diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c
9a19bc
index 670f83f..7638823 100644
9a19bc
--- a/lib/nghttp2_session.c
9a19bc
+++ b/lib/nghttp2_session.c
9a19bc
@@ -458,6 +458,7 @@ static int session_new(nghttp2_session **session_ptr,
9a19bc
 
9a19bc
   (*session_ptr)->max_send_header_block_length = NGHTTP2_MAX_HEADERSLEN;
9a19bc
   (*session_ptr)->max_outbound_ack = NGHTTP2_DEFAULT_MAX_OBQ_FLOOD_ITEM;
9a19bc
+  (*session_ptr)->max_settings = NGHTTP2_DEFAULT_MAX_SETTINGS;
9a19bc
 
9a19bc
   if (option) {
9a19bc
     if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE) &&
9a19bc
@@ -521,6 +522,11 @@ static int session_new(nghttp2_session **session_ptr,
9a19bc
     if (option->opt_set_mask & NGHTTP2_OPT_MAX_OUTBOUND_ACK) {
9a19bc
       (*session_ptr)->max_outbound_ack = option->max_outbound_ack;
9a19bc
     }
9a19bc
+
9a19bc
+    if ((option->opt_set_mask & NGHTTP2_OPT_MAX_SETTINGS) &&
9a19bc
+        option->max_settings) {
9a19bc
+      (*session_ptr)->max_settings = option->max_settings;
9a19bc
+    }
9a19bc
   }
9a19bc
 
9a19bc
   rv = nghttp2_hd_deflate_init2(&(*session_ptr)->hd_deflater,
9a19bc
@@ -5658,6 +5664,16 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in,
9a19bc
           iframe->max_niv =
9a19bc
               iframe->frame.hd.length / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH + 1;
9a19bc
 
9a19bc
+          if (iframe->max_niv - 1 > session->max_settings) {
9a19bc
+            rv = nghttp2_session_terminate_session_with_reason(
9a19bc
+                session, NGHTTP2_ENHANCE_YOUR_CALM,
9a19bc
+                "SETTINGS: too many setting entries");
9a19bc
+            if (nghttp2_is_fatal(rv)) {
9a19bc
+              return rv;
9a19bc
+            }
9a19bc
+            return (ssize_t)inlen;
9a19bc
+          }
9a19bc
+
9a19bc
           iframe->iv = nghttp2_mem_malloc(mem, sizeof(nghttp2_settings_entry) *
9a19bc
                                                    iframe->max_niv);
9a19bc
 
9a19bc
@@ -7413,6 +7429,11 @@ static int nghttp2_session_upgrade_internal(nghttp2_session *session,
9a19bc
   if (settings_payloadlen % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
9a19bc
     return NGHTTP2_ERR_INVALID_ARGUMENT;
9a19bc
   }
9a19bc
+  /* SETTINGS frame contains too many settings */
9a19bc
+  if (settings_payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH
9a19bc
+        > session->max_settings) {
9a19bc
+    return NGHTTP2_ERR_TOO_MANY_SETTINGS;
9a19bc
+  }
9a19bc
   rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload,
9a19bc
                                               settings_payloadlen, mem);
9a19bc
   if (rv != 0) {
9a19bc
diff --git a/lib/nghttp2_session.h b/lib/nghttp2_session.h
9a19bc
index 2969364..e62a3bb 100644
9a19bc
--- a/lib/nghttp2_session.h
9a19bc
+++ b/lib/nghttp2_session.h
9a19bc
@@ -269,6 +269,8 @@ struct nghttp2_session {
9a19bc
   /* The maximum length of header block to send.  Calculated by the
9a19bc
      same way as nghttp2_hd_deflate_bound() does. */
9a19bc
   size_t max_send_header_block_length;
9a19bc
+  /* The maximum number of settings accepted per SETTINGS frame. */
9a19bc
+  size_t max_settings;
9a19bc
   /* Next Stream ID. Made unsigned int to detect >= (1 << 31). */
9a19bc
   uint32_t next_stream_id;
9a19bc
   /* The last stream ID this session initiated.  For client session,
9a19bc
diff --git a/tests/main.c b/tests/main.c
9a19bc
index 13865de..1f795cd 100644
9a19bc
--- a/tests/main.c
9a19bc
+++ b/tests/main.c
9a19bc
@@ -313,6 +313,8 @@ int main() {
9a19bc
                    test_nghttp2_session_set_local_window_size) ||
9a19bc
       !CU_add_test(pSuite, "session_cancel_from_before_frame_send",
9a19bc
                    test_nghttp2_session_cancel_from_before_frame_send) ||
9a19bc
+      !CU_add_test(pSuite, "session_too_many_settings",
9a19bc
+                   test_nghttp2_session_too_many_settings) ||
9a19bc
       !CU_add_test(pSuite, "session_removed_closed_stream",
9a19bc
                    test_nghttp2_session_removed_closed_stream) ||
9a19bc
       !CU_add_test(pSuite, "session_pause_data",
9a19bc
diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c
9a19bc
index 0013e92..ab76ab4 100644
9a19bc
--- a/tests/nghttp2_session_test.c
9a19bc
+++ b/tests/nghttp2_session_test.c
9a19bc
@@ -10450,6 +10450,67 @@ void test_nghttp2_session_cancel_from_before_frame_send(void) {
9a19bc
   nghttp2_session_del(session);
9a19bc
 }
9a19bc
 
9a19bc
+void test_nghttp2_session_too_many_settings(void) {
9a19bc
+  nghttp2_session *session;
9a19bc
+  nghttp2_option *option;
9a19bc
+  nghttp2_session_callbacks callbacks;
9a19bc
+  nghttp2_frame frame;
9a19bc
+  nghttp2_bufs bufs;
9a19bc
+  nghttp2_buf *buf;
9a19bc
+  ssize_t rv;
9a19bc
+  my_user_data ud;
9a19bc
+  nghttp2_settings_entry iv[3];
9a19bc
+  nghttp2_mem *mem;
9a19bc
+  nghttp2_outbound_item *item;
9a19bc
+
9a19bc
+  mem = nghttp2_mem_default();
9a19bc
+  frame_pack_bufs_init(&bufs);
9a19bc
+
9a19bc
+  memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
9a19bc
+  callbacks.on_frame_recv_callback = on_frame_recv_callback;
9a19bc
+  callbacks.send_callback = null_send_callback;
9a19bc
+
9a19bc
+  nghttp2_option_new(&option);
9a19bc
+  nghttp2_option_set_max_settings(option, 1);
9a19bc
+
9a19bc
+  nghttp2_session_client_new2(&session, &callbacks, &ud, option);
9a19bc
+
9a19bc
+  CU_ASSERT(1 == session->max_settings);
9a19bc
+
9a19bc
+  nghttp2_option_del(option);
9a19bc
+
9a19bc
+  iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
9a19bc
+  iv[0].value = 3000;
9a19bc
+
9a19bc
+  iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
9a19bc
+  iv[1].value = 16384;
9a19bc
+
9a19bc
+  nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, dup_iv(iv, 2),
9a19bc
+                              2);
9a19bc
+
9a19bc
+  rv = nghttp2_frame_pack_settings(&bufs, &frame.settings);
9a19bc
+
9a19bc
+  CU_ASSERT(0 == rv);
9a19bc
+  CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
9a19bc
+
9a19bc
+  nghttp2_frame_settings_free(&frame.settings, mem);
9a19bc
+
9a19bc
+  buf = &bufs.head->buf;
9a19bc
+  assert(nghttp2_bufs_len(&bufs) == nghttp2_buf_len(buf));
9a19bc
+
9a19bc
+  ud.frame_recv_cb_called = 0;
9a19bc
+
9a19bc
+  rv = nghttp2_session_mem_recv(session, buf->pos, nghttp2_buf_len(buf));
9a19bc
+  CU_ASSERT((ssize_t)nghttp2_buf_len(buf) == rv);
9a19bc
+
9a19bc
+  item = nghttp2_session_get_next_ob_item(session);
9a19bc
+  CU_ASSERT(NGHTTP2_GOAWAY == item->frame.hd.type);
9a19bc
+
9a19bc
+  nghttp2_bufs_reset(&bufs);
9a19bc
+  nghttp2_bufs_free(&bufs);
9a19bc
+  nghttp2_session_del(session);
9a19bc
+}
9a19bc
+
9a19bc
 static void
9a19bc
 prepare_session_removed_closed_stream(nghttp2_session *session,
9a19bc
                                       nghttp2_hd_deflater *deflater) {
9a19bc
diff --git a/tests/nghttp2_session_test.h b/tests/nghttp2_session_test.h
9a19bc
index 35a48b8..c5095c2 100644
9a19bc
--- a/tests/nghttp2_session_test.h
9a19bc
+++ b/tests/nghttp2_session_test.h
9a19bc
@@ -155,6 +155,7 @@ void test_nghttp2_session_repeated_priority_change(void);
9a19bc
 void test_nghttp2_session_repeated_priority_submission(void);
9a19bc
 void test_nghttp2_session_set_local_window_size(void);
9a19bc
 void test_nghttp2_session_cancel_from_before_frame_send(void);
9a19bc
+void test_nghttp2_session_too_many_settings(void);
9a19bc
 void test_nghttp2_session_removed_closed_stream(void);
9a19bc
 void test_nghttp2_session_pause_data(void);
9a19bc
 void test_nghttp2_session_no_closed_streams(void);
9a19bc
-- 
9a19bc
2.21.3
9a19bc
9a19bc
9a19bc
From da3f4a5730ffa015a9e2d62e6e876a02f1dced20 Mon Sep 17 00:00:00 2001
9a19bc
From: James M Snell <jasnell@gmail.com>
9a19bc
Date: Sun, 19 Apr 2020 09:12:24 -0700
9a19bc
Subject: [PATCH 2/2] Earlier check for settings flood
9a19bc
9a19bc
Upstream-commit: f8da73bd042f810f34d19f9eae02b46d870af394
9a19bc
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
9a19bc
---
9a19bc
 lib/nghttp2_session.c | 6 ++++++
9a19bc
 1 file changed, 6 insertions(+)
9a19bc
9a19bc
diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c
9a19bc
index 7638823..8271198 100644
9a19bc
--- a/lib/nghttp2_session.c
9a19bc
+++ b/lib/nghttp2_session.c
9a19bc
@@ -5654,6 +5654,12 @@ ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in,
9a19bc
           break;
9a19bc
         }
9a19bc
 
9a19bc
+        /* Check the settings flood counter early to be safe */
9a19bc
+        if (session->obq_flood_counter_ >= session->max_outbound_ack &&
9a19bc
+            !(iframe->frame.hd.flags & NGHTTP2_FLAG_ACK)) {
9a19bc
+          return NGHTTP2_ERR_FLOODED;
9a19bc
+        }
9a19bc
+
9a19bc
         iframe->state = NGHTTP2_IB_READ_SETTINGS;
9a19bc
 
9a19bc
         if (iframe->payloadleft) {
9a19bc
-- 
9a19bc
2.21.3
9a19bc