Skip to content

Commit cead683

Browse files
authored
Merge pull request iqiyi#938 from donghaobo/random
use better random source
2 parents 3ec1d59 + 180e1fb commit cead683

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/ipvs/ip_vs_synproxy.c

+26-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*
1717
*/
1818
#include <assert.h>
19+
#include <sys/types.h>
20+
#include <sys/stat.h>
21+
#include <fcntl.h>
1922
#include <netinet/ip.h>
2023
#include <netinet/tcp.h>
2124
#include <openssl/md5.h>
@@ -117,15 +120,35 @@ static int second_timer_expire(void *priv)
117120
}
118121
#endif
119122

123+
static int generate_random_key(void *key, unsigned length)
124+
{
125+
int fd;
126+
int ret;
127+
128+
fd = open("/dev/urandom", O_RDONLY);
129+
if (fd < 0) {
130+
return -1;
131+
}
132+
ret = read(fd, key, length);
133+
close(fd);
134+
135+
if (ret != (signed)length) {
136+
return -1;
137+
}
138+
return 0;
139+
}
140+
120141
int dp_vs_synproxy_init(void)
121142
{
122143
int i;
123144
char ack_mbufpool_name[32];
124145
struct timeval tv;
125146

126-
for (i = 0; i < MD5_LBLOCK; i++) {
127-
g_net_secret[0][i] = (uint32_t)random();
128-
g_net_secret[1][i] = (uint32_t)random();
147+
if (generate_random_key(g_net_secret, sizeof(g_net_secret))) {
148+
for (i = 0; i < MD5_LBLOCK; i++) {
149+
g_net_secret[0][i] = (uint32_t)random();
150+
g_net_secret[1][i] = (uint32_t)random();
151+
}
129152
}
130153

131154
rte_atomic32_set(&g_minute_count, (uint32_t)random());

0 commit comments

Comments
 (0)