Hash :
eb26e56b
Author :
Date :
2014-10-22T13:06:36
undef LIBRESSL_INTERNAL for the pidwrap test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
* Checks if LibreSSL's PRNG is fork-safe.
* From https://www.agwa.name/blog/post/libressls_prng_is_unsafe_on_linux
* This code is in the public domain.
*
* Original source: https://gist.github.com/AGWA/eb84e55ca25a7da1deb0
*/
#undef LIBRESSL_INTERNAL
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
static void random_bytes (unsigned char* p, size_t len)
{
if (RAND_bytes(p, len) != 1) {
fprintf(stderr, "RAND_bytes failed\n");
abort();
}
}
static void random_stir (void)
{
if (RAND_poll() != 1) {
fprintf(stderr, "RAND_poll failed\n");
abort();
}
}
static void print_buffer (unsigned char* p, size_t len)
{
while (len--) {
printf("%02x", (unsigned int)*p++);
}
}
int main ()
{
char c = 0;
int pipefd[2];
pipe(pipefd);
setbuf(stdout, NULL);
if (fork() == 0) {
unsigned char buffer[32];
pid_t grandparent_pid = getpid();
random_bytes(buffer, sizeof(buffer));
if (fork() == 0) {
random_stir();
setsid();
while (1) {
pid_t grandchild_pid = fork();
if (grandchild_pid == 0) {
random_stir();
if (getpid() == grandparent_pid) {
random_bytes(buffer, sizeof(buffer));
print_buffer(buffer, sizeof(buffer));
printf("\n");
}
_exit(0);
}
wait(NULL);
if (grandchild_pid == grandparent_pid) {
break;
}
}
write(pipefd[1], &c, 1);
_exit(0);
}
random_bytes(buffer, sizeof(buffer));
print_buffer(buffer, sizeof(buffer));
printf(" ");
_exit(0);
}
wait(NULL);
close(pipefd[1]);
read(pipefd[0], &c, 1);
return 0;
}