altarch-user / rpms / httpd

Forked from rpms/httpd 2 years ago
Clone

Blame SOURCES/httpd-2.4.6-CVE-2015-3183.patch

008793
diff --git a/include/http_protocol.h b/include/http_protocol.h
008793
index 415270b..67fa02f 100644
008793
--- a/include/http_protocol.h
008793
+++ b/include/http_protocol.h
008793
@@ -502,6 +502,23 @@ AP_DECLARE(int) ap_should_client_block(request_rec *r);
008793
  */
008793
 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
008793
 
008793
+/*
008793
+ * Map specific APR codes returned by the filter stack to HTTP error
008793
+ * codes, or the default status code provided. Use it as follows:
008793
+ *
008793
+ * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
008793
+ *
008793
+ * If the filter has already handled the error, AP_FILTER_ERROR will
008793
+ * be returned, which is cleanly passed through.
008793
+ *
008793
+ * These mappings imply that the filter stack is reading from the
008793
+ * downstream client, the proxy will map these codes differently.
008793
+ * @param rv APR status code
008793
+ * @param status Default HTTP code should the APR code not be recognised
008793
+ * @return Mapped HTTP status code
008793
+ */
008793
+AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status);
008793
+
008793
 /**
008793
  * In HTTP/1.1, any method can have a body.  However, most GET handlers
008793
  * wouldn't know what to do with a request body if they received one.
008793
diff --git a/modules/http/http_filters.c b/modules/http/http_filters.c
008793
index 1dde402..ed8749f 100644
008793
--- a/modules/http/http_filters.c
008793
+++ b/modules/http/http_filters.c
008793
@@ -57,24 +57,29 @@
008793
 
008793
 APLOG_USE_MODULE(http);
008793
 
008793
-#define INVALID_CHAR -2
008793
-
008793
-static long get_chunk_size(char *);
008793
-
008793
-typedef struct http_filter_ctx {
008793
+typedef struct http_filter_ctx
008793
+{
008793
     apr_off_t remaining;
008793
     apr_off_t limit;
008793
     apr_off_t limit_used;
008793
-    enum {
008793
-        BODY_NONE,
008793
-        BODY_LENGTH,
008793
-        BODY_CHUNK,
008793
-        BODY_CHUNK_PART
008793
+    apr_int32_t chunk_used;
008793
+    apr_int32_t chunk_bws;
008793
+    apr_int32_t chunkbits;
008793
+    enum
008793
+    {
008793
+        BODY_NONE, /* streamed data */
008793
+        BODY_LENGTH, /* data constrained by content length */
008793
+        BODY_CHUNK, /* chunk expected */
008793
+        BODY_CHUNK_PART, /* chunk digits */
008793
+        BODY_CHUNK_EXT, /* chunk extension */
008793
+        BODY_CHUNK_CR, /* got space(s) after digits, expect [CR]LF or ext */
008793
+        BODY_CHUNK_LF, /* got CR after digits or ext, expect LF */
008793
+        BODY_CHUNK_DATA, /* data constrained by chunked encoding */
008793
+        BODY_CHUNK_END, /* chunked data terminating CRLF */
008793
+        BODY_CHUNK_END_LF, /* got CR after data, expect LF */
008793
+        BODY_CHUNK_TRAILER /* trailers */
008793
     } state;
008793
-    int eos_sent;
008793
-    char chunk_ln[32];
008793
-    char *pos;
008793
-    apr_off_t linesize;
008793
+    unsigned int eos_sent :1;
008793
     apr_bucket_brigade *bb;
008793
 } http_ctx_t;
008793
 
