Skip to content

Commit caa7bb4

Browse files
authored
Fix potential integer overflow in hash container create/resize (#1813)
The sized constructors, reserve(), and rehash() methods of absl::{flat,node}_hash_{set,map} did not impose an upper bound on their size argument. As a result, it was possible for a caller to pass a very large size that would cause an integer overflow when computing the size of the container's backing store. Subsequent accesses to the container might then access out-of-bounds memory. The fix is in two parts: 1) Update max_size() to return the maximum number of items that can be stored in the container 2) Validate the size arguments to the constructors, reserve(), and rehash() methods, and abort the program when the argument is invalid We've looked at uses of these containers in Google codebases like Chrome, and determined this vulnerability is likely to be difficult to exploit. This is primarily because container sizes are rarely attacker-controlled. The bug was discovered by Dmitry Vyukov <[email protected]>.
1 parent 929c17c commit caa7bb4

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

absl/base/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
// LTS releases can be obtained from
113113
// https://github.com/abseil/abseil-cpp/releases.
114114
#define ABSL_LTS_RELEASE_VERSION 20230802
115-
#define ABSL_LTS_RELEASE_PATCH_LEVEL 2
115+
#define ABSL_LTS_RELEASE_PATCH_LEVEL 3
116116

117117
// Helper macro to convert a CPP variable to a string literal.
118118
#define ABSL_INTERNAL_DO_TOKEN_STR(x) #x

absl/container/internal/raw_hash_set.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,12 @@ inline size_t NormalizeCapacity(size_t n) {
10761076
return n ? ~size_t{} >> countl_zero(n) : 1;
10771077
}
10781078

1079+
template <size_t kSlotSize>
1080+
size_t MaxValidCapacity() {
1081+
return NormalizeCapacity((std::numeric_limits<size_t>::max)() / 4 /
1082+
kSlotSize);
1083+
}
1084+
10791085
// General notes on capacity/growth methods below:
10801086
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
10811087
// average of two empty slots per group.
@@ -1717,6 +1723,8 @@ class raw_hash_set {
17171723
const allocator_type& alloc = allocator_type())
17181724
: settings_(CommonFields{}, hash, eq, alloc) {
17191725
if (bucket_count) {
1726+
ABSL_RAW_CHECK(bucket_count <= MaxValidCapacity<sizeof(slot_type)>(),
1727+
"Hash table size overflow");
17201728
common().set_capacity(NormalizeCapacity(bucket_count));
17211729
initialize_slots();
17221730
}
@@ -1916,7 +1924,10 @@ class raw_hash_set {
19161924
bool empty() const { return !size(); }
19171925
size_t size() const { return common().size(); }
19181926
size_t capacity() const { return common().capacity(); }
1919-
size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
1927+
size_t max_size() const {
1928+
return CapacityToGrowth(MaxValidCapacity<sizeof(slot_type)>());
1929+
}
1930+
19201931

19211932
ABSL_ATTRIBUTE_REINITIALIZES void clear() {
19221933
// Iterating over this container is O(bucket_count()). When bucket_count()
@@ -2266,6 +2277,8 @@ class raw_hash_set {
22662277
auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size()));
22672278
// n == 0 unconditionally rehashes as per the standard.
22682279
if (n == 0 || m > capacity()) {
2280+
ABSL_RAW_CHECK(m <= MaxValidCapacity<sizeof(slot_type)>(),
2281+
"Hash table size overflow");
22692282
resize(m);
22702283

22712284
// This is after resize, to ensure that we have completed the allocation
@@ -2276,6 +2289,7 @@ class raw_hash_set {
22762289

22772290
void reserve(size_t n) {
22782291
if (n > size() + growth_left()) {
2292+
ABSL_RAW_CHECK(n <= max_size(), "Hash table size overflow");
22792293
size_t m = GrowthToLowerboundCapacity(n);
22802294
resize(NormalizeCapacity(m));
22812295

absl/container/internal/raw_hash_set_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,14 @@ TEST(Iterator, InvalidComparisonDifferentTables) {
25102510
"Invalid iterator comparison.*non-end");
25112511
}
25122512

2513+
TEST(Table, MaxSizeOverflow) {
2514+
size_t overflow = (std::numeric_limits<size_t>::max)();
2515+
EXPECT_DEATH_IF_SUPPORTED(IntTable t(overflow), "Hash table size overflow");
2516+
IntTable t;
2517+
EXPECT_DEATH_IF_SUPPORTED(t.reserve(overflow), "Hash table size overflow");
2518+
EXPECT_DEATH_IF_SUPPORTED(t.rehash(overflow), "Hash table size overflow");
2519+
}
2520+
25132521
} // namespace
25142522
} // namespace container_internal
25152523
ABSL_NAMESPACE_END

0 commit comments

Comments
 (0)