Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: make coverity scan ignore random() calls #6251

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion babeld/babel_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ THE SOFTWARE.
#include "vector.h"
#include "distribute.h"
#include "lib_errors.h"
#include "network.h"

#include "babel_main.h"
#include "util.h"
Expand Down Expand Up @@ -1396,7 +1397,7 @@ babel_interface_allocate (void)
/* All flags are unset */
babel_ifp->bucket_time = babel_now.tv_sec;
babel_ifp->bucket = BUCKET_TOKENS_MAX;
babel_ifp->hello_seqno = (random() & 0xFFFF);
babel_ifp->hello_seqno = (frr_weak_random() & 0xFFFF);
babel_ifp->rtt_min = 10000;
babel_ifp->rtt_max = 120000;
babel_ifp->max_rtt_penalty = 150;
Expand Down
3 changes: 2 additions & 1 deletion babeld/babeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ THE SOFTWARE.
#include "filter.h"
#include "plist.h"
#include "lib_errors.h"
#include "network.h"

#include "babel_main.h"
#include "babeld.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ babel_read_protocol (struct thread *thread)
static int
babel_init_routing_process(struct thread *thread)
{
myseqno = (random() & 0xFFFF);
myseqno = (frr_weak_random() & 0xFFFF);
babel_get_myid();
babel_load_state_file();
debugf(BABEL_DEBUG_COMMON, "My ID is : %s.", format_eui64(myid));
Expand Down
6 changes: 4 additions & 2 deletions babeld/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ THE SOFTWARE.
#include <netinet/in.h>
#include <arpa/inet.h>

#include "lib/network.h"

#include "babel_main.h"
#include "babeld.h"
#include "util.h"
Expand All @@ -51,7 +53,7 @@ roughly(int value)
else if(value <= 1)
return value;
else
return value * 3 / 4 + random() % (value / 2);
return value * 3 / 4 + frr_weak_random() % (value / 2);
}

/* d = s1 - s2 */
Expand Down Expand Up @@ -145,7 +147,7 @@ timeval_min_sec(struct timeval *d, time_t secs)
{
if(d->tv_sec == 0 || d->tv_sec > secs) {
d->tv_sec = secs;
d->tv_usec = random() % 1000000;
d->tv_usec = frr_weak_random() % 1000000;
}
}

Expand Down
7 changes: 4 additions & 3 deletions bfdd/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <zebra.h>

#include "lib/jhash.h"
#include "lib/network.h"

#include "bfd.h"

Expand Down Expand Up @@ -236,8 +237,8 @@ static uint32_t ptm_bfd_gen_ID(void)
* random session identification numbers.
*/
do {
session_id = ((random() << 16) & 0xFFFF0000)
| (random() & 0x0000FFFF);
session_id = ((frr_weak_random() << 16) & 0xFFFF0000)
| (frr_weak_random() & 0x0000FFFF);
} while (session_id == 0 || bfd_id_lookup(session_id) != NULL);

return session_id;
Expand All @@ -258,7 +259,7 @@ void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
* between 75% and 90%.
*/
maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100;
jitter = (xmt_TO * (75 + (frr_weak_random() % maxpercent))) / 100;
/* XXX remove that division above */

