]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/priv/priv_vfs_admin.c
This commit was generated by cvs2svn to compensate for changes in r168305,
[FreeBSD/FreeBSD.git] / tools / regression / priv / priv_vfs_admin.c
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson for the TrustedBSD
6  * Project under contract to nCircle Network Security, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
28  *
29  * $FreeBSD$
30  */
31
32 /*
33  * Check that privilege is required for a variety of administrative
34  * activities on a file owned by another user.  Admin privileges are required
35  * for the following services:
36  *
37  * - Set file flags.
38  * - Set utimes to non-NULL.
39  * - Set file mode.
40  * - Set file ownership.
41  * - Remove a file from a sticky directory. (XXXRW: Not tested here.)
42  * - Set the ACL on a file.  (XXXRW: Not tested here.)
43  * - Delete the ACL on a file.  (XXXRW: Not tested here.)
44  */
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "main.h"
56
57 static u_long
58 getflags(char *fpathp)
59 {
60         struct stat sb;
61
62         if (stat(fpathp, &sb) < 0)
63                 err(-1, "stat(%s)", fpathp);
64
65         return (sb.st_flags);
66 }
67
68 static void
69 priv_vfs_admin_chflags(void)
70 {
71         char fpath[1024];
72         u_long flags;
73         int error;
74
75         /*
76          * Test that setting file flags works as and not as the file owner
77          * when running with privilege, but only as the file owner when
78          * running without privilege.
79          */
80         setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
81         flags = getflags(fpath);
82         flags |= UF_NODUMP;
83         if (chflags(fpath, flags) < 0) {
84                 warn("chflags(%s, UF_NODUMP) owner as root", fpath);
85                 goto out;
86         }
87         (void)unlink(fpath);
88
89         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
90         flags = getflags(fpath);
91         flags |= UF_NODUMP;
92         if (chflags(fpath, flags) < 0) {
93                 warn("chflags(%s, UF_NODUMP) !owner as root", fpath);
94                 goto out;
95         }
96         (void)unlink(fpath);
97
98         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
99         flags = getflags(fpath);
100         flags |= UF_NODUMP;
101         set_euid(UID_OWNER);
102         if (chflags(fpath, flags) < 0) {
103                 warn("chflags(%s, UF_NODUMP) owner as !root", fpath);
104                 goto out;
105         }
106         set_euid(UID_ROOT);
107         (void)unlink(fpath);
108
109         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
110         flags = getflags(fpath);
111         flags |= UF_NODUMP;
112         set_euid(UID_OTHER);
113         error = chflags(fpath, flags);
114         if (error == 0) {
115                 warnx("chflags(%s, UF_NODUMP) succeeded !owner as !root",
116                     fpath);
117                 goto out;
118         }
119         if (errno != EPERM) {
120                 warn("chflags(%s, UF_NODUMP) wrong errno %d !owner a !root",
121                     fpath, errno);
122                 goto out;
123         }
124         set_euid(UID_ROOT);
125         (void)unlink(fpath);
126
127 out:
128         (void)seteuid(UID_ROOT);
129         (void)unlink(fpath);
130 }
131
132 static void
133 priv_vfs_admin_utimes(void)
134 {
135         struct timeval tv[2];
136         char fpath[1024];
137         int error;
138
139         /*
140          * Actual values don't matter here.
141          */
142         tv[0].tv_sec = 0;
143         tv[0].tv_usec = 0;
144         tv[1].tv_sec = 0;
145         tv[1].tv_usec = 0;
146
147         /*
148          * When using a non-NULL argument to utimes(), must either hold
149          * privilege or be the file owner.  Check all four possible
150          * combinations of privilege, ownership.
151          */
152         setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
153         if (utimes(fpath, tv) < 0) {
154                 warn("utimes(%s, !NULL) owner as root", fpath);
155                 goto out;
156         }
157         (void)unlink(fpath);
158
159         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
160         if (utimes(fpath, tv) < 0) {
161                 warn("utimes(%s, !NULL) !owner as root", fpath);
162                 goto out;
163         }
164         (void)unlink(fpath);
165
166         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
167         set_euid(UID_OWNER);
168         if (utimes(fpath, tv) < 0) {
169                 warn("utimes(%s, !NULL) owner as !root", fpath);
170                 goto out;
171         }
172         set_euid(UID_ROOT);
173         (void)unlink(fpath);
174
175         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
176         set_euid(UID_OTHER);
177         error = utimes(fpath, tv);
178         if (error == 0) {
179                 warnx("utimes(%s, !NULL) succeeded !owner as !root",
180                     fpath);
181                 goto out;
182         }
183         if (errno != EPERM) {
184                 warn("utimes(%s, !NULL) wrong errno %d !owner a !root",
185                     fpath, errno);
186                 goto out;
187         }
188         set_euid(UID_ROOT);
189         (void)unlink(fpath);
190
191 out:
192         (void)seteuid(UID_ROOT);
193         (void)unlink(fpath);
194 }
195
196 static void
197 priv_vfs_admin_chmod(void)
198 {
199         char fpath[1024];
200         int error;
201
202         /*
203          * Test that setting file permissions works either as file owner or
204          * not when running with privilege, but only as file owner when
205         * running without privilege.
206          */
207         setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
208         if (chmod(fpath, 0640) < 0) {
209                 warn("chmod(%s, 0640) owner as root", fpath);
210                 goto out;
211         }
212         (void)unlink(fpath);
213
214         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
215         if (chmod(fpath, 0640) < 0) {
216                 warn("chmod(%s, 0640) !owner as root", fpath);
217                 goto out;
218         }
219         (void)unlink(fpath);
220
221         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
222         set_euid(UID_OWNER);
223         if (chmod(fpath, 0640) < 0) {
224                 warn("chmod(%s, 0640) owner as !root", fpath);
225                 goto out;
226         }
227         set_euid(UID_ROOT);
228         (void)unlink(fpath);
229
230         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
231         set_euid(UID_OTHER);
232         error = chmod(fpath, 0640);
233         if (error == 0) {
234                 warnx("chmod(%s, 0640) succeeded !owner as !root",
235                     fpath);
236                 goto out;
237         }
238         if (errno != EPERM) {
239                 warn("chmod(%s, 0640) wrong errno %d !owner a !root",
240                     fpath, errno);
241                 goto out;
242         }
243         set_euid(UID_ROOT);
244         (void)unlink(fpath);
245
246 out:
247         (void)seteuid(UID_ROOT);
248         (void)unlink(fpath);
249 }
250
251 static const gid_t gidset[] = { GID_WHEEL, GID_OWNER, GID_OTHER};
252
253 static void
254 priv_vfs_admin_chown(void)
255 {
256         char fpath[1024];
257         int error;
258
259         /*
260          * Test that the group of the file can only be changed with privilege
261          * or as the owner.  These test is run last as it frobs the group
262          * context.  We change the file group from one group we're in to
263          * another we're in to avoid any other access control checks failing.
264          */
265         if (setgroups(3, gidset) < 0)
266                 err(-1, "priv_vfs_admin_chown:setgroups(3, {%d, %d, %d})",
267                     GID_WHEEL, GID_OWNER, GID_OTHER);
268
269         /*
270          * Test that setting file permissions works either as file owner or
271          * not when running with privilege, but only as file owner when
272         * running without privilege.
273          */
274         setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
275         if (chown(fpath, -1, GID_OWNER) < 0) {
276                 warn("chown(%s, %d) owner as root", fpath, GID_OWNER);
277                 goto out;
278         }
279         (void)unlink(fpath);
280
281         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
282         if (chown(fpath, -1, GID_OWNER) < 0) {
283                 warn("chown(%s, %d) !owner as root", fpath, GID_OWNER);
284                 goto out;
285         }
286         (void)unlink(fpath);
287
288         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
289         set_euid(UID_OWNER);
290         if (chown(fpath, -1, GID_OWNER) < 0) {
291                 warn("chown(%s, %d) owner as !root", fpath, GID_OWNER);
292                 goto out;
293         }
294         set_euid(UID_ROOT);
295         (void)unlink(fpath);
296
297         setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
298         set_euid(UID_OTHER);
299         error = chown(fpath, -1, GID_OWNER);
300         if (error == 0) {
301                 warnx("chown(%s, %d) succeeded !owner as !root",
302                     fpath, GID_OWNER);
303                 goto out;
304         }
305         if (errno != EPERM) {
306                 warn("chown(%s, %d) wrong errno %d !owner a !root",
307                     fpath, GID_OWNER, errno);
308                 goto out;
309         }
310         set_euid(UID_ROOT);
311         (void)unlink(fpath);
312
313 out:
314         (void)seteuid(UID_ROOT);
315         (void)unlink(fpath);
316 }
317
318 void
319 priv_vfs_admin(void)
320 {
321
322         assert_root();
323
324         priv_vfs_admin_chflags();
325         priv_vfs_admin_utimes();
326         priv_vfs_admin_chmod();
327         priv_vfs_admin_chown(); /* Run this last. */
328 }