From df2508d7a84bb31260c2dc480e5a693ed77cc748 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sat, 13 Sep 2014 16:27:52 +0300 Subject: [PATCH] P2P: Check os_get_random() return value more consistently In theory, this call could fail, so check the return value before using the received data. These specific cases would not really care much about the failures, but this keeps the code more consistent and keeps static analyzer warnings more useful. (CID 72678, CID 72679, CID 72680, CID 72683, CID 72689, CID 72698, CID 72703) Signed-off-by: Jouni Malinen --- src/p2p/p2p.c | 9 ++++++--- src/p2p/p2p_utils.c | 3 ++- wpa_supplicant/p2p_supplicant.c | 24 ++++++++++++++++-------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/p2p/p2p.c b/src/p2p/p2p.c index 565cbdbd2..e746310e5 100644 --- a/src/p2p/p2p.c +++ b/src/p2p/p2p.c @@ -259,7 +259,8 @@ static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc) return; } - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + r = 0; tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + p2p->min_disc_int) * 100; if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu) @@ -2480,7 +2481,8 @@ struct p2p_data * p2p_init(const struct p2p_config *cfg) p2p->max_disc_int = 3; p2p->max_disc_tu = -1; - os_get_random(&p2p->next_tie_breaker, 1); + if (os_get_random(&p2p->next_tie_breaker, 1) < 0) + p2p->next_tie_breaker = 0; p2p->next_tie_breaker &= 0x01; if (cfg->sd_request) p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; @@ -3003,7 +3005,8 @@ static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) * make it less likely to hit cases where we could end up in * sync with peer not listening. */ - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + r = 0; timeout += r % 100000; } p2p_set_timeout(p2p, 0, timeout); diff --git a/src/p2p/p2p_utils.c b/src/p2p/p2p_utils.c index 189300adf..508a60943 100644 --- a/src/p2p/p2p_utils.c +++ b/src/p2p/p2p_utils.c @@ -441,7 +441,8 @@ void p2p_channels_dump(struct p2p_data *p2p, const char *title, static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels) { unsigned int r; - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + r = 0; r %= num_channels; return channels[r]; } diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c index 5d63de332..e1a384b93 100644 --- a/wpa_supplicant/p2p_supplicant.c +++ b/wpa_supplicant/p2p_supplicant.c @@ -3927,7 +3927,8 @@ int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) * Pick one of the social channels randomly as the listen * channel. */ - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + return -1; p2p.channel = 1 + (r % 3) * 5; p2p.channel_forced = 0; } @@ -3947,7 +3948,8 @@ int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) * Use random operation channel from (1, 6, 11) if no other * preference is indicated. */ - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + return -1; p2p.op_channel = 1 + (r % 3) * 5; p2p.cfg_op_channel = 0; wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " @@ -5030,7 +5032,8 @@ static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " "channel: %d MHz", freq); } else { - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + return -1; freq = 2412 + (r % 3) * 25; wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " "channel: %d MHz", freq); @@ -5047,7 +5050,8 @@ static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " "channel: %d MHz", freq); } else { - os_get_random((u8 *) &r, sizeof(r)); + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + return -1; freq = 5180 + (r % 4) * 20; if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { wpa_printf(MSG_DEBUG, "P2P: Could not select " @@ -6329,8 +6333,10 @@ void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) * Pick one of the social channels randomly as the * listen channel. */ - os_get_random((u8 *) &r, sizeof(r)); - channel = 1 + (r % 3) * 5; + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + channel = 1; + else + channel = 1 + (r % 3) * 5; channel_forced = 0; } ret = p2p_set_listen_channel(p2p, reg_class, channel, @@ -6354,8 +6360,10 @@ void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) * Use random operation channel from (1, 6, 11) *if no other preference is indicated. */ - os_get_random((u8 *) &r, sizeof(r)); - op_channel = 1 + (r % 3) * 5; + if (os_get_random((u8 *) &r, sizeof(r)) < 0) + op_channel = 1; + else + op_channel = 1 + (r % 3) * 5; cfg_op_channel = 0; } ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,