19fa5f6b4SRobert Watson /*-
29fa5f6b4SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc.
3d903306aSRobert Watson * Copyright (c) 2007 Robert N. M. Watson
49fa5f6b4SRobert Watson * All rights reserved.
59fa5f6b4SRobert Watson *
69fa5f6b4SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD
79fa5f6b4SRobert Watson * Project under contract to nCircle Network Security, Inc.
89fa5f6b4SRobert Watson *
99fa5f6b4SRobert Watson * Redistribution and use in source and binary forms, with or without
109fa5f6b4SRobert Watson * modification, are permitted provided that the following conditions
119fa5f6b4SRobert Watson * are met:
129fa5f6b4SRobert Watson * 1. Redistributions of source code must retain the above copyright
139fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer.
149fa5f6b4SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
159fa5f6b4SRobert Watson * notice, this list of conditions and the following disclaimer in the
169fa5f6b4SRobert Watson * documentation and/or other materials provided with the distribution.
179fa5f6b4SRobert Watson *
189fa5f6b4SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199fa5f6b4SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209fa5f6b4SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219fa5f6b4SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
229fa5f6b4SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239fa5f6b4SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
249fa5f6b4SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
259fa5f6b4SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
269fa5f6b4SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
279fa5f6b4SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
289fa5f6b4SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299fa5f6b4SRobert Watson */
309fa5f6b4SRobert Watson
319fa5f6b4SRobert Watson /*
329fa5f6b4SRobert Watson * Test that privilege is required to lower nice value; first test with, then
339fa5f6b4SRobert Watson * without. There are two failure modes associated with privilege: the right
349fa5f6b4SRobert Watson * to renice a process with a different uid, and the right to renice to a
359fa5f6b4SRobert Watson * lower priority. Because both the real and effective uid are part of the
369fa5f6b4SRobert Watson * permissions test, we have to create two children processes with different
379fa5f6b4SRobert Watson * uids.
389fa5f6b4SRobert Watson */
399fa5f6b4SRobert Watson
409fa5f6b4SRobert Watson #include <sys/types.h>
419fa5f6b4SRobert Watson #include <sys/resource.h>
429fa5f6b4SRobert Watson #include <sys/wait.h>
439fa5f6b4SRobert Watson
449fa5f6b4SRobert Watson #include <err.h>
459fa5f6b4SRobert Watson #include <errno.h>
469fa5f6b4SRobert Watson #include <signal.h>
479fa5f6b4SRobert Watson #include <stdlib.h>
489fa5f6b4SRobert Watson #include <unistd.h>
499fa5f6b4SRobert Watson
509fa5f6b4SRobert Watson #include "main.h"
519fa5f6b4SRobert Watson
52d903306aSRobert Watson static int childproc_running;
53d903306aSRobert Watson static pid_t childproc;
549fa5f6b4SRobert Watson
55d903306aSRobert Watson int
priv_sched_setpriority_setup(int asroot,int injail,struct test * test)56d903306aSRobert Watson priv_sched_setpriority_setup(int asroot, int injail, struct test *test)
57d903306aSRobert Watson {
58d903306aSRobert Watson int another_uid, need_child;
59d903306aSRobert Watson
60d903306aSRobert Watson /*
61d903306aSRobert Watson * Some tests require a second process with specific credentials.
62d903306aSRobert Watson * Set that up here, and kill in cleanup.
63d903306aSRobert Watson */
64d903306aSRobert Watson need_child = 0;
65d903306aSRobert Watson if (test->t_test_func == priv_sched_setpriority_aproc) {
66d903306aSRobert Watson need_child = 1;
67d903306aSRobert Watson another_uid = 1;
68d903306aSRobert Watson }
69d903306aSRobert Watson if (test->t_test_func == priv_sched_setpriority_myproc)
70d903306aSRobert Watson need_child = 1;
71d903306aSRobert Watson
72d903306aSRobert Watson if (need_child) {
73d903306aSRobert Watson childproc = fork();
74d903306aSRobert Watson if (childproc < 0) {
75d903306aSRobert Watson warn("priv_sched_setup: fork");
76d903306aSRobert Watson return (-1);
77d903306aSRobert Watson }
78d903306aSRobert Watson if (childproc == 0) {
79d903306aSRobert Watson if (another_uid) {
80d903306aSRobert Watson if (setresuid(UID_THIRD, UID_THIRD,
81d903306aSRobert Watson UID_THIRD) < 0)
82d903306aSRobert Watson err(-1, "setresuid(%d)", UID_THIRD);
83d903306aSRobert Watson }
849fa5f6b4SRobert Watson while (1)
859fa5f6b4SRobert Watson sleep(1);
869fa5f6b4SRobert Watson }
87d903306aSRobert Watson childproc_running = 1;
88d903306aSRobert Watson sleep(1); /* Allow dummy thread to change uids. */
899fa5f6b4SRobert Watson }
90d903306aSRobert Watson return (0);
919fa5f6b4SRobert Watson }
929fa5f6b4SRobert Watson
939fa5f6b4SRobert Watson void
priv_sched_setpriority_curproc(int asroot,int injail,struct test * test)94d903306aSRobert Watson priv_sched_setpriority_curproc(int asroot, int injail, struct test *test)
959fa5f6b4SRobert Watson {
96d903306aSRobert Watson int error;
979fa5f6b4SRobert Watson
98d903306aSRobert Watson error = setpriority(PRIO_PROCESS, 0, -1);
99d903306aSRobert Watson if (asroot && injail)
100d903306aSRobert Watson expect("priv_sched_setpriority_curproc(asroot, injail)",
101d903306aSRobert Watson error, -1, EACCES);
102d903306aSRobert Watson if (asroot && !injail)
103d903306aSRobert Watson expect("priv_sched_setpriority_curproc(asroot, !injail)",
104d903306aSRobert Watson error, 0, 0);
105d903306aSRobert Watson if (!asroot && injail)
106d903306aSRobert Watson expect("priv_sched_setpriority_curproc(!asroot, injail)",
107d903306aSRobert Watson error, -1, EACCES);
108d903306aSRobert Watson if (!asroot && !injail)
109d903306aSRobert Watson expect("priv_sched_setpriority_curproc(!asroot, !injail)",
110d903306aSRobert Watson error, -1, EACCES);
1119fa5f6b4SRobert Watson }
1129fa5f6b4SRobert Watson
113d903306aSRobert Watson void
priv_sched_setpriority_myproc(int asroot,int injail,struct test * test)114d903306aSRobert Watson priv_sched_setpriority_myproc(int asroot, int injail, struct test *test)
115d903306aSRobert Watson {
116d903306aSRobert Watson int error;
117d903306aSRobert Watson
118d903306aSRobert Watson error = setpriority(PRIO_PROCESS, 0, -1);
119d903306aSRobert Watson if (asroot && injail)
120d903306aSRobert Watson expect("priv_sched_setpriority_myproc(asroot, injail)",
121d903306aSRobert Watson error, -1, EACCES);
122d903306aSRobert Watson if (asroot && !injail)
123d903306aSRobert Watson expect("priv_sched_setpriority_myproc(asroot, !injail)",
124d903306aSRobert Watson error, 0, 0);
125d903306aSRobert Watson if (!asroot && injail)
126d903306aSRobert Watson expect("priv_sched_setpriority_myproc(!asroot, injail)",
127d903306aSRobert Watson error, -1, EACCES);
128d903306aSRobert Watson if (!asroot && !injail)
129d903306aSRobert Watson expect("priv_sched_setpriority_myproc(!asroot, !injail)",
130d903306aSRobert Watson error, -1, EACCES);
1319fa5f6b4SRobert Watson }
1329fa5f6b4SRobert Watson
133d903306aSRobert Watson void
priv_sched_setpriority_aproc(int asroot,int injail,struct test * test)134d903306aSRobert Watson priv_sched_setpriority_aproc(int asroot, int injail, struct test *test)
135d903306aSRobert Watson {
136d903306aSRobert Watson int error;
137d903306aSRobert Watson
138d903306aSRobert Watson error = setpriority(PRIO_PROCESS, 0, -1);
139d903306aSRobert Watson if (asroot && injail)
140d903306aSRobert Watson expect("priv_sched_setpriority_aproc(asroot, injail)",
141d903306aSRobert Watson error, -1, EACCES);
142d903306aSRobert Watson if (asroot && !injail)
143d903306aSRobert Watson expect("priv_sched_setpriority_aproc(asroot, !injail)",
144d903306aSRobert Watson error, 0, 0);
145d903306aSRobert Watson if (!asroot && injail)
146d903306aSRobert Watson expect("priv_sched_setpriority_aproc(!asroot, injail)",
147d903306aSRobert Watson error, -1, EACCES);
148d903306aSRobert Watson if (!asroot && !injail)
149d903306aSRobert Watson expect("priv_sched_setpriority_aproc(!asroot, !injail)",
150d903306aSRobert Watson error, -1, EACCES);
151d903306aSRobert Watson }
152d903306aSRobert Watson
153d903306aSRobert Watson void
priv_sched_setpriority_cleanup(int asroot,int injail,struct test * test)154d903306aSRobert Watson priv_sched_setpriority_cleanup(int asroot, int injail, struct test *test)
155d903306aSRobert Watson {
156d903306aSRobert Watson pid_t pid;
157d903306aSRobert Watson
158d903306aSRobert Watson if (childproc_running) {
159d903306aSRobert Watson (void)kill(childproc, SIGKILL);
160d903306aSRobert Watson while (1) {
161d903306aSRobert Watson pid = waitpid(childproc, NULL, 0);
162d903306aSRobert Watson if (pid == -1)
163d903306aSRobert Watson warn("waitpid(%d (test), NULL, 0)",
164d903306aSRobert Watson childproc);
165d903306aSRobert Watson if (pid == childproc)
166d903306aSRobert Watson break;
167d903306aSRobert Watson }
168d903306aSRobert Watson childproc_running = 0;
169d903306aSRobert Watson childproc = -1;
170d903306aSRobert Watson }
1719fa5f6b4SRobert Watson }
172