008793
@@ -87,6 +92,23 @@ static apr_status_t bail_out_on_error(http_ctx_t *ctx,
008793
     apr_bucket_brigade *bb = ctx->bb;
008793
 
008793
     apr_brigade_cleanup(bb);
008793
+
008793
+    if (f->r->proxyreq == PROXYREQ_RESPONSE) {
008793
+        switch (http_error) {
008793
+        case HTTP_REQUEST_ENTITY_TOO_LARGE:
008793
+            return APR_ENOSPC;
008793
+
008793
+        case HTTP_REQUEST_TIME_OUT:
008793
+            return APR_INCOMPLETE;
008793
+
008793
+        case HTTP_NOT_IMPLEMENTED:
008793
+            return APR_ENOTIMPL;
008793
+
008793
+        default:
008793
+            return APR_EGENERAL;
008793
+        }
008793
+    }
008793
+
008793
     e = ap_bucket_error_create(http_error,
008793
                                NULL, f->r->pool,
008793
                                f->c->bucket_alloc);
008793
@@ -102,117 +124,154 @@ static apr_status_t bail_out_on_error(http_ctx_t *ctx,
008793
     return ap_pass_brigade(f->r->output_filters, bb);
008793
 }
008793
 
008793
-static apr_status_t get_remaining_chunk_line(http_ctx_t *ctx,
008793
-                                             apr_bucket_brigade *b,
008793
-                                             int linelimit)
008793
+/**
008793
+ * Parse a chunk line with optional extension, detect overflow.
008793
+ * There are two error cases:
008793
+ *  1) If the conversion would require too many bits, APR_EGENERAL is returned.
008793
+ *  2) If the conversion used the correct number of bits, but an overflow
008793
+ *     caused only the sign bit to flip, then APR_ENOSPC is returned.
008793
+ * In general, any negative number can be considered an overflow error.
008793
+ */
008793
+static apr_status_t parse_chunk_size(http_ctx_t *ctx, const char *buffer,
008793
+                                     apr_size_t len, int linelimit)
008793
 {
008793
-    apr_status_t rv;
008793
-    apr_off_t brigade_length;
008793
-    apr_bucket *e;
008793
-    const char *lineend;
008793
-    apr_size_t len = 0;
008793
+    apr_size_t i = 0;
008793
 
008793
-    /*
008793
-     * As the brigade b should have been requested in mode AP_MODE_GETLINE
008793
-     * all buckets in this brigade are already some type of memory
008793
-     * buckets (due to the needed scanning for LF in mode AP_MODE_GETLINE)
008793
-     * or META buckets.
008793
-     */
008793
-    rv = apr_brigade_length(b, 0, &brigade_length);
008793
-    if (rv != APR_SUCCESS) {
008793
-        return rv;
008793
-    }
008793
-    /* Sanity check. Should never happen. See above. */
008793
-    if (brigade_length == -1) {
008793
-        return APR_EGENERAL;
008793
-    }
008793
-    if (!brigade_length) {
008793
-        return APR_EAGAIN;
008793
-    }
008793
-    ctx->linesize += brigade_length;
008793
-    if (ctx->linesize > linelimit) {
008793
-        return APR_ENOSPC;
008793
-    }
008793
-    /*
008793
-     * As all buckets are already some type of memory buckets or META buckets
008793
-     * (see above), we only need to check the last byte in the last data bucket.
008793
-     */
008793
-    for (e = APR_BRIGADE_LAST(b);
008793
-         e != APR_BRIGADE_SENTINEL(b);
008793
-         e = APR_BUCKET_PREV(e)) {
008793
+    while (i < len) {
008793
+        char c = buffer[i];
008793
+
008793
+        ap_xlate_proto_from_ascii(&c, 1);
008793
 
008793
-        if (APR_BUCKET_IS_METADATA(e)) {
008793
+        /* handle CRLF after the chunk */
008793
+        if (ctx->state == BODY_CHUNK_END
008793
+                || ctx->state == BODY_CHUNK_END_LF) {
008793
+            if (c == LF) {
008793
+                ctx->state = BODY_CHUNK;
008793
+            }
008793
+            else if (c == CR && ctx->state == BODY_CHUNK_END) {
008793
+                ctx->state = BODY_CHUNK_END_LF;
008793
+            }
008793
+            else {
008793
+                /*
008793
+                 * LF expected.
008793
+                 */
008793
+                return APR_EINVAL;
008793
+            }
008793
+            i++;
008793
             continue;
008793
         }
008793
-        rv = apr_bucket_read(e, &lineend, &len, APR_BLOCK_READ);
008793
-        if (rv != APR_SUCCESS) {
008793
-            return rv;
008793
+
008793
+        /* handle start of the chunk */
008793
+        if (ctx->state == BODY_CHUNK) {
008793
+            if (!apr_isxdigit(c)) {
008793
+                /*
008793
+                 * Detect invalid character at beginning. This also works for
008793
+                 * empty chunk size lines.
008793
+                 */
008793
+                return APR_EINVAL;
008793
+            }
008793
+            else {
008793
+                ctx->state = BODY_CHUNK_PART;
008793
+            }
008793
+            ctx->remaining = 0;
008793
+            ctx->chunkbits = sizeof(apr_off_t) * 8;
008793
+            ctx->chunk_used = 0;
008793
+            ctx->chunk_bws = 0;
008793
         }
008793
-        if (len > 0) {
008793
-            break;  /* we got the data we want */
008793
+
008793
+        if (c == LF) {
008793
+            if (ctx->remaining) {
008793
+                ctx->state = BODY_CHUNK_DATA;
008793
+            }
008793
+            else {
008793
+                ctx->state = BODY_CHUNK_TRAILER;
008793
+            }
008793
         }
008793
-        /* If we got a zero-length data bucket, we try the next one */
008793
-    }
008793
-    /* We had no data in this brigade */
008793
-    if (!len || e == APR_BRIGADE_SENTINEL(b)) {
008793
-        return APR_EAGAIN;
008793
-    }
008793
-    if (lineend[len - 1] != APR_ASCII_LF) {
008793
-        return APR_EAGAIN;
008793
-    }
008793
-    /* Line is complete. So reset ctx for next round. */
008793
-    ctx->linesize = 0;
008793
-    ctx->pos = ctx->chunk_ln;
008793
-    return APR_SUCCESS;
008793
-}
008793
+        else if (ctx->state == BODY_CHUNK_LF) {
008793
+            /*
008793
+             * LF expected.
008793
+             */
008793
+            return APR_EINVAL;
008793
+        }
008793
+        else if (c == CR) {
008793
+            ctx->state = BODY_CHUNK_LF;
008793
+        }
008793
+        else if (c == ';') {
008793
+            ctx->state = BODY_CHUNK_EXT;
008793
+        }
008793
+        else if (ctx->state == BODY_CHUNK_EXT) {
008793
+            /*
008793
+             * Control chars (but tabs) are invalid.
008793
+             */
008793
+            if (c != '\t' && apr_iscntrl(c)) {
008793
+                return APR_EINVAL;
008793
+            }
008793
+        }
008793
+        else if (c == ' ' || c == '\t') {
008793
+            /* Be lenient up to 10 BWS (term from rfc7230 - 3.2.3).
008793
+             */
008793
+            ctx->state = BODY_CHUNK_CR;
008793
+            if (++ctx->chunk_bws > 10) {
008793
+                return APR_EINVAL;
008793
+            }
008793
+        }
008793
+        else if (ctx->state == BODY_CHUNK_CR) {
008793
+            /*
008793
+             * ';', CR or LF expected.
008793
+             */
008793
+            return APR_EINVAL;
008793
+        }
008793
+        else if (ctx->state == BODY_CHUNK_PART) {
008793
+            int xvalue;
008793
 
008793
-static apr_status_t get_chunk_line(http_ctx_t *ctx, apr_bucket_brigade *b,
008793
-                                   int linelimit)
008793
-{
008793
-    apr_size_t len;
008793
-    int tmp_len;
008793
-    apr_status_t rv;
008793
+            /* ignore leading zeros */
008793
+            if (!ctx->remaining && c == '0') {
008793
+                i++;
008793
+                continue;
008793
+            }
008793
 
008793
-    tmp_len = sizeof(ctx->chunk_ln) - (ctx->pos - ctx->chunk_ln) - 1;
008793
-    /* Saveguard ourselves against underflows */
008793
-    if (tmp_len < 0) {
008793
-        len = 0;
008793
-    }
008793
-    else {
008793
-        len = (apr_size_t) tmp_len;
008793
-    }
008793
-    /*
008793
-     * Check if there is space left in ctx->chunk_ln. If not, then either
008793
-     * the chunk size is insane or we have chunk-extensions. Ignore both
008793
-     * by discarding the remaining part of the line via
008793
-     * get_remaining_chunk_line. Only bail out if the line is too long.
008793
-     */
008793
-    if (len > 0) {
008793
-        rv = apr_brigade_flatten(b, ctx->pos, &len;;
008793
-        if (rv != APR_SUCCESS) {
008793
-            return rv;
008793
+            ctx->chunkbits -= 4;
008793
+            if (ctx->chunkbits < 0) {
008793
+                /* overflow */
008793
+                return APR_ENOSPC;
008793
+            }
008793
+
008793
+            if (c >= '0' && c <= '9') {
008793
+                xvalue = c - '0';
008793
+            }
008793
+            else if (c >= 'A' && c <= 'F') {
008793
+                xvalue = c - 'A' + 0xa;
008793
+            }
008793
+            else if (c >= 'a' && c <= 'f') {
008793
+                xvalue = c - 'a' + 0xa;
008793
+            }
008793
+            else {
008793
+                /* bogus character */
008793
+                return APR_EINVAL;
008793
+            }
008793
+
008793
+            ctx->remaining = (ctx->remaining << 4) | xvalue;
008793
+            if (ctx->remaining < 0) {
008793
+                /* overflow */
008793
+                return APR_ENOSPC;
008793
+            }
008793
         }
008793
-        ctx->pos += len;
008793
-        ctx->linesize += len;
008793
-        *(ctx->pos) = '\0';
008793
-        /*
008793
-         * Check if we really got a full line. If yes the
008793
-         * last char in the just read buffer must be LF.
008793
-         * If not advance the buffer and return APR_EAGAIN.
008793
-         * We do not start processing until we have the
008793
-         * full line.
008793
-         */
008793
-        if (ctx->pos[-1] != APR_ASCII_LF) {
008793
-            /* Check if the remaining data in the brigade has the LF */
008793
-            return get_remaining_chunk_line(ctx, b, linelimit);
008793
+        else {
008793
+            /* Should not happen */
008793
+            return APR_EGENERAL;
008793
         }
008793
-        /* Line is complete. So reset ctx->pos for next round. */
008793
-        ctx->pos = ctx->chunk_ln;
008793
-        return APR_SUCCESS;
008793
+
008793
+        i++;
008793
     }
008793
-    return get_remaining_chunk_line(ctx, b, linelimit);
008793
-}
008793
 
008793
+    /* sanity check */
008793
+    ctx->chunk_used += len;
008793
+    if (ctx->chunk_used < 0 || ctx->chunk_used > linelimit) {
008793
+        return APR_ENOSPC;
008793
+    }
008793
+
008793
+    return APR_SUCCESS;
008793
+}
008793
 
008793
 static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
008793
                                           apr_bucket_brigade *b, int merge)
008793
@@ -226,7 +285,6 @@ static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
008793
     r->status = HTTP_OK;
008793
     r->headers_in = r->trailers_in;
008793
     apr_table_clear(r->headers_in);
008793
-    ctx->state = BODY_NONE;
008793
     ap_get_mime_headers(r);
008793
 
008793
     if(r->status == HTTP_OK) {
008793
@@ -239,7 +297,7 @@ static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
008793
     else {
008793
         const char *error_notes = apr_table_get(r->notes,
008793
                                                 "error-notes");
008793
-        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
008793
+        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02656)
008793
                       "Error while reading HTTP trailer: %i%s%s",
008793
                       r->status, error_notes ? ": " : "",
008793
                       error_notes ? error_notes : "");
008793
@@ -270,9 +328,9 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
     apr_bucket *e;
008793
     http_ctx_t *ctx = f->ctx;
008793
     apr_status_t rv;
008793
-    apr_off_t totalread;
008793
     int http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
008793
     apr_bucket_brigade *bb;
008793
+    int again;
008793
 
008793
     conf = (core_server_config *)
008793
         ap_get_module_config(f->r->server->module_config, &core_module);
008793
@@ -286,7 +344,6 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
         const char *tenc, *lenp;
008793
         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
008793
         ctx->state = BODY_NONE;
008793
-        ctx->pos = ctx->chunk_ln;
008793
         ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
008793
         bb = ctx->bb;
008793
 
008793
@@ -306,25 +363,33 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
         lenp = apr_table_get(f->r->headers_in, "Content-Length");
008793
 
008793
         if (tenc) {
008793
-            if (!strcasecmp(tenc, "chunked")) {
008793
+            if (strcasecmp(tenc, "chunked") == 0 /* fast path */
008793
+                    || ap_find_last_token(f->r->pool, tenc, "chunked")) {
008793
                 ctx->state = BODY_CHUNK;
008793
             }
008793
-            /* test lenp, because it gives another case we can handle */
008793
-            else if (!lenp) {
008793
-                /* Something that isn't in HTTP, unless some future
008793
+            else if (f->r->proxyreq == PROXYREQ_RESPONSE) {
008793
+                /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
008793
+                 * Section 3.3.3.3: "If a Transfer-Encoding header field is
008793
+                 * present in a response and the chunked transfer coding is not
008793
+                 * the final encoding, the message body length is determined by
008793
+                 * reading the connection until it is closed by the server."
008793
+                 */
008793
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(02555)
008793
+                              "Unknown Transfer-Encoding: %s; "
008793
+                              "using read-until-close", tenc);
008793
+                tenc = NULL;
008793
+            }
008793
+            else {
008793
+                /* Something that isn't a HTTP request, unless some future
008793
                  * edition defines new transfer encodings, is unsupported.
008793
                  */
008793
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01585)
008793
                               "Unknown Transfer-Encoding: %s", tenc);
008793
-                return bail_out_on_error(ctx, f, HTTP_NOT_IMPLEMENTED);
008793
-            }
008793
-            else {
008793
-                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r, APLOGNO(01586)
008793
-                  "Unknown Transfer-Encoding: %s; using Content-Length", tenc);
008793
-                tenc = NULL;
008793
+                return bail_out_on_error(ctx, f, HTTP_BAD_REQUEST);
008793
             }
008793
+            lenp = NULL;
008793
         }
008793
-        if (lenp && !tenc) {
008793
+        if (lenp) {
008793
             char *endstr;
008793
 
008793
             ctx->state = BODY_LENGTH;
008793
@@ -339,7 +404,7 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01587)
008793
                               "Invalid Content-Length");
008793
 
008793
-                return bail_out_on_error(ctx, f, HTTP_REQUEST_ENTITY_TOO_LARGE);
008793
+                return bail_out_on_error(ctx, f, HTTP_BAD_REQUEST);
008793
             }
008793
 
008793
             /* If we have a limit in effect and we know the C-L ahead of
008793
@@ -381,7 +446,8 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
             if (!ap_is_HTTP_SUCCESS(f->r->status)) {
008793
                 ctx->state = BODY_NONE;
008793
                 ctx->eos_sent = 1;
008793
-            } else {
008793
+            }
008793
+            else {
008793
                 char *tmp;
008793
                 int len;
008793
 
008793
@@ -389,7 +455,7 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
                  * in a state of expecting one.
008793
                  */
008793
                 f->r->expecting_100 = 0;
008793
-                tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL, " ",
008793
+                tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL " ",
008793
                                   ap_get_status_line(HTTP_CONTINUE), CRLF CRLF,
008793
                                   NULL);
008793
                 len = strlen(tmp);
008793
@@ -401,279 +467,205 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
008793
                 e = apr_bucket_flush_create(f->c->bucket_alloc);
008793
                 APR_BRIGADE_INSERT_TAIL(bb, e);
008793
 
008793
-                ap_pass_brigade(f->c->output_filters, bb);
008793
-            }
008793
-        }
008793
-
008793
-        /* We can't read the chunk until after sending 100 if required. */
008793
-        if (ctx->state == BODY_CHUNK) {
008793
-            apr_brigade_cleanup(bb);
008793
-
008793
-            rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
008793
-                                block, 0);
008793
-
008793
-            /* for timeout */
008793
-            if (block == APR_NONBLOCK_READ &&
008793
-                ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
008793
-                  (APR_STATUS_IS_EAGAIN(rv)) )) {
008793
-                ctx->state = BODY_CHUNK_PART;
008793
-                return APR_EAGAIN;
008793
-            }
008793
-
008793
-            if (rv == APR_SUCCESS) {
008793
-                rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
008793
-                if (APR_STATUS_IS_EAGAIN(rv)) {
008793
-                    apr_brigade_cleanup(bb);
008793
-                    ctx->state = BODY_CHUNK_PART;
008793
-                    return rv;
008793
-                }
008793
-                if (rv == APR_SUCCESS) {
008793
-                    ctx->remaining = get_chunk_size(ctx->chunk_ln);
008793
-                    if (ctx->remaining == INVALID_CHAR) {
008793
-                        rv = APR_EGENERAL;
008793
-                        http_error = HTTP_BAD_REQUEST;
008793
-                    }
008793
+                rv = ap_pass_brigade(f->c->output_filters, bb);
008793
+                if (rv != APR_SUCCESS) {
008793
+                    return AP_FILTER_ERROR;
008793
                 }
008793
             }
008793
-            apr_brigade_cleanup(bb);
008793
-
008793
-            /* Detect chunksize error (such as overflow) */
008793
-            if (rv != APR_SUCCESS || ctx->remaining < 0) {
008793
-                ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01589) "Error reading first chunk %s ",
008793
-                              (ctx->remaining < 0) ? "(overflow)" : "");
008793
-                ctx->remaining = 0; /* Reset it in case we have to
008793
-                                     * come back here later */
008793
-                if (APR_STATUS_IS_TIMEUP(rv)) {
008793
-                    http_error = HTTP_REQUEST_TIME_OUT;
008793
-                }
008793
-                return bail_out_on_error(ctx, f, http_error);
008793
-            }
008793
-
008793
-            if (!ctx->remaining) {
008793
-                return read_chunked_trailers(ctx, f, b,
008793
-                        conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
008793
-            }
008793
         }
