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