]> granicus.if.org Git - apache/commitdiff
Fix for memory consumption DoS, CVE CAN-2004-0942:
authorJoe Orton <jorton@apache.org>
Thu, 4 Nov 2004 14:50:31 +0000 (14:50 +0000)
committerJoe Orton <jorton@apache.org>
Thu, 4 Nov 2004 14:50:31 +0000 (14:50 +0000)
* server/protocol.c (ap_rgetline_core): Don't trim trailing whitespace
from the buffer here.
(ap_get_mime_headers_core): Trim trailing whitespace here, after
reading a complete field including continuation lines.  Also simplify
code to remove whitespace between field-name and colon.

Reviewed by: Andr�� Malo, Bill Stoddard

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@105680 13f79535-47bb-0310-9956-ffa450edef68

server/protocol.c

index 349a058b4f3491fb753749e9485f146c3726d454..6b46d89eb37fd0af499854c2a508a201c7544b71 100644 (file)
@@ -306,35 +306,13 @@ AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
         }
     }
 
-    /* We now go backwards over any CR (if present) or white spaces.
-     *
-     * Trim any extra trailing spaces or tabs except for the first
-     * space or tab at the beginning of a blank string.  This makes
-     * it much easier to check field values for exact matches, and
-     * saves memory as well.  Terminate string at end of line.
-     */
-    pos = last_char;
-    if (pos > *s && *(pos - 1) == APR_ASCII_CR) {
-        --pos;
-    }
-
-    /* Trim any extra trailing spaces or tabs except for the first
-     * space or tab at the beginning of a blank string.  This makes
-     * it much easier to check field values for exact matches, and
-     * saves memory as well.
-     */
-    while (pos > ((*s) + 1)
-           && (*(pos - 1) == APR_ASCII_BLANK || *(pos - 1) == APR_ASCII_TAB)) {
-        --pos;
+    /* Now NUL-terminate the string at the end of the line; 
+     * if the last-but-one character is a CR, terminate there */
+    if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
+        last_char--;
     }
-
-    /* Since we want to remove the LF from the line, we'll go ahead
-     * and set this last character to be the term NULL and reset
-     * bytes_handled accordingly.
-     */
-    *pos = '\0';
-    last_char = pos;
-    bytes_handled = pos - *s;
+    *last_char = '\0';
+    bytes_handled = last_char - *s;
 
     /* If we're folding, we have more work to do.
      *
@@ -760,7 +738,7 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                 last_len += len;
                 folded = 1;
             }
-            else {
+            else /* not a continuation line */ {
 
                 if (r->server->limit_req_fields
                     && (++fields_read > r->server->limit_req_fields)) {
@@ -783,29 +761,26 @@ AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb
                                                "</pre>\n", NULL));
                     return;
                 }
+                
+                tmp_field = value - 1; /* last character of field-name */
+
+                *value++ = '\0'; /* NUL-terminate at colon */
 
-                *value = '\0';
-                tmp_field = value;  /* used to trim the whitespace between key
-                                     * token and separator
-                                     */
-                ++value;
                 while (*value == ' ' || *value == '\t') {
                     ++value;            /* Skip to start of value   */
                 }
 
-                /* This check is to avoid any invalid memory reference while
-                 * traversing backwards in the key. To avoid a case where
-                 * the header starts with ':' (or with just some white
-                 * space and the ':') followed by the value
-                 */
-                if (tmp_field > last_field) {
-                    --tmp_field;
-                    while ((tmp_field > last_field) &&
-                           (*tmp_field == ' ' || *tmp_field == '\t')) {
-                        --tmp_field;   /* Removing LWS between key and ':' */
-                    }
-                    ++tmp_field;
-                    *tmp_field = '\0';
+                /* Strip LWS after field-name: */
+                while (tmp_field > last_field 
+                       && (*tmp_field == ' ' || *tmp_field == '\t')) {
+                    *tmp_field-- = '\0';
+                }
+                
+                /* Strip LWS after field-value: */
+                tmp_field = last_field + last_len - 1;
+                while (tmp_field > value
+                       && (*tmp_field == ' ' || *tmp_field == '\t')) {
+                    *tmp_field-- = '\0';
                 }
 
                 apr_table_addn(r->headers_in, last_field, value);