008793
     }
008793
-    else {
008793
-        bb = ctx->bb;
008793
-    }
008793
 
008793
+    /* sanity check in case we're read twice */
008793
     if (ctx->eos_sent) {
008793
         e = apr_bucket_eos_create(f->c->bucket_alloc);
008793
         APR_BRIGADE_INSERT_TAIL(b, e);
008793
         return APR_SUCCESS;
008793
     }
008793
 
008793
-    if (!ctx->remaining) {
008793
+    do {
008793
+        apr_brigade_cleanup(b);
008793
+        again = 0; /* until further notice */
008793
+
008793
+        /* read and handle the brigade */
008793
         switch (ctx->state) {
008793
-        case BODY_NONE:
008793
-            break;
008793
-        case BODY_LENGTH:
008793
-            e = apr_bucket_eos_create(f->c->bucket_alloc);
008793
-            APR_BRIGADE_INSERT_TAIL(b, e);
008793
-            ctx->eos_sent = 1;
008793
-            return APR_SUCCESS;
008793
         case BODY_CHUNK:
008793
         case BODY_CHUNK_PART:
008793
-            {
008793
-                apr_brigade_cleanup(bb);
008793
+        case BODY_CHUNK_EXT:
008793
+        case BODY_CHUNK_CR:
008793
+        case BODY_CHUNK_LF:
008793
+        case BODY_CHUNK_END:
008793
+        case BODY_CHUNK_END_LF: {
008793
 
008793
-                /* We need to read the CRLF after the chunk.  */
008793
-                if (ctx->state == BODY_CHUNK) {
008793
-                    rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
008793
-                                        block, 0);
008793
-                    if (block == APR_NONBLOCK_READ &&
008793
-                        ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
008793
-                          (APR_STATUS_IS_EAGAIN(rv)) )) {
008793
-                        return APR_EAGAIN;
008793
-                    }
008793
-                    /* If we get an error, then leave */
008793
-                    if (rv != APR_SUCCESS) {
008793
-                        return rv;
008793
-                    }
008793
-                    /*
008793
-                     * We really don't care whats on this line. If it is RFC
008793
-                     * compliant it should be only \r\n. If there is more
008793
-                     * before we just ignore it as long as we do not get over
008793
-                     * the limit for request lines.
008793
-                     */
008793
-                    rv = get_remaining_chunk_line(ctx, bb,
008793
-                                                  f->r->server->limit_req_line);
008793
-                    apr_brigade_cleanup(bb);
008793
-                    if (APR_STATUS_IS_EAGAIN(rv)) {
008793
-                        return rv;
008793
-                    }
008793
-                } else {
008793
-                    rv = APR_SUCCESS;
008793
-                }
008793
+            rv = ap_get_brigade(f->next, b, AP_MODE_GETLINE, block, 0);
008793
+
008793
+            /* for timeout */
008793
+            if (block == APR_NONBLOCK_READ
008793
+                    && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
008793
+                            || (APR_STATUS_IS_EAGAIN(rv)))) {
008793
+                return APR_EAGAIN;
008793
+            }
008793
+
008793
+            if (rv == APR_EOF) {
008793
+                return APR_INCOMPLETE;
008793
+            }
008793
+
008793
+            if (rv != APR_SUCCESS) {
008793
+                return rv;
008793
+            }
008793
+
008793
+            e = APR_BRIGADE_FIRST(b);
008793
+            while (e != APR_BRIGADE_SENTINEL(b)) {
008793
+                const char *buffer;
008793
+                apr_size_t len;
008793
+
008793
+                if (!APR_BUCKET_IS_METADATA(e)) {
008793
+                    int parsing = 0;
008793
+
008793
+                    rv = apr_bucket_read(e, &buffer, &len, APR_BLOCK_READ);
008793
 
008793
-                if (rv == APR_SUCCESS) {
008793
-                    /* Read the real chunk line. */
008793
-                    rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
008793
-                                        block, 0);
008793
-                    /* Test timeout */
008793
-                    if (block == APR_NONBLOCK_READ &&
008793
-                        ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
008793
-                          (APR_STATUS_IS_EAGAIN(rv)) )) {
008793
-                        ctx->state = BODY_CHUNK_PART;
008793
-                        return APR_EAGAIN;
008793
-                    }
008793
-                    ctx->state = BODY_CHUNK;
008793
                     if (rv == APR_SUCCESS) {
008793
-                        rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
008793
-                        if (APR_STATUS_IS_EAGAIN(rv)) {
008793
-                            ctx->state = BODY_CHUNK_PART;
008793
-                            apr_brigade_cleanup(bb);
008793
-                            return rv;
008793
-                        }
008793
-                        if (rv == APR_SUCCESS) {
008793
-                            ctx->remaining = get_chunk_size(ctx->chunk_ln);
008793
-                            if (ctx->remaining == INVALID_CHAR) {
008793
-                                rv = APR_EGENERAL;
008793
+                        parsing = 1;
008793
+                        rv = parse_chunk_size(ctx, buffer, len,
008793
+                                f->r->server->limit_req_fieldsize);
008793
+                    }
008793
+                    if (rv != APR_SUCCESS) {
008793
+                        ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590)
008793
+                                      "Error reading/parsing chunk %s ",
008793
+                                      (APR_ENOSPC == rv) ? "(overflow)" : "");
008793
+                        if (parsing) {
008793
+                            if (rv != APR_ENOSPC) {
008793
                                 http_error = HTTP_BAD_REQUEST;
008793
                             }
008793
+                            return bail_out_on_error(ctx, f, http_error);
008793
                         }
008793
+                        return rv;
008793
                     }
008793
-                    apr_brigade_cleanup(bb);
008793
                 }
008793
 
008793
-                /* Detect chunksize error (such as overflow) */
008793
-                if (rv != APR_SUCCESS || ctx->remaining < 0) {
008793
-                    ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590) "Error reading chunk %s ",
008793
-                                  (ctx->remaining < 0) ? "(overflow)" : "");
008793
-                    ctx->remaining = 0; /* Reset it in case we have to
008793
-                                         * come back here later */
008793
-                    if (APR_STATUS_IS_TIMEUP(rv)) {
008793
-                        http_error = HTTP_REQUEST_TIME_OUT;
008793
-                    }
008793
-                    return bail_out_on_error(ctx, f, http_error);
008793
-                }
008793
+                apr_bucket_delete(e);
008793
+                e = APR_BRIGADE_FIRST(b);
008793
+            }
008793
+            again = 1; /* come around again */
008793
 
