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

Commit

Permalink
Get random bytes without opening a file
Browse files Browse the repository at this point in the history
  • Loading branch information
robgjansen committed Mar 15, 2021
1 parent e1ed051 commit 064c460
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/tor/shadowtor-preload.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <event2/dns.h>
Expand Down Expand Up @@ -145,15 +146,9 @@ int RAND_poll() {
}

static int _shadowtorpreload_getRandomBytes(unsigned char* buf, int numBytes) {
int bytesWritten = 0;

/* shadow interposes this and will fill the buffer for us */
int fd = open("/dev/random", O_RDONLY);
int res = read(fd, buf, (size_t)numBytes);
assert(res > 0);
close(fd);

return 1;
// shadow interposes this and will fill the buffer for us
// return 1 on success, 0 otherwise
return (numBytes == syscall(SYS_getrandom, buf, (size_t)numBytes, 0)) ? 1 : 0;
}

int RAND_bytes(unsigned char *buf, int num) {
Expand Down

0 comments on commit 064c460

Please sign in to comment.