1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/types.h>
34#include <sys/capsicum.h>
35#include <sys/procdesc.h>
36#include <sys/wait.h>
37
38#include <err.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43
44#include "misc.h"
45
46int
47main(void)
48{
49	unsigned int mode;
50	pid_t pid;
51	int pfd;
52
53	printf("1..27\n");
54
55	mode = 666;
56	CHECK(cap_getmode(&mode) == 0);
57	/* If cap_getmode() succeeded mode should be modified. */
58	CHECK(mode != 666);
59	/* We are not in capability mode. */
60	CHECK(mode == 0);
61
62	/* Expect EFAULT. */
63	errno = 0;
64	CHECK(cap_getmode(NULL) == -1);
65	CHECK(errno == EFAULT);
66	errno = 0;
67	CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);
68	CHECK(errno == EFAULT);
69
70	/* If parent is not in capability mode, child after fork() also won't be. */
71	pid = fork();
72	switch (pid) {
73	case -1:
74		err(1, "fork() failed");
75	case 0:
76		mode = 666;
77		CHECK(cap_getmode(&mode) == 0);
78		/* If cap_getmode() succeeded mode should be modified. */
79		CHECK(mode != 666);
80		/* We are not in capability mode. */
81		CHECK(mode == 0);
82		exit(0);
83	default:
84		if (waitpid(pid, NULL, 0) == -1)
85			err(1, "waitpid() failed");
86	}
87
88	/* If parent is not in capability mode, child after pdfork() also won't be. */
89	pid = pdfork(&pfd, 0);
90	switch (pid) {
91	case -1:
92		err(1, "pdfork() failed");
93	case 0:
94		mode = 666;
95		CHECK(cap_getmode(&mode) == 0);
96		/* If cap_getmode() succeeded mode should be modified. */
97		CHECK(mode != 666);
98		/* We are not in capability mode. */
99		CHECK(mode == 0);
100		exit(0);
101	default:
102		if (pdwait(pfd) == -1)
103			err(1, "pdwait() failed");
104		close(pfd);
105	}
106
107	/* In capability mode... */
108
109	CHECK(cap_enter() == 0);
110
111	mode = 666;
112	CHECK(cap_getmode(&mode) == 0);
113	/* If cap_getmode() succeeded mode should be modified. */
114	CHECK(mode != 666);
115	/* We are in capability mode. */
116	CHECK(mode == 1);
117
118	/* Expect EFAULT. */
119	errno = 0;
120	CHECK(cap_getmode(NULL) == -1);
121	CHECK(errno == EFAULT);
122	errno = 0;
123	CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);
124	CHECK(errno == EFAULT);
125
126	/* If parent is in capability mode, child after fork() also will be. */
127	pid = fork();
128	switch (pid) {
129	case -1:
130		err(1, "fork() failed");
131	case 0:
132		mode = 666;
133		CHECK(cap_getmode(&mode) == 0);
134		/* If cap_getmode() succeeded mode should be modified. */
135		CHECK(mode != 666);
136		/* We are in capability mode. */
137		CHECK(mode == 1);
138		exit(0);
139	default:
140		/*
141		 * wait(2) and friends are not permitted in the capability mode,
142		 * so we can only just wait for a while.
143		 */
144		sleep(1);
145	}
146
147	/* If parent is in capability mode, child after pdfork() also will be. */
148	pid = pdfork(&pfd, 0);
149	switch (pid) {
150	case -1:
151		err(1, "pdfork() failed");
152	case 0:
153		mode = 666;
154		CHECK(cap_getmode(&mode) == 0);
155		/* If cap_getmode() succeeded mode should be modified. */
156		CHECK(mode != 666);
157		/* We are in capability mode. */
158		CHECK(mode == 1);
159		exit(0);
160	default:
161		if (pdwait(pfd) == -1)
162			err(1, "pdwait() failed");
163		close(pfd);
164	}
165
166	exit(0);
167}
168