summaryrefslogtreecommitdiff
path: root/usr/src/test/libc-tests/tests/random/arc4random_fork.c
blob: 88cd2adb41f785bc5dfb8e89e495843ba0d6f7da (plain)
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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright (c) 2015, Joyent, Inc.
 */

/*
 * Tests to make sure that a parent and child do not get the same arc4random
 * state across a fork. This source file is used to make two tests. One which
 * initializes the data in advance, one of which does not.
 */

#include <stdlib.h>
#include <sys/mman.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

typedef struct arc4_fork {
	uint32_t	af_parent;
	uint32_t	af_child;
	uint8_t		af_pbuf[4096];
	uint8_t		af_cbuf[4096];
} arc4_fork_t;

arc4_fork_t *fork_data;

int
main(void)
{
	int e, i;
	pid_t p, child;

#ifdef	ARC4_PREINIT
	(void) arc4random();
#endif

	fork_data = (arc4_fork_t *)mmap(NULL, sizeof (arc4_fork_t),
	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	assert(fork_data != MAP_FAILED);

	p = fork();
	assert(p != -1);
	if (p == 0) {
		fork_data->af_child = arc4random();
		arc4random_buf(fork_data->af_cbuf, sizeof (fork_data->af_cbuf));
		exit(0);
	}

	fork_data->af_parent = arc4random();
	arc4random_buf(fork_data->af_pbuf, sizeof (fork_data->af_pbuf));
	do {
		child = wait(&e);
	} while (child == -1 && errno == EINTR);
	assert(child == p);

	/* Now verify our data doesn't match */
	assert(fork_data->af_parent != fork_data->af_child);

	/*
	 * For the buffer here, we're mostly concerned that they aren't somehow
	 * getting the same stream.
	 */
	for (i = 0; i < sizeof (fork_data->af_pbuf); i++) {
		if (fork_data->af_pbuf[i] != fork_data->af_cbuf[i])
			break;
	}
	assert(i != sizeof (fork_data->af_pbuf));

	return (0);
}