if (is_echo)
Expand Down
3 changes: 2 additions & 1 deletion bgpd/bgp_routemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "hash.h"
#include "queue.h"
#include "frrstr.h"
#include "network.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
Expand Down Expand Up @@ -1535,7 +1536,7 @@ static enum route_map_cmd_result_t
route_match_probability(void *rule, const struct prefix *prefix,
route_map_object_t type, void *object)
{
long r = random();
long r = frr_weak_random();

switch (*(long *)rule) {
case 0:
Expand Down
3 changes: 2 additions & 1 deletion isisd/isis_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "hash.h"
#include "if.h"
#include "command.h"
#include "network.h"

#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
Expand Down Expand Up @@ -413,7 +414,7 @@ unsigned long isis_jitter(unsigned long timer, unsigned long jitter)
* most IS-IS timers are no longer than 16 bit
*/

j = 1 + (int)((RANDOM_SPREAD * random()) / (RAND_MAX + 1.0));
j = 1 + (int)((RANDOM_SPREAD * frr_weak_random()) / (RAND_MAX + 1.0));

k = timer - (timer * (100 - jitter)) / 100;

Expand Down
3 changes: 2 additions & 1 deletion lib/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "hook.h"
#include "lib_errors.h"
#include "northbound_cli.h"
#include "network.h"

DEFINE_MTYPE_STATIC(LIB, HOST, "Host config")
DEFINE_MTYPE(LIB, COMPLETION, "Completion item")
Expand Down Expand Up @@ -366,7 +367,7 @@ static char *zencrypt(const char *passwd)

gettimeofday(&tv, 0);

to64(&salt[0], random(), 3);
to64(&salt[0], frr_weak_random(), 3);
to64(&salt[3], tv.tv_usec, 3);
salt[5] = '\0';

Expand Down
18 changes: 18 additions & 0 deletions lib/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,21 @@ float ntohf(float net)
{
return htonf(net);
}

/**
* Helper function that returns a random long value. The main purpose of
* this function is to hide a `random()` call that gets flagged by coverity
* scan and put it into one place.
*
* The main usage of this function should be for generating jitter or weak
* random values for simple purposes.
*
* See 'man 3 random' for more information.
*
* \returns random long integer.
*/
long frr_weak_random(void)
{
/* coverity[dont_call] */
return random();
}
2 changes: 2 additions & 0 deletions lib/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern int set_cloexec(int fd);
extern float htonf(float);
extern float ntohf(float);

extern long frr_weak_random(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions lib/qobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "log.h"
#include "qobj.h"
#include "jhash.h"
#include "network.h"

static uint32_t qobj_hash(const struct qobj_node *node)
{
Expand Down Expand Up @@ -53,8 +54,8 @@ void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type)
node->type = type;
pthread_rwlock_wrlock(&nodes_lock);
do {
node->nid = (uint64_t)random();
node->nid ^= (uint64_t)random() << 32;
node->nid = (uint64_t)frr_weak_random();
node->nid ^= (uint64_t)frr_weak_random() << 32;
} while (!node->nid || qobj_nodes_find(&nodes, node));
qobj_nodes_add(&nodes, node);
pthread_rwlock_unlock(&nodes_lock);
Expand Down
3 changes: 2 additions & 1 deletion lib/skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "vty.h"
#include "skiplist.h"
#include "lib_errors.h"
#include "network.h"

DEFINE_MTYPE_STATIC(LIB, SKIP_LIST, "Skip List")
DEFINE_MTYPE_STATIC(LIB, SKIP_LIST_NODE, "Skip Node")
Expand Down Expand Up @@ -95,7 +96,7 @@ static int randomLevel(void)

do {
if (randomsLeft <= 0) {
randomBits = random();
randomBits = frr_weak_random();
randomsLeft = BitsInRandom / 2;
}
b = randomBits & 3;
Expand Down
3 changes: 2 additions & 1 deletion lib/typesafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "typesafe.h"
#include "memory.h"
#include "network.h"

DEFINE_MTYPE_STATIC(LIB, TYPEDHASH_BUCKET, "Typed-hash bucket")
DEFINE_MTYPE_STATIC(LIB, SKIPLIST_OFLOW, "Skiplist overflow")
Expand Down Expand Up @@ -196,7 +197,7 @@ struct sskip_item *typesafe_skiplist_add(struct sskip_head *head,
int cmpval;

/* level / newlevel are 1-counted here */
newlevel = __builtin_ctz(random()) + 1;
newlevel = __builtin_ctz(frr_weak_random()) + 1;
if (newlevel > SKIPLIST_MAXDEPTH)
newlevel = SKIPLIST_MAXDEPTH;

Expand Down
4 changes: 3 additions & 1 deletion ospfd/ospf_lsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "hash.h"
#include "sockunion.h" /* for inet_aton() */
#include "checksum.h"
#include "network.h"

#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
Expand Down Expand Up @@ -3523,7 +3524,8 @@ void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
* 1680s
* and 1740s.
*/
delay = (random() % (max_delay - min_delay)) + min_delay;
delay = (frr_weak_random() % (max_delay - min_delay))
+ min_delay;

current_index = ospf->lsa_refresh_queue.index
+ (monotime(NULL) - ospf->lsa_refresher_started)
Expand Down
3 changes: 2 additions & 1 deletion ospfd/ospf_nsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "table.h"
#include "log.h"
#include "command.h"
#include "network.h"

#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
Expand Down Expand Up @@ -723,7 +724,7 @@ static void nsm_change_state(struct ospf_neighbor *nbr, int state)
/* Start DD exchange protocol */
if (state == NSM_ExStart) {
if (nbr->dd_seqnum == 0)
nbr->dd_seqnum = (uint32_t)random();
nbr->dd_seqnum = (uint32_t)frr_weak_random();
else
nbr->dd_seqnum++;

Expand Down
6 changes: 4 additions & 2 deletions pimd/pim_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "plist.h"
#include "hash.h"
#include "ferr.h"
#include "network.h"

#include "pimd.h"
#include "pim_instance.h"
Expand Down Expand Up @@ -1103,7 +1104,8 @@ int pim_if_t_override_msec(struct interface *ifp)
effective_override_interval_msec =
pim_if_effective_override_interval_msec(ifp);

t_override_msec = random() % (effective_override_interval_msec + 1);
t_override_msec =
frr_weak_random() % (effective_override_interval_msec + 1);

return t_override_msec;
}
Expand Down Expand Up @@ -1181,7 +1183,7 @@ long pim_if_t_suppressed_msec(struct interface *ifp)
return 0;

/* t_suppressed = t_periodic * rand(1.1, 1.4) */
ramount = 1100 + (random() % (1400 - 1100 + 1));
ramount = 1100 + (frr_weak_random() % (1400 - 1100 + 1));
t_suppressed_msec = router->t_periodic * ramount;

return t_suppressed_msec;
Expand Down
3 changes: 2 additions & 1 deletion pimd/pim_pim.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "thread.h"
#include "memory.h"
#include "if.h"
#include "network.h"

#include "pimd.h"
#include "pim_pim.h"
Expand Down Expand Up @@ -878,7 +879,7 @@ int pim_sock_add(struct interface *ifp)
old_genid = pim_ifp->pim_generation_id;

while (old_genid == pim_ifp->pim_generation_id)
pim_ifp->pim_generation_id = random();
pim_ifp->pim_generation_id = frr_weak_random();

zlog_info("PIM INTERFACE UP: on interface %s ifindex=%d", ifp->name,
ifp->ifindex);
Expand Down
3 changes: 2 additions & 1 deletion pimd/pim_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "hash.h"
#include "jhash.h"
#include "wheel.h"
#include "network.h"

#include "pimd.h"
#include "pim_pim.h"
Expand Down Expand Up @@ -1762,7 +1763,7 @@ void pim_upstream_start_register_stop_timer(struct pim_upstream *up,
if (!null_register) {
uint32_t lower = (0.5 * PIM_REGISTER_SUPPRESSION_PERIOD);
uint32_t upper = (1.5 * PIM_REGISTER_SUPPRESSION_PERIOD);
time = lower + (random() % (upper - lower + 1))
time = lower + (frr_weak_random() % (upper - lower + 1))
- PIM_REGISTER_PROBE_PERIOD;
} else
time = PIM_REGISTER_PROBE_PERIOD;
Expand Down
6 changes: 4 additions & 2 deletions ripd/ripd.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "privs.h"
#include "lib_errors.h"
#include "northbound_cli.h"
#include "network.h"

#include "ripd/ripd.h"
#include "ripd/rip_nb.h"
Expand Down Expand Up @@ -2647,7 +2648,7 @@ static int rip_triggered_update(struct thread *t)
random interval between 1 and 5 seconds. If other changes that
would trigger updates occur before the timer expires, a single
update is triggered when the timer expires. */
interval = (random() % 5) + 1;
interval = (frr_weak_random() % 5) + 1;

rip->t_triggered_interval = NULL;
thread_add_timer(master, rip_triggered_interval, rip, interval,
Expand Down Expand Up @@ -2844,7 +2845,8 @@ static int rip_update_jitter(unsigned long time)
if (jitter_input < JITTER_BOUND)
jitter_input = JITTER_BOUND;

jitter = (((random() % ((jitter_input * 2) + 1)) - jitter_input));
jitter = (((frr_weak_random() % ((jitter_input * 2) + 1))
- jitter_input));

return jitter / JITTER_BOUND;
}
Expand Down
5 changes: 3 additions & 2 deletions ripngd/ripngd.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "privs.h"
#include "lib_errors.h"
#include "northbound_cli.h"
#include "network.h"

#include "ripngd/ripngd.h"
#include "ripngd/ripng_route.h"
Expand Down Expand Up @@ -1545,7 +1546,7 @@ int ripng_triggered_update(struct thread *t)
random interval between 1 and 5 seconds. If other changes that
would trigger updates occur before the timer expires, a single
update is triggered when the timer expires. */
interval = (random() % 5) + 1;
interval = (frr_weak_random() % 5) + 1;

ripng->t_triggered_interval = NULL;
thread_add_timer(master, ripng_triggered_interval, ripng, interval,
Expand Down Expand Up @@ -1950,7 +1951,7 @@ int ripng_request(struct interface *ifp)

static int ripng_update_jitter(int time)
{
return ((random() % (time + 1)) - (time / 2));
return ((frr_weak_random() % (time + 1)) - (time / 2));
}

void ripng_event(struct ripng *ripng, enum ripng_event event, int sock)
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/test_checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <time.h>

#include "checksum.h"
#include "network.h"

struct thread_master *master;

Expand Down Expand Up @@ -477,7 +478,7 @@ int main(int argc, char **argv)
exercise %= MAXDATALEN;

for (i = 0; i < exercise; i += sizeof(long int)) {
long int rand = random();
long int rand = frr_weak_random();

for (j = sizeof(long int); j > 0; j--)
buffer[i + (sizeof(long int) - j)] =
Expand Down
Loading