2 * Copyright (c) 2006 nCircle Network Security, Inc.
3 * Copyright (c) 2007 Robert N. M. Watson
6 * This software was developed by Robert N. M. Watson for the TrustedBSD
7 * Project under contract to nCircle Network Security, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Test privilege associated with real time process settings. There are
33 * three relevant notions of privilege:
35 * - Privilege to set the real-time priority of the current process.
36 * - Privilege to set the real-time priority of another process.
37 * - Privilege to set the idle priority of another process.
38 * - No privilege to set the idle priority of the current process.
40 * This requires a test process and a target (dummy) process running with
41 * various uids. This test is based on the code in the setpriority() test.
44 #include <sys/types.h>
45 #include <sys/rtprio.h>
56 static int childproc_running;
57 static pid_t childproc;
60 priv_sched_rtprio_setup(int asroot, int injail, struct test *test)
62 int another_uid, need_child;
65 * Some tests require a second process with specific credentials.
66 * Set that up here, and kill in cleanup.
69 if (test->t_test_func == priv_sched_rtprio_aproc_normal ||
70 test->t_test_func == priv_sched_rtprio_aproc_idle ||
71 test->t_test_func == priv_sched_rtprio_aproc_realtime) {
75 if (test->t_test_func == priv_sched_rtprio_myproc_normal ||
76 test->t_test_func == priv_sched_rtprio_myproc_idle ||
77 test->t_test_func == priv_sched_rtprio_myproc_realtime) {
84 warn("priv_sched_setup: fork");
89 if (setresuid(UID_THIRD, UID_THIRD,
91 err(-1, "setresuid(%d)", UID_THIRD);
96 childproc_running = 1;
97 sleep(1); /* Allow dummy thread to change uids. */
103 priv_sched_rtprio_curproc_normal(int asroot, int injail, struct test *test)
108 rtp.type = RTP_PRIO_NORMAL;
110 error = rtprio(RTP_SET, 0, &rtp);
111 if (asroot && injail)
112 expect("priv_sched_rtprio_curproc_normal(asroot, injail)",
114 if (asroot && !injail)
115 expect("priv_sched_rtprio_curproc_normal(asroot, !injail)",
117 if (!asroot && injail)
118 expect("priv_sched_rtprio_curproc_normal(!asroot, injail)",
120 if (!asroot && !injail)
121 expect("priv_sched_rtprio_curproc_normal(!asroot, !injail)",
126 priv_sched_rtprio_curproc_idle(int asroot, int injail, struct test *test)
131 rtp.type = RTP_PRIO_IDLE;
133 error = rtprio(RTP_SET, 0, &rtp);
134 if (asroot && injail)
135 expect("priv_sched_rtprio_curproc_idle(asroot, injail)",
137 if (asroot && !injail)
138 expect("priv_sched_rtprio_curproc_idle(asroot, !injail)",
140 if (!asroot && injail)
141 expect("priv_sched_rtprio_curproc_idle(!asroot, injail)",
143 if (!asroot && !injail)
144 expect("priv_sched_rtprio_curproc_idle(!asroot, !injail)",
149 priv_sched_rtprio_curproc_realtime(int asroot, int injail, struct test *test)
154 rtp.type = RTP_PRIO_REALTIME;
156 error = rtprio(RTP_SET, 0, &rtp);
157 if (asroot && injail)
158 expect("priv_sched_rtprio_curproc_realtime(asroot, injail)",
160 if (asroot && !injail)
161 expect("priv_sched_rtprio_curproc_realtime(asroot, !injail)",
163 if (!asroot && injail)
164 expect("priv_sched_rtprio_curproc_realtime(!asroot, injail)",
166 if (!asroot && !injail)
167 expect("priv_sched_rtprio_curproc_realtime(!asroot, !injail)",
172 priv_sched_rtprio_myproc_normal(int asroot, int injail, struct test *test)
177 rtp.type = RTP_PRIO_NORMAL;
179 error = rtprio(RTP_SET, 0, &rtp);
180 if (asroot && injail)
181 expect("priv_sched_rtprio_myproc_normal(asroot, injail)",
183 if (asroot && !injail)
184 expect("priv_sched_rtprio_myproc_normal(asroot, !injail)",
186 if (!asroot && injail)
187 expect("priv_sched_rtprio_myproc_normal(!asroot, injail)",
189 if (!asroot && !injail)
190 expect("priv_sched_rtprio_myproc_normal(!asroot, !injail)",
195 priv_sched_rtprio_myproc_idle(int asroot, int injail, struct test *test)
200 rtp.type = RTP_PRIO_IDLE;
202 error = rtprio(RTP_SET, 0, &rtp);
203 if (asroot && injail)
204 expect("priv_sched_rtprio_myproc_idle(asroot, injail)",
206 if (asroot && !injail)
207 expect("priv_sched_rtprio_myproc_idle(asroot, !injail)",
209 if (!asroot && injail)
210 expect("priv_sched_rtprio_myproc_idle(!asroot, injail)",
212 if (!asroot && !injail)
213 expect("priv_sched_rtprio_myproc_idle(!asroot, !injail)",
218 priv_sched_rtprio_myproc_realtime(int asroot, int injail, struct test *test)
223 rtp.type = RTP_PRIO_REALTIME;
225 error = rtprio(RTP_SET, 0, &rtp);
226 if (asroot && injail)
227 expect("priv_sched_rtprio_myproc_realtime(asroot, injail)",
229 if (asroot && !injail)
230 expect("priv_sched_rtprio_myproc_realtime(asroot, !injail)",
232 if (!asroot && injail)
233 expect("priv_sched_rtprio_myproc_realtime(!asroot, injail)",
235 if (!asroot && !injail)
236 expect("priv_sched_rtprio_myproc_realtime(!asroot, !injail)",
241 priv_sched_rtprio_aproc_normal(int asroot, int injail, struct test *test)
246 rtp.type = RTP_PRIO_NORMAL;
248 error = rtprio(RTP_SET, childproc, &rtp);
249 if (asroot && injail)
250 expect("priv_sched_rtprio_aproc_normal(asroot, injail)",
252 if (asroot && !injail)
253 expect("priv_sched_rtprio_aproc_normal(asroot, !injail)",
255 if (!asroot && injail)
256 expect("priv_sched_rtprio_aproc_normal(!asroot, injail)",
258 if (!asroot && !injail)
259 expect("priv_sched_rtprio_aproc_normal(!asroot, !injail)",
264 priv_sched_rtprio_aproc_idle(int asroot, int injail, struct test *test)
269 rtp.type = RTP_PRIO_IDLE;
271 error = rtprio(RTP_SET, childproc, &rtp);
272 if (asroot && injail)
273 expect("priv_sched_rtprio_aproc_idle(asroot, injail)",
275 if (asroot && !injail)
276 expect("priv_sched_rtprio_aproc_idle(asroot, !injail)",
278 if (!asroot && injail)
279 expect("priv_sched_rtprio_aproc_idle(!asroot, injail)",
281 if (!asroot && !injail)
282 expect("priv_sched_rtprio_aroc_idle(!asroot, !injail)",
287 priv_sched_rtprio_aproc_realtime(int asroot, int injail, struct test *test)
292 rtp.type = RTP_PRIO_REALTIME;
294 error = rtprio(RTP_SET, childproc, &rtp);
295 if (asroot && injail)
296 expect("priv_sched_rtprio_aproc_realtime(asroot, injail)",
298 if (asroot && !injail)
299 expect("priv_sched_rtprio_aproc_realtime(asroot, !injail)",
301 if (!asroot && injail)
302 expect("priv_sched_rtprio_aproc_realtime(!asroot, injail)",
304 if (!asroot && !injail)
305 expect("priv_sched_rtprio_aproc_realtime(!asroot, !injail)",
310 priv_sched_rtprio_cleanup(int asroot, int injail, struct test *test)
314 if (childproc_running) {
315 (void)kill(childproc, SIGKILL);
317 pid = waitpid(childproc, NULL, 0);
319 warn("waitpid(%d (test), NULL, 0)",
321 if (pid == childproc)
324 childproc_running = 0;