1 /*- 2 * Copyright (c) 2008-2009 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 28 /* 29 * Test routines to make sure a variety of system calls are or are not 30 * available in capability mode. The goal is not to see if they work, just 31 * whether or not they return the expected ECAPMODE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/capsicum.h> 36 #include <sys/errno.h> 37 #include <sys/mman.h> 38 #include <sys/mount.h> 39 #include <sys/socket.h> 40 #include <sys/stat.h> 41 #include <sys/wait.h> 42 43 #include <machine/sysarch.h> 44 45 #include <err.h> 46 #include <fcntl.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include "cap_test.h" 52 53 #define CHECK_SYSCALL_VOID_NOT_ECAPMODE(syscall, ...) do { \ 54 errno = 0; \ 55 (void)syscall(__VA_ARGS__); \ 56 if (errno == ECAPMODE) \ 57 FAIL("capmode: %s failed with ECAPMODE", #syscall); \ 58 } while (0) 59 60 int 61 test_capmode(void) 62 { 63 struct statfs statfs; 64 struct stat sb; 65 long sysarch_arg = 0; 66 int fd_close, fd_dir, fd_file, fd_socket, fd2[2]; 67 int success = PASSED; 68 pid_t pid, wpid; 69 char ch; 70 71 /* Open some files to play with. */ 72 REQUIRE(fd_file = open("/tmp/cap_capmode", O_RDWR|O_CREAT, 0644)); 73 REQUIRE(fd_close = open("/dev/null", O_RDWR)); 74 REQUIRE(fd_dir = open("/tmp", O_RDONLY)); 75 REQUIRE(fd_socket = socket(PF_INET, SOCK_DGRAM, 0)); 76 77 /* Enter capability mode. */ 78 REQUIRE(cap_enter()); 79 80 /* 81 * System calls that are not permitted in capability mode. 82 */ 83 CHECK_CAPMODE(access, "/tmp/cap_capmode_access", F_OK); 84 CHECK_CAPMODE(acct, "/tmp/cap_capmode_acct"); 85 CHECK_CAPMODE(bind, PF_INET, NULL, 0); 86 CHECK_CAPMODE(chdir, "/tmp/cap_capmode_chdir"); 87 CHECK_CAPMODE(chflags, "/tmp/cap_capmode_chflags", UF_NODUMP); 88 CHECK_CAPMODE(chmod, "/tmp/cap_capmode_chmod", 0644); 89 CHECK_CAPMODE(chown, "/tmp/cap_capmode_chown", -1, -1); 90 CHECK_CAPMODE(chroot, "/tmp/cap_capmode_chroot"); 91 CHECK_CAPMODE(connect, PF_INET, NULL, 0); 92 CHECK_CAPMODE(creat, "/tmp/cap_capmode_creat", 0644); 93 CHECK_CAPMODE(fchdir, fd_dir); 94 CHECK_CAPMODE(getfsstat, &statfs, sizeof(statfs), MNT_NOWAIT); 95 CHECK_CAPMODE(link, "/tmp/foo", "/tmp/bar"); 96 CHECK_CAPMODE(lstat, "/tmp/cap_capmode_lstat", &sb); 97 CHECK_CAPMODE(mknod, "/tmp/capmode_mknod", 06440, 0); 98 CHECK_CAPMODE(mount, "procfs", "/not_mounted", 0, NULL); 99 CHECK_CAPMODE(open, "/dev/null", O_RDWR); 100 CHECK_CAPMODE(readlink, "/tmp/cap_capmode_readlink", NULL, 0); 101 CHECK_CAPMODE(revoke, "/tmp/cap_capmode_revoke"); 102 CHECK_CAPMODE(stat, "/tmp/cap_capmode_stat", &sb); 103 CHECK_CAPMODE(symlink, 104 "/tmp/cap_capmode_symlink_from", 105 "/tmp/cap_capmode_symlink_to"); 106 CHECK_CAPMODE(unlink, "/tmp/cap_capmode_unlink"); 107 CHECK_CAPMODE(unmount, "/not_mounted", 0); 108 109 /* 110 * System calls that are permitted in capability mode. 111 */ 112 CHECK_SYSCALL_SUCCEEDS(close, fd_close); 113 CHECK_SYSCALL_SUCCEEDS(dup, fd_file); 114 CHECK_SYSCALL_SUCCEEDS(fstat, fd_file, &sb); 115 CHECK_SYSCALL_SUCCEEDS(lseek, fd_file, 0, SEEK_SET); 116 CHECK_SYSCALL_SUCCEEDS(msync, &fd_file, 8192, MS_ASYNC); 117 CHECK_SYSCALL_SUCCEEDS(profil, NULL, 0, 0, 0); 118 CHECK_SYSCALL_SUCCEEDS(read, fd_file, &ch, sizeof(ch)); 119 CHECK_SYSCALL_SUCCEEDS(recvfrom, fd_socket, NULL, 0, 0, NULL, NULL); 120 CHECK_SYSCALL_SUCCEEDS(setuid, getuid()); 121 CHECK_SYSCALL_SUCCEEDS(write, fd_file, &ch, sizeof(ch)); 122 123 /* 124 * These calls will fail for lack of e.g. a proper name to send to, 125 * but they are allowed in capability mode, so errno != ECAPMODE. 126 */ 127 CHECK_NOT_CAPMODE(accept, fd_socket, NULL, NULL); 128 CHECK_NOT_CAPMODE(getpeername, fd_socket, NULL, NULL); 129 CHECK_NOT_CAPMODE(getsockname, fd_socket, NULL, NULL); 130 CHECK_NOT_CAPMODE(fchflags, fd_file, UF_NODUMP); 131 CHECK_NOT_CAPMODE(recvmsg, fd_socket, NULL, 0); 132 CHECK_NOT_CAPMODE(sendmsg, fd_socket, NULL, 0); 133 CHECK_NOT_CAPMODE(sendto, fd_socket, NULL, 0, 0, NULL, 0); 134 135 /* 136 * System calls which should be allowed in capability mode, but which 137 * don't return errors, and are thus difficult to check. 138 * 139 * We will try anyway, by checking errno. 140 */ 141 CHECK_SYSCALL_VOID_NOT_ECAPMODE(getegid); 142 CHECK_SYSCALL_VOID_NOT_ECAPMODE(geteuid); 143 CHECK_SYSCALL_VOID_NOT_ECAPMODE(getgid); 144 CHECK_SYSCALL_VOID_NOT_ECAPMODE(getpid); 145 CHECK_SYSCALL_VOID_NOT_ECAPMODE(getppid); 146 CHECK_SYSCALL_VOID_NOT_ECAPMODE(getuid); 147 148 /* 149 * Finally, tests for system calls that don't fit the pattern very well. 150 */ 151 pid = fork(); 152 if (pid >= 0) { 153 if (pid == 0) { 154 exit(0); 155 } else if (pid > 0) { 156 wpid = waitpid(pid, NULL, 0); 157 if (wpid < 0) { 158 if (errno != ECAPMODE) 159 FAIL("capmode:waitpid"); 160 } else 161 FAIL("capmode:waitpid succeeded"); 162 } 163 } else 164 FAIL("capmode:fork"); 165 166 if (getlogin() == NULL) 167 FAIL("test_sycalls:getlogin %d", errno); 168 169 if (getsockname(fd_socket, NULL, NULL) < 0) { 170 if (errno == ECAPMODE) 171 FAIL("capmode:getsockname"); 172 } 173 174 /* XXXRW: ktrace */ 175 176 if (pipe(fd2) == 0) { 177 close(fd2[0]); 178 close(fd2[1]); 179 } else if (errno == ECAPMODE) 180 FAIL("capmode:pipe"); 181 182 /* XXXRW: ptrace. */ 183 184 /* sysarch() is, by definition, architecture-dependent */ 185 #if defined (__amd64__) || defined (__i386__) 186 CHECK_CAPMODE(sysarch, I386_SET_IOPERM, &sysarch_arg); 187 #else 188 /* XXXJA: write a test for arm */ 189 FAIL("capmode:no sysarch() test for current architecture"); 190 #endif 191 192 /* XXXRW: No error return from sync(2) to test. */ 193 194 return (success); 195 } 196