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.
34 * Test privilege associated with real time process settings. There are
35 * three relevant notions of privilege:
37 * - Privilege to set the real-time priority of the current process.
38 * - Privilege to set the real-time priority of another process.
39 * - Privilege to set the idle priority of another process.
40 * - No privilege to set the idle priority of the current process.
42 * This requires a test process and a target (dummy) process running with
43 * various uids. This test is based on the code in the setpriority() test.
46 #include <sys/types.h>
47 #include <sys/rtprio.h>
58 static int childproc_running;
59 static pid_t childproc;
62 priv_sched_rtprio_setup(int asroot, int injail, struct test *test)
64 int another_uid, need_child;
67 * Some tests require a second process with specific credentials.
68 * Set that up here, and kill in cleanup.
71 if (test->t_test_func == priv_sched_rtprio_aproc_normal ||
72 test->t_test_func == priv_sched_rtprio_aproc_idle ||
73 test->t_test_func == priv_sched_rtprio_aproc_realtime) {
77 if (test->t_test_func == priv_sched_rtprio_myproc_normal ||
78 test->t_test_func == priv_sched_rtprio_myproc_idle ||
79 test->t_test_func == priv_sched_rtprio_myproc_realtime) {
86 warn("priv_sched_setup: fork");
91 if (setresuid(UID_THIRD, UID_THIRD,
93 err(-1, "setresuid(%d)", UID_THIRD);
98 childproc_running = 1;
99 sleep(1); /* Allow dummy thread to change uids. */
105 priv_sched_rtprio_curproc_normal(int asroot, int injail, struct test *test)
110 rtp.type = RTP_PRIO_NORMAL;
112 error = rtprio(RTP_SET, 0, &rtp);
113 if (asroot && injail)
114 expect("priv_sched_rtprio_curproc_normal(asroot, injail)",
116 if (asroot && !injail)
117 expect("priv_sched_rtprio_curproc_normal(asroot, !injail)",
119 if (!asroot && injail)
120 expect("priv_sched_rtprio_curproc_normal(!asroot, injail)",
122 if (!asroot && !injail)
123 expect("priv_sched_rtprio_curproc_normal(!asroot, !injail)",
128 priv_sched_rtprio_curproc_idle(int asroot, int injail, struct test *test)
133 rtp.type = RTP_PRIO_IDLE;
135 error = rtprio(RTP_SET, 0, &rtp);
136 if (asroot && injail)
137 expect("priv_sched_rtprio_curproc_idle(asroot, injail)",
139 if (asroot && !injail)
140 expect("priv_sched_rtprio_curproc_idle(asroot, !injail)",
142 if (!asroot && injail)
143 expect("priv_sched_rtprio_curproc_idle(!asroot, injail)",
145 if (!asroot && !injail)
146 expect("priv_sched_rtprio_curproc_idle(!asroot, !injail)",
151 priv_sched_rtprio_curproc_realtime(int asroot, int injail, struct test *test)
156 rtp.type = RTP_PRIO_REALTIME;
158 error = rtprio(RTP_SET, 0, &rtp);
159 if (asroot && injail)
160 expect("priv_sched_rtprio_curproc_realtime(asroot, injail)",
162 if (asroot && !injail)
163 expect("priv_sched_rtprio_curproc_realtime(asroot, !injail)",
165 if (!asroot && injail)
166 expect("priv_sched_rtprio_curproc_realtime(!asroot, injail)",
168 if (!asroot && !injail)
169 expect("priv_sched_rtprio_curproc_realtime(!asroot, !injail)",
174 priv_sched_rtprio_myproc_normal(int asroot, int injail, struct test *test)
179 rtp.type = RTP_PRIO_NORMAL;
181 error = rtprio(RTP_SET, 0, &rtp);
182 if (asroot && injail)
183 expect("priv_sched_rtprio_myproc_normal(asroot, injail)",
185 if (asroot && !injail)
186 expect("priv_sched_rtprio_myproc_normal(asroot, !injail)",
188 if (!asroot && injail)
189 expect("priv_sched_rtprio_myproc_normal(!asroot, injail)",
191 if (!asroot && !injail)
192 expect("priv_sched_rtprio_myproc_normal(!asroot, !injail)",
197 priv_sched_rtprio_myproc_idle(int asroot, int injail, struct test *test)
202 rtp.type = RTP_PRIO_IDLE;
204 error = rtprio(RTP_SET, 0, &rtp);
205 if (asroot && injail)
206 expect("priv_sched_rtprio_myproc_idle(asroot, injail)",
208 if (asroot && !injail)
209 expect("priv_sched_rtprio_myproc_idle(asroot, !injail)",
211 if (!asroot && injail)
212 expect("priv_sched_rtprio_myproc_idle(!asroot, injail)",
214 if (!asroot && !injail)
215 expect("priv_sched_rtprio_myproc_idle(!asroot, !injail)",
220 priv_sched_rtprio_myproc_realtime(int asroot, int injail, struct test *test)
225 rtp.type = RTP_PRIO_REALTIME;
227 error = rtprio(RTP_SET, 0, &rtp);
228 if (asroot && injail)
229 expect("priv_sched_rtprio_myproc_realtime(asroot, injail)",
231 if (asroot && !injail)
232 expect("priv_sched_rtprio_myproc_realtime(asroot, !injail)",
234 if (!asroot && injail)
235 expect("priv_sched_rtprio_myproc_realtime(!asroot, injail)",
237 if (!asroot && !injail)
238 expect("priv_sched_rtprio_myproc_realtime(!asroot, !injail)",
243 priv_sched_rtprio_aproc_normal(int asroot, int injail, struct test *test)
248 rtp.type = RTP_PRIO_NORMAL;
250 error = rtprio(RTP_SET, childproc, &rtp);
251 if (asroot && injail)
252 expect("priv_sched_rtprio_aproc_normal(asroot, injail)",
254 if (asroot && !injail)
255 expect("priv_sched_rtprio_aproc_normal(asroot, !injail)",
257 if (!asroot && injail)
258 expect("priv_sched_rtprio_aproc_normal(!asroot, injail)",
260 if (!asroot && !injail)
261 expect("priv_sched_rtprio_aproc_normal(!asroot, !injail)",
266 priv_sched_rtprio_aproc_idle(int asroot, int injail, struct test *test)
271 rtp.type = RTP_PRIO_IDLE;
273 error = rtprio(RTP_SET, childproc, &rtp);
274 if (asroot && injail)
275 expect("priv_sched_rtprio_aproc_idle(asroot, injail)",
277 if (asroot && !injail)
278 expect("priv_sched_rtprio_aproc_idle(asroot, !injail)",
280 if (!asroot && injail)
281 expect("priv_sched_rtprio_aproc_idle(!asroot, injail)",
283 if (!asroot && !injail)
284 expect("priv_sched_rtprio_aroc_idle(!asroot, !injail)",
289 priv_sched_rtprio_aproc_realtime(int asroot, int injail, struct test *test)
294 rtp.type = RTP_PRIO_REALTIME;
296 error = rtprio(RTP_SET, childproc, &rtp);
297 if (asroot && injail)
298 expect("priv_sched_rtprio_aproc_realtime(asroot, injail)",
300 if (asroot && !injail)
301 expect("priv_sched_rtprio_aproc_realtime(asroot, !injail)",
303 if (!asroot && injail)
304 expect("priv_sched_rtprio_aproc_realtime(!asroot, injail)",
306 if (!asroot && !injail)
307 expect("priv_sched_rtprio_aproc_realtime(!asroot, !injail)",
312 priv_sched_rtprio_cleanup(int asroot, int injail, struct test *test)
316 if (childproc_running) {
317 (void)kill(childproc, SIGKILL);
319 pid = waitpid(childproc, NULL, 0);
321 warn("waitpid(%d (test), NULL, 0)",
323 if (pid == childproc)
326 childproc_running = 0;