1 /* $NetBSD: t_o_search.c,v 1.5 2017/01/10 22:25:01 christos 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.5 2017/01/10 22:25:01 christos 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 #ifndef __FreeBSD__ 262 ATF_REQUIRE((dfd = open(FILE, O_CREAT|O_RDWR|O_SEARCH, 0644)) != -1); 263 #else 264 ATF_REQUIRE((dfd = open(FILE, O_CREAT|O_SEARCH, 0644)) != -1); 265 #endif 266 ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) == -1); 267 ATF_REQUIRE(errno == ENOTDIR); 268 ATF_REQUIRE(close(dfd) == 0); 269 } 270 271 #ifdef USE_O_SEARCH 272 ATF_TC(o_search_nord); 273 ATF_TC_HEAD(o_search_nord, tc) 274 { 275 atf_tc_set_md_var(tc, "descr", "See that openat succeeds with no read permission"); 276 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 277 } 278 ATF_TC_BODY(o_search_nord, tc) 279 { 280 int dfd, fd; 281 282 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 283 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 284 ATF_REQUIRE(close(fd) == 0); 285 286 ATF_REQUIRE(chmod(DIR, 0100) == 0); 287 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 288 289 ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) != -1); 290 291 ATF_REQUIRE(close(dfd) == 0); 292 } 293 294 ATF_TC(o_search_getdents); 295 ATF_TC_HEAD(o_search_getdents, tc) 296 { 297 atf_tc_set_md_var(tc, "descr", "See that O_SEARCH forbids getdents"); 298 } 299 ATF_TC_BODY(o_search_getdents, tc) 300 { 301 char buf[1024]; 302 int dfd; 303 304 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 305 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 306 ATF_REQUIRE(getdents(dfd, buf, sizeof(buf)) < 0); 307 ATF_REQUIRE(close(dfd) == 0); 308 } 309 310 ATF_TC(o_search_revokex); 311 ATF_TC_HEAD(o_search_revokex, tc) 312 { 313 atf_tc_set_md_var(tc, "descr", "See that *at behaves after chmod -x"); 314 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 315 } 316 ATF_TC_BODY(o_search_revokex, tc) 317 { 318 int dfd, fd; 319 struct stat sb; 320 321 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 322 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 323 ATF_REQUIRE(close(fd) == 0); 324 325 ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1); 326 327 /* Drop permissions. The kernel must still not check the exec bit. */ 328 ATF_REQUIRE(chmod(DIR, 0000) == 0); 329 ATF_REQUIRE(fstatat(dfd, BASEFILE, &sb, 0) == 0); 330 331 ATF_REQUIRE(close(dfd) == 0); 332 } 333 #endif /* USE_O_SEARCH */ 334 335 ATF_TP_ADD_TCS(tp) 336 { 337 338 ATF_TP_ADD_TC(tp, o_search_perm1); 339 #ifdef USE_O_SEARCH 340 ATF_TP_ADD_TC(tp, o_search_root_flag1); 341 ATF_TP_ADD_TC(tp, o_search_unpriv_flag1); 342 #endif 343 ATF_TP_ADD_TC(tp, o_search_perm2); 344 #ifdef USE_O_SEARCH 345 ATF_TP_ADD_TC(tp, o_search_root_flag2); 346 ATF_TP_ADD_TC(tp, o_search_unpriv_flag2); 347 #endif 348 ATF_TP_ADD_TC(tp, o_search_notdir); 349 #ifdef USE_O_SEARCH 350 ATF_TP_ADD_TC(tp, o_search_nord); 351 ATF_TP_ADD_TC(tp, o_search_getdents); 352 ATF_TP_ADD_TC(tp, o_search_revokex); 353 #endif 354 355 return atf_no_error(); 356 } 357