1 /* $NetBSD: t_revoke.c,v 1.2 2017/01/13 21:15:57 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_revoke.c,v 1.2 2017/01/13 21:15:57 christos Exp $");
33 
34 #include <sys/resource.h>
35 #include <sys/wait.h>
36 
37 #include <atf-c.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 static const char path[] = "revoke";
47 
48 ATF_TC_WITH_CLEANUP(revoke_basic);
49 ATF_TC_HEAD(revoke_basic, tc)
50 {
51 	atf_tc_set_md_var(tc, "descr", "A basic test of revoke(2)");
52 }
53 
54 ATF_TC_BODY(revoke_basic, tc)
55 {
56 	struct rlimit res;
57 	char tmp[10];
58 	size_t i, n;
59 	int *buf;
60 
61 #ifdef __FreeBSD__
62 	atf_tc_skip("revoke(2) is only implemented for devfs(5).");
63 #endif
64 	(void)memset(&res, 0, sizeof(struct rlimit));
65 	(void)getrlimit(RLIMIT_NOFILE, &res);
66 
67 	if ((n = res.rlim_cur / 10) == 0)
68 		n = 10;
69 
70 	buf = calloc(n, sizeof(int));
71 	ATF_REQUIRE(buf != NULL);
72 
73 	buf[0] = open(path, O_RDWR | O_CREAT, 0600);
74 	ATF_REQUIRE(buf[0] >= 0);
75 
76 	for (i = 1; i < n; i++) {
77 		buf[i] = open(path, O_RDWR);
78 		ATF_REQUIRE(buf[i] >= 0);
79 	}
80 
81 	ATF_REQUIRE(revoke(path) == 0);
82 
83 	for (i = 0; i < n; i++) {
84 
85 		ATF_REQUIRE(read(buf[i], tmp, sizeof(tmp)) == -1);
86 
87 		(void)close(buf[i]);
88 	}
89 
90 	free(buf);
91 
92 	(void)unlink(path);
93 }
94 
95 ATF_TC_CLEANUP(revoke_basic, tc)
96 {
97 	(void)unlink(path);
98 }
99 
100 ATF_TC(revoke_err);
101 ATF_TC_HEAD(revoke_err, tc)
102 {
103 	atf_tc_set_md_var(tc, "descr", "Test errors from revoke(2)");
104 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
105 }
106 
107 ATF_TC_BODY(revoke_err, tc)
108 {
109 	char buf[1024 + 1];	/* XXX: From the manual page... */
110 
111 	(void)memset(buf, 'x', sizeof(buf));
112 
113 	errno = 0;
114 	ATF_REQUIRE_ERRNO(EFAULT, revoke((char *)-1) == -1);
115 
116 	errno = 0;
117 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, revoke(buf) == -1);
118 
119 #ifdef __FreeBSD__
120 	atf_tc_skip("revoke(2) is only implemented for devfs(5).");
121 #endif
122 	errno = 0;
123 	ATF_REQUIRE_ERRNO(EPERM, revoke("/etc/passwd") == -1);
124 
125 	errno = 0;
126 	ATF_REQUIRE_ERRNO(ENOENT, revoke("/etc/xxx/yyy") == -1);
127 }
128 
129 ATF_TC_WITH_CLEANUP(revoke_perm);
130 ATF_TC_HEAD(revoke_perm, tc)
131 {
132 	atf_tc_set_md_var(tc, "descr", "Test permissions revoke(2)");
133 	atf_tc_set_md_var(tc, "require.user", "root");
134 }
135 
136 ATF_TC_BODY(revoke_perm, tc)
137 {
138 	struct passwd *pw;
139 	int fd, sta;
140 	pid_t pid;
141 
142 #ifdef __FreeBSD__
143 	atf_tc_skip("revoke(2) is only implemented for devfs(5).");
144 #endif
145 	pw = getpwnam("nobody");
146 	fd = open(path, O_RDWR | O_CREAT, 0600);
147 
148 	ATF_REQUIRE(fd >= 0);
149 	ATF_REQUIRE(pw != NULL);
150 	ATF_REQUIRE(revoke(path) == 0);
151 
152 	pid = fork();
153 	ATF_REQUIRE(pid >= 0);
154 
155 	if (pid == 0) {
156 
157 		if (setuid(pw->pw_uid) != 0)
158 			_exit(EXIT_FAILURE);
159 
160 		errno = 0;
161 
162 		if (revoke(path) == 0)
163 			_exit(EXIT_FAILURE);
164 
165 		if (errno != EACCES)
166 			_exit(EXIT_FAILURE);
167 
168 		if (close(fd) != 0)
169 			_exit(EXIT_FAILURE);
170 
171 		_exit(EXIT_SUCCESS);
172 	}
173 
174 	(void)wait(&sta);
175 
176 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
177 		atf_tc_fail("revoke(2) did not obey permissions");
178 
179 	(void)close(fd);
180 	ATF_REQUIRE(unlink(path) == 0);
181 }
182 
183 ATF_TC_CLEANUP(revoke_perm, tc)
184 {
185 	(void)unlink(path);
186 }
187 
188 ATF_TP_ADD_TCS(tp)
189 {
190 
191 	ATF_TP_ADD_TC(tp, revoke_basic);
192 	ATF_TP_ADD_TC(tp, revoke_err);
193 	ATF_TP_ADD_TC(tp, revoke_perm);
194 
195 	return atf_no_error();
196 }
197