Skip to content

Commit

Permalink
wrap rand() in functions explicitly marked as non-secure
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Dec 30, 2023
1 parent 66ebe10 commit 862dc64
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,16 +624,18 @@ int load_DHT_nodeslist(void)
static void DHT_bootstrap(Tox *m)
{
pthread_mutex_lock(&thread_data.lock);
const size_t num_nodes = Nodes.count;
size_t num_nodes = Nodes.count;
pthread_mutex_unlock(&thread_data.lock);

if (num_nodes == 0) {
return;
}

size_t i;

pthread_mutex_lock(&thread_data.lock);

for (size_t i = 0; i < NUM_BOOTSTRAP_NODES; ++i) {
for (i = 0; i < NUM_BOOTSTRAP_NODES; ++i) {
struct Node *node = &Nodes.list[rand() % Nodes.count];
const char *addr = node->have_ip4 ? node->ip4 : node->ip6;

Expand Down
4 changes: 2 additions & 2 deletions src/game_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,8 @@ void game_random_coords(const GameData *game, Coords *coords)
const int y_top_bound = ((max_y - game_max_y) / 2) + 1;
const int y_bottom_bound = ((max_y + game_max_y) / 2) - 1;

coords->x = (rand() % (x_right_bound - x_left_bound + 1)) + x_left_bound;
coords->y = (rand() % (y_bottom_bound - y_top_bound + 1)) + y_top_bound;
coords->x = (int)rand_range_not_secure(x_right_bound - x_left_bound + 1) + x_left_bound;
coords->y = (int)rand_range_not_secure(y_bottom_bound - y_top_bound + 1) + y_top_bound;
}

void game_max_x_y(const GameData *game, int *x, int *y)
Expand Down
33 changes: 17 additions & 16 deletions src/game_centipede.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static size_t cent_enemy_agent_speed(size_t base_speed, size_t level)
return base_speed;
}

int r = rand() % (level / 2);
const unsigned int r = rand_range_not_secure(level / 2);

return MIN(base_speed + r, CENT_MAX_ENEMY_AGENT_SPEED);
}
Expand Down Expand Up @@ -520,7 +520,7 @@ static int cent_birth_centipede(const GameData *game, CentState *state, size_t l

static int cent_init_level_centipedes(const GameData *game, CentState *state, size_t level)
{
Direction dir = rand() % 2 == 0 ? WEST : EAST;
Direction dir = rand_range_not_secure(2) == 0 ? WEST : EAST;

// First level we spawn one full size centipede
if (level == 1) {
Expand Down Expand Up @@ -1093,11 +1093,11 @@ static void cent_do_reproduce(const GameData *game, CentState *state, Segment *h
return;
}

Direction dir = rand() % 2 == 0 ? WEST : EAST;
const Direction dir = rand_range_not_secure(2) == 0 ? WEST : EAST;

Coords new_coords;
new_coords.x = dir == EAST ? x_left : x_right;
new_coords.y = y_bottom - (rand() % CENT_INVISIBLE_H_WALL);
new_coords.y = MAX(0, y_bottom - (int)rand_range_not_secure(CENT_INVISIBLE_H_WALL));

if (cent_birth_centipede(game, state, 0, dir, &new_coords) == 0) {
head->last_time_reproduced = get_unix_time();
Expand Down Expand Up @@ -1151,7 +1151,7 @@ static void cent_try_spawn_flea(const GameData *game, EnemyAgent *flea)

flea->was_killed = false;

if (rand() % 4 == 0) {
if (rand_range_not_secure(4) == 0) {
return;
}

Expand All @@ -1169,7 +1169,7 @@ static void cent_try_spawn_flea(const GameData *game, EnemyAgent *flea)
const int x_right = game_x_right_bound(game);

flea->coords.y = y_top;
flea->coords.x = (rand() % (x_right - x_left + 1)) + x_left;
flea->coords.x = (int)rand_range_not_secure(x_right - x_left + 1) + x_left;
}

static void cent_do_flea(GameData *game, CentState *state, TIME_MS cur_time)
Expand All @@ -1189,7 +1189,7 @@ static void cent_do_flea(GameData *game, CentState *state, TIME_MS cur_time)

const int y_bottom = game_y_bottom_bound(game);

if (flea->coords.y < (y_bottom - 5) && rand() % 4 == 0) {
if (flea->coords.y < y_bottom - 5 && rand_range_not_secure(4) == 0) {
cent_mushroom_grow(game, state, &flea->coords, false);
}

Expand All @@ -1215,7 +1215,7 @@ static bool cent_scorpion_spawn_check(EnemyAgent *scorpion, size_t level)

scorpion->last_time_despawned = get_unix_time();

return (rand() % 4) < 3;
return rand_range_not_secure(4) < 3;
}

static void cent_try_spawn_scorpion(const GameData *game, CentState *state, EnemyAgent *scorpion)
Expand All @@ -1235,7 +1235,7 @@ static void cent_try_spawn_scorpion(const GameData *game, CentState *state, Enem
scorpion->attributes = CENT_SCORPION_DEFAULT_ATTR;
scorpion->display_char = CENT_SCORPTION_CHAR;
scorpion->health = CENT_SCORPTION_START_HEALTH;
scorpion->direction = rand() % 2 == 0 ? WEST : EAST;
scorpion->direction = rand_range_not_secure(2) == 0 ? WEST : EAST;

const int y_bottom = game_y_bottom_bound(game);
const int x_left = game_x_left_bound(game);
Expand All @@ -1244,7 +1244,7 @@ static void cent_try_spawn_scorpion(const GameData *game, CentState *state, Enem
const int y_mid = y_top + ((y_bottom - y_top) / 2);

scorpion->coords.x = scorpion->direction == WEST ? x_right : x_left;
scorpion->coords.y = (y_mid - 5) + (rand() % 5);
scorpion->coords.y = (y_mid - 5) + (int)rand_range_not_secure(5);
}

static void cent_do_scorpion(GameData *game, CentState *state, TIME_MS cur_time)
Expand Down Expand Up @@ -1286,7 +1286,7 @@ static void cent_try_spawn_spider(const GameData *game, EnemyAgent *spider)
return;
}

if (rand() % 4 == 0) {
if (rand_range_not_secure(4) == 0) {
spider->last_time_despawned = get_unix_time();
return;
}
Expand All @@ -1297,7 +1297,7 @@ static void cent_try_spawn_spider(const GameData *game, EnemyAgent *spider)
spider->speed = cent_enemy_agent_speed(CENT_SPIDER_DEFAULT_SPEED, level);
spider->attributes = CENT_SPIDER_DEFAULT_ATTR;
spider->display_char = CENT_SPIDER_CHAR;
spider->start_direction = rand() % 2 == 0 ? WEST : EAST;
spider->start_direction = rand_range_not_secure(2) == 0 ? WEST : EAST;
spider->direction = spider->start_direction;
spider->health = CENT_SPIDER_START_HEALTH;

Expand All @@ -1306,7 +1306,8 @@ static void cent_try_spawn_spider(const GameData *game, EnemyAgent *spider)
const int x_right = game_x_right_bound(game);

spider->coords.x = spider->direction == WEST ? x_right : x_left;
spider->coords.y = (rand() % (y_bottom - (y_bottom - CENT_INVISIBLE_H_WALL))) + (y_bottom - CENT_INVISIBLE_H_WALL);
spider->coords.y = (int)rand_range_not_secure(y_bottom - (y_bottom - CENT_INVISIBLE_H_WALL))
+ (y_bottom - CENT_INVISIBLE_H_WALL);
}

static void cent_do_spider(GameData *game, CentState *state, TIME_MS cur_time)
Expand All @@ -1329,7 +1330,7 @@ static void cent_do_spider(GameData *game, CentState *state, TIME_MS cur_time)
spider->coords.y,
};

int r = rand();
const unsigned int r = rand_not_secure();

if (spider->direction == spider->start_direction) {
if (r % 4 == 0) {
Expand Down Expand Up @@ -1634,7 +1635,7 @@ static void cent_populate_mushrooms(const GameData *game, CentState *state, int
const int y_floor_bound = game_y_bottom_bound(game);

for (size_t i = 0; i < CENT_MUSHROOMS_LENGTH; ++i) {
if (rand() % population_const != 0) {
if (rand_range_not_secure(population_const) != 0) {
continue;
}

Expand Down Expand Up @@ -1674,7 +1675,7 @@ static int cent_init_state(GameData *game, CentState *state)
Centipedes *centipedes = &state->centipedes;
memset(centipedes->heads, 0, sizeof(centipedes->heads));

Direction dir = rand() % 2 == 0 ? WEST : EAST;
Direction dir = rand_range_not_secure(2) == 0 ? WEST : EAST;

if (cent_birth_centipede(game, state, CENT_MAX_NUM_SEGMENTS, dir, NULL) == -1) {
free(mushrooms);
Expand Down
2 changes: 1 addition & 1 deletion src/game_chess.c
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ int chess_initialize(GameData *game, const uint8_t *init_data, size_t length, bo
return -3;
}

bool self_is_white = rand() % 2 == 0;
bool self_is_white = rand_range_not_secure(2) == 0;

if (!self_host) {
if (length != CHESS_PACKET_SEND_INVITE_LENGTH) {
Expand Down
5 changes: 3 additions & 2 deletions src/game_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdint.h>

#include "game_util.h"
#include "misc_tools.h"
#include "toxic.h"
#include "windows.h"

Expand Down Expand Up @@ -80,7 +81,7 @@ Direction game_util_move_towards(const Coords *coords_a, const Coords *coords_b,

Direction game_util_random_direction(void)
{
int r = rand() % 4;
const unsigned int r = rand_range_not_secure(4);

switch (r) {
case 0:
Expand Down Expand Up @@ -137,7 +138,7 @@ void game_util_move_coords(Direction direction, Coords *coords)

int game_util_random_colour(void)
{
int r = rand() % 6;
const unsigned int r = rand_range_not_secure(6);

switch (r) {
case 0:
Expand Down
6 changes: 3 additions & 3 deletions src/global_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ void cmd_game(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
return;
}

uint32_t id = rand();
int ret = game_initialize(self, m, type, id, NULL, 0, true);
const unsigned int id = rand_not_secure();
const int ret = game_initialize(self, m, type, (uint32_t)id, NULL, 0, true);

switch (ret) {
case 0: {
Expand Down Expand Up @@ -798,7 +798,7 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA

void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
long int nospam = rand();
long int nospam = (long int)rand_not_secure(); // the nospam isn't cryptographically sensitive

if (argc > 0) {
nospam = strtol(argv[1], NULL, 16);
Expand Down
14 changes: 14 additions & 0 deletions src/misc_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,17 @@ void **malloc_ptr_array(size_t length, size_t bytes)

return arr;
}

unsigned int rand_range_not_secure(unsigned int upper_bound)
{
const unsigned int n = (unsigned int)rand();

return n % MIN(RAND_MAX, upper_bound);
}

unsigned int rand_not_secure(void)
{
const unsigned int n = (unsigned int)rand();

return n;
}
15 changes: 15 additions & 0 deletions src/misc_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,19 @@ void free_ptr_array(void **arr);
*/
void **malloc_ptr_array(size_t length, size_t bytes);

/*
* Returns a non-cryptographically secure random unsigned integer between zero and `upper_bound`
* which is limited by RAND_MAX.
*
* This function should only be used for non-crypto related things.
*/
unsigned int rand_range_not_secure(unsigned int upper_bound);

/*
* Returns a non-cryptographically secure random unsigned integer between zero and RAND_MAX.
*
* This function should only be used for non-crypto related things.
*/
unsigned int rand_not_secure(void);

#endif /* MISC_TOOLS_H */

0 comments on commit 862dc64

Please sign in to comment.