1 /* $OpenBSD: t_revoke.c,v 1.2 2020/11/09 23:18:51 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 #ifdef __OpenBSD__ 122 ATF_REQUIRE_ERRNO(ENOTTY, revoke("/etc/passwd") == -1); 123 #else 124 ATF_REQUIRE_ERRNO(EPERM, revoke("/etc/passwd") == -1); 125 #endif 126 127 errno = 0; 128 ATF_REQUIRE_ERRNO(ENOENT, revoke("/etc/xxx/yyy") == -1); 129 } 130 131 ATF_TC_WITH_CLEANUP(revoke_perm); 132 ATF_TC_HEAD(revoke_perm, tc) 133 { 134 atf_tc_set_md_var(tc, "descr", "Test permissions revoke(2)"); 135 atf_tc_set_md_var(tc, "require.user", "root"); 136 } 137 138 ATF_TC_BODY(revoke_perm, tc) 139 { 140 struct passwd *pw; 141 int fd, sta; 142 pid_t pid; 143 144 pw = getpwnam("nobody"); 145 fd = open(path, O_RDWR | O_CREAT, 0600); 146 147 ATF_REQUIRE(fd >= 0); 148 ATF_REQUIRE(pw != NULL); 149 ATF_REQUIRE(revoke(path) == 0); 150 151 pid = fork(); 152 ATF_REQUIRE(pid >= 0); 153 154 if (pid == 0) { 155 156 if (setuid(pw->pw_uid) != 0) 157 _exit(EXIT_FAILURE); 158 159 errno = 0; 160 161 if (revoke(path) == 0) 162 _exit(EXIT_FAILURE); 163 164 if (errno != EACCES) 165 _exit(EXIT_FAILURE); 166 167 if (close(fd) != 0) 168 _exit(EXIT_FAILURE); 169 170 _exit(EXIT_SUCCESS); 171 } 172 173 (void)wait(&sta); 174 175 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 176 atf_tc_fail("revoke(2) did not obey permissions"); 177 178 (void)close(fd); 179 ATF_REQUIRE(unlink(path) == 0); 180 } 181 182 ATF_TC_CLEANUP(revoke_perm, tc) 183 { 184 (void)unlink(path); 185 } 186 187 ATF_TP_ADD_TCS(tp) 188 { 189 190 #ifndef __OpenBSD__ 191 /* OpenBSD supports revoke only on ttys */ 192 ATF_TP_ADD_TC(tp, revoke_basic); 193 #endif 194 ATF_TP_ADD_TC(tp, revoke_err); 195 #ifndef __OpenBSD__ 196 /* OpenBSD supports revoke only on ttys */ 197 ATF_TP_ADD_TC(tp, revoke_perm); 198 #endif 199 200 return atf_no_error(); 201 } 202