xref: /freebsd/contrib/netbsd-tests/fs/vfs/t_unpriv.c (revision 780fb4a2)
1 /*	$NetBSD: t_unpriv.c,v 1.13 2017/01/13 21:30:40 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 
32 #include <atf-c.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <unistd.h>
36 
37 #include <rump/rump_syscalls.h>
38 #include <rump/rump.h>
39 
40 #include "../common/h_fsmacros.h"
41 #include "h_macros.h"
42 
43 #define USES_OWNER							 \
44 	if (FSTYPE_MSDOS(tc))						 \
45 	    atf_tc_skip("owner not supported by file system")
46 
47 static void
48 owner(const atf_tc_t *tc, const char *mp)
49 {
50 
51 	USES_OWNER;
52 
53 	FSTEST_ENTER();
54 
55 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
56 	if (rump_sys_setuid(1) == -1)
57 		atf_tc_fail_errno("setuid");
58         if (rump_sys_chown(".", 1, -1) != -1 || errno != EPERM)
59 		atf_tc_fail_errno("chown");
60         if (rump_sys_chmod(".", 0000) != -1 || errno != EPERM)
61                 atf_tc_fail_errno("chmod");
62 	rump_pub_lwproc_releaselwp();
63 
64 	if (rump_sys_chown(".", 1, -1) == -1)
65 		atf_tc_fail_errno("chown");
66 
67 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
68 	if (rump_sys_setuid(1) == -1)
69 		atf_tc_fail_errno("setuid");
70         if (rump_sys_chown(".", 1, -1) == -1)
71 		atf_tc_fail_errno("chown");
72         if (rump_sys_chmod(".", 0000) == -1)
73                 atf_tc_fail_errno("chmod");
74 	rump_pub_lwproc_releaselwp();
75 
76 	FSTEST_EXIT();
77 }
78 
79 static void
80 dirperms(const atf_tc_t *tc, const char *mp)
81 {
82 	char name[] = "dir.test/file.test";
83 	char *dir = dirname(name);
84 	int fd;
85 
86 	if (FSTYPE_SYSVBFS(tc))
87 		atf_tc_skip("directories not supported by file system");
88 
89 	FSTEST_ENTER();
90 
91 	if (rump_sys_mkdir(dir, 0777) == -1)
92 		atf_tc_fail_errno("mkdir");
93 
94 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
95 	if (rump_sys_setuid(1) == -1)
96 		atf_tc_fail_errno("setuid");
97 	if (FSTYPE_ZFS(tc))
98 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
99         if (rump_sys_open(name, O_RDWR|O_CREAT, 0666) != -1 || errno != EACCES)
100 		atf_tc_fail_errno("open");
101 	rump_pub_lwproc_releaselwp();
102 
103 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
104 		atf_tc_fail_errno("open");
105 	if (rump_sys_close(fd) == -1)
106 		atf_tc_fail_errno("close");
107 
108 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
109 	if (rump_sys_setuid(1) == -1)
110 		atf_tc_fail_errno("setuid");
111         if (rump_sys_unlink(name) != -1 || errno != EACCES)
112 		atf_tc_fail_errno("unlink");
113 	rump_pub_lwproc_releaselwp();
114 
115         if (rump_sys_unlink(name) == -1)
116 		atf_tc_fail_errno("unlink");
117 
118 	if (rump_sys_rmdir(dir) == -1)
119 		atf_tc_fail_errno("rmdir");
120 
121 	FSTEST_EXIT();
122 }
123 
124 static void
125 times(const atf_tc_t *tc, const char *mp)
126 {
127 	const char *name = "file.test";
128 	int fd;
129 	unsigned int i, j;
130 	struct timeval tmv[2];
131 	static struct timeval tmvs[] = {
132 		{ QUAD_MIN, 0 },
133 		{ 0, 0 },
134 		{ QUAD_MAX, 999999 }
135 	};
136 
137 	FSTEST_ENTER();
138 
139 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
140 		atf_tc_fail_errno("open");
141 	if (rump_sys_close(fd) == -1)
142 		atf_tc_fail_errno("close");
143 
144 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
145 	if (rump_sys_setuid(1) == -1)
146 		atf_tc_fail_errno("setuid");
147 	if (FSTYPE_ZFS(tc))
148 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
149 	if (rump_sys_utimes(name, NULL) != -1 || errno != EACCES)
150 		atf_tc_fail_errno("utimes");
151 	rump_pub_lwproc_releaselwp();
152 
153 	if (rump_sys_utimes(name, NULL) == -1)
154 		atf_tc_fail_errno("utimes");
155 
156 	for (i = 0; i < sizeof(tmvs) / sizeof(tmvs[0]); i++) {
157 		for (j = 0; j < sizeof(tmvs) / sizeof(tmvs[0]); j++) {
158 			tmv[0] = tmvs[i];
159 			tmv[1] = tmvs[j];
160 			rump_pub_lwproc_rfork(RUMP_RFCFDG);
161 			if (rump_sys_setuid(1) == -1)
162 				atf_tc_fail_errno("setuid");
163 			if (rump_sys_utimes(name, tmv) != -1 || errno != EPERM)
164 				atf_tc_fail_errno("utimes");
165 			rump_pub_lwproc_releaselwp();
166 
167 			if (rump_sys_utimes(name, tmv) == -1)
168 				atf_tc_fail_errno("utimes");
169 		}
170 	}
171 
172 	if (rump_sys_unlink(name) == -1)
173 		atf_tc_fail_errno("unlink");
174 
175 	FSTEST_EXIT();
176 }
177 
178 static void
179 flags(const atf_tc_t *tc, const char *mp)
180 {
181 	const char *name = "file.test";
182 	int fd, fflags;
183 	struct stat st;
184 
185 	FSTEST_ENTER();
186 
187 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
188 		atf_tc_fail_errno("open");
189 	if (rump_sys_close(fd) == -1)
190 		atf_tc_fail_errno("close");
191 
192 	if (rump_sys_stat(name, &st) == -1)
193 		atf_tc_fail_errno("stat");
194 	if (FSTYPE_ZFS(tc))
195 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
196 	if (rump_sys_chflags(name, st.st_flags) == -1) {
197 		if (errno == EOPNOTSUPP)
198 			atf_tc_skip("file flags not supported by file system");
199 		atf_tc_fail_errno("chflags");
200 	}
201 
202 	fflags = st.st_flags | UF_IMMUTABLE;
203 
204 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
205 	if (rump_sys_setuid(1) == -1)
206 		atf_tc_fail_errno("setuid");
207 	fflags |= UF_IMMUTABLE;
208 	if (rump_sys_chflags(name, fflags) != -1 || errno != EPERM)
209 		atf_tc_fail_errno("chflags");
210 	rump_pub_lwproc_releaselwp();
211 
212 	if (rump_sys_chflags(name, fflags) == -1)
213 		atf_tc_fail_errno("chflags");
214 
215 	fflags &= ~UF_IMMUTABLE;
216 	if (rump_sys_chflags(name, fflags) == -1)
217 		atf_tc_fail_errno("chflags");
218 
219 	if (rump_sys_unlink(name) == -1)
220 		atf_tc_fail_errno("unlink");
221 
222 	FSTEST_EXIT();
223 }
224 
225 ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
226 ATF_TC_FSAPPLY(dirperms, "directory permission checks");
227 ATF_TC_FSAPPLY(times, "time set checks");
228 ATF_TC_FSAPPLY(flags, "file flags checks");
229 
230 ATF_TP_ADD_TCS(tp)
231 {
232 
233 	ATF_TP_FSAPPLY(owner);
234 	ATF_TP_FSAPPLY(dirperms);
235 	ATF_TP_FSAPPLY(times);
236 	ATF_TP_FSAPPLY(flags);
237 
238 	return atf_no_error();
239 }
240