008793
-                if (!ctx->remaining) {
008793
-                    return read_chunked_trailers(ctx, f, b,
008793
+            if (ctx->state == BODY_CHUNK_TRAILER) {
008793
+                /* Treat UNSET as DISABLE - trailers aren't merged by default */
008793
+                return read_chunked_trailers(ctx, f, b,
008793
                             conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
008793
-                }
008793
             }
008793
+
008793
             break;
008793
         }
008793
-    }
008793
+        case BODY_NONE:
008793
+        case BODY_LENGTH:
008793
+        case BODY_CHUNK_DATA: {
008793
 
008793
-    /* Ensure that the caller can not go over our boundary point. */
008793
-    if (ctx->state == BODY_LENGTH || ctx->state == BODY_CHUNK) {
008793
-        if (ctx->remaining < readbytes) {
008793
-            readbytes = ctx->remaining;
008793
-        }
008793
-        AP_DEBUG_ASSERT(readbytes > 0);
008793
-    }
008793
+            /* Ensure that the caller can not go over our boundary point. */
008793
+            if (ctx->state != BODY_NONE && ctx->remaining < readbytes) {
008793
+                readbytes = ctx->remaining;
008793
+            }
008793
+            if (readbytes > 0) {
008793
+                apr_off_t totalread;
008793
 
008793
-    rv = ap_get_brigade(f->next, b, mode, block, readbytes);
008793
+                rv = ap_get_brigade(f->next, b, mode, block, readbytes);
008793
 
008793
-    if (rv != APR_SUCCESS) {
008793
-        return rv;
008793
-    }
008793
+                /* for timeout */
008793
+                if (block == APR_NONBLOCK_READ
008793
+                        && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
008793
+                                || (APR_STATUS_IS_EAGAIN(rv)))) {
008793
+                    return APR_EAGAIN;
008793
+                }
008793
 
008793
-    /* How many bytes did we just read? */
008793
-    apr_brigade_length(b, 0, &totalread);
008793
+                if (rv == APR_EOF && ctx->state != BODY_NONE
008793
+                        && ctx->remaining > 0) {
008793
+                    return APR_INCOMPLETE;
008793
+                }
008793
 
008793
-    /* If this happens, we have a bucket of unknown length.  Die because
008793
-     * it means our assumptions have changed. */
008793
-    AP_DEBUG_ASSERT(totalread >= 0);
008793
+                if (rv != APR_SUCCESS) {
008793
+                    return rv;
008793
+                }
008793
 
008793
-    if (ctx->state != BODY_NONE) {
008793
-        ctx->remaining -= totalread;
008793
-        if (ctx->remaining > 0) {
008793
-            e = APR_BRIGADE_LAST(b);
008793
-            if (APR_BUCKET_IS_EOS(e))
008793
-                return APR_EOF;
008793
-        }
008793
-    }
008793
+                /* How many bytes did we just read? */
008793
+                apr_brigade_length(b, 0, &totalread);
008793
 
008793
-    /* If we have no more bytes remaining on a C-L request,
008793
-     * save the callter a roundtrip to discover EOS.
008793
-     */
008793
-    if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
008793
-        e = apr_bucket_eos_create(f->c->bucket_alloc);
008793
-        APR_BRIGADE_INSERT_TAIL(b, e);
008793
-    }
008793
+                /* If this happens, we have a bucket of unknown length.  Die because
008793
+                 * it means our assumptions have changed. */
008793
+                AP_DEBUG_ASSERT(totalread >= 0);
008793
 
