2 * Copyright (c) 2007 Robert M. M. Watson
5 * This software was developed by Robert N. M. Watson for the TrustedBSD
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 * Confirm that various UID/GID/etc-related system calls require root
34 * privilege in the absense of any saved/real/etc variations in the
35 * credential. It would be nice to also check cases where those bits of the
36 * credential are more interesting.
38 * XXXRW: Add support for testing more diverse real/saved scenarios.
41 #include <sys/types.h>
51 priv_cred_setup(int asroot
, int injail
, struct test
*test
)
58 priv_cred_setuid(int asroot
, int injail
, struct test
*test
)
62 error
= setuid(UID_OTHER
);
64 expect("priv_setuid(asroot, injail)", error
, 0, 0);
65 if (asroot
&& !injail
)
66 expect("priv_setuid(asroot, !injail)", error
, 0, 0);
67 if (!asroot
&& injail
)
68 expect("priv_setuid(!asroot, injail)", error
, -1, EPERM
);
69 if (!asroot
&& !injail
)
70 expect("priv_setuid(!asroot, !injail)", error
, -1, EPERM
);
74 priv_cred_seteuid(int asroot
, int injail
, struct test
*test
)
78 error
= seteuid(UID_OTHER
);
80 expect("priv_seteuid(asroot, injail)", error
, 0, 0);
81 if (asroot
&& !injail
)
82 expect("priv_seteuid(asroot, !injail)", error
, 0, 0);
83 if (!asroot
&& injail
)
84 expect("priv_seteuid(!asroot, injail)", error
, -1, EPERM
);
85 if (!asroot
&& !injail
)
86 expect("priv_seteuid(!asroot, !injail)", error
, -1, EPERM
);
90 priv_cred_setgid(int asroot
, int injail
, struct test
*test
)
94 error
= setgid(GID_OTHER
);
96 expect("priv_setgid(asroot, injail)", error
, 0, 0);
97 if (asroot
&& !injail
)
98 expect("priv_setgid(asroot, !injail)", error
, 0, 0);
99 if (!asroot
&& injail
)
100 expect("priv_setgid(!asroot, injail)", error
, -1, EPERM
);
101 if (!asroot
&& !injail
)
102 expect("priv_setgid(!asroot, !injail)", error
, -1, EPERM
);
106 priv_cred_setegid(int asroot
, int injail
, struct test
*test
)
110 error
= setegid(GID_OTHER
);
111 if (asroot
&& injail
)
112 expect("priv_setegid(asroot, injail)", error
, 0, 0);
113 if (asroot
&& !injail
)
114 expect("priv_setegid(asroot, !injail)", error
, 0, 0);
115 if (!asroot
&& injail
)
116 expect("priv_setegd(!asroot, injail)", error
, -1, EPERM
);
117 if (!asroot
&& !injail
)
118 expect("priv_setegid(!asroot, !injail)", error
, -1, EPERM
);
121 static const gid_t gidset
[] = {GID_WHEEL
, GID_OTHER
};
122 static const int gidset_len
= sizeof(gidset
) / sizeof(gid_t
);
125 priv_cred_setgroups(int asroot
, int injail
, struct test
*test
)
129 error
= setgroups(gidset_len
, gidset
);
130 if (asroot
&& injail
)
131 expect("priv_setgroups(asroot, injail)", error
, 0, 0);
132 if (asroot
&& !injail
)
133 expect("priv_setgroups(asroot, !injail)", error
, 0, 0);
134 if (!asroot
&& injail
)
135 expect("priv_setgroups(!asroot, injail)", error
, -1, EPERM
);
136 if (!asroot
&& !injail
)
137 expect("priv_setgroups(!asroot, !injail)", error
, -1, EPERM
);
141 priv_cred_setreuid(int asroot
, int injail
, struct test
*test
)
145 error
= setreuid(UID_OTHER
, UID_OTHER
);
146 if (asroot
&& injail
)
147 expect("priv_setreuid(asroot, injail)", error
, 0, 0);
148 if (asroot
&& !injail
)
149 expect("priv_setreuid(asroot, !injail)", error
, 0, 0);
150 if (!asroot
&& injail
)
151 expect("priv_setreuid(!asroot, injail)", error
, -1, EPERM
);
152 if (!asroot
&& !injail
)
153 expect("priv_setreuid(!asroot, !injail)", error
, -1, EPERM
);
157 priv_cred_setregid(int asroot
, int injail
, struct test
*test
)
161 error
= setregid(GID_OTHER
, GID_OTHER
);
162 if (asroot
&& injail
)
163 expect("priv_setregid(asroot, injail)", error
, 0, 0);
164 if (asroot
&& !injail
)
165 expect("priv_setregid(asroot, !injail)", error
, 0, 0);
166 if (!asroot
&& injail
)
167 expect("priv_setregid(!asroot, injail)", error
, -1, EPERM
);
168 if (!asroot
&& !injail
)
169 expect("priv_setregid(!asroot, !injail)", error
, -1, EPERM
);
173 priv_cred_setresuid(int asroot
, int injail
, struct test
*test
)
177 error
= setresuid(UID_OTHER
, UID_OTHER
, UID_OTHER
);
178 if (asroot
&& injail
)
179 expect("priv_setresuid(asroot, injail)", error
, 0, 0);
180 if (asroot
&& !injail
)
181 expect("priv_setresuid(asroot, !injail)", error
, 0, 0);
182 if (!asroot
&& injail
)
183 expect("priv_setresuid(!asroot, injail)", error
, -1, EPERM
);
184 if (!asroot
&& !injail
)
185 expect("priv_setresuid(!asroot, !injail)", error
, -1, EPERM
);
189 priv_cred_setresgid(int asroot
, int injail
, struct test
*test
)
193 error
= setresgid(GID_OTHER
, GID_OTHER
, GID_OTHER
);
194 if (asroot
&& injail
)
195 expect("priv_setresgid(asroot, injail)", error
, 0, 0);
196 if (asroot
&& !injail
)
197 expect("priv_setresgid(asroot, !injail)", error
, 0, 0);
198 if (!asroot
&& injail
)
199 expect("priv_setresgid(!asroot, injail)", error
, -1, EPERM
);
200 if (!asroot
&& !injail
)
201 expect("priv_setresgid(!asroot, !injail)", error
, -1, EPERM
);
205 priv_cred_cleanup(int asroot
, int injail
, struct test
*test
)