Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Commit 064c460

Browse files
committed
Get random bytes without opening a file
1 parent e1ed051 commit 064c460

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/tor/shadowtor-preload.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <assert.h>
88
#include <fcntl.h>
99
#include <string.h>
10+
#include <sys/syscall.h>
1011
#include <unistd.h>
1112

1213
#include <event2/dns.h>
@@ -145,15 +146,9 @@ int RAND_poll() {
145146
}
146147

147148
static int _shadowtorpreload_getRandomBytes(unsigned char* buf, int numBytes) {
148-
int bytesWritten = 0;
149-
150-
/* shadow interposes this and will fill the buffer for us */
151-
int fd = open("/dev/random", O_RDONLY);
152-
int res = read(fd, buf, (size_t)numBytes);
153-
assert(res > 0);
154-
close(fd);
155-
156-
return 1;
149+
// shadow interposes this and will fill the buffer for us
150+
// return 1 on success, 0 otherwise
151+
return (numBytes == syscall(SYS_getrandom, buf, (size_t)numBytes, 0)) ? 1 : 0;
157152
}
158153

159154
int RAND_bytes(unsigned char *buf, int num) {

0 commit comments

Comments
 (0)