008793
-    /* We have a limit in effect. */
008793
-    if (ctx->limit) {
008793
-        /* FIXME: Note that we might get slightly confused on chunked inputs
008793
-         * as we'd need to compensate for the chunk lengths which may not
008793
-         * really count.  This seems to be up for interpretation.  */
008793
-        ctx->limit_used += totalread;
008793
-        if (ctx->limit < ctx->limit_used) {
008793
-            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01591)
008793
-                          "Read content-length of %" APR_OFF_T_FMT
008793
-                          " is larger than the configured limit"
008793
-                          " of %" APR_OFF_T_FMT, ctx->limit_used, ctx->limit);
008793
-            apr_brigade_cleanup(bb);
008793
-            e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
008793
-                                       f->r->pool,
008793
-                                       f->c->bucket_alloc);
008793
-            APR_BRIGADE_INSERT_TAIL(bb, e);
008793
-            e = apr_bucket_eos_create(f->c->bucket_alloc);
008793
-            APR_BRIGADE_INSERT_TAIL(bb, e);
008793
-            ctx->eos_sent = 1;
008793
-            return ap_pass_brigade(f->r->output_filters, bb);
008793
-        }
008793
-    }
008793
+                if (ctx->state != BODY_NONE) {
008793
+                    ctx->remaining -= totalread;
008793
+                    if (ctx->remaining > 0) {
008793
+                        e = APR_BRIGADE_LAST(b);
008793
+                        if (APR_BUCKET_IS_EOS(e)) {
008793
+                            apr_bucket_delete(e);
008793
+                            return APR_INCOMPLETE;
008793
+                        }
008793
+                    }
008793
+                    else if (ctx->state == BODY_CHUNK_DATA) {
008793
+                        /* next chunk please */
008793
+                        ctx->state = BODY_CHUNK_END;
008793
+                        ctx->chunk_used = 0;
008793
+                    }
008793
+                }
008793
 
