Skip to content

Commit dd4c89b

Browse files
authored
Fix potential integer overflow in hash container create/resize (#1811)
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 4447c75 commit dd4c89b

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

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

absl/base/config.h

Lines changed: 1 addition & 1 deletion
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 20240722
121-
#define ABSL_LTS_RELEASE_PATCH_LEVEL 0
121+
#define ABSL_LTS_RELEASE_PATCH_LEVEL 1
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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,9 @@ class RawHashSetLayout {
12081208
// Given the capacity of a table, computes the total size of the backing
12091209
// array.
12101210
size_t alloc_size(size_t slot_size) const {
1211+
ABSL_HARDENING_ASSERT(
1212+
slot_size <=
1213+
((std::numeric_limits<size_t>::max)() - slot_offset_) / capacity_);
12111214
return slot_offset_ + capacity_ * slot_size;
12121215
}
12131216

@@ -1500,6 +1503,12 @@ inline size_t NormalizeCapacity(size_t n) {
15001503
return n ? ~size_t{} >> countl_zero(n) : 1;
15011504
}
15021505

1506+
template <size_t kSlotSize>
1507+
size_t MaxValidCapacity() {
1508+
return NormalizeCapacity((std::numeric_limits<size_t>::max)() / 4 /
1509+
kSlotSize);
1510+
}
1511+
15031512
// General notes on capacity/growth methods below:
15041513
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
15051514
// average of two empty slots per group.
@@ -2614,6 +2623,8 @@ class raw_hash_set {
26142623
: settings_(CommonFields::CreateDefault<SooEnabled()>(), hash, eq,
26152624
alloc) {
26162625
if (bucket_count > (SooEnabled() ? SooCapacity() : 0)) {
2626+
ABSL_RAW_CHECK(bucket_count <= MaxValidCapacity<sizeof(slot_type)>(),
2627+
"Hash table size overflow");
26172628
resize(NormalizeCapacity(bucket_count));
26182629
}
26192630
}
@@ -2871,7 +2882,9 @@ class raw_hash_set {
28712882
ABSL_ASSUME(!kEnabled || cap >= kCapacity);
28722883
return cap;
28732884
}
2874-
size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
2885+
size_t max_size() const {
2886+
return CapacityToGrowth(MaxValidCapacity<sizeof(slot_type)>());
2887+
}
28752888

28762889
ABSL_ATTRIBUTE_REINITIALIZES void clear() {
28772890
// Iterating over this container is O(bucket_count()). When bucket_count()
@@ -3260,6 +3273,8 @@ class raw_hash_set {
32603273
auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size()));
32613274
// n == 0 unconditionally rehashes as per the standard.
32623275
if (n == 0 || m > cap) {
3276+
ABSL_RAW_CHECK(m <= MaxValidCapacity<sizeof(slot_type)>(),
3277+
"Hash table size overflow");
32633278
resize(m);
32643279

32653280
// This is after resize, to ensure that we have completed the allocation
@@ -3272,6 +3287,7 @@ class raw_hash_set {
32723287
const size_t max_size_before_growth =
32733288
is_soo() ? SooCapacity() : size() + growth_left();
32743289
if (n > max_size_before_growth) {
3290+
ABSL_RAW_CHECK(n <= max_size(), "Hash table size overflow");
32753291
size_t m = GrowthToLowerboundCapacity(n);
32763292
resize(NormalizeCapacity(m));
32773293

absl/container/internal/raw_hash_set_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,6 +3594,14 @@ TEST(Iterator, InconsistentHashEqFunctorsValidation) {
35943594
"hash/eq functors are inconsistent.");
35953595
}
35963596

3597+
TEST(Table, MaxSizeOverflow) {
3598+
size_t overflow = (std::numeric_limits<size_t>::max)();
3599+
EXPECT_DEATH_IF_SUPPORTED(IntTable t(overflow), "Hash table size overflow");
3600+
IntTable t;
3601+
EXPECT_DEATH_IF_SUPPORTED(t.reserve(overflow), "Hash table size overflow");
3602+
EXPECT_DEATH_IF_SUPPORTED(t.rehash(overflow), "Hash table size overflow");
3603+
}
3604+
35973605
} // namespace
35983606
} // namespace container_internal
35993607
ABSL_NAMESPACE_END

0 commit comments

Comments
 (0)