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