Skip to content

Commit

Permalink
mlx5: Use getrandom() to seed a strong PRNG
Browse files Browse the repository at this point in the history
Add get_random() API to get random data with static seed, which is
generated with getrandom() for better randomness.

Signed-off-by: Mark Zhang <[email protected]>
Signed-off-by: Yishai Hadas <[email protected]>
  • Loading branch information
MarkZhang81 authored and Yishai Hadas committed Oct 15, 2020
1 parent 3932eff commit 1927017
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ if (NOT NL_KIND EQUAL 0)
endif()
RDMA_AddOptCFlag(CMAKE_C_FLAGS HAVE_C_WREDUNDANT_DECLS "-Wredundant-decls")

# Support of getrandom() was added to glibc in version 2.25
CHECK_C_SOURCE_COMPILES("
#include <sys/random.h>
int main(int argc,const char *argv[]) {char buf[64]; return getrandom(buf, 64, GRND_NONBLOCK);}"
HAVE_GLIBC_GETRANDOM)
RDMA_DoFixup("${HAVE_GLIBC_GETRANDOM}" "sys/random.h")

#-------------------------
# Build Prep
# Write out a git ignore file to the build directory if it isn't the source
Expand Down
13 changes: 13 additions & 0 deletions buildlib/fixup-include/sys-random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _FIXUP_SYS_RANDOM_H
#define _FIXUP_SYS_RANDOM_H

#include <sys/types.h>

/* Flags for use with getrandom. */
#define GRND_NONBLOCK 0x01

static inline ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
{
return -1;
}
#endif
5 changes: 3 additions & 2 deletions providers/mlx5/verbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include <util/compiler.h>
#include <util/mmio.h>
#include <util/util.h>
#include <rdma/ib_user_ioctl_cmds.h>
#include <rdma/mlx5_user_ioctl_cmds.h>
#include <infiniband/cmd_write.h>
Expand Down Expand Up @@ -2905,8 +2906,8 @@ static void mlx5_ah_set_udp_sport(struct mlx5_ah *ah,
if (fl)
sport = ibv_flow_label_to_udp_sport(fl);
else
sport = rand() % (IB_ROCE_UDP_ENCAP_VALID_PORT_MAX + 1
- IB_ROCE_UDP_ENCAP_VALID_PORT_MIN)
sport = get_random() % (IB_ROCE_UDP_ENCAP_VALID_PORT_MAX + 1
- IB_ROCE_UDP_ENCAP_VALID_PORT_MIN)
+ IB_ROCE_UDP_ENCAP_VALID_PORT_MIN;

ah->av.rlid = htobe16(sport);
Expand Down
25 changes: 25 additions & 0 deletions util/util.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* GPLv2 or OpenIB.org BSD (MIT) See COPYING file */
#include <stdlib.h>
#include <sys/random.h>
#include <sys/types.h>
#include <time.h>
#include <util/util.h>
#include <unistd.h>
#include <fcntl.h>
Expand All @@ -20,3 +24,24 @@ int set_fd_nonblock(int fd, bool nonblock)
return -1;
return 0;
}

#ifndef GRND_INSECURE
#define GRND_INSECURE 0x0004
#endif
unsigned int get_random(void)
{
static unsigned int seed;
ssize_t sz;

if (!seed) {
sz = getrandom(&seed, sizeof(seed),
GRND_NONBLOCK | GRND_INSECURE);
if (sz < 0)
sz = getrandom(&seed, sizeof(seed), GRND_NONBLOCK);

if (sz != sizeof(seed))
seed = time(NULL);
}

return rand_r(&seed);
}
1 change: 1 addition & 0 deletions util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ int set_fd_nonblock(int fd, bool nonblock);

int open_cdev(const char *devname_hint, dev_t cdev);

unsigned int get_random(void);
#endif

0 comments on commit 1927017

Please sign in to comment.