Blame SOURCES/httpd-2.4.34-CVE-2022-31813.patch

879b17
diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c
879b17
index 42a963f..713825d 100644
879b17
--- a/modules/proxy/proxy_util.c
879b17
+++ b/modules/proxy/proxy_util.c
879b17
@@ -3566,12 +3566,14 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
                                             char **old_cl_val,
879b17
                                             char **old_te_val)
879b17
 {
879b17
+    int rc = OK;
879b17
     conn_rec *c = r->connection;
879b17
     int counter;
879b17
     char *buf;
879b17
+    apr_table_t *saved_headers_in = r->headers_in;
879b17
+    const char *saved_host = apr_table_get(saved_headers_in, "Host");
879b17
     const apr_array_header_t *headers_in_array;
879b17
     const apr_table_entry_t *headers_in;
879b17
-    apr_table_t *saved_headers_in;
879b17
     apr_bucket *e;
879b17
     int do_100_continue;
879b17
     conn_rec *origin = p_conn->connection;
879b17
@@ -3608,6 +3610,52 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
     ap_xlate_proto_to_ascii(buf, strlen(buf));
879b17
     e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
879b17
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
879b17
+
879b17
+    /*
879b17
+     * Make a copy on r->headers_in for the request we make to the backend,
879b17
+     * modify the copy in place according to our configuration and connection
879b17
+     * handling, use it to fill in the forwarded headers' brigade, and finally
879b17
+     * restore the saved/original ones in r->headers_in.
879b17
+     *
879b17
+     * Note: We need to take r->pool for apr_table_copy as the key / value
879b17
+     * pairs in r->headers_in have been created out of r->pool and
879b17
+     * p might be (and actually is) a longer living pool.
879b17
+     * This would trigger the bad pool ancestry abort in apr_table_copy if
879b17
+     * apr is compiled with APR_POOL_DEBUG.
879b17
+     *
879b17
+     * icing: if p indeed lives longer than r->pool, we should allocate
879b17
+     * all new header values from r->pool as well and avoid leakage.
879b17
+     */
879b17
+    r->headers_in = apr_table_copy(r->pool, saved_headers_in);
879b17
+
879b17
+    /* Return the original Transfer-Encoding and/or Content-Length values
879b17
+     * then drop the headers, they must be set by the proxy handler based
879b17
+     * on the actual body being forwarded.
879b17
+     */
879b17
+    if ((*old_te_val = (char *)apr_table_get(r->headers_in,
879b17
+                                             "Transfer-Encoding"))) {
879b17
+        apr_table_unset(r->headers_in, "Transfer-Encoding");
879b17
+    }
879b17
+    if ((*old_cl_val = (char *)apr_table_get(r->headers_in,
879b17
+                                             "Content-Length"))) {
879b17
+        apr_table_unset(r->headers_in, "Content-Length");
879b17
+    }
879b17
+
879b17
+    /* Clear out hop-by-hop request headers not to forward */
879b17
+    if (ap_proxy_clear_connection(r, r->headers_in) < 0) {
879b17
+        rc = HTTP_BAD_REQUEST;
879b17
+        goto cleanup;
879b17
+    }
879b17
+
879b17
+    /* RFC2616 13.5.1 says we should strip these */
879b17
+    apr_table_unset(r->headers_in, "Keep-Alive");
879b17
+    apr_table_unset(r->headers_in, "Upgrade");
879b17
+    apr_table_unset(r->headers_in, "Trailer");
879b17
+    apr_table_unset(r->headers_in, "TE");
879b17
+
879b17
+    /* We used to send `Host: ` always first, so let's keep it that
879b17
+     * way. No telling which legacy backend is relying no this.
879b17
+     */
879b17
     if (dconf->preserve_host == 0) {
879b17
         if (ap_strchr_c(uri->hostname, ':')) { /* if literal IPv6 address */
879b17
             if (uri->port_str && uri->port != DEFAULT_HTTP_PORT) {
879b17
@@ -3629,7 +3677,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
         /* don't want to use r->hostname, as the incoming header might have a
879b17
          * port attached
879b17
          */
879b17
-        const char* hostname = apr_table_get(r->headers_in,"Host");
879b17
+        const char* hostname = saved_host;
879b17
         if (!hostname) {
879b17
             hostname =  r->server->server_hostname;
879b17
             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01092)
879b17
@@ -3643,21 +3691,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
     ap_xlate_proto_to_ascii(buf, strlen(buf));
879b17
     e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
879b17
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
879b17
-
879b17
-    /*
879b17
-     * Save the original headers in here and restore them when leaving, since
879b17
-     * we will apply proxy purpose only modifications (eg. clearing hop-by-hop
879b17
-     * headers, add Via or X-Forwarded-* or Expect...), whereas the originals
879b17
-     * will be needed later to prepare the correct response and logging.
879b17
-     *
879b17
-     * Note: We need to take r->pool for apr_table_copy as the key / value
879b17
-     * pairs in r->headers_in have been created out of r->pool and
879b17
-     * p might be (and actually is) a longer living pool.
879b17
-     * This would trigger the bad pool ancestry abort in apr_table_copy if
879b17
-     * apr is compiled with APR_POOL_DEBUG.
879b17
-     */
879b17
-    saved_headers_in = r->headers_in;
879b17
-    r->headers_in = apr_table_copy(r->pool, saved_headers_in);
879b17
+    apr_table_unset(r->headers_in, "Host");
879b17
 
879b17
     /* handle Via */
879b17
     if (conf->viaopt == via_block) {
879b17
@@ -3732,8 +3766,6 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
      */
879b17
     if (dconf->add_forwarded_headers) {
879b17
         if (PROXYREQ_REVERSE == r->proxyreq) {
879b17
-            const char *buf;
879b17
-
879b17
             /* Add X-Forwarded-For: so that the upstream has a chance to
879b17
              * determine, where the original request came from.
879b17
              */
879b17
@@ -3743,8 +3775,9 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
             /* Add X-Forwarded-Host: so that upstream knows what the
879b17
              * original request hostname was.
879b17
              */
879b17
-            if ((buf = apr_table_get(r->headers_in, "Host"))) {
879b17
-                apr_table_mergen(r->headers_in, "X-Forwarded-Host", buf);
879b17
+            if (saved_host) {
879b17
+                apr_table_mergen(r->headers_in, "X-Forwarded-Host",
879b17
+                                 saved_host);
879b17
             }
879b17
 
879b17
             /* Add X-Forwarded-Server: so that upstream knows what the
879b17
@@ -3756,11 +3789,28 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
         }
879b17
     }
879b17
 
879b17
-    proxy_run_fixups(r);
879b17
-    if (ap_proxy_clear_connection(r, r->headers_in) < 0) {
879b17
-        return HTTP_BAD_REQUEST;
879b17
+    /* Do we want to strip Proxy-Authorization ?
879b17
+     * If we haven't used it, then NO
879b17
+     * If we have used it then MAYBE: RFC2616 says we MAY propagate it.
879b17
+     * So let's make it configurable by env.
879b17
+     */
879b17
+    if (r->user != NULL /* we've authenticated */
879b17
+        && !apr_table_get(r->subprocess_env, "Proxy-Chain-Auth")) {
879b17
+        apr_table_unset(r->headers_in, "Proxy-Authorization");
879b17
     }
879b17
 
879b17
+    /* for sub-requests, ignore freshness/expiry headers */
879b17
+    if (r->main) {
879b17
+        apr_table_unset(r->headers_in, "If-Match");
879b17
+        apr_table_unset(r->headers_in, "If-Modified-Since");
879b17
+        apr_table_unset(r->headers_in, "If-Range");
879b17
+        apr_table_unset(r->headers_in, "If-Unmodified-Since");
879b17
+        apr_table_unset(r->headers_in, "If-None-Match");
879b17
+     }
879b17
+
879b17
+    /* run hook to fixup the request we are about to send */
879b17
+    proxy_run_fixups(r);
879b17
+
879b17
     creds = apr_table_get(r->notes, "proxy-basic-creds");
879b17
     if (creds) {
879b17
         apr_table_mergen(r->headers_in, "Proxy-Authorization", creds);
879b17
@@ -3771,55 +3821,8 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
     headers_in = (const apr_table_entry_t *) headers_in_array->elts;
879b17
     for (counter = 0; counter < headers_in_array->nelts; counter++) {
879b17
         if (headers_in[counter].key == NULL
879b17
-            || headers_in[counter].val == NULL
879b17
-
879b17
-            /* Already sent */
879b17
-            || !strcasecmp(headers_in[counter].key, "Host")
879b17
-
879b17
-            /* Clear out hop-by-hop request headers not to send
879b17
-             * RFC2616 13.5.1 says we should strip these headers
879b17
-             */
879b17
-            || !strcasecmp(headers_in[counter].key, "Keep-Alive")
879b17
-            || !strcasecmp(headers_in[counter].key, "TE")
879b17
-            || !strcasecmp(headers_in[counter].key, "Trailer")
879b17
-            || !strcasecmp(headers_in[counter].key, "Upgrade")
879b17
-
879b17
-            ) {
879b17
-            continue;
879b17
-        }
879b17
-        /* Do we want to strip Proxy-Authorization ?
879b17
-         * If we haven't used it, then NO
879b17
-         * If we have used it then MAYBE: RFC2616 says we MAY propagate it.
879b17
-         * So let's make it configurable by env.
879b17
-         */
879b17
-        if (!strcasecmp(headers_in[counter].key,"Proxy-Authorization")) {
879b17
-            if (r->user != NULL) { /* we've authenticated */
879b17
-                if (!apr_table_get(r->subprocess_env, "Proxy-Chain-Auth")) {
879b17
-                    continue;
879b17
-                }
879b17
-            }
879b17
-        }
879b17
-
879b17
-        /* Skip Transfer-Encoding and Content-Length for now.
879b17
-         */
879b17
-        if (!strcasecmp(headers_in[counter].key, "Transfer-Encoding")) {
879b17
-            *old_te_val = headers_in[counter].val;
879b17
-            continue;
879b17
-        }
879b17
-        if (!strcasecmp(headers_in[counter].key, "Content-Length")) {
879b17
-            *old_cl_val = headers_in[counter].val;
879b17
-            continue;
879b17
-        }
879b17
-
879b17
-        /* for sub-requests, ignore freshness/expiry headers */
879b17
-        if (r->main) {
879b17
-            if (   !strcasecmp(headers_in[counter].key, "If-Match")
879b17
-                || !strcasecmp(headers_in[counter].key, "If-Modified-Since")
879b17
-                || !strcasecmp(headers_in[counter].key, "If-Range")
879b17
-                || !strcasecmp(headers_in[counter].key, "If-Unmodified-Since")
879b17
-                || !strcasecmp(headers_in[counter].key, "If-None-Match")) {
879b17
-                continue;
879b17
-            }
879b17
+            || headers_in[counter].val == NULL) {
879b17
+             continue;
879b17
         }
879b17
 
879b17
         buf = apr_pstrcat(p, headers_in[counter].key, ": ",
879b17
@@ -3830,11 +3833,9 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
879b17
         APR_BRIGADE_INSERT_TAIL(header_brigade, e);
879b17
     }
879b17
 
879b17
-    /* Restore the original headers in (see comment above),
879b17
-     * we won't modify them anymore.
879b17
-     */
879b17
+cleanup:
879b17
     r->headers_in = saved_headers_in;
879b17
-    return OK;
879b17
+    return rc;
879b17
 }
879b17
 
879b17
 PROXY_DECLARE(int) ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,