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