t_unpriv.c revision 1.6 1 /* $NetBSD: t_unpriv.c,v 1.6 2012/03/15 12:57:27 njoly 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/time.h>
30
31 #include <atf-c.h>
32 #include <libgen.h>
33 #include <unistd.h>
34
35 #include <rump/rump_syscalls.h>
36 #include <rump/rump.h>
37
38 #include "../common/h_fsmacros.h"
39 #include "../../h_macros.h"
40
41 #define USES_OWNER \
42 if (FSTYPE_MSDOS(tc)) \
43 atf_tc_skip("owner not supported by file system")
44
45 static void
46 owner(const atf_tc_t *tc, const char *mp)
47 {
48
49 USES_OWNER;
50
51 FSTEST_ENTER();
52
53 rump_pub_lwproc_rfork(RUMP_RFCFDG);
54 if (rump_sys_setuid(1) == -1)
55 atf_tc_fail_errno("setuid");
56 if (rump_sys_chown(".", 1, -1) != -1 || errno != EPERM)
57 atf_tc_fail_errno("chown");
58 if (rump_sys_chmod(".", 0000) != -1 || errno != EPERM)
59 atf_tc_fail_errno("chmod");
60 rump_pub_lwproc_releaselwp();
61
62 if (rump_sys_chown(".", 1, -1) == -1)
63 atf_tc_fail_errno("chown");
64
65 rump_pub_lwproc_rfork(RUMP_RFCFDG);
66 if (rump_sys_setuid(1) == -1)
67 atf_tc_fail_errno("setuid");
68 if (rump_sys_chown(".", 1, -1) == -1)
69 atf_tc_fail_errno("chown");
70 if (rump_sys_chmod(".", 0000) == -1)
71 atf_tc_fail_errno("chmod");
72 rump_pub_lwproc_releaselwp();
73
74 FSTEST_EXIT();
75 }
76
77 static void
78 dirperms(const atf_tc_t *tc, const char *mp)
79 {
80 char name[] = "dir.test/file.test";
81 char *dir = dirname(name);
82 int fd;
83
84 if (FSTYPE_SYSVBFS(tc))
85 atf_tc_skip("directories not supported by file system");
86
87 FSTEST_ENTER();
88
89 if (rump_sys_mkdir(dir, 0777) == -1)
90 atf_tc_fail_errno("mkdir");
91
92 rump_pub_lwproc_rfork(RUMP_RFCFDG);
93 if (rump_sys_setuid(1) == -1)
94 atf_tc_fail_errno("setuid");
95 if (rump_sys_open(name, O_RDWR|O_CREAT, 0666) != -1 || errno != EACCES)
96 atf_tc_fail_errno("open");
97 rump_pub_lwproc_releaselwp();
98
99 if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
100 atf_tc_fail_errno("open");
101 if (rump_sys_close(fd) == -1)
102 atf_tc_fail_errno("close");
103
104 rump_pub_lwproc_rfork(RUMP_RFCFDG);
105 if (rump_sys_setuid(1) == -1)
106 atf_tc_fail_errno("setuid");
107 if (rump_sys_unlink(name) != -1 || errno != EACCES)
108 atf_tc_fail_errno("unlink");
109 rump_pub_lwproc_releaselwp();
110
111 if (rump_sys_unlink(name) == -1)
112 atf_tc_fail_errno("unlink");
113
114 if (rump_sys_rmdir(dir) == -1)
115 atf_tc_fail_errno("rmdir");
116
117 FSTEST_EXIT();
118 }
119
120 static void
121 times(const atf_tc_t *tc, const char *mp)
122 {
123 const char *name = "file.test";
124 int fd;
125
126 FSTEST_ENTER();
127
128 if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
129 atf_tc_fail_errno("open");
130 if (rump_sys_close(fd) == -1)
131 atf_tc_fail_errno("close");
132
133 rump_pub_lwproc_rfork(RUMP_RFCFDG);
134 if (rump_sys_setuid(1) == -1)
135 atf_tc_fail_errno("setuid");
136 if (rump_sys_utimes(name, NULL) != -1 || errno != EACCES)
137 atf_tc_fail_errno("utimes");
138 rump_pub_lwproc_releaselwp();
139
140 if (rump_sys_utimes(name, NULL) == -1)
141 atf_tc_fail_errno("utimes");
142
143 if (rump_sys_unlink(name) == -1)
144 atf_tc_fail_errno("unlink");
145
146 FSTEST_EXIT();
147 }
148
149 ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
150 ATF_TC_FSAPPLY(dirperms, "directory permission checks");
151 ATF_TC_FSAPPLY(times, "time set checks");
152
153 ATF_TP_ADD_TCS(tp)
154 {
155
156 ATF_TP_FSAPPLY(owner);
157 ATF_TP_FSAPPLY(dirperms);
158 ATF_TP_FSAPPLY(times);
159
160 return atf_no_error();
161 }
162