Skip to content

Commit

Permalink
[1.6] Fix CVE-2020-25017 (#44) (#267)
Browse files Browse the repository at this point in the history
* Apply patch 2

* patch 3

* Patch 4

* Fixing build failures

Co-authored-by: Dmitri Dolguikh <ddolguik@redhat.com>

Co-authored-by: Dmitri Dolguikh <ddolguik@redhat.com>
  • Loading branch information
brian-avery and Dmitri Dolguikh authored Oct 2, 2020
1 parent a1ef7b2 commit b118f52
Show file tree
Hide file tree
Showing 38 changed files with 709 additions and 127 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.14.4
1.14.5-dev
27 changes: 27 additions & 0 deletions docs/root/intro/version_history.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
Version history
---------------

1.14.5 (Pending)
================

Changes
-------
* http: fixed CVE-2020-25017. Previously header matching did not match on all headers for non-inline headers.
This patch changes the default behavior to always logically match on all headers. Multiple individual
headers will be logically concatenated with ',' similar to what is done with inline headers. This
makes the behavior effectively consistent. This behavior can be temporary reverted by setting
the runtime value "envoy.reloadable_features.header_match_on_all_headers" to "false".

Targeted fixes have been additionally performed on the following extensions which make them
consider all duplicate headers by default as a comma concatenated list:

1. Any extension using CEL matching on headers.
2. The header to metadata filter.
3. The JWT filter.
4. The Lua filter.

Like primary header matching used in routing, RBAC, etc. this behavior can be disabled by setting
the runtime value "envoy.reloadable_features.header_match_on_all_headers" to false.
* http: fixed CVE-2020-25017. The setCopy() header map API previously only set the first header in the case of duplicate
non-inline headers. setCopy() now behaves similarly to the other set*() APIs and replaces all found
headers with a single value. This may have had security implications in the extauth filter which
uses this API. This behavior can be disabled by setting the runtime value
"envoy.reloadable_features.http_set_copy_replace_all_headers" to false.

1.14.4 (July 7, 2020)
=====================
* tls: fixed a bug where wilcard matching for "\*.foo.com" also matched domains of the form "a.b.foo.com". This behavior can be temporarily reverted by setting runtime feature `envoy.reloadable_features.fix_wildcard_matching` to false.
Expand Down
31 changes: 31 additions & 0 deletions include/envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class HeaderString {
absl::get<InlineHeaderVector>(buffer_).begin(), unary_op);
}

/**
* Trim trailing whitespaces from the HeaderString. Only supported by the "Inline" HeaderString
* representation.
*/
void rtrim();

/**
* Get an absl::string_view. It will NOT be NUL terminated!
*
Expand Down Expand Up @@ -537,6 +543,31 @@ class HeaderMap {
*/
virtual const HeaderEntry* get(const LowerCaseString& key) const PURE;

/**
* This is a wrapper for the return result from getAll(). It avoids a copy when translating from
* non-const HeaderEntry to const HeaderEntry and only provides const access to the result.
*/
using NonConstGetResult = absl::InlinedVector<HeaderEntry*, 1>;
class GetResult {
public:
GetResult() = default;
explicit GetResult(NonConstGetResult&& result) : result_(std::move(result)) {}

bool empty() const { return result_.empty(); }
size_t size() const { return result_.size(); }
const HeaderEntry* operator[](size_t i) const { return result_[i]; }

private:
NonConstGetResult result_;
};

/**
* Get a header by key.
* @param key supplies the header key.
* @return all header entries matching the key.
*/
virtual GetResult getAll(const LowerCaseString& key) const PURE;

// aliases to make iterate() and iterateReverse() callbacks easier to read
enum class Iterate { Continue, Break };

Expand Down
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ envoy_cc_library(
"//source/common/common:empty_string",
"//source/common/common:non_copyable",
"//source/common/common:utility_lib",
"//source/common/runtime:runtime_features_lib",
"//source/common/singleton:const_singleton",
],
)
Expand Down
78 changes: 52 additions & 26 deletions source/common/http/header_map_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/common/assert.h"
#include "common/common/dump_state_utils.h"
#include "common/common/empty_string.h"
#include "common/runtime/runtime_features.h"
#include "common/singleton/const_singleton.h"

