summaryrefslogtreecommitdiffstats
path: root/tools/regression/priv/priv_vfs_admin.c
blob: f06e9b26215d4b54998cb10d09d9d3d2cf48aa2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*-
 * Copyright (c) 2006 nCircle Network Security, Inc.
 * All rights reserved.
 *
 * This software was developed by Robert N. M. Watson for the TrustedBSD
 * Project under contract to nCircle Network Security, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Check that privilege is required for a variety of administrative
 * activities on a file owned by another user.  Admin privileges are required
 * for the following services:
 *
 * - Set file flags.
 * - Set utimes to non-NULL.
 * - Set file mode.
 * - Set file ownership.
 * - Remove a file from a sticky directory. (XXXRW: Not tested here.)
 * - Set the ACL on a file.  (XXXRW: Not tested here.)
 * - Delete the ACL on a file.  (XXXRW: Not tested here.)
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "main.h"

static u_long
getflags(char *fpathp)
{
	struct stat sb;

	if (stat(fpathp, &sb) < 0)
		err(-1, "stat(%s)", fpathp);

	return (sb.st_flags);
}

static void
priv_vfs_admin_chflags(void)
{
	char fpath[1024];
	u_long flags;
	int error;

	/*
	 * Test that setting file flags works as and not as the file owner
	 * when running with privilege, but only as the file owner when
	 * running without privilege.
	 */
	setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
	flags = getflags(fpath);
	flags |= UF_NODUMP;
	if (chflags(fpath, flags) < 0) {
		warn("chflags(%s, UF_NODUMP) owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	flags = getflags(fpath);
	flags |= UF_NODUMP;
	if (chflags(fpath, flags) < 0) {
		warn("chflags(%s, UF_NODUMP) !owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	flags = getflags(fpath);
	flags |= UF_NODUMP;
	set_euid(UID_OWNER);
	if (chflags(fpath, flags) < 0) {
		warn("chflags(%s, UF_NODUMP) owner as !root", fpath);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	flags = getflags(fpath);
	flags |= UF_NODUMP;
	set_euid(UID_OTHER);
	error = chflags(fpath, flags);
	if (error == 0) {
		warnx("chflags(%s, UF_NODUMP) succeeded !owner as !root",
		    fpath);
		goto out;
	}
	if (errno != EPERM) {
		warn("chflags(%s, UF_NODUMP) wrong errno %d !owner a !root",
		    fpath, errno);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

out:
	(void)seteuid(UID_ROOT);
	(void)unlink(fpath);
}

static void
priv_vfs_admin_utimes(void)
{
	struct timeval tv[2];
	char fpath[1024];
	int error;

	/*
	 * Actual values don't matter here.
	 */
	tv[0].tv_sec = 0;
	tv[0].tv_usec = 0;
	tv[1].tv_sec = 0;
	tv[1].tv_usec = 0;

	/*
	 * When using a non-NULL argument to utimes(), must either hold
	 * privilege or be the file owner.  Check all four possible
	 * combinations of privilege, ownership.
	 */
	setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
	if (utimes(fpath, tv) < 0) {
		warn("utimes(%s, !NULL) owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	if (utimes(fpath, tv) < 0) {
		warn("utimes(%s, !NULL) !owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OWNER);
	if (utimes(fpath, tv) < 0) {
		warn("utimes(%s, !NULL) owner as !root", fpath);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OTHER);
	error = utimes(fpath, tv);
	if (error == 0) {
		warnx("utimes(%s, !NULL) succeeded !owner as !root",
		    fpath);
		goto out;
	}
	if (errno != EPERM) {
		warn("utimes(%s, !NULL) wrong errno %d !owner a !root",
		    fpath, errno);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

out:
	(void)seteuid(UID_ROOT);
	(void)unlink(fpath);
}

static void
priv_vfs_admin_chmod(void)
{
	char fpath[1024];
	int error;

	/*
	 * Test that setting file permissions works either as file owner or
	 * not when running with privilege, but only as file owner when
	* running without privilege.
	 */
	setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
	if (chmod(fpath, 0640) < 0) {
		warn("chmod(%s, 0640) owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	if (chmod(fpath, 0640) < 0) {
		warn("chmod(%s, 0640) !owner as root", fpath);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OWNER);
	if (chmod(fpath, 0640) < 0) {
		warn("chmod(%s, 0640) owner as !root", fpath);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OTHER);
	error = chmod(fpath, 0640);
	if (error == 0) {
		warnx("chmod(%s, 0640) succeeded !owner as !root",
		    fpath);
		goto out;
	}
	if (errno != EPERM) {
		warn("chmod(%s, 0640) wrong errno %d !owner a !root",
		    fpath, errno);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

out:
	(void)seteuid(UID_ROOT);
	(void)unlink(fpath);
}

static const gid_t gidset[] = { GID_WHEEL, GID_OWNER, GID_OTHER};

static void
priv_vfs_admin_chown(void)
{
	char fpath[1024];
	int error;

	/*
	 * Test that the group of the file can only be changed with privilege
	 * or as the owner.  These test is run last as it frobs the group
	 * context.  We change the file group from one group we're in to
	 * another we're in to avoid any other access control checks failing.
	 */
	if (setgroups(3, gidset) < 0)
		err(-1, "priv_vfs_admin_chown:setgroups(3, {%d, %d, %d})",
		    GID_WHEEL, GID_OWNER, GID_OTHER);

	/*
	 * Test that setting file permissions works either as file owner or
	 * not when running with privilege, but only as file owner when
	* running without privilege.
	 */
	setup_file(fpath, UID_ROOT, GID_WHEEL, 0600);
	if (chown(fpath, -1, GID_OWNER) < 0) {
		warn("chown(%s, %d) owner as root", fpath, GID_OWNER);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	if (chown(fpath, -1, GID_OWNER) < 0) {
		warn("chown(%s, %d) !owner as root", fpath, GID_OWNER);
		goto out;
	}
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OWNER);
	if (chown(fpath, -1, GID_OWNER) < 0) {
		warn("chown(%s, %d) owner as !root", fpath, GID_OWNER);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

	setup_file(fpath, UID_OWNER, GID_OWNER, 0600);
	set_euid(UID_OTHER);
	error = chown(fpath, -1, GID_OWNER);
	if (error == 0) {
		warnx("chown(%s, %d) succeeded !owner as !root",
		    fpath, GID_OWNER);
		goto out;
	}
	if (errno != EPERM) {
		warn("chown(%s, %d) wrong errno %d !owner a !root",
		    fpath, GID_OWNER, errno);
		goto out;
	}
	set_euid(UID_ROOT);
	(void)unlink(fpath);

out:
	(void)seteuid(UID_ROOT);
	(void)unlink(fpath);
}

void
priv_vfs_admin(void)
{

	assert_root();

	priv_vfs_admin_chflags();
	priv_vfs_admin_utimes();
	priv_vfs_admin_chmod();
	priv_vfs_admin_chown();	/* Run this last. */
}
OpenPOWER on IntegriCloud