2 * Copyright (c) 2006 nCircle Network Security, Inc.
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
6 * Project under contract to nCircle Network Security, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR, NCIRCLE NETWORK SECURITY,
21 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Test privilege associated with real time process settings. There are
34 * three relevant notions of privilege:
36 * - Privilege to set the real-time priority of the current process.
37 * - Privilege to set the real-time priority of another process.
38 * - Privilege to set the idle priority of another process.
39 * - No privilege to set the idle priority of the current process.
41 * This requires a test process and a target (dummy) process running with
42 * various uids. This test is based on the code in the setpriority() test.
45 #include <sys/types.h>
46 #include <sys/rtprio.h>
66 collect(pid_t test_pid, pid_t dummy_pid)
71 * First, collect the main test process. When it has exited, then
72 * kill off the dummy process.
76 pid = waitpid(test_pid, NULL, 0);
78 warn("waitpid(%d (test), NULL, 0)", test_pid);
84 if (kill(dummy_pid, SIGKILL) < 0)
85 err(-1, "kill(%d, SIGKILL)", dummy_pid);
88 pid = waitpid(dummy_pid, NULL, 0);
90 warn("waitpid(%d, NULL, 0)", dummy_pid);
103 * Tests first as root. Test that we can set normal, realtime, and
104 * idle priorities on the current thread and on the dummy thread.
106 rtp.type = RTP_PRIO_REALTIME;
108 if (rtprio(RTP_SET, 0, &rtp) < 0)
109 err(-1, "rtprio(RTP_SET, 0, {REALTIME, 0}) as root");
111 rtp.type = RTP_PRIO_IDLE;
113 if (rtprio(RTP_SET, 0, &rtp) < 0)
114 err(-1, "rtprio(RTP_SET, 0, {IDLE, 0}) as root");
116 rtp.type = RTP_PRIO_NORMAL;
118 if (rtprio(RTP_SET, 0, &rtp) < 0)
119 err(-1, "rtprio(RTP_SET, 0, {NORMAL, 0) as root");
121 rtp.type = RTP_PRIO_REALTIME;
123 if (rtprio(RTP_SET, dummy_pid, &rtp) < 0)
124 err(-1, "rtprio(RTP_SET, %d, {REALTIME, 0}) as root",
127 rtp.type = RTP_PRIO_IDLE;
129 if (rtprio(RTP_SET, dummy_pid, &rtp) < 0)
130 err(-1, "rtprio(RTP_SET, %d, {IDLE, 0}) as root", dummy_pid);
132 rtp.type = RTP_PRIO_NORMAL;
134 if (rtprio(RTP_SET, dummy_pid, &rtp) < 0)
135 err(-1, "rtprio(RTP_SET, %d, {NORMAL, 0) as root",
139 * Then test again as a different credential.
141 if (setresuid(UID_OTHER, UID_OTHER, UID_OTHER) < 0)
142 err(-1, "setresuid(%d)", UID_OTHER);
144 rtp.type = RTP_PRIO_REALTIME;
146 error = rtprio(RTP_SET, 0, &rtp);
149 "rtprio(RTP_SET, 0, {REALTIME, 0}) succeeded as !root");
151 err(-1, "rtprio(RTP_SET, 0, {REALTIME, 0}) wrong errno %d as"
154 rtp.type = RTP_PRIO_IDLE;
156 error = rtprio(RTP_SET, 0, &rtp);
158 errx(-1, "rtprio(RTP_SET, 0, {IDLE, 0}) succeeded as !root");
160 err(-1, "rtprio(RTP_SET, 0, {IDLE, 0}) wrong errno %d as "
163 rtp.type = RTP_PRIO_NORMAL;
165 if (rtprio(RTP_SET, 0, &rtp) < 0)
166 err(-1, "rtprio(RTP_SET, 0, {NORMAL, 0}) as !root");
168 rtp.type = RTP_PRIO_REALTIME;
170 error = rtprio(RTP_SET, dummy_pid, &rtp);
173 "rtprio(RTP_SET, %d, {REALTIME, 0}) succeeded as !root",
176 err(-1, "rtprio(RTP_SET, %d, {REALTIME, 0}) wrong errno %d as"
177 " !root", dummy_pid, errno);
179 rtp.type = RTP_PRIO_IDLE;
181 error = rtprio(RTP_SET, dummy_pid, &rtp);
183 errx(-1, "rtprio(RTP_SET, %d, {IDLE, 0}) succeeded as !root",
187 "rtprio(RTP_SET, %d, {IDLE, 0}) wrong errno %d as !root",
190 rtp.type = RTP_PRIO_NORMAL;
192 error = rtprio(RTP_SET, dummy_pid, &rtp);
195 "rtprio(RTP_SET, %d, {NORMAL, 0) succeeded as !root",
198 err(-1, "rtprio(RTP_SET, %d, {NORMAL, 0}) wrong errno %d as "
199 "!root", dummy_pid, errno);
205 priv_sched_rtprio(void)
207 pid_t dummy_pid, test_pid;
212 * Set up dummy process, which we will kill before exiting.
216 err(-1, "fork - dummy");
217 if (dummy_pid == 0) {
218 if (setresuid(UID_THIRD, UID_THIRD, UID_THIRD) < 0)
219 err(-1, "setresuid(%d)", UID_THIRD);
222 sleep(1); /* Allow dummy thread to change uids. */
227 collect(-1, dummy_pid);
233 collect(test_pid, dummy_pid);