#include "absl/strings/match.h"
Expand Down Expand Up @@ -87,6 +88,15 @@ void HeaderString::append(const char* data, uint32_t data_size) {
get_in_vec(buffer_).insert(get_in_vec(buffer_).end(), data, data + data_size);
}

void HeaderString::rtrim() {
ASSERT(type() == Type::Inline);
absl::string_view original = getStringView();
absl::string_view rtrimmed = StringUtil::rtrim(original);
if (original.size() != rtrimmed.size()) {
get_in_vec(buffer_).resize(rtrimmed.size());
}
}

absl::string_view HeaderString::getStringView() const {
if (type() == Type::Reference) {
return get_str_view(buffer_);
Expand Down Expand Up @@ -388,39 +398,37 @@ void HeaderMapImpl::addCopy(const LowerCaseString& key, absl::string_view value)

void HeaderMapImpl::appendCopy(const LowerCaseString& key, absl::string_view value) {
// TODO(#9221): converge on and document a policy for coalescing multiple headers.
auto* entry = getExisting(key);
if (entry) {
const uint64_t added_size = appendToHeader(entry->value(), value);
auto entry = getExisting(key);
if (!entry.empty()) {
const uint64_t added_size = appendToHeader(entry[0]->value(), value);
addSize(added_size);
} else {
addCopy(key, value);
}
}

void HeaderMapImpl::setReference(const LowerCaseString& key, absl::string_view value) {
HeaderString ref_key(key);
HeaderString ref_value(value);
remove(key);
insertByKey(std::move(ref_key), std::move(ref_value));
addReference(key, value);
}

void HeaderMapImpl::setReferenceKey(const LowerCaseString& key, absl::string_view value) {
HeaderString ref_key(key);
HeaderString new_value;
new_value.setCopy(value);
remove(key);
insertByKey(std::move(ref_key), std::move(new_value));
ASSERT(new_value.empty()); // NOLINT(bugprone-use-after-move)
addReferenceKey(key, value);
}

void HeaderMapImpl::setCopy(const LowerCaseString& key, absl::string_view value) {
// Replaces the first occurrence of a header if it exists, otherwise adds by copy.
// TODO(#9221): converge on and document a policy for coalescing multiple headers.
auto* entry = getExisting(key);
if (entry) {
updateSize(entry->value().size(), value.size());
entry->value(value);
if (!Runtime::runtimeFeatureEnabled(
"envoy.reloadable_features.http_set_copy_replace_all_headers")) {
auto entry = getExisting(key);
if (!entry.empty()) {
updateSize(entry[0]->value().size(), value.size());
entry[0]->value(value);
} else {
addCopy(key, value);
}
} else {
remove(key);
addCopy(key, value);
}
}
Expand All @@ -438,23 +446,41 @@ void HeaderMapImpl::verifyByteSizeInternalForTest() const {
}

const HeaderEntry* HeaderMapImpl::get(const LowerCaseString& key) const {
for (const HeaderEntryImpl& header : headers_) {
if (header.key() == key.get().c_str()) {
return &header;
}
}
const auto result = getAll(key);
return result.empty() ? nullptr : result[0];
}

return nullptr;
HeaderMap::GetResult HeaderMapImpl::getAll(const LowerCaseString& key) const {
return HeaderMap::GetResult(const_cast<HeaderMapImpl*>(this)->getExisting(key));
}

HeaderEntry* HeaderMapImpl::getExisting(const LowerCaseString& key) {
HeaderMap::NonConstGetResult HeaderMapImpl::getExisting(const LowerCaseString& key) {
// Attempt a trie lookup first to see if the user is requesting an O(1) header. This may be
// relatively common in certain header matching / routing patterns.
// TODO(mattklein123): Add inline handle support directly to the header matcher code to support
// this use case more directly.
HeaderMap::NonConstGetResult ret;
auto lookup = staticLookup(key.get());
if (lookup.has_value()) {
if (*lookup.value().entry_ != nullptr) {
ret.push_back(*lookup.value().entry_);
}
return ret;
}

// If the requested header is not an O(1) header we do a full scan. Doing the trie lookup is
// wasteful in the miss case, but is present for code consistency with other functions that do
// similar things.
// TODO(mattklein123): The full scan here and in remove() are the biggest issues with this
// implementation for certain use cases. We can either replace this with a totally different
// implementation or potentially create a lazy map if the size of the map is above a threshold.
for (HeaderEntryImpl& header : headers_) {
if (header.key() == key.get().c_str()) {
return &header;
ret.push_back(&header);
}
}

return nullptr;
return ret;
}

void HeaderMapImpl::iterate(ConstIterateCb cb, void* context) const {
Expand Down
4 changes: 3 additions & 1 deletion source/common/http/header_map_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class HeaderMapImpl : public virtual HeaderMap, NonCopyable {
void setCopy(const LowerCaseString& key, absl::string_view value) override;
uint64_t byteSize() const override;
const HeaderEntry* get(const LowerCaseString& key) const override;
HeaderMap::GetResult getAll(const LowerCaseString& key) const override;
void iterate(ConstIterateCb cb, void* context) const override;
void iterateReverse(ConstIterateCb cb, void* context) const override;
Lookup lookup(const LowerCaseString& key, const HeaderEntry** entry) const override;
Expand Down Expand Up @@ -213,8 +214,9 @@ class HeaderMapImpl : public virtual HeaderMap, NonCopyable {
HeaderEntryImpl& maybeCreateInline(HeaderEntryImpl** entry, const LowerCaseString& key);
HeaderEntryImpl& maybeCreateInline(HeaderEntryImpl** entry, const LowerCaseString& key,
HeaderString&& value);
HeaderEntry* getExisting(const LowerCaseString& key);
HeaderMap::NonConstGetResult getExisting(const LowerCaseString& key);
HeaderEntryImpl* getExistingInline(absl::string_view key);

size_t removeInline(HeaderEntryImpl** entry);
void updateSize(uint64_t from_size, uint64_t to_size);
void addSize(uint64_t size);
Expand Down
49 changes: 39 additions & 10 deletions source/common/http/header_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,65 @@ bool HeaderUtility::matchHeaders(const HeaderMap& request_headers,
return true;
}

HeaderUtility::GetAllOfHeaderAsStringResult
HeaderUtility::getAllOfHeaderAsString(const HeaderMap& headers, const Http::LowerCaseString& key) {
GetAllOfHeaderAsStringResult result;
const auto header_value = headers.getAll(key);

if (header_value.empty()) {
// Empty for clarity. Avoid handling the empty case in the block below if the runtime feature
// is disabled.
} else if (header_value.size() == 1 ||
!Runtime::runtimeFeatureEnabled(
"envoy.reloadable_features.http_match_on_all_headers")) {
result.result_ = header_value[0]->value().getStringView();
} else {
// In this case we concatenate all found headers using a ',' delimiter before performing the
// final match. We use an InlinedVector of absl::string_view to invoke the optimized join
// algorithm. This requires a copying phase before we invoke join. The 3 used as the inline
// size has been arbitrarily chosen.
// TODO(mattklein123): Do we need to normalize any whitespace here?
absl::InlinedVector<absl::string_view, 3> string_view_vector;
string_view_vector.reserve(header_value.size());
for (size_t i = 0; i < header_value.size(); i++) {
string_view_vector.push_back(header_value[i]->value().getStringView());
}
result.result_backing_string_ = absl::StrJoin(string_view_vector, ",");
}

return result;
}

bool HeaderUtility::matchHeaders(const HeaderMap& request_headers, const HeaderData& header_data) {
const HeaderEntry* header = request_headers.get(header_data.name_);
const auto header_value = getAllOfHeaderAsString(request_headers, header_data.name_);

if (header == nullptr) {
if (!header_value.result().has_value()) {
return header_data.invert_match_ && header_data.header_match_type_ == HeaderMatchType::Present;
}

bool match;
const absl::string_view header_view = header->value().getStringView();
switch (header_data.header_match_type_) {
case HeaderMatchType::Value:
match = header_data.value_.empty() || header_view == header_data.value_;
match = header_data.value_.empty() || header_value.result().value() == header_data.value_;
break;
case HeaderMatchType::Regex:
match = header_data.regex_->match(header_view);
match = header_data.regex_->match(header_value.result().value());
break;
case HeaderMatchType::Range: {
int64_t header_value = 0;
match = absl::SimpleAtoi(header_view, &header_value) &&
header_value >= header_data.range_.start() && header_value < header_data.range_.end();
int64_t header_int_value = 0;
match = absl::SimpleAtoi(header_value.result().value(), &header_int_value) &&
header_int_value >= header_data.range_.start() &&
header_int_value < header_data.range_.end();
break;
}
case HeaderMatchType::Present:
match = true;
break;
case HeaderMatchType::Prefix:
match = absl::StartsWith(header_view, header_data.value_);
match = absl::StartsWith(header_value.result().value(), header_data.value_);
break;
case HeaderMatchType::Suffix:
match = absl::EndsWith(header_view, header_data.value_);
match = absl::EndsWith(header_value.result().value(), header_data.value_);
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
Expand Down
29 changes: 29 additions & 0 deletions source/common/http/header_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ class HeaderUtility {
static void getAllOfHeader(const HeaderMap& headers, absl::string_view key,
std::vector<absl::string_view>& out);

/**
* Get all header values as a single string. Multiple headers are concatenated with ','.
*/
class GetAllOfHeaderAsStringResult {
public:
// The ultimate result of the concatenation. If absl::nullopt, no header values were found.
// If the final string required a string allocation, the memory is held in
// backingString(). This allows zero allocation in the common case of a single header
// value.
absl::optional<absl::string_view> result() const {
// This is safe for move/copy of this class as the backing string will be moved or copied.
// Otherwise result_ is valid. The assert verifies that both are empty or only 1 is set.
ASSERT((!result_.has_value() && result_backing_string_.empty()) ||
(result_.has_value() ^ !result_backing_string_.empty()));
return !result_backing_string_.empty() ? result_backing_string_ : result_;
}

const std::string& backingString() const { return result_backing_string_; }

private:
absl::optional<absl::string_view> result_;
// Valid only if result_ relies on memory allocation that must live beyond the call. See above.
std::string result_backing_string_;

friend class HeaderUtility;
};
static GetAllOfHeaderAsStringResult getAllOfHeaderAsString(const HeaderMap& headers,
const Http::LowerCaseString& key);

// A HeaderData specifies one of exact value or regex or range element
// to match in a request's header, specified in the header_match_type_ member.
// It is the runtime equivalent of the HeaderMatchSpecifier proto in RDS API.
Expand Down
15 changes: 12 additions & 3 deletions source/common/http/http1/codec_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ void ConnectionImpl::completeLastHeader() {
auto& headers_or_trailers = headersOrTrailers();
if (!current_header_field_.empty()) {
current_header_field_.inlineTransform([](char c) { return absl::ascii_tolower(c); });
// Strip trailing whitespace of the current header value if any. Leading whitespace was trimmed
// in ConnectionImpl::onHeaderValue. http_parser does not strip leading or trailing whitespace
// as the spec requires: https://tools.ietf.org/html/rfc7230#section-3.2.4
current_header_value_.rtrim();
headers_or_trailers.addViaMove(std::move(current_header_field_),
std::move(current_header_value_));
}
Expand Down Expand Up @@ -556,9 +560,7 @@ void ConnectionImpl::onHeaderValue(const char* data, size_t length) {
return;
}

// Work around a bug in http_parser where trailing whitespace is not trimmed
// as the spec requires: https://tools.ietf.org/html/rfc7230#section-3.2.4
const absl::string_view header_value = StringUtil::trim(absl::string_view(data, length));
absl::string_view header_value{data, length};

if (strict_header_validation_) {
if (!Http::HeaderUtility::headerValueIsValid(header_value)) {
Expand All @@ -570,6 +572,13 @@ void ConnectionImpl::onHeaderValue(const char* data, size_t length) {
}

header_parsing_state_ = HeaderParsingState::Value;
if (current_header_value_.empty()) {
// Strip leading whitespace if the current header value input contains the first bytes of the
// encoded header value. Trailing whitespace is stripped once the full header value is known in
// ConnectionImpl::completeLastHeader. http_parser does not strip leading or trailing whitespace
// as the spec requires: https://tools.ietf.org/html/rfc7230#section-3.2.4 .
header_value = StringUtil::ltrim(header_value);
}
current_header_value_.append(header_value.data(), header_value.length());

checkMaxHeadersSize();
Expand Down
2 changes: 2 additions & 0 deletions source/common/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ envoy_cc_library(
"runtime_features.h",
],
deps = [
"//include/envoy/runtime:runtime_interface",
"//source/common/common:assert_lib",
"//source/common/common:hash_lib",
"//source/common/singleton:const_singleton",
],
Expand Down
Loading

0 comments on commit b118f52

Please sign in to comment.