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 28 /* 29 * Test that fcntl works in capability mode. 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <sys/capsicum.h> 35 #include <sys/errno.h> 36 #include <sys/ipc.h> 37 #include <sys/mman.h> 38 #include <sys/socket.h> 39 #include <sys/stat.h> 40 #include <sys/sysctl.h> 41 #include <sys/wait.h> 42 43 #include <err.h> 44 #include <fcntl.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <unistd.h> 48 49 #include "cap_test.h" 50 51 /* A filename->descriptor mapping. */ 52 struct fd { 53 char *f_name; 54 int f_fd; 55 }; 56 57 /* 58 * Ensure that fcntl() works consistently for both regular file descriptors and 59 * capability-wrapped ones. 60 */ 61 int 62 test_fcntl(void) 63 { 64 int success = PASSED; 65 cap_rights_t rights = CAP_READ | CAP_FCNTL; 66 67 /* 68 * Open some files of different types, and wrap them in capabilities. 69 */ 70 struct fd files[] = { 71 { "file", open("/etc/passwd", O_RDONLY) }, 72 { "socket", socket(PF_LOCAL, SOCK_STREAM, 0) }, 73 { "SHM", shm_open(SHM_ANON, O_RDWR, 0600) }, 74 }; 75 REQUIRE(files[0].f_fd); 76 REQUIRE(files[1].f_fd); 77 REQUIRE(files[2].f_fd); 78 79 struct fd caps[] = { 80 { "file cap", cap_new(files[0].f_fd, rights) }, 81 { "socket cap", cap_new(files[1].f_fd, rights) }, 82 { "SHM cap", cap_new(files[2].f_fd, rights) }, 83 }; 84 REQUIRE(caps[0].f_fd); 85 REQUIRE(caps[1].f_fd); 86 REQUIRE(caps[2].f_fd); 87 88 struct fd all[] = { 89 files[0], caps[0], 90 files[1], caps[1], 91 files[2], caps[2], 92 }; 93 const size_t len = sizeof(all) / sizeof(struct fd); 94 95 REQUIRE(cap_enter()); 96 97 /* 98 * Ensure that we can fcntl() all the files that we opened above. 99 */ 100 for (size_t i = 0; i < len; i++) 101 { 102 struct fd f = all[i]; 103 int cap; 104 105 CHECK_SYSCALL_SUCCEEDS(fcntl, f.f_fd, F_GETFL, 0); 106 REQUIRE(cap = cap_new(f.f_fd, CAP_READ)); 107 if (fcntl(f.f_fd, F_GETFL, 0) == -1) 108 FAIL("Error calling fcntl('%s', F_GETFL)", f.f_name); 109 else 110 CHECK_NOTCAPABLE(fcntl, cap, F_GETFL, 0); 111 } 112 113 return (success); 114 } 115 116