Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Better handling of truncated data when reading containers
Summary: Currently we read the container size and blindly pre-allocate the container of that size. This allows malicious attacker to send few bytes message and cause server to allocate GBs of memory. This diff changes the logic to check if we have at least 1b/element in our buffer, thus forcing attacker to send that much data. This is a partial fix for CVE-2019-3553. Reviewed By: yfeldblum, vitaut Differential Revision: D14392438 fbshipit-source-id: b92e300a98e29faee564e2f5069027b28cb2cca4
- Loading branch information
1 parent
099fe6f
commit 3f15620
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace cpp2 thrift.test | ||
|
|
||
| struct TestStruct { | ||
| 1: optional list<i64> i64_list, | ||
| 2: optional set<i32> i32_set, | ||
| 3: optional map<i32,i16> i32_i16_map, | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <thrift/lib/cpp2/protocol/Serializer.h> | ||
| #include <thrift/test/gen-cpp2/ProtocolTruncatedData_types.h> | ||
|
|
||
| using namespace apache::thrift; | ||
| using namespace thrift::test; | ||
|
|
||
| template <class Serializer, class T> | ||
| void testPartialDataHandling(const T& val, size_t bytesToPassTheCheck) { | ||
| auto buf = Serializer::template serialize<folly::IOBufQueue>(val).move(); | ||
| buf->coalesce(); | ||
|
|
||
| // Check that deserializing doesn't throw. | ||
| EXPECT_NO_THROW(Serializer::template deserialize<T>(buf.get())); | ||
|
|
||
| // Trim the buffer to the point that is *just enough* to pass the check for | ||
| // minimum required bytes. | ||
| buf->trimEnd(buf->length() - bytesToPassTheCheck); | ||
| // We'll hit underflow exception when pulling yet another element. | ||
| EXPECT_THROW( | ||
| Serializer::template deserialize<T>(buf.get()), std::out_of_range); | ||
|
|
||
| // Trim one more byte. | ||
| buf->trimEnd(1); | ||
| // We'll fail the deserialization straight when we read the length. | ||
| EXPECT_THROW( | ||
| Serializer::template deserialize<T>(buf.get()), | ||
| apache::thrift::protocol::TProtocolException); | ||
| } | ||
|
|
||
| TEST(ProtocolTruncatedDataTest, TruncatedList) { | ||
| TestStruct s; | ||
| s.i64_list_ref() = {}; | ||
| for (size_t i = 0; i < 30; ++i) { | ||
| s.i64_list_ref()->emplace_back((1ull << i)); | ||
| } | ||
|
|
||
| testPartialDataHandling<CompactSerializer>( | ||
| s, 3 /* headers */ + 30 /* 1b / element */); | ||
| } | ||
|
|
||
| TEST(ProtocolTruncatedDataTest, TruncatedSet) { | ||
| TestStruct s; | ||
| s.i32_set_ref() = {}; | ||
| for (size_t i = 0; i < 30; ++i) { | ||
| s.i32_set_ref()->emplace((1ull << i)); | ||
| } | ||
|
|
||
| testPartialDataHandling<CompactSerializer>( | ||
| s, 3 /* headers */ + 30 /* 1b / element */); | ||
| } | ||
|
|
||
| TEST(ProtocolTruncatedDataTest, TruncatedMap) { | ||
| TestStruct s; | ||
| s.i32_i16_map_ref() = {}; | ||
| for (size_t i = 0; i < 30; ++i) { | ||
| s.i32_i16_map_ref()->emplace((1ull << i), i); | ||
| } | ||
|
|
||
| testPartialDataHandling<CompactSerializer>( | ||
| s, 3 /* headers */ + 30 * 2 /* 2b / kv pair */); | ||
| } |