Skip to content

Commit 5073882

Browse files
committed
fix: Allow onion paths to be built from more random nodes.
Right now it only gets built from the first 2 friends in the DHT friend list: either friend 0 and then 1 or friend 1 and then 0. The randomness in this code doesn't make sense unless the intention was to select from all friends, which the code will now do. Also: use uniform random distribution to select the friends rather than modulus, which is only uniform for powers of 2.
1 parent 27c27b7 commit 5073882

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

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

toxcore/DHT.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,11 +2598,12 @@ uint16_t randfriends_nodes(const DHT *dht, Node_format *nodes, uint16_t max_num)
25982598
return 0;
25992599
}
26002600

2601+
assert(dht->num_friends >= DHT_FAKE_FRIEND_NUMBER);
2602+
const uint32_t r = random_range_u32(dht->rng, dht->num_friends - DHT_FAKE_FRIEND_NUMBER);
26012603
uint16_t count = 0;
2602-
const uint32_t r = random_u32(dht->rng);
26032604

26042605
for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2605-
count += list_nodes(dht->rng, dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list,
2606+
count += list_nodes(dht->rng, dht->friends_list[r + i].client_list,
26062607
MAX_FRIEND_CLIENTS, dht->cur_time,
26072608
nodes + count, max_num - count);
26082609

toxcore/crypto_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ non_null()
501501
static uint32_t sys_random_uniform(void *obj, uint32_t upper_bound)
502502
{
503503
#ifdef VANILLA_NACL
504+
if (upper_bound == 0) {
505+
return 0;
506+
}
507+
504508
uint32_t randnum;
505509
sys_random_bytes(obj, (uint8_t *)&randnum, sizeof(randnum));
506510
return randnum % upper_bound;

0 commit comments

Comments
 (0)