xref: /freebsd/tools/regression/capsicum/syscalls/cap_getmode.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/types.h>
32 #include <sys/capsicum.h>
33 #include <sys/procdesc.h>
34 #include <sys/wait.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 
42 #include "misc.h"
43 
44 int
45 main(void)
46 {
47 	unsigned int mode;
48 	pid_t pid;
49 	int pfd;
50 
51 	printf("1..27\n");
52 
53 	mode = 666;
54 	CHECK(cap_getmode(&mode) == 0);
55 	/* If cap_getmode() succeeded mode should be modified. */
56 	CHECK(mode != 666);
57 	/* We are not in capability mode. */
58 	CHECK(mode == 0);
59 
60 	/* Expect EFAULT. */
61 	errno = 0;
62 	CHECK(cap_getmode(NULL) == -1);
63 	CHECK(errno == EFAULT);
64 	errno = 0;
65 	CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);
66 	CHECK(errno == EFAULT);
67 
68 	/* If parent is not in capability mode, child after fork() also won't be. */
69 	pid = fork();
70 	switch (pid) {
71 	case -1:
72 		err(1, "fork() failed");
73 	case 0:
74 		mode = 666;
75 		CHECK(cap_getmode(&mode) == 0);
76 		/* If cap_getmode() succeeded mode should be modified. */
77 		CHECK(mode != 666);
78 		/* We are not in capability mode. */
79 		CHECK(mode == 0);
80 		exit(0);
81 	default:
82 		if (waitpid(pid, NULL, 0) == -1)
83 			err(1, "waitpid() failed");
84 	}
85 
86 	/* If parent is not in capability mode, child after pdfork() also won't be. */
87 	pid = pdfork(&pfd, 0);
88 	switch (pid) {
89 	case -1:
90 		err(1, "pdfork() failed");
91 	case 0:
92 		mode = 666;
93 		CHECK(cap_getmode(&mode) == 0);
94 		/* If cap_getmode() succeeded mode should be modified. */
95 		CHECK(mode != 666);
96 		/* We are not in capability mode. */
97 		CHECK(mode == 0);
98 		exit(0);
99 	default:
100 		if (pdwait(pfd) == -1)
101 			err(1, "pdwait() failed");
102 		close(pfd);
103 	}
104 
105 	/* In capability mode... */
106 
107 	CHECK(cap_enter() == 0);
108 
109 	mode = 666;
110 	CHECK(cap_getmode(&mode) == 0);
111 	/* If cap_getmode() succeeded mode should be modified. */
112 	CHECK(mode != 666);
113 	/* We are in capability mode. */
114 	CHECK(mode == 1);
115 
116 	/* Expect EFAULT. */
117 	errno = 0;
118 	CHECK(cap_getmode(NULL) == -1);
119 	CHECK(errno == EFAULT);
120 	errno = 0;
121 	CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);
122 	CHECK(errno == EFAULT);
123 
124 	/* If parent is in capability mode, child after fork() also will be. */
125 	pid = fork();
126 	switch (pid) {
127 	case -1:
128 		err(1, "fork() failed");
129 	case 0:
130 		mode = 666;
131 		CHECK(cap_getmode(&mode) == 0);
132 		/* If cap_getmode() succeeded mode should be modified. */
133 		CHECK(mode != 666);
134 		/* We are in capability mode. */
135 		CHECK(mode == 1);
136 		exit(0);
137 	default:
138 		/*
139 		 * wait(2) and friends are not permitted in the capability mode,
140 		 * so we can only just wait for a while.
141 		 */
142 		sleep(1);
143 	}
144 
145 	/* If parent is in capability mode, child after pdfork() also will be. */
146 	pid = pdfork(&pfd, 0);
147 	switch (pid) {
148 	case -1:
149 		err(1, "pdfork() failed");
150 	case 0:
151 		mode = 666;
152 		CHECK(cap_getmode(&mode) == 0);
153 		/* If cap_getmode() succeeded mode should be modified. */
154 		CHECK(mode != 666);
155 		/* We are in capability mode. */
156 		CHECK(mode == 1);
157 		exit(0);
158 	default:
159 		if (pdwait(pfd) == -1)
160 			err(1, "pdwait() failed");
161 		close(pfd);
162 	}
163 
164 	exit(0);
165 }
166