Skip to content

Commit

Permalink
fix: Allow onion paths to be built from more random nodes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iphydf committed Apr 10, 2022
1 parent 27c27b7 commit 5073882
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ea227a21dcaed2f54d61bd9175c6deb02480ebd894ebd589061556a1708c0c9f /usr/local/bin/tox-bootstrapd
624c610327a1288eb58196fb0e93d98d5a3c01ad86835799b90c1936fcbbc156 /usr/local/bin/tox-bootstrapd
5 changes: 3 additions & 2 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -2598,11 +2598,12 @@ uint16_t randfriends_nodes(const DHT *dht, Node_format *nodes, uint16_t max_num)
return 0;
}

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

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

Expand Down
4 changes: 4 additions & 0 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ non_null()
static uint32_t sys_random_uniform(void *obj, uint32_t upper_bound)
{
#ifdef VANILLA_NACL
if (upper_bound == 0) {
return 0;
}

uint32_t randnum;
sys_random_bytes(obj, (uint8_t *)&randnum, sizeof(randnum));
return randnum % upper_bound;
Expand Down

0 comments on commit 5073882

Please sign in to comment.