1 /* $NetBSD: t_o_search.c,v 1.9 2020/02/06 12:18:06 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 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_o_search.c,v 1.9 2020/02/06 12:18:06 martin Exp $"); 33 34 #include <atf-c.h> 35 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 39 #include <dirent.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <limits.h> 43 #include <paths.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <pwd.h> 48 49 /* 50 * dholland 20130112: disable tests that require O_SEARCH semantics 51 * until a decision is reached about the semantics of O_SEARCH and a 52 * non-broken implementation is available. 53 */ 54 #if defined(__FreeBSD__) || (O_MASK & O_SEARCH) != 0 55 #define USE_O_SEARCH 56 #endif 57 58 #define DIR "dir" 59 #define FILE "dir/o_search" 60 #define BASEFILE "o_search" 61 62 63 ATF_TC(o_search_perm1); 64 ATF_TC_HEAD(o_search_perm1, tc) 65 { 66 atf_tc_set_md_var(tc, "descr", "See that openat enforces search permission"); 67 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 68 } 69 ATF_TC_BODY(o_search_perm1, tc) 70 { 71 int dfd; 72 int fd; 73 74 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 75 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 76 ATF_REQUIRE(close(fd) == 0); 77 78 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 79 80 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 81 ATF_REQUIRE(close(fd) == 0); 82 83 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 84 85 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) == -1); 86 ATF_REQUIRE(errno == EACCES); 87 88 ATF_REQUIRE(close(dfd) == 0); 89 } 90 91 #ifdef USE_O_SEARCH 92 93 ATF_TC(o_search_root_flag1); 94 ATF_TC_HEAD(o_search_root_flag1, tc) 95 { 96 atf_tc_set_md_var(tc, "descr", "See that root openat honours O_SEARCH"); 97 atf_tc_set_md_var(tc, "require.user", "root"); 98 } 99 ATF_TC_BODY(o_search_root_flag1, tc) 100 { 101 int dfd; 102 int fd; 103 104 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 105 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 106 ATF_REQUIRE(close(fd) == 0); 107 108 ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1); 109 110 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 111 ATF_REQUIRE(close(fd) == 0); 112 113 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 114 115 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 116 ATF_REQUIRE(close(fd) == 0); 117 118 ATF_REQUIRE(fchmod(dfd, 0444) == 0); 119 120 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 121 122 ATF_REQUIRE(close(dfd) == 0); 123 } 124 125 ATF_TC(o_search_unpriv_flag1); 126 ATF_TC_HEAD(o_search_unpriv_flag1, tc) 127 { 128 atf_tc_set_md_var(tc, "descr", "See that openat honours O_SEARCH"); 129 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 130 } 131 ATF_TC_BODY(o_search_unpriv_flag1, tc) 132 { 133 int dfd; 134 int fd; 135 136 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 137 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 138 ATF_REQUIRE(close(fd) == 0); 139 140 ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1); 141 142 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 143 ATF_REQUIRE(close(fd) == 0); 144 145 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 146 147 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 148 ATF_REQUIRE(close(fd) == 0); 149 150 ATF_REQUIRE(fchmod(dfd, 0444) == 0); 151 152 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1); 153 154 ATF_REQUIRE(close(dfd) == 0); 155 } 156 157 #endif /* USE_O_SEARCH */ 158 159 ATF_TC(o_search_perm2); 160 ATF_TC_HEAD(o_search_perm2, tc) 161 { 162 atf_tc_set_md_var(tc, "descr", "See that faccessat enforces search permission"); 163 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 164 } 165 ATF_TC_BODY(o_search_perm2, tc) 166 { 167 int dfd; 168 int fd; 169 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 170 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 171 ATF_REQUIRE(close(fd) == 0); 172 173 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 174 175 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 176 177 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 178 179 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == -1); 180 ATF_REQUIRE(errno == EACCES); 181 182 ATF_REQUIRE(close(dfd) == 0); 183 } 184 185 #ifdef USE_O_SEARCH 186 187 ATF_TC(o_search_root_flag2); 188 ATF_TC_HEAD(o_search_root_flag2, tc) 189 { 190 atf_tc_set_md_var(tc, "descr", "See that root fstatat honours O_SEARCH"); 191 atf_tc_set_md_var(tc, "require.user", "root"); 192 } 193 ATF_TC_BODY(o_search_root_flag2, tc) 194 { 195 int dfd; 196 int fd; 197 198 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 199 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 200 ATF_REQUIRE(close(fd) == 0); 201 202 ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1); 203 204 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 205 206 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 207 208 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 209 210 ATF_REQUIRE(fchmod(dfd, 0444) == 0); 211 212 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 213 214 ATF_REQUIRE(close(dfd) == 0); 215 } 216 217 ATF_TC(o_search_unpriv_flag2); 218 ATF_TC_HEAD(o_search_unpriv_flag2, tc) 219 { 220 atf_tc_set_md_var(tc, "descr", "See that fstatat honours O_SEARCH"); 221 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 222 } 223 ATF_TC_BODY(o_search_unpriv_flag2, tc) 224 { 225 int dfd; 226 int fd; 227 228 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 229 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 230 ATF_REQUIRE(close(fd) == 0); 231 232 ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1); 233 234 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 235 236 ATF_REQUIRE(fchmod(dfd, 0644) == 0); 237 238 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 239 240 ATF_REQUIRE(fchmod(dfd, 0444) == 0); 241 242 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0); 243 244 ATF_REQUIRE(close(dfd) == 0); 245 } 246 247 #endif /* USE_O_SEARCH */ 248 249 250 ATF_TC(o_search_notdir); 251 ATF_TC_HEAD(o_search_notdir, tc) 252 { 253 atf_tc_set_md_var(tc, "descr", "See that openat fails with non dir fd"); 254 } 255 ATF_TC_BODY(o_search_notdir, tc) 256 { 257 int dfd; 258 int fd; 259 260 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 261 ATF_REQUIRE((dfd = open(FILE, O_CREAT|O_SEARCH, 0644)) != -1); 262 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) == -1); 263 ATF_REQUIRE(errno == ENOTDIR); 264 ATF_REQUIRE(close(dfd) == 0); 265 } 266 267 #ifdef USE_O_SEARCH 268 ATF_TC(o_search_nord); 269 ATF_TC_HEAD(o_search_nord, tc) 270 { 271 atf_tc_set_md_var(tc, "descr", "See that openat succeeds with no read permission"); 272 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 273 } 274 ATF_TC_BODY(o_search_nord, tc) 275 { 276 int dfd, fd; 277 278 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 279 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 280 ATF_REQUIRE(close(fd) == 0); 281 282 ATF_REQUIRE(chmod(DIR, 0100) == 0); 283 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 284 285 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) != -1); 286 287 ATF_REQUIRE(close(dfd) == 0); 288 } 289 290 ATF_TC(o_search_getdents); 291 ATF_TC_HEAD(o_search_getdents, tc) 292 { 293 atf_tc_set_md_var(tc, "descr", "See that O_SEARCH forbids getdents"); 294 } 295 ATF_TC_BODY(o_search_getdents, tc) 296 { 297 char buf[1024]; 298 int dfd; 299 300 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 301 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 302 ATF_REQUIRE(getdents(dfd, buf, sizeof(buf)) < 0); 303 ATF_REQUIRE(close(dfd) == 0); 304 } 305 306 ATF_TC(o_search_revokex); 307 ATF_TC_HEAD(o_search_revokex, tc) 308 { 309 atf_tc_set_md_var(tc, "descr", "See that *at behaves after chmod -x"); 310 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 311 } 312 ATF_TC_BODY(o_search_revokex, tc) 313 { 314 int dfd, fd; 315 struct stat sb; 316 317 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 318 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 319 ATF_REQUIRE(close(fd) == 0); 320 321 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 322 323 /* Drop permissions. The kernel must still not check the exec bit. */ 324 ATF_REQUIRE(chmod(DIR, 0000) == 0); 325 ATF_REQUIRE(fstatat(dfd, BASEFILE, &sb, 0) == 0); 326 327 ATF_REQUIRE(close(dfd) == 0); 328 } 329 #endif /* USE_O_SEARCH */ 330 331 ATF_TP_ADD_TCS(tp) 332 { 333 334 ATF_TP_ADD_TC(tp, o_search_perm1); 335 #ifdef USE_O_SEARCH 336 ATF_TP_ADD_TC(tp, o_search_root_flag1); 337 ATF_TP_ADD_TC(tp, o_search_unpriv_flag1); 338 #endif 339 ATF_TP_ADD_TC(tp, o_search_perm2); 340 #ifdef USE_O_SEARCH 341 ATF_TP_ADD_TC(tp, o_search_root_flag2); 342 ATF_TP_ADD_TC(tp, o_search_unpriv_flag2); 343 #endif 344 ATF_TP_ADD_TC(tp, o_search_notdir); 345 #ifdef USE_O_SEARCH 346 ATF_TP_ADD_TC(tp, o_search_nord); 347 ATF_TP_ADD_TC(tp, o_search_getdents); 348 ATF_TP_ADD_TC(tp, o_search_revokex); 349 #endif 350 351 return atf_no_error(); 352 } 353