1 /* $OpenBSD: t_access.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */ 2 /* $NetBSD: t_access.c,v 1.3 2019/07/16 17:29:18 martin 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 "atf-c.h" 36 37 #include <sys/stat.h> 38 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <limits.h> 42 #include <stdint.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 static const char path[] = "access"; 47 static const int mode[4] = { R_OK, W_OK, X_OK, F_OK }; 48 49 ATF_TC_WITH_CLEANUP(access_access); 50 ATF_TC_HEAD(access_access, tc) 51 { 52 atf_tc_set_md_var(tc, "descr", "Test access(2) for EACCES"); 53 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 54 } 55 56 ATF_TC_BODY(access_access, tc) 57 { 58 const int perm[3] = { 0200, 0400, 0000 }; 59 size_t i; 60 int fd; 61 62 fd = open(path, O_RDONLY | O_CREAT, 0600); 63 64 if (fd < 0) 65 return; 66 67 for (i = 0; i < __arraycount(mode) - 1; i++) { 68 69 ATF_REQUIRE(fchmod(fd, perm[i]) == 0); 70 71 errno = 0; 72 73 ATF_REQUIRE(access(path, mode[i]) != 0); 74 ATF_REQUIRE(errno == EACCES); 75 } 76 77 ATF_REQUIRE(close(fd) == 0); 78 } 79 80 ATF_TC_CLEANUP(access_access, tc) 81 { 82 (void)unlink(path); 83 } 84 85 ATF_TC(access_fault); 86 ATF_TC_HEAD(access_fault, tc) 87 { 88 atf_tc_set_md_var(tc, "descr", "Test access(2) for EFAULT"); 89 } 90 91 ATF_TC_BODY(access_fault, tc) 92 { 93 size_t i; 94 95 for (i = 0; i < __arraycount(mode); i++) { 96 97 errno = 0; 98 99 ATF_REQUIRE(access(NULL, mode[i]) != 0); 100 ATF_REQUIRE(errno == EFAULT); 101 102 errno = 0; 103 104 ATF_REQUIRE(access((char *)-1, mode[i]) != 0); 105 ATF_REQUIRE(errno == EFAULT); 106 } 107 } 108 109 ATF_TC(access_inval); 110 ATF_TC_HEAD(access_inval, tc) 111 { 112 atf_tc_set_md_var(tc, "descr", "Test access(2) for EINVAL"); 113 } 114 115 ATF_TC_BODY(access_inval, tc) 116 { 117 118 errno = 0; 119 120 ATF_REQUIRE(access("/usr", -1) != 0); 121 ATF_REQUIRE(errno == EINVAL); 122 } 123 124 ATF_TC(access_notdir); 125 ATF_TC_HEAD(access_notdir, tc) 126 { 127 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOTDIR"); 128 } 129 130 ATF_TC_BODY(access_notdir, tc) 131 { 132 size_t i; 133 134 for (i = 0; i < __arraycount(mode); i++) { 135 136 errno = 0; 137 138 /* 139 * IEEE Std 1003.1-2008 about ENOTDIR: 140 * 141 * "A component of the path prefix is not a directory, 142 * or the path argument contains at least one non-<slash> 143 * character and ends with one or more trailing <slash> 144 * characters and the last pathname component names an 145 * existing file that is neither a directory nor a symbolic 146 * link to a directory." 147 */ 148 ATF_REQUIRE(access("/etc/passwd//", mode[i]) != 0); 149 ATF_REQUIRE(errno == ENOTDIR); 150 } 151 } 152 153 ATF_TC(access_notexist); 154 ATF_TC_HEAD(access_notexist, tc) 155 { 156 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENOENT"); 157 } 158 159 ATF_TC_BODY(access_notexist, tc) 160 { 161 size_t i; 162 163 for (i = 0; i < __arraycount(mode); i++) { 164 165 errno = 0; 166 167 ATF_REQUIRE(access("", mode[i]) != 0); 168 ATF_REQUIRE(errno == ENOENT); 169 } 170 } 171 172 ATF_TC(access_toolong); 173 ATF_TC_HEAD(access_toolong, tc) 174 { 175 atf_tc_set_md_var(tc, "descr", "Test access(2) for ENAMETOOLONG"); 176 } 177 178 ATF_TC_BODY(access_toolong, tc) 179 { 180 char *buf; 181 size_t i; 182 183 buf = malloc(PATH_MAX); 184 185 if (buf == NULL) 186 return; 187 188 for (i = 0; i < PATH_MAX; i++) 189 buf[i] = 'x'; 190 191 for (i = 0; i < __arraycount(mode); i++) { 192 193 errno = 0; 194 195 ATF_REQUIRE(access(buf, mode[i]) != 0); 196 ATF_REQUIRE(errno == ENAMETOOLONG); 197 } 198 199 free(buf); 200 } 201 202 ATF_TP_ADD_TCS(tp) 203 { 204 205 ATF_TP_ADD_TC(tp, access_access); 206 ATF_TP_ADD_TC(tp, access_fault); 207 ATF_TP_ADD_TC(tp, access_inval); 208 ATF_TP_ADD_TC(tp, access_notdir); 209 ATF_TP_ADD_TC(tp, access_notexist); 210 ATF_TP_ADD_TC(tp, access_toolong); 211 212 return atf_no_error(); 213 } 214