t_unpriv.c revision 1.16.12.2 1 /* $NetBSD: t_unpriv.c,v 1.16.12.2 2025/08/02 05:58:00 perseant 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) || FSTYPE_EXFATFS(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 fprintf(stderr, "OPEN AS USER #1\n");
98 if (rump_sys_open(name, O_RDWR|O_CREAT, 0666) != -1 || errno != EACCES)
99 atf_tc_fail_errno("open");
100 rump_pub_lwproc_releaselwp();
101
102 fprintf(stderr, "OPEN AS USER #0\n");
103 if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
104 atf_tc_fail_errno("open");
105 fprintf(stderr, "CLOSE AS USER #0\n");
106 if (rump_sys_close(fd) == -1)
107 atf_tc_fail_errno("close");
108
109 rump_pub_lwproc_rfork(RUMP_RFCFDG);
110 if (rump_sys_setuid(1) == -1)
111 atf_tc_fail_errno("setuid");
112 fprintf(stderr, "UNLINK AS USER #1\n");
113 if (rump_sys_unlink(name) != -1)
114 atf_tc_fail_errno("unlink[1]: succeeded");
115 if (errno != EACCES)
116 atf_tc_fail_errno("unlink[1]: not EACCES");
117 rump_pub_lwproc_releaselwp();
118
119 fprintf(stderr, "UNLINK AS USER #0\n");
120 if (rump_sys_unlink(name) == -1)
121 atf_tc_fail_errno("unlink[0]");
122
123 if (rump_sys_rmdir(dir) == -1)
124 atf_tc_fail_errno("rmdir");
125
126 FSTEST_EXIT();
127 }
128
129 static void
130 times(const atf_tc_t *tc, const char *mp)
131 {
132 const char *name = "file.test";
133 int fd;
134 unsigned int i, j;
135 struct timeval tmv[2];
136 static struct timeval tmvs[] = {
137 { QUAD_MIN, 0 },
138 { 0, 0 },
139 { QUAD_MAX, 999999 }
140 };
141
142 FSTEST_ENTER();
143
144 if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
145 atf_tc_fail_errno("open");
146 if (rump_sys_close(fd) == -1)
147 atf_tc_fail_errno("close");
148
149 rump_pub_lwproc_rfork(RUMP_RFCFDG);
150 if (rump_sys_setuid(1) == -1)
151 atf_tc_fail_errno("setuid");
152 if (rump_sys_utimes(name, NULL) != -1)
153 atf_tc_fail_errno("utimes: succeeded");
154 if (errno != EACCES)
155 atf_tc_fail_errno("utimes: not EACCES");
156 rump_pub_lwproc_releaselwp();
157
158 if (rump_sys_utimes(name, NULL) == -1)
159 atf_tc_fail_errno("utimes");
160
161 for (i = 0; i < sizeof(tmvs) / sizeof(tmvs[0]); i++) {
162 for (j = 0; j < sizeof(tmvs) / sizeof(tmvs[0]); j++) {
163 tmv[0] = tmvs[i];
164 tmv[1] = tmvs[j];
165 rump_pub_lwproc_rfork(RUMP_RFCFDG);
166 if (rump_sys_setuid(1) == -1)
167 atf_tc_fail_errno("setuid");
168 if (rump_sys_utimes(name, tmv) != -1)
169 atf_tc_fail_errno("utimes[1]: succeeded");
170 if (errno != EPERM)
171 atf_tc_fail_errno("utimes[1]: not EPERM");
172 rump_pub_lwproc_releaselwp();
173
174 if (rump_sys_utimes(name, tmv) == -1)
175 atf_tc_fail_errno("utimes");
176 }
177 }
178
179 if (rump_sys_unlink(name) == -1)
180 atf_tc_fail_errno("unlink");
181
182 FSTEST_EXIT();
183 }
184
185 static void
186 flags(const atf_tc_t *tc, const char *mp)
187 {
188 const char *name = "file.test";
189 int fd, fflags;
190 struct stat st;
191
192 FSTEST_ENTER();
193
194 if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
195 atf_tc_fail_errno("open");
196 if (rump_sys_close(fd) == -1)
197 atf_tc_fail_errno("close");
198
199 if (rump_sys_stat(name, &st) == -1)
200 atf_tc_fail_errno("stat");
201 if (rump_sys_chflags(name, st.st_flags) == -1) {
202 if (errno == EOPNOTSUPP)
203 atf_tc_skip("file flags not supported by file system");
204 atf_tc_fail_errno("chflags[0]");
205 }
206
207 fflags = st.st_flags | UF_NODUMP;
208
209 if (rump_sys_stat(mp, &st) == -1)
210 atf_tc_fail_errno("stat[0]");
211 fprintf(stderr, "stat(%s) -> mode=%o\n", mp, st.st_mode);
212 rump_pub_lwproc_rfork(RUMP_RFCFDG);
213 if (rump_sys_setuid(1) == -1)
214 atf_tc_fail_errno("setuid");
215 if (rump_sys_stat(name, &st) == -1)
216 atf_tc_fail_errno("stat[1]");
217 fflags |= UF_NODUMP;
218 if (rump_sys_chflags(name, fflags) != -1)
219 atf_tc_fail_errno("chflags[1]: succeeded");
220 if (errno != EPERM) {
221 fprintf(stderr, "errno = %d\n", errno);
222 atf_tc_fail_errno("chflags[1]: not EPERM");
223 }
224 rump_pub_lwproc_releaselwp();
225
226 if (rump_sys_chflags(name, fflags) == -1)
227 atf_tc_fail_errno("chflags[2]");
228
229 fflags &= ~UF_NODUMP;
230 if (rump_sys_chflags(name, fflags) == -1)
231 atf_tc_fail_errno("chflags[3]");
232
233 if (rump_sys_unlink(name) == -1)
234 atf_tc_fail_errno("unlink");
235
236 FSTEST_EXIT();
237 }
238
239 ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
240 ATF_TC_FSAPPLY(dirperms, "directory permission checks");
241 ATF_TC_FSAPPLY(times, "time set checks");
242 ATF_TC_FSAPPLY(flags, "file flags checks");
243
244 ATF_TP_ADD_TCS(tp)
245 {
246
247 ATF_TP_FSAPPLY(owner);
248 ATF_TP_FSAPPLY(dirperms);
249 ATF_TP_FSAPPLY(times);
250 ATF_TP_FSAPPLY(flags);
251
252 return atf_no_error();
253 }
254