Blame SOURCES/mod_http2-1.15.7-CVE-2020-9490.patch

848061
From b8a8c5061eada0ce3339b24ba1d587134552bc0c Mon Sep 17 00:00:00 2001
848061
From: Stefan Eissing <stefan.eissing@greenbytes.de>
848061
Date: Wed, 29 Jul 2020 14:41:38 +0200
848061
Subject: [PATCH]  * Removing support for abandoned draft of http-wg regarding
848061
 cache-digests.
848061
848061
---
848061
848061
diff --git a/mod_http2/h2_push.c b/mod_http2/h2_push.c
848061
index 4a70674..8ae0b49 100644
848061
--- a/mod_http2/h2_push.c
848061
+++ b/mod_http2/h2_push.c
848061
@@ -464,33 +464,6 @@ apr_array_header_t *h2_push_collect(apr_pool_t *p, const h2_request *req,
848061
     return NULL;
848061
 }
848061
 
848061
-/*******************************************************************************
848061
- * push diary 
848061
- *
848061
- * - The push diary keeps track of resources already PUSHed via HTTP/2 on this
848061
- *   connection. It records a hash value from the absolute URL of the resource
848061
- *   pushed.
848061
- * - Lacking openssl, it uses 'apr_hashfunc_default' for the value
848061
- * - with openssl, it uses SHA256 to calculate the hash value
848061
- * - whatever the method to generate the hash, the diary keeps a maximum of 64
848061
- *   bits per hash, limiting the memory consumption to about 
848061
- *      H2PushDiarySize * 8 
848061
- *   bytes. Entries are sorted by most recently used and oldest entries are
848061
- *   forgotten first.
848061
- * - Clients can initialize/replace the push diary by sending a 'Cache-Digest'
848061
- *   header. Currently, this is the base64url encoded value of the cache digest
848061
- *   as specified in https://datatracker.ietf.org/doc/draft-kazuho-h2-cache-digest/
848061
- *   This draft can be expected to evolve and the definition of the header
848061
- *   will be added there and refined.
848061
- * - The cache digest header is a Golomb Coded Set of hash values, but it may
848061
- *   limit the amount of bits per hash value even further. For a good description
848061
- *   of GCS, read here:
848061
- *      http://giovanni.bajo.it/post/47119962313/golomb-coded-sets-smaller-than-bloom-filters
848061
- * - The means that the push diary might be initialized with hash values of much
848061
- *   less than 64 bits, leading to more false positives, but smaller digest size.
848061
- ******************************************************************************/
848061
- 
848061
- 
848061
 #define GCSLOG_LEVEL   APLOG_TRACE1
848061
 
848061
 typedef struct h2_push_diary_entry {
848061
@@ -618,38 +591,48 @@ static int h2_push_diary_find(h2_push_diary *diary, apr_uint64_t hash)
848061
     return -1;
848061
 }
848061
 
