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

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