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 that privilege is required to lower nice value; first test with, then
35 * without. There are two failure modes associated with privilege: the right
36 * to renice a process with a different uid, and the right to renice to a
37 * lower priority. Because both the real and effective uid are part of the
38 * permissions test, we have to create two children processes with different
42 #include <sys/types.h>
43 #include <sys/resource.h>
54 static int childproc_running
;
55 static pid_t childproc
;
58 priv_sched_setpriority_setup(int asroot
, int injail
, struct test
*test
)
60 int another_uid
, need_child
;
63 * Some tests require a second process with specific credentials.
64 * Set that up here, and kill in cleanup.
67 if (test
->t_test_func
== priv_sched_setpriority_aproc
) {
71 if (test
->t_test_func
== priv_sched_setpriority_myproc
)
77 warn("priv_sched_setup: fork");
82 if (setresuid(UID_THIRD
, UID_THIRD
,
84 err(-1, "setresuid(%d)", UID_THIRD
);
89 childproc_running
= 1;
90 sleep(1); /* Allow dummy thread to change uids. */
96 priv_sched_setpriority_curproc(int asroot
, int injail
, struct test
*test
)
100 error
= setpriority(PRIO_PROCESS
, 0, -1);
101 if (asroot
&& injail
)
102 expect("priv_sched_setpriority_curproc(asroot, injail)",
104 if (asroot
&& !injail
)
105 expect("priv_sched_setpriority_curproc(asroot, !injail)",
107 if (!asroot
&& injail
)
108 expect("priv_sched_setpriority_curproc(!asroot, injail)",
110 if (!asroot
&& !injail
)
111 expect("priv_sched_setpriority_curproc(!asroot, !injail)",
116 priv_sched_setpriority_myproc(int asroot
, int injail
, struct test
*test
)
120 error
= setpriority(PRIO_PROCESS
, 0, -1);
121 if (asroot
&& injail
)
122 expect("priv_sched_setpriority_myproc(asroot, injail)",
124 if (asroot
&& !injail
)
125 expect("priv_sched_setpriority_myproc(asroot, !injail)",
127 if (!asroot
&& injail
)
128 expect("priv_sched_setpriority_myproc(!asroot, injail)",
130 if (!asroot
&& !injail
)
131 expect("priv_sched_setpriority_myproc(!asroot, !injail)",
136 priv_sched_setpriority_aproc(int asroot
, int injail
, struct test
*test
)
140 error
= setpriority(PRIO_PROCESS
, 0, -1);
141 if (asroot
&& injail
)
142 expect("priv_sched_setpriority_aproc(asroot, injail)",
144 if (asroot
&& !injail
)
145 expect("priv_sched_setpriority_aproc(asroot, !injail)",
147 if (!asroot
&& injail
)
148 expect("priv_sched_setpriority_aproc(!asroot, injail)",
150 if (!asroot
&& !injail
)
151 expect("priv_sched_setpriority_aproc(!asroot, !injail)",
156 priv_sched_setpriority_cleanup(int asroot
, int injail
, struct test
*test
)
160 if (childproc_running
) {
161 (void)kill(childproc
, SIGKILL
);
163 pid
= waitpid(childproc
, NULL
, 0);
165 warn("waitpid(%d (test), NULL, 0)",
167 if (pid
== childproc
)
170 childproc_running
= 0;