From 5073882e0f0ea114e4f0efe719a72ecf7b168926 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 10 Apr 2022 21:44:12 +0000 Subject: [PATCH] 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. --- other/bootstrap_daemon/docker/tox-bootstrapd.sha256 | 2 +- toxcore/DHT.c | 5 +++-- toxcore/crypto_core.c | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 750f59c1..f16b2a07 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -ea227a21dcaed2f54d61bd9175c6deb02480ebd894ebd589061556a1708c0c9f /usr/local/bin/tox-bootstrapd +624c610327a1288eb58196fb0e93d98d5a3c01ad86835799b90c1936fcbbc156 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/DHT.c b/toxcore/DHT.c index fd7ca57e..22053700 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -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); diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index ccfda08d..7d9ed020 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -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;