008793
-    return APR_SUCCESS;
008793
-}
008793
+                /* We have a limit in effect. */
008793
+                if (ctx->limit) {
008793
+                    /* FIXME: Note that we might get slightly confused on
008793
+                     * chunked inputs as we'd need to compensate for the chunk
008793
+                     * lengths which may not really count.  This seems to be up
008793
+                     * for interpretation.
008793
+                     */
008793
+                    ctx->limit_used += totalread;
008793
+                    if (ctx->limit < ctx->limit_used) {
008793
+                        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r,
008793
+                                      APLOGNO(01591) "Read content length of "
008793
+                                      "%" APR_OFF_T_FMT " is larger than the "
008793
+                                      "configured limit of %" APR_OFF_T_FMT,
008793
+                                      ctx->limit_used, ctx->limit);
008793
+                        return bail_out_on_error(ctx, f,
008793
+                                                 HTTP_REQUEST_ENTITY_TOO_LARGE);
008793
+                    }
008793
+                }
008793
+            }
008793
 
008793
-/**
008793
- * Parse a chunk extension, detect overflow.
008793
- * There are two error cases:
008793
- *  1) If the conversion would require too many bits, a -1 is returned.
008793
- *  2) If the conversion used the correct number of bits, but an overflow
008793
- *     caused only the sign bit to flip, then that negative number is
008793
- *     returned.
008793
- * In general, any negative number can be considered an overflow error.
008793
- */
008793
-static long get_chunk_size(char *b)
008793
-{
008793
-    long chunksize = 0;
008793
-    size_t chunkbits = sizeof(long) * 8;
008793
+            /* If we have no more bytes remaining on a C-L request,
008793
+             * save the caller a round trip to discover EOS.
008793
+             */
008793
+            if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
008793
+                e = apr_bucket_eos_create(f->c->bucket_alloc);
008793
+                APR_BRIGADE_INSERT_TAIL(b, e);
008793
+                ctx->eos_sent = 1;
008793
+            }
008793
 
