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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* Copyright (c) 2014 Google Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/mman.h>
#include <sys/wait.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#define CHECK(x) assert(x)
#define CHECK_EQ(a, b) assert((a) == (b))
#define CHECK_NE(a, b) assert((a) != (b))
#define CHECK_GE(a, b) assert((a) >= (b))
#define CHECK_LE(a, b) assert((a) <= (b))
/* Test arc4random_buf(3) instead of arc4random(3). */
static int flagbuf;
/* Initialize arc4random(3) before forking. */
static int flagprefork;
enum {
N = 4096
};
typedef struct {
uint32_t x[N];
} Buf;
static int
isfullbuf(const Buf *buf)
{
size_t i;
for (i = 0; i < N; i++)
if (buf->x[i])
return (1);
return (0);
}
static void
fillbuf(Buf *buf)
{
if (flagbuf) {
arc4random_buf(buf->x, sizeof(buf->x));
} else {
size_t i;
for (i = 0; i < N; i++)
buf->x[i] = arc4random();
}
}
static void
usage()
{
errx(1, "usage: arc4random-fork [-bp]");
}
static pid_t
safewaitpid(pid_t pid, int *status, int options)
{
pid_t ret;
do {
ret = waitpid(pid, status, options);
} while (ret == -1 && errno == EINTR);
return (ret);
}
int
main(int argc, char *argv[])
{
int opt, status;
Buf *bufparent, *bufchildone, *bufchildtwo;
pid_t pidone, pidtwo;
size_t i, countone = 0, counttwo = 0, countkids = 0;
/* Ensure SIGCHLD isn't set to SIG_IGN. */
const struct sigaction sa = {
.sa_handler = SIG_DFL,
};
CHECK_EQ(0, sigaction(SIGCHLD, &sa, NULL));
while ((opt = getopt(argc, argv, "bp")) != -1) {
switch (opt) {
case 'b':
flagbuf = 1;
break;
case 'p':
flagprefork = 1;
break;
default:
usage();
}
}
if (flagprefork)
arc4random();
bufparent = mmap(NULL, sizeof(Buf), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
CHECK_NE(MAP_FAILED, bufparent);
bufchildone = mmap(NULL, sizeof(Buf), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED, -1, 0);
CHECK_NE(MAP_FAILED, bufchildone);
bufchildtwo = mmap(NULL, sizeof(Buf), PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED, -1, 0);
CHECK_NE(MAP_FAILED, bufchildtwo);
pidone = fork();
CHECK_GE(pidone, 0);
if (pidone == 0) {
fillbuf(bufchildone);
_exit(0);
}
pidtwo = fork();
CHECK_GE(pidtwo, 0);
if (pidtwo == 0) {
fillbuf(bufchildtwo);
_exit(0);
}
fillbuf(bufparent);
CHECK_EQ(pidone, safewaitpid(pidone, &status, 0));
CHECK(WIFEXITED(status));
CHECK_EQ(0, WEXITSTATUS(status));
CHECK_EQ(pidtwo, safewaitpid(pidtwo, &status, 0));
CHECK(WIFEXITED(status));
CHECK_EQ(0, WEXITSTATUS(status));
CHECK(isfullbuf(bufchildone));
CHECK(isfullbuf(bufchildtwo));
for (i = 0; i < N; i++) {
countone += bufparent->x[i] == bufchildone->x[i];
counttwo += bufparent->x[i] == bufchildtwo->x[i];
countkids += bufchildone->x[i] == bufchildtwo->x[i];
}
/*
* These checks are inherently probabilistic and theoretically risk
* flaking, but there's less than a 1 in 2^40 chance of more than
* one pairwise match between two vectors of 4096 32-bit integers.
*/
CHECK_LE(countone, 1);
CHECK_LE(counttwo, 1);
CHECK_LE(countkids, 1);
return (0);
}
|