Blame SOURCES/squid-4.11-CVE-2020-15049.patch

818ced
commit ea12a34d338b962707d5078d6d1fc7c6eb119a22
818ced
Author: Alex Rousskov <rousskov@measurement-factory.com>
818ced
Date:   2020-05-13 14:05:00 +0000
818ced
818ced
    Validate Content-Length value prefix (#629)
818ced
    
818ced
    The new code detects all invalid Content-Length prefixes but the old
818ced
    code was already rejecting most invalid prefixes using strtoll(). The
818ced
    newly covered (and now rejected) invalid characters are
818ced
    
818ced
    * explicit "+" sign;
818ced
    * explicit "-" sign in "-0" values;
818ced
    * isspace(3) characters that are not (relaxed) OWS characters.
818ced
    
818ced
    In most deployment environments, the last set is probably empty because
818ced
    the relaxed OWS set has all the POSIX/C isspace(3) characters but the
818ced
    new line, and the new line is unlikely to sneak in past other checks.
818ced
    
818ced
    Thank you, Amit Klein <amit.klein@safebreach.com>, for elevating the
818ced
    importance of this 2016 TODO (added in commit a1b9ec2).
818ced
818ced
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
818ced
index 36957f2..c10a221 100644
818ced
--- a/CONTRIBUTORS
818ced
+++ b/CONTRIBUTORS
818ced
@@ -25,6 +25,7 @@ Thank you!
818ced
     Alex Wu <alex_wu2012@hotmail.com>
818ced
     Alin Nastac <mrness@gentoo.org>
818ced
     Alter <alter@alter.org.ua>
818ced
+    Amit Klein <amit.klein@safebreach.com>
818ced
     Amos Jeffries
818ced
     Amos Jeffries <amosjeffries@squid-cache.org>
818ced
     Amos Jeffries <squid3@treenet.co.nz>
818ced
diff --git a/src/http/ContentLengthInterpreter.cc b/src/http/ContentLengthInterpreter.cc
818ced
index 3fdf7de..a3741eb 100644
818ced
--- a/src/http/ContentLengthInterpreter.cc
818ced
+++ b/src/http/ContentLengthInterpreter.cc
818ced
@@ -28,6 +28,24 @@ Http::ContentLengthInterpreter::ContentLengthInterpreter(const int aDebugLevel):
818ced
 {
818ced
 }
818ced
 
818ced
+/// checks whether all characters before the Content-Length number are allowed
818ced
+/// \returns the start of the digit sequence (or nil on errors)
818ced
+const char *
818ced
+Http::ContentLengthInterpreter::findDigits(const char *prefix, const char * const valueEnd) const
818ced
+{
818ced
+    // skip leading OWS in RFC 7230's `OWS field-value OWS`
818ced
+    const CharacterSet &whitespace = Http::One::Parser::WhitespaceCharacters();
818ced
+    while (prefix < valueEnd) {
818ced
+        const auto ch = *prefix;
818ced
+        if (CharacterSet::DIGIT[ch])
818ced
+            return prefix; // common case: a pre-trimmed field value
818ced
+        if (!whitespace[ch])
818ced
+            return nullptr; // (trimmed) length does not start with a digit
818ced
+        ++prefix;
818ced
+    }
818ced
+    return nullptr; // empty or whitespace-only value
818ced
+}
818ced
+
818ced
 /// checks whether all characters after the Content-Length are allowed
818ced
 bool
818ced
 Http::ContentLengthInterpreter::goodSuffix(const char *suffix, const char * const end) const
818ced
@@ -52,10 +70,19 @@ Http::ContentLengthInterpreter::checkValue(const char *rawValue, const int value
818ced
 {
818ced
     Must(!sawBad);
818ced
 
818ced
+    const auto valueEnd = rawValue + valueSize;
818ced
+
818ced
+    const auto digits = findDigits(rawValue, valueEnd);
818ced
+    if (!digits) {
818ced
+        debugs(55, debugLevel, "WARNING: Leading garbage or empty value in" << Raw("Content-Length", rawValue, valueSize));
818ced
+        sawBad = true;
818ced
+        return false;
818ced
+    }
818ced
+
818ced
     int64_t latestValue = -1;
818ced
     char *suffix = nullptr;
818ced
-    // TODO: Handle malformed values with leading signs (e.g., "-0" or "+1").
818ced
-    if (!httpHeaderParseOffset(rawValue, &latestValue, &suffix)) {
818ced
+
818ced
+    if (!httpHeaderParseOffset(digits, &latestValue, &suffix)) {
818ced
         debugs(55, DBG_IMPORTANT, "WARNING: Malformed" << Raw("Content-Length", rawValue, valueSize));
818ced
         sawBad = true;
818ced
         return false;
818ced
@@ -68,7 +95,7 @@ Http::ContentLengthInterpreter::checkValue(const char *rawValue, const int value
818ced
     }
818ced
 
818ced
     // check for garbage after the number
818ced
-    if (!goodSuffix(suffix, rawValue + valueSize)) {
818ced
+    if (!goodSuffix(suffix, valueEnd)) {
818ced
         debugs(55, debugLevel, "WARNING: Trailing garbage in" << Raw("Content-Length", rawValue, valueSize));
818ced
         sawBad = true;
818ced
         return false;
818ced
diff --git a/src/http/ContentLengthInterpreter.h b/src/http/ContentLengthInterpreter.h
818ced
index ce36e22..f22de91 100644
818ced
--- a/src/http/ContentLengthInterpreter.h
818ced
+++ b/src/http/ContentLengthInterpreter.h
818ced
@@ -46,6 +46,7 @@ public:
818ced
     bool sawGood;
818ced
 
818ced
 protected:
818ced
+    const char *findDigits(const char *prefix, const char *valueEnd) const;
818ced
     bool goodSuffix(const char *suffix, const char * const end) const;
818ced
     bool checkValue(const char *start, const int size);
818ced
     bool checkList(const String &list);