Skip to content

Commit d78ee9b

Browse files
committed
fix: Fix potential array out-of-bounds in DHT random node retrieval.
It can't happen in almost every reality, except when the RNG is fairly broken and doesn't add 2 fake DHT friends on startup. Still, this code should be defensive and never index outside `num_friends` elements.
1 parent 60b71ad commit d78ee9b

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
624c610327a1288eb58196fb0e93d98d5a3c01ad86835799b90c1936fcbbc156 /usr/local/bin/tox-bootstrapd
1+
bded6f7ca320d8dfcb123a02c2c06aa9615b0e29e1d1d5b33b94bf88e85524d3 /usr/local/bin/tox-bootstrapd

testing/fuzzing/bootstrap_harness.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
124124

125125
Tox_Err_New error_new;
126126
Tox *tox = tox_new(opts, &error_new);
127+
tox_options_free(opts);
127128

128129
if (tox == nullptr) {
129130
// It might fail, because some I/O happens in tox_new, and the fuzzer
@@ -133,8 +134,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
133134

134135
assert(error_new == TOX_ERR_NEW_OK);
135136

136-
tox_options_free(opts);
137-
138137
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};
139138

140139
const bool success = tox_bootstrap(tox, "127.0.0.1", 12345, pub_key, nullptr);

toxcore/DHT.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,7 @@ uint16_t randfriends_nodes(const DHT *dht, Node_format *nodes, uint16_t max_num)
26022602
const uint32_t r = random_range_u32(dht->rng, dht->num_friends - DHT_FAKE_FRIEND_NUMBER);
26032603
uint16_t count = 0;
26042604

2605-
for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2605+
for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) {
26062606
count += list_nodes(dht->rng, dht->friends_list[r + i].client_list,
26072607
MAX_FRIEND_CLIENTS, dht->cur_time,
26082608
nodes + count, max_num - count);
@@ -2766,6 +2766,12 @@ DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time
27662766
}
27672767
}
27682768

2769+
if (dht->num_friends != DHT_FAKE_FRIEND_NUMBER) {
2770+
LOGGER_ERROR(log, "the RNG provided seems to be broken: it generated the same keypair twice");
2771+
kill_dht(dht);
2772+
return nullptr;
2773+
}
2774+
27692775
return dht;
27702776
}
27712777

0 commit comments

Comments
 (0)