008793
-    ap_xlate_proto_from_ascii(b, strlen(b));
008793
+            break;
008793
+        }
008793
+        case BODY_CHUNK_TRAILER: {
008793
 
008793
-    if (!apr_isxdigit(*b)) {
008793
-        /*
008793
-         * Detect invalid character at beginning. This also works for empty
008793
-         * chunk size lines.
008793
-         */
008793
-        return INVALID_CHAR;
008793
-    }
008793
-    /* Skip leading zeros */
008793
-    while (*b == '0') {
008793
-        ++b;
008793
-    }
008793
+            rv = ap_get_brigade(f->next, b, mode, block, readbytes);
008793
 
008793
-    while (apr_isxdigit(*b) && (chunkbits > 0)) {
008793
-        int xvalue = 0;
008793
+            /* for timeout */
008793
+            if (block == APR_NONBLOCK_READ
008793
+                    && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
008793
+                            || (APR_STATUS_IS_EAGAIN(rv)))) {
008793
+                return APR_EAGAIN;
008793
+            }
008793
+
008793
+            if (rv != APR_SUCCESS) {
008793
+                return rv;
008793
+            }
008793
 
008793
-        if (*b >= '0' && *b <= '9') {
008793
-            xvalue = *b - '0';
008793
+            break;
008793
         }
008793
-        else if (*b >= 'A' && *b <= 'F') {
008793
-            xvalue = *b - 'A' + 0xa;
008793
+        default: {
008793
+            /* Should not happen */
008793
+            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(02901)
008793
+                          "Unexpected body state (%i)", (int)ctx->state);
008793
+            return APR_EGENERAL;
008793
         }
008793
-        else if (*b >= 'a' && *b <= 'f') {
008793
-            xvalue = *b - 'a' + 0xa;
008793
         }
008793
 
008793
-        chunksize = (chunksize << 4) | xvalue;
008793
-        chunkbits -= 4;
008793
-        ++b;
008793
-    }
008793
-    if (apr_isxdigit(*b)) {
008793
-        /* overflow */
008793
-        return -1;
008793
-    }
008793
+    } while (again);
008793
 
