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

100199
diff --git a/modules/http/http_filters.c b/modules/http/http_filters.c
100199
index 5fa4063..1ebb9cc 100644
100199
--- a/modules/http/http_filters.c
100199
+++ b/modules/http/http_filters.c
100199
@@ -1591,9 +1591,9 @@ AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status)
100199
  */
100199
 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
100199
 {
100199
+    int rc = OK;
100199
+    conn_rec *c = r->connection;
100199
     apr_bucket_brigade *bb;
100199
-    int seen_eos;
100199
-    apr_status_t rv;
100199
 
100199
     /* Sometimes we'll get in a state where the input handling has
100199
      * detected an error where we want to drop the connection, so if
100199
@@ -1602,54 +1602,57 @@ AP_DECLARE(int) ap_discard_request_body(request_rec *r)
100199
      *
100199
      * This function is also a no-op on a subrequest.
100199
      */
100199
-    if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
100199
-        ap_status_drops_connection(r->status)) {
100199
+    if (r->main || c->keepalive == AP_CONN_CLOSE) {
100199
+        return OK;
100199
+    }
100199
+    if (ap_status_drops_connection(r->status)) {
100199
+        c->keepalive = AP_CONN_CLOSE;
100199
         return OK;
100199
     }
100199
 
100199
     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
100199
-    seen_eos = 0;
100199
-    do {
100199
-        apr_bucket *bucket;
100199
+    for (;;) {
100199
+        apr_status_t rv;
100199
 
100199
         rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
100199
                             APR_BLOCK_READ, HUGE_STRING_LEN);
100199
-
100199
         if (rv != APR_SUCCESS) {
100199
-            apr_brigade_destroy(bb);
100199
-            return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
100199
+            rc = ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
100199
+            goto cleanup;
100199
         }
100199
 
100199
-        for (bucket = APR_BRIGADE_FIRST(bb);
100199
-             bucket != APR_BRIGADE_SENTINEL(bb);
100199
-             bucket = APR_BUCKET_NEXT(bucket))
100199
-        {
100199
-            const char *data;
100199
-            apr_size_t len;
100199
+        while (!APR_BRIGADE_EMPTY(bb)) {
100199
+            apr_bucket *b = APR_BRIGADE_FIRST(bb);
100199
 
100199
-            if (APR_BUCKET_IS_EOS(bucket)) {
100199
-                seen_eos = 1;
100199
-                break;
100199
+            if (APR_BUCKET_IS_EOS(b)) {
100199
+                goto cleanup;
100199
             }
100199
 
100199
-            /* These are metadata buckets. */
100199
-            if (bucket->length == 0) {
100199
-                continue;
100199
-            }
100199
-
100199
-            /* We MUST read because in case we have an unknown-length
100199
-             * bucket or one that morphs, we want to exhaust it.
100199
+            /* There is no need to read empty or metadata buckets or
100199
+             * buckets of known length, but we MUST read buckets of
100199
+             * unknown length in order to exhaust them.
100199
              */
100199
-            rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
100199
+            if (b->length == (apr_size_t)-1) {
100199
+                apr_size_t len;
100199
+                const char *data;
100199
+
100199
+                rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
100199
             if (rv != APR_SUCCESS) {
100199
-                apr_brigade_destroy(bb);
100199
-                return HTTP_BAD_REQUEST;
100199
+                    rc = HTTP_BAD_REQUEST;
100199
+                    goto cleanup;
100199
             }
100199
         }
100199
-        apr_brigade_cleanup(bb);
100199
-    } while (!seen_eos);
100199
 
100199
-    return OK;
100199
+            apr_bucket_delete(b);
100199
+        }
100199
+    }
100199
+
100199
+cleanup:
100199
+    apr_brigade_cleanup(bb);
100199
+    if (rc != OK) {
100199
+        c->keepalive = AP_CONN_CLOSE;
100199
+    }
100199
+    return rc;
100199
 }
100199
 
100199
 /* Here we deal with getting the request message body from the client.
100199
diff --git a/server/protocol.c b/server/protocol.c
100199
index 708160f..c77da24 100644
100199
--- a/server/protocol.c
100199
+++ b/server/protocol.c
100199
@@ -1559,23 +1559,29 @@ AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
100199
     rnew->main = (request_rec *) r;
100199
 }
100199
 
100199
-static void end_output_stream(request_rec *r)
100199
+static void end_output_stream(request_rec *r, int status)
100199
 {
100199
     conn_rec *c = r->connection;
100199
     apr_bucket_brigade *bb;
100199
     apr_bucket *b;
100199
 
100199
     bb = apr_brigade_create(r->pool, c->bucket_alloc);
100199
+    if (status != OK) {
100199
+        b = ap_bucket_error_create(status, NULL, r->pool, c->bucket_alloc);
100199
+        APR_BRIGADE_INSERT_TAIL(bb, b);
100199
+    }
100199
     b = apr_bucket_eos_create(c->bucket_alloc);
100199
     APR_BRIGADE_INSERT_TAIL(bb, b);
100199
+
100199
     ap_pass_brigade(r->output_filters, bb);
100199
+    apr_brigade_cleanup(bb);
100199
 }
100199
 
100199
 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
100199
 {
100199
     /* tell the filter chain there is no more content coming */
100199
     if (!sub->eos_sent) {
100199
-        end_output_stream(sub);
100199
+        end_output_stream(sub, OK);
100199
     }
100199
 }
100199
 
100199
@@ -1586,11 +1592,11 @@ AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
100199
  */
100199
 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r)
100199
 {
100199
-    (void) ap_discard_request_body(r);
100199
+    int status = ap_discard_request_body(r);
100199
 
100199
     /* tell the filter chain there is no more content coming */
100199
     if (!r->eos_sent) {
100199
-        end_output_stream(r);
100199
+        end_output_stream(r, status);
100199
     }
100199
 }
100199