Boot-to-ramdisk image generation scripts
[minix3.git] / tests / fs / vfs / t_unpriv.c
blob6da771b45bcd5937adac77db522c648ce09af54d
1 /* $NetBSD: t_unpriv.c,v 1.12 2015/04/09 19:51:13 riastradh Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
29 #include <sys/stat.h>
30 #include <sys/time.h>
32 #include <atf-c.h>
33 #include <libgen.h>
34 #include <limits.h>
35 #include <unistd.h>
37 #include <rump/rump_syscalls.h>
38 #include <rump/rump.h>
40 #include "../common/h_fsmacros.h"
41 #include "../../h_macros.h"
43 #define USES_OWNER \
44 if (FSTYPE_MSDOS(tc)) \
45 atf_tc_skip("owner not supported by file system")
47 static void
48 owner(const atf_tc_t *tc, const char *mp)
51 USES_OWNER;
53 FSTEST_ENTER();
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();
64 if (rump_sys_chown(".", 1, -1) == -1)
65 atf_tc_fail_errno("chown");
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();
76 FSTEST_EXIT();
79 static void
80 dirperms(const atf_tc_t *tc, const char *mp)
82 char name[] = "dir.test/file.test";
83 char *dir = dirname(name);
84 int fd;
86 if (FSTYPE_SYSVBFS(tc))
87 atf_tc_skip("directories not supported by file system");
89 FSTEST_ENTER();
91 if (rump_sys_mkdir(dir, 0777) == -1)
92 atf_tc_fail_errno("mkdir");
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();
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");
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();
115 if (rump_sys_unlink(name) == -1)
116 atf_tc_fail_errno("unlink");
118 if (rump_sys_rmdir(dir) == -1)
119 atf_tc_fail_errno("rmdir");
121 FSTEST_EXIT();
124 static void
125 times(const atf_tc_t *tc, const char *mp)
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 }
137 FSTEST_ENTER();
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");
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();
153 if (rump_sys_utimes(name, NULL) == -1)
154 atf_tc_fail_errno("utimes");
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();
167 if (rump_sys_utimes(name, tmv) == -1)
168 atf_tc_fail_errno("utimes");
172 if (rump_sys_unlink(name) == -1)
173 atf_tc_fail_errno("unlink");
175 FSTEST_EXIT();
178 static void
179 flags(const atf_tc_t *tc, const char *mp)
181 const char *name = "file.test";
182 int fd, fflags;
183 struct stat st;
185 FSTEST_ENTER();
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");
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");
202 fflags = st.st_flags | UF_IMMUTABLE;
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();
212 if (rump_sys_chflags(name, fflags) == -1)
213 atf_tc_fail_errno("chflags");
215 fflags &= ~UF_IMMUTABLE;
216 if (rump_sys_chflags(name, fflags) == -1)
217 atf_tc_fail_errno("chflags");
219 if (rump_sys_unlink(name) == -1)
220 atf_tc_fail_errno("unlink");
222 FSTEST_EXIT();
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");
230 ATF_TP_ADD_TCS(tp)
233 ATF_TP_FSAPPLY(owner);
234 ATF_TP_FSAPPLY(dirperms);
235 ATF_TP_FSAPPLY(times);
236 ATF_TP_FSAPPLY(flags);
238 return atf_no_error();