848061
-static h2_push_diary_entry *move_to_last(h2_push_diary *diary, apr_size_t idx)
848061
+static void move_to_last(h2_push_diary *diary, apr_size_t idx)
848061
 {
848061
     h2_push_diary_entry *entries = (h2_push_diary_entry*)diary->entries->elts;
848061
     h2_push_diary_entry e;
848061
-    apr_size_t lastidx = (apr_size_t)diary->entries->nelts;
848061
+    int lastidx;
848061
     
848061
+    /* Move an existing entry to the last place */
848061
+    if (diary->entries->nelts <= 0)
848061
+        return;
848061
+
848061
     /* move entry[idx] to the end */
848061
-    if (idx+1 < lastidx) {
848061
+    lastidx = diary->entries->nelts - 1;
848061
+    if (idx < lastidx) {
848061
         e =  entries[idx];
848061
-        memmove(entries+idx, entries+idx+1, sizeof(e) * (lastidx - idx));
848061
+        memmove(entries+idx, entries+idx+1, sizeof(h2_push_diary_entry) * (lastidx - idx));
848061
         entries[lastidx] = e;
848061
     }
848061
-    return &entries[lastidx];
848061
 }
848061
 
848061
-static void h2_push_diary_append(h2_push_diary *diary, h2_push_diary_entry *e)
848061
+static void remove_first(h2_push_diary *diary)
848061
 {
848061
-    h2_push_diary_entry *ne;
848061
+    h2_push_diary_entry *entries = (h2_push_diary_entry*)diary->entries->elts;
848061
+    int lastidx;
848061
     
848061
-    if (diary->entries->nelts < diary->N) {
848061
-        /* append a new diary entry at the end */
848061
-        APR_ARRAY_PUSH(diary->entries, h2_push_diary_entry) = *e;
848061
-        ne = &APR_ARRAY_IDX(diary->entries, diary->entries->nelts-1, h2_push_diary_entry);
848061
+    /* move remaining entries to index 0 */
848061
+    lastidx = diary->entries->nelts - 1;
848061
+    if (lastidx > 0) {
848061
+        --diary->entries->nelts;
848061
+        memmove(entries, entries+1, sizeof(h2_push_diary_entry) * diary->entries->nelts);
848061
     }
848061
-    else {
848061
-        /* replace content with new digest. keeps memory usage constant once diary is full */
848061
-        ne = move_to_last(diary, 0);
848061
-        *ne = *e;
848061
+}
848061
+
848061
+static void h2_push_diary_append(h2_push_diary *diary, h2_push_diary_entry *e)
848061
+{
848061
+    while (diary->entries->nelts >= diary->N) {
848061
+        remove_first(diary);
848061
     }
848061
+    /* append a new diary entry at the end */
848061
+    APR_ARRAY_PUSH(diary->entries, h2_push_diary_entry) = *e;
848061
     /* Intentional no APLOGNO */
848061
     ap_log_perror(APLOG_MARK, GCSLOG_LEVEL, 0, diary->entries->pool,
848061
-                  "push_diary_append: %"APR_UINT64_T_HEX_FMT, ne->hash);
848061
+                  "push_diary_append: %"APR_UINT64_T_HEX_FMT, e->hash);
848061
 }
848061
 
848061
 apr_array_header_t *h2_push_diary_update(h2_session *session, apr_array_header_t *pushes)
848061
@@ -692,30 +675,12 @@ apr_array_header_t *h2_push_collect_update(h2_stream *stream,
848061
                                            const struct h2_request *req, 
848061
                                            const struct h2_headers *res)
848061
 {
848061
-    h2_session *session = stream->session;
848061
-    const char *cache_digest = apr_table_get(req->headers, "Cache-Digest");
848061
     apr_array_header_t *pushes;
848061
-    apr_status_t status;
848061
     
848061
-    if (cache_digest && session->push_diary) {
848061
-        status = h2_push_diary_digest64_set(session->push_diary, req->authority, 
848061
-                                            cache_digest, stream->pool);
848061
-        if (status != APR_SUCCESS) {
848061
-            ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, session->c,
848061
-                          H2_SSSN_LOG(APLOGNO(03057), session,
848061
-                          "push diary set from Cache-Digest: %s"), cache_digest);
848061
-        }
848061
-    }
848061
     pushes = h2_push_collect(stream->pool, req, stream->push_policy, res);
848061
     return h2_push_diary_update(stream->session, pushes);
848061
 }
848061
 
848061
-static apr_int32_t h2_log2inv(unsigned char log2)
848061
-{
848061
-    return log2? (1 << log2) : 1;
848061
-}
848061
-
848061
-
848061
 typedef struct {
848061
     h2_push_diary *diary;
848061
     unsigned char log2p;
848061
@@ -830,11 +795,6 @@ apr_status_t h2_push_diary_digest_get(h2_push_diary *diary, apr_pool_t *pool,
848061
     apr_size_t hash_count;
848061
     
848061
     nelts = diary->entries->nelts;
848061
-    
848061
-    if ((apr_uint32_t)nelts > APR_UINT32_MAX) {
848061
-        /* should not happen */
848061
-        return APR_ENOTIMPL;
848061
-    }
848061
     N = ceil_power_of_2(nelts);
848061
     log2n = h2_log2(N);
848061
     
848061
@@ -896,166 +856,3 @@ apr_status_t h2_push_diary_digest_get(h2_push_diary *diary, apr_pool_t *pool,
848061
     return APR_SUCCESS;
848061
 }
848061
 
848061
-typedef struct {
848061
-    h2_push_diary *diary;
848061
-    apr_pool_t *pool;
848061
-    unsigned char log2p;
848061
-    const unsigned char *data;
848061
-    apr_size_t datalen;
848061
-    apr_size_t offset;
848061
-    unsigned int bit;
848061
-    apr_uint64_t last_val;
848061
-} gset_decoder;
848061
-
848061
-static int gset_decode_next_bit(gset_decoder *decoder)
848061
-{
848061
-    if (++decoder->bit >= 8) {
848061
-        if (++decoder->offset >= decoder->datalen) {
848061
-            return -1;
848061
-        }
848061
-        decoder->bit = 0;
848061
-    }
848061
-    return (decoder->data[decoder->offset] & cbit_mask[decoder->bit])? 1 : 0;
848061
-}
848061
-
848061
-static apr_status_t gset_decode_next(gset_decoder *decoder, apr_uint64_t *phash)
848061
-{
848061
-    apr_uint64_t flex = 0, fixed = 0, delta;
848061
-    int i;
848061
-    
848061
-    /* read 1 bits until we encounter 0, then read log2n(diary-P) bits.
848061
-     * On a malformed bit-string, this will not fail, but produce results
848061
-     * which are pbly too large. Luckily, the diary will modulo the hash.
848061
-     */
848061
-    while (1) {
848061
-        int bit = gset_decode_next_bit(decoder);
848061
-        if (bit == -1) {
848061
-            return APR_EINVAL;
848061
-        }
848061
-        if (!bit) {
848061
-            break;
848061
-        }
848061
-        ++flex;
848061
-    }
848061
-    
848061
-    for (i = 0; i < decoder->log2p; ++i) {
848061
-        int bit = gset_decode_next_bit(decoder);
848061
-        if (bit == -1) {
848061
-            return APR_EINVAL;
848061
-        }
848061
-        fixed = (fixed << 1) | bit;
848061
-    }
848061
-    
848061
-    delta = (flex << decoder->log2p) | fixed;
848061
-    *phash = delta + decoder->last_val;
848061
-    decoder->last_val = *phash;
848061
-    
848061
-    /* Intentional no APLOGNO */
848061
-    ap_log_perror(APLOG_MARK, GCSLOG_LEVEL, 0, decoder->pool,
848061
-                  "h2_push_diary_digest_dec: val=%"APR_UINT64_T_HEX_FMT", delta=%"
848061
-                  APR_UINT64_T_HEX_FMT", flex=%d, fixed=%"APR_UINT64_T_HEX_FMT, 
848061
-                  *phash, delta, (int)flex, fixed);
848061
-                  
848061
-    return APR_SUCCESS;
848061
-}
848061
-
848061
-/**
848061
- * Initialize the push diary by a cache digest as described in 
848061
- * https://datatracker.ietf.org/doc/draft-kazuho-h2-cache-digest/
848061
- * .
848061
- * @param diary the diary to set the digest into
848061
- * @param data the binary cache digest
848061
- * @param len the length of the cache digest
848061
- * @return APR_EINVAL if digest was not successfully parsed
848061
- */
848061
-apr_status_t h2_push_diary_digest_set(h2_push_diary *diary, const char *authority, 
848061
-                                      const char *data, apr_size_t len)
848061
-{
848061
-    gset_decoder decoder;
848061
-    unsigned char log2n, log2p;
848061
-    int N, i;
848061
-    apr_pool_t *pool = diary->entries->pool;
848061
-    h2_push_diary_entry e;
848061
-    apr_status_t status = APR_SUCCESS;
848061
-    
848061
-    if (len < 2) {
848061
-        /* at least this should be there */
848061
-        return APR_EINVAL;
848061
-    }
848061
-    log2n = data[0];
848061
-    log2p = data[1];
848061
-    diary->mask_bits = log2n + log2p;
848061
-    if (diary->mask_bits > 64) {
848061
-        /* cannot handle */
848061
-        return APR_ENOTIMPL;
848061
-    }
848061
-    
848061
-    /* whatever is in the digest, it replaces the diary entries */
848061
-    apr_array_clear(diary->entries);
848061
-    if (!authority || !strcmp("*", authority)) {
848061
-        diary->authority = NULL;
848061
-    }
848061
-    else if (!diary->authority || strcmp(diary->authority, authority)) {
848061
-        diary->authority = apr_pstrdup(diary->entries->pool, authority);
848061
-    }
848061
-
848061
-    N = h2_log2inv(log2n + log2p);
848061
-
848061
-    decoder.diary    = diary;
848061
-    decoder.pool     = pool;
848061
-    decoder.log2p    = log2p;
848061
-    decoder.data     = (const unsigned char*)data;
848061
-    decoder.datalen  = len;
848061
-    decoder.offset   = 1;
848061
-    decoder.bit      = 8;
848061
-    decoder.last_val = 0;
848061
-    
848061
-    diary->N = N;
848061
-    /* Determine effective N we use for storage */
848061
-    if (!N) {
848061
-        /* a totally empty cache digest. someone tells us that she has no
848061
-         * entries in the cache at all. Use our own preferences for N+mask 
848061
-         */
848061
-        diary->N = diary->NMax;
848061
-        return APR_SUCCESS;
848061
-    }
848061
-    else if (N > diary->NMax) {
848061
-        /* Store not more than diary is configured to hold. We open us up
848061
-         * to DOS attacks otherwise. */
848061
-        diary->N = diary->NMax;
848061
-    }
848061
-    
848061
-    /* Intentional no APLOGNO */
848061
-    ap_log_perror(APLOG_MARK, GCSLOG_LEVEL, 0, pool,
848061
-                  "h2_push_diary_digest_set: N=%d, log2n=%d, "
848061
-                  "diary->mask_bits=%d, dec.log2p=%d", 
848061
-                  (int)diary->N, (int)log2n, diary->mask_bits, 
848061
-                  (int)decoder.log2p);
848061
-                  
848061
-    for (i = 0; i < diary->N; ++i) {
848061
-        if (gset_decode_next(&decoder, &e.hash) != APR_SUCCESS) {
848061
-            /* the data may have less than N values */
848061
-            break;
848061
-        }
848061
-        h2_push_diary_append(diary, &e);
848061
-    }
848061
-    
848061
-    /* Intentional no APLOGNO */
848061
-    ap_log_perror(APLOG_MARK, GCSLOG_LEVEL, 0, pool,
848061
-                  "h2_push_diary_digest_set: diary now with %d entries, mask_bits=%d", 
848061
-                  (int)diary->entries->nelts, diary->mask_bits);
848061
-    return status;
848061
-}
848061
-
848061
-apr_status_t h2_push_diary_digest64_set(h2_push_diary *diary, const char *authority, 
848061
-                                        const char *data64url, apr_pool_t *pool)
848061
-{
848061
-    const char *data;
848061
-    apr_size_t len = h2_util_base64url_decode(&data, data64url, pool);
848061
-    /* Intentional no APLOGNO */
848061
-    ap_log_perror(APLOG_MARK, GCSLOG_LEVEL, 0, pool,
848061
-                  "h2_push_diary_digest64_set: digest=%s, dlen=%d", 
848061
-                  data64url, (int)len);
848061
-    return h2_push_diary_digest_set(diary, authority, data, len);
848061
-}
848061
-
848061
diff --git a/mod_http2/h2_push.h b/mod_http2/h2_push.h
848061
index 0533853..5dc189f 100644
848061
--- a/mod_http2/h2_push.h
848061
+++ b/mod_http2/h2_push.h
848061
@@ -35,6 +35,44 @@ typedef enum {
848061
     H2_PUSH_DIGEST_SHA256
848061
 } h2_push_digest_type;
848061
 
848061
+/*******************************************************************************
848061
+ * push diary 
848061
+ *
848061
+ * - The push diary keeps track of resources already PUSHed via HTTP/2 on this
848061
+ *   connection. It records a hash value from the absolute URL of the resource
848061
+ *   pushed.
848061
+ * - Lacking openssl, 
848061
+ * - with openssl, it uses SHA256 to calculate the hash value, otherwise it
848061
+ *   falls back to apr_hashfunc_default()
848061
+ * - whatever the method to generate the hash, the diary keeps a maximum of 64
848061
+ *   bits per hash, limiting the memory consumption to about 
848061
+ *      H2PushDiarySize * 8 
848061
+ *   bytes. Entries are sorted by most recently used and oldest entries are
848061
+ *   forgotten first.
848061
+ * - While useful by itself to avoid duplicated PUSHes on the same connection,
848061
+ *   the original idea was that clients provided a 'Cache-Digest' header with
848061
+ *   the values of *their own* cached resources. This was described in
848061
+ *   <https://datatracker.ietf.org/doc/draft-kazuho-h2-cache-digest/> 
848061
+ *   and some subsequent revisions that tweaked values but kept the overall idea.
848061
+ * - The draft was abandoned by the IETF http-wg, as support from major clients,
848061
+ *   e.g. browsers, was lacking for various reasons.
848061
+ * - For these reasons, mod_h2 abandoned its support for client supplied values
848061
+ *   but keeps the diary. It seems to provide value for applications using PUSH,
848061
+ *   is configurable in size and defaults to a very moderate amount of memory
848061
+ *   used.
848061
+ * - The cache digest header is a Golomb Coded Set of hash values, but it may
848061
+ *   limit the amount of bits per hash value even further. For a good description
848061
+ *   of GCS, read here:
848061
+ *   <http://giovanni.bajo.it/post/47119962313/golomb-coded-sets-smaller-than-bloom-filters>
848061
+ ******************************************************************************/
848061
+ 
848061
+ 
848061
+/*
848061
+ * The push diary is based on the abandoned draft 
848061
+ * <https://datatracker.ietf.org/doc/draft-kazuho-h2-cache-digest/>
848061
+ * that describes how to use golomb filters.
848061
+ */
848061
+
848061
 typedef struct h2_push_diary h2_push_diary;
848061
 
848061
 typedef void h2_push_digest_calc(h2_push_diary *diary, apr_uint64_t *phash, h2_push *push);
848061
@@ -101,20 +139,4 @@ apr_status_t h2_push_diary_digest_get(h2_push_diary *diary, apr_pool_t *p,
848061
                                       int maxP, const char *authority, 
848061
                                       const char **pdata, apr_size_t *plen);
848061
 
848061
-/**
848061
- * Initialize the push diary by a cache digest as described in 
848061
- * https://datatracker.ietf.org/doc/draft-kazuho-h2-cache-digest/
848061
- * .
848061
- * @param diary the diary to set the digest into
848061
- * @param authority the authority to set the data for
848061
- * @param data the binary cache digest
848061
- * @param len the length of the cache digest
848061
- * @return APR_EINVAL if digest was not successfully parsed
848061
- */
848061
-apr_status_t h2_push_diary_digest_set(h2_push_diary *diary, const char *authority, 
848061
-                                      const char *data, apr_size_t len);
848061
-
848061
-apr_status_t h2_push_diary_digest64_set(h2_push_diary *diary, const char *authority, 
848061
-                                        const char *data64url, apr_pool_t *pool);
848061
-
848061
 #endif /* defined(__mod_h2__h2_push__) */