xref: /illumos-gate/usr/src/test/libc-tests/tests/random/arc4random_fork.c (revision eb9a1df2aeb866bf1de4494433b6d7e5fa07b3ae)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Tests to make sure that a parent and child do not get the same arc4random
18  * state across a fork. This source file is used to make two tests. One which
19  * initializes the data in advance, one of which does not.
20  */
21 
22 #include <stdlib.h>
23 #include <sys/mman.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <sys/wait.h>
29 
30 typedef struct arc4_fork {
31 	uint32_t	af_parent;
32 	uint32_t	af_child;
33 	uint8_t		af_pbuf[4096];
34 	uint8_t		af_cbuf[4096];
35 } arc4_fork_t;
36 
37 arc4_fork_t *fork_data;
38 
39 int
40 main(void)
41 {
42 	int e, i;
43 	pid_t p, child;
44 
45 #ifdef	ARC4_PREINIT
46 	(void) arc4random();
47 #endif
48 
49 	fork_data = (arc4_fork_t *)mmap(NULL, sizeof (arc4_fork_t),
50 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
51 	assert(fork_data != MAP_FAILED);
52 
53 	p = fork();
54 	assert(p != -1);
55 	if (p == 0) {
56 		fork_data->af_child = arc4random();
57 		arc4random_buf(fork_data->af_cbuf, sizeof (fork_data->af_cbuf));
58 		exit(0);
59 	}
60 
61 	fork_data->af_parent = arc4random();
62 	arc4random_buf(fork_data->af_pbuf, sizeof (fork_data->af_pbuf));
63 	do {
64 		child = wait(&e);
65 	} while (child == -1 && errno == EINTR);
66 	assert(child == p);
67 
68 	/* Now verify our data doesn't match */
69 	assert(fork_data->af_parent != fork_data->af_child);
70 
71 	/*
72 	 * For the buffer here, we're mostly concerned that they aren't somehow
73 	 * getting the same stream.
74 	 */
75 	for (i = 0; i < sizeof (fork_data->af_pbuf); i++) {
76 		if (fork_data->af_pbuf[i] != fork_data->af_cbuf[i])
77 			break;
78 	}
79 	assert(i != sizeof (fork_data->af_pbuf));
80 
81 	return (0);
82 }
83