1 /*- 2 * Copyright (c) 2009-2011 Robert N. M. Watson 3 * Copyright (c) 2011 Jonathan Anderson 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/capsicum.h> 35 #include <sys/errno.h> 36 37 #include <err.h> 38 #include <fcntl.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #include "cap_test.h" 44 45 /* 46 * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum 47 * "strict relative" rules: 48 * 49 * 1. Use strict relative lookups in capability mode or when operating 50 * relative to a capability. 51 * 2. When performing strict relative lookups, absolute paths (including 52 * symlinks to absolute paths) are not allowed, nor are paths containing 53 * '..' components. 54 */ 55 int 56 test_relative(void) 57 { 58 int success = PASSED; 59 int fd, etc, etc_cap, etc_cap_ro, etc_cap_base, etc_cap_all; 60 cap_rights_t baserights = CAP_READ | CAP_WRITE | CAP_SEEK | CAP_LOOKUP; 61 cap_rights_t rights; 62 63 REQUIRE(etc = open("/etc/", O_RDONLY)); 64 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc, &rights); 65 CHECK_RIGHTS(rights, CAP_ALL); 66 67 MAKE_CAPABILITY(etc_cap, etc, CAP_READ); 68 MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP); 69 MAKE_CAPABILITY(etc_cap_base, etc, baserights); 70 MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID); 71 72 /* 73 * openat(2) with regular file descriptors in non-capability mode 74 * should Just Work (tm). 75 */ 76 CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY); 77 CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY); 78 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY); 79 CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY); 80 81 /* 82 * Lookups relative to capabilities should be strictly relative. 83 * 84 * When not in capability mode, we don't actually require CAP_LOOKUP. 85 */ 86 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY); 87 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY); 88 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY); 89 90 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY); 91 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY); 92 93 /* 94 * This requires discussion: do we treat a capability with 95 * CAP_MASK_VALID *exactly* like a non-capability file descriptor 96 * (currently, the implementation says yes)? 97 */ 98 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY); 99 100 /* 101 * A file opened relative to a capability should itself be a capability. 102 */ 103 CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights); 104 105 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY)); 106 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 107 CHECK_RIGHTS(rights, baserights); 108 109 /* 110 * Enter capability mode; now ALL lookups are strictly relative. 111 */ 112 REQUIRE(cap_enter()); 113 114 /* 115 * Relative lookups on regular files or capabilities with CAP_LOOKUP 116 * ought to succeed. 117 */ 118 CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY); 119 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY); 120 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY); 121 CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY); 122 123 /* 124 * Lookup relative to capabilities without CAP_LOOKUP should fail. 125 */ 126 CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY); 127 128 /* 129 * Absolute lookups should fail. 130 */ 131 CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY); 132 CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY); 133 134 /* 135 * Lookups containing '..' should fail in capability mode. 136 */ 137 CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY); 138 CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY); 139 CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY); 140 141 REQUIRE(fd = openat(etc, "passwd", O_RDONLY)); 142 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 143 144 /* 145 * A file opened relative to a capability should itself be a capability. 146 */ 147 REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY)); 148 CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 149 CHECK_RIGHTS(rights, baserights); 150 151 return success; 152 } 153