xref: /netbsd-src/tests/fs/vfs/t_unpriv.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: t_unpriv.c,v 1.8 2012/03/26 15:13:20 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 	struct timeval tmv[2];
126 
127 	FSTEST_ENTER();
128 
129 	if ((fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666)) == -1)
130 		atf_tc_fail_errno("open");
131 	if (rump_sys_close(fd) == -1)
132 		atf_tc_fail_errno("close");
133 
134 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
135 	if (rump_sys_setuid(1) == -1)
136 		atf_tc_fail_errno("setuid");
137 	if (rump_sys_utimes(name, NULL) != -1 || errno != EACCES)
138 		atf_tc_fail_errno("utimes");
139 	rump_pub_lwproc_releaselwp();
140 
141 	if (rump_sys_utimes(name, NULL) == -1)
142 		atf_tc_fail_errno("utimes");
143 
144 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
145 	if (rump_sys_setuid(1) == -1)
146 		atf_tc_fail_errno("setuid");
147 	if (rump_sys_utimes(name, tmv) != -1 || errno != EPERM)
148 		atf_tc_fail_errno("utimes");
149 	rump_pub_lwproc_releaselwp();
150 
151 	if (rump_sys_utimes(name, tmv) == -1)
152 		atf_tc_fail_errno("utimes");
153 
154 	if (rump_sys_unlink(name) == -1)
155 		atf_tc_fail_errno("unlink");
156 
157 	FSTEST_EXIT();
158 }
159 
160 ATF_TC_FSAPPLY(owner, "owner unprivileged checks");
161 ATF_TC_FSAPPLY(dirperms, "directory permission checks");
162 ATF_TC_FSAPPLY(times, "time set checks");
163 
164 ATF_TP_ADD_TCS(tp)
165 {
166 
167 	ATF_TP_FSAPPLY(owner);
168 	ATF_TP_FSAPPLY(dirperms);
169 	ATF_TP_FSAPPLY(times);
170 
171 	return atf_no_error();
172 }
173