008793
-    return chunksize;
008793
+    return APR_SUCCESS;
008793
 }
008793
 
008793
 typedef struct header_struct {
008793
@@ -1385,6 +1377,39 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
008793
     return ap_pass_brigade(f->next, b);
008793
 }
008793
 
008793
+/*
008793
+ * Map specific APR codes returned by the filter stack to HTTP error
008793
+ * codes, or the default status code provided. Use it as follows:
008793
+ *
008793
+ * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
008793
+ *
008793
+ * If the filter has already handled the error, AP_FILTER_ERROR will
008793
+ * be returned, which is cleanly passed through.
008793
+ *
008793
+ * These mappings imply that the filter stack is reading from the
008793
+ * downstream client, the proxy will map these codes differently.
008793
+ */
008793
+AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status)
008793
+{
008793
+    switch (rv) {
008793
+    case AP_FILTER_ERROR: {
008793
+        return AP_FILTER_ERROR;
008793
+    }
008793
+    case APR_ENOSPC: {
008793
+        return HTTP_REQUEST_ENTITY_TOO_LARGE;
008793
+    }
008793
+    case APR_ENOTIMPL: {
008793
+        return HTTP_NOT_IMPLEMENTED;
008793
+    }
008793
+    case APR_ETIMEDOUT: {
008793
+        return HTTP_REQUEST_TIME_OUT;
008793
+    }
008793
+    default: {
008793
+        return status;
008793
+    }
008793
+    }
008793
+}
008793
+
008793
 /* In HTTP/1.1, any method can have a body.  However, most GET handlers
008793
  * wouldn't know what to do with a request body if they received one.
008793
  * This helper routine tests for and reads any message body in the request,
008793
@@ -1402,7 +1427,8 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
008793
 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
008793
 {
008793
     apr_bucket_brigade *bb;
008793
-    int rv, seen_eos;
008793
+    int seen_eos;
008793
+    apr_status_t rv;
008793
 
008793
     /* Sometimes we'll get in a state where the input handling has
008793
      * detected an error where we want to drop the connection, so if
008793
@@ -1425,21 +1451,8 @@ AP_DECLARE(int) ap_discard_request_body(request_rec *r)
008793
                             APR_BLOCK_READ, HUGE_STRING_LEN);
008793
 
008793
         if (rv != APR_SUCCESS) {
008793
-            /* FIXME: If we ever have a mapping from filters (apr_status_t)
008793
-             * to HTTP error codes, this would be a good place for them.
008793
-             *
008793
-             * If we received the special case AP_FILTER_ERROR, it means
008793
-             * that the filters have already handled this error.
008793
-             * Otherwise, we should assume we have a bad request.
008793
-             */
008793
-            if (rv == AP_FILTER_ERROR) {
008793
-                apr_brigade_destroy(bb);
008793
-                return rv;
008793
-            }
008793
-            else {
008793
-                apr_brigade_destroy(bb);
008793
-                return HTTP_BAD_REQUEST;
008793
-            }
008793
+            apr_brigade_destroy(bb);
008793
+            return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
008793
         }
008793
 
008793
         for (bucket = APR_BRIGADE_FIRST(bb);
008793
@@ -1608,6 +1621,13 @@ AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
008793
     /* We lose the failure code here.  This is why ap_get_client_block should
008793
      * not be used.
008793
      */
008793
+    if (rv == AP_FILTER_ERROR) {
008793
+        /* AP_FILTER_ERROR means a filter has responded already,
008793
+         * we are DONE.
008793
+         */
008793
+        apr_brigade_destroy(bb);
008793
+        return -1;
008793
+    }
008793
     if (rv != APR_SUCCESS) {
008793
         /* if we actually fail here, we want to just return and
008793
          * stop trying to read data from the client.