Skip to content

Commit 54fac21

Browse files
authoredJan 23, 2025
Fix potential integer overflow in hash container create/resize (#1812)
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 d7aaad8 commit 54fac21

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed
 

‎MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
module(
1818
name = "abseil-cpp",
19-
version = "20240116.2",
19+
version = "20240116.3",
2020
compatibility_level = 1,
2121
)
2222

‎absl/base/config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
// LTS releases can be obtained from
119119
// https://github.com/abseil/abseil-cpp/releases.
120120
#define ABSL_LTS_RELEASE_VERSION 20240116
121-
#define ABSL_LTS_RELEASE_PATCH_LEVEL 2
121+
#define ABSL_LTS_RELEASE_PATCH_LEVEL 3
122122

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

‎absl/container/internal/raw_hash_set.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,12 @@ inline size_t NormalizeCapacity(size_t n) {
11841184
return n ? ~size_t{} >> countl_zero(n) : 1;
11851185
}
11861186

1187+
template <size_t kSlotSize>
1188+
size_t MaxValidCapacity() {
1189+
return NormalizeCapacity((std::numeric_limits<size_t>::max)() / 4 /
1190+
kSlotSize);
1191+
}
1192+
11871193
// General notes on capacity/growth methods below:
11881194
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
11891195
// average of two empty slots per group.
@@ -2093,6 +2099,8 @@ class raw_hash_set {
20932099
const allocator_type& alloc = allocator_type())
20942100
: settings_(CommonFields{}, hash, eq, alloc) {
20952101
if (bucket_count) {
2102+
ABSL_RAW_CHECK(bucket_count <= MaxValidCapacity<sizeof(slot_type)>(),
2103+
"Hash table size overflow");
20962104
resize(NormalizeCapacity(bucket_count));
20972105
}
20982106
}
@@ -2286,7 +2294,9 @@ class raw_hash_set {
22862294
bool empty() const { return !size(); }
22872295
size_t size() const { return common().size(); }
22882296
size_t capacity() const { return common().capacity(); }
2289-
size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
2297+
size_t max_size() const {
2298+
return CapacityToGrowth(MaxValidCapacity<sizeof(slot_type)>());
2299+
}
22902300

22912301
ABSL_ATTRIBUTE_REINITIALIZES void clear() {
22922302
// Iterating over this container is O(bucket_count()). When bucket_count()
@@ -2624,6 +2634,8 @@ class raw_hash_set {
26242634
auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size()));
26252635
// n == 0 unconditionally rehashes as per the standard.
26262636
if (n == 0 || m > capacity()) {
2637+
ABSL_RAW_CHECK(m <= MaxValidCapacity<sizeof(slot_type)>(),
2638+
"Hash table size overflow");
26272639
resize(m);
26282640

26292641
// This is after resize, to ensure that we have completed the allocation
@@ -2634,6 +2646,7 @@ class raw_hash_set {
26342646

26352647
void reserve(size_t n) {
26362648
if (n > size() + growth_left()) {
2649+
ABSL_RAW_CHECK(n <= max_size(), "Hash table size overflow");
26372650
size_t m = GrowthToLowerboundCapacity(n);
26382651
resize(NormalizeCapacity(m));
26392652

‎absl/container/internal/raw_hash_set_test.cc

+8
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,14 @@ TEST(Table, CountedHash) {
26782678
}
26792679
}
26802680

2681+
TEST(Table, MaxSizeOverflow) {
2682+
size_t overflow = (std::numeric_limits<size_t>::max)();
2683+
EXPECT_DEATH_IF_SUPPORTED(IntTable t(overflow), "Hash table size overflow");
2684+
IntTable t;
2685+
EXPECT_DEATH_IF_SUPPORTED(t.reserve(overflow), "Hash table size overflow");
2686+
EXPECT_DEATH_IF_SUPPORTED(t.rehash(overflow), "Hash table size overflow");
2687+
}
2688+
26812689
} // namespace
26822690
} // namespace container_internal
26832691
ABSL_NAMESPACE_END

0 commit comments

Comments
 (0)
Please sign in to comment.