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 * Privilege test framework. Each test is encapsulated on a .c file exporting
34 * a function that implements the test. Each test is run from its own child
35 * process, and they are run in sequence one at a time.
38 #include <sys/types.h>
51 * Common routines used across many tests.
57 if (getuid() != UID_ROOT || geteuid() != UID_ROOT)
58 err(-1, "must be run as root");
62 setup_file(char *fpathp, uid_t uid, gid_t gid, mode_t mode)
66 strcpy(fpathp, "/tmp/priv.XXXXXXXXXXX");
71 if (fchown(fd, uid, gid) < 0)
72 err(-1, "fchown(%s, %d, %d)", fpathp, uid, gid);
74 if (fchmod(fd, mode) < 0)
75 err(-1, "chmod(%s, 0%o)", fpathp, mode);
81 * When downgrading privileges, set the gid before the uid; when upgrading,
85 set_creds(uid_t uid, gid_t gid)
89 err(-1, "setegid(%d)", gid);
91 err(-1, "seteuid(%d)", uid);
99 err(-1, "seteuid(%d)", uid);
106 if (seteuid(UID_ROOT) < 0)
107 err(-1, "seteuid(%d)", UID_ROOT);
108 if (setegid(GID_WHEEL) < 0)
109 err(-1, "setegid(%d)", GID_WHEEL);
113 * Execute tests in a child process so they don't contaminate each other,
114 * especially with regard to file descriptors, credentials, working
115 * directories, and chroot status.
118 run(const char *funcname, void (*func)(void))
122 printf("running %s\n", funcname);
127 err(-1, "test %s unable to fork", funcname);
129 setprogname(funcname);
136 pid = waitpid(childpid, NULL, 0);
138 warn("waitpid %s", funcname);
148 main(int argc, char *argv[])
151 run("priv_acct", priv_acct);
152 run("priv_adjtime", priv_adjtime);
153 run("priv_clock_settime", priv_clock_settime);
154 run("priv_io", priv_io);
155 run("priv_kenv_set", priv_kenv_set);
156 run("priv_kenv_unset", priv_kenv_unset);
157 run("priv_proc_setlogin", priv_proc_setlogin);
158 run("priv_proc_setrlimit", priv_proc_setrlimit);
159 run("priv_sched_rtprio", priv_sched_rtprio);
160 run("priv_sched_setpriority", priv_sched_setpriority);
161 run("priv_settimeofday", priv_settimeofday);
162 run("priv_sysctl_write", priv_sysctl_write);
163 run("priv_vfs_admin", priv_vfs_admin);
164 run("priv_vfs_chown", priv_vfs_chown);
165 run("priv_vfs_chroot", priv_vfs_chroot);
166 run("priv_vfs_clearsugid", priv_vfs_clearsugid);
167 run("priv_vfs_extattr_system", priv_vfs_extattr_system);
168 run("priv_vfs_fhopen", priv_vfs_fhopen);
169 run("priv_vfs_fhstat", priv_vfs_fhstat);
170 run("priv_vfs_fhstatfs", priv_vfs_fhstatfs);
171 run("priv_vfs_generation", priv_vfs_generation);
172 run("priv_vfs_getfh", priv_vfs_getfh);
173 run("priv_vfs_read", priv_vfs_read);
174 run("priv_vfs_setgid", priv_vfs_setgid);
175 run("priv_vfs_stickyfile", priv_vfs_stickyfile);
176 run("priv_vfs_write", priv_vfs_write);
177 run("priv_vm_madv_protect", priv_vm_madv_protect);
178 run("priv_vm_mlock", priv_vm_mlock);
179 run("priv_vm_munlock", priv_vm_munlock);
181 run("test_utimes", test_utimes);