1*de503941SPawel Jakub Dawidek /*- 2*de503941SPawel Jakub Dawidek * Copyright (c) 2012 The FreeBSD Foundation 3*de503941SPawel Jakub Dawidek * All rights reserved. 4*de503941SPawel Jakub Dawidek * 5*de503941SPawel Jakub Dawidek * This software was developed by Pawel Jakub Dawidek under sponsorship from 6*de503941SPawel Jakub Dawidek * the FreeBSD Foundation. 7*de503941SPawel Jakub Dawidek * 8*de503941SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 9*de503941SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 10*de503941SPawel Jakub Dawidek * are met: 11*de503941SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 12*de503941SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 13*de503941SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 14*de503941SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 15*de503941SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 16*de503941SPawel Jakub Dawidek * 17*de503941SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18*de503941SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*de503941SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*de503941SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21*de503941SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*de503941SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*de503941SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*de503941SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*de503941SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*de503941SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*de503941SPawel Jakub Dawidek * SUCH DAMAGE. 28*de503941SPawel Jakub Dawidek */ 29*de503941SPawel Jakub Dawidek 30*de503941SPawel Jakub Dawidek #include <sys/cdefs.h> 31*de503941SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 32*de503941SPawel Jakub Dawidek 33*de503941SPawel Jakub Dawidek #include <sys/types.h> 34*de503941SPawel Jakub Dawidek #include <sys/capability.h> 35*de503941SPawel Jakub Dawidek #include <sys/procdesc.h> 36*de503941SPawel Jakub Dawidek #include <sys/wait.h> 37*de503941SPawel Jakub Dawidek 38*de503941SPawel Jakub Dawidek #include <err.h> 39*de503941SPawel Jakub Dawidek #include <errno.h> 40*de503941SPawel Jakub Dawidek #include <stdio.h> 41*de503941SPawel Jakub Dawidek #include <stdlib.h> 42*de503941SPawel Jakub Dawidek #include <unistd.h> 43*de503941SPawel Jakub Dawidek 44*de503941SPawel Jakub Dawidek #include "misc.h" 45*de503941SPawel Jakub Dawidek 46*de503941SPawel Jakub Dawidek int 47*de503941SPawel Jakub Dawidek main(void) 48*de503941SPawel Jakub Dawidek { 49*de503941SPawel Jakub Dawidek unsigned int mode; 50*de503941SPawel Jakub Dawidek pid_t pid; 51*de503941SPawel Jakub Dawidek int pfd; 52*de503941SPawel Jakub Dawidek 53*de503941SPawel Jakub Dawidek printf("1..27\n"); 54*de503941SPawel Jakub Dawidek 55*de503941SPawel Jakub Dawidek mode = 666; 56*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 57*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 58*de503941SPawel Jakub Dawidek CHECK(mode != 666); 59*de503941SPawel Jakub Dawidek /* We are not in capability mode. */ 60*de503941SPawel Jakub Dawidek CHECK(mode == 0); 61*de503941SPawel Jakub Dawidek 62*de503941SPawel Jakub Dawidek /* Expect EFAULT. */ 63*de503941SPawel Jakub Dawidek errno = 0; 64*de503941SPawel Jakub Dawidek CHECK(cap_getmode(NULL) == -1); 65*de503941SPawel Jakub Dawidek CHECK(errno == EFAULT); 66*de503941SPawel Jakub Dawidek errno = 0; 67*de503941SPawel Jakub Dawidek CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1); 68*de503941SPawel Jakub Dawidek CHECK(errno == EFAULT); 69*de503941SPawel Jakub Dawidek 70*de503941SPawel Jakub Dawidek /* If parent is not in capability mode, child after fork() also won't be. */ 71*de503941SPawel Jakub Dawidek pid = fork(); 72*de503941SPawel Jakub Dawidek switch (pid) { 73*de503941SPawel Jakub Dawidek case -1: 74*de503941SPawel Jakub Dawidek err(1, "fork() failed"); 75*de503941SPawel Jakub Dawidek case 0: 76*de503941SPawel Jakub Dawidek mode = 666; 77*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 78*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 79*de503941SPawel Jakub Dawidek CHECK(mode != 666); 80*de503941SPawel Jakub Dawidek /* We are not in capability mode. */ 81*de503941SPawel Jakub Dawidek CHECK(mode == 0); 82*de503941SPawel Jakub Dawidek exit(0); 83*de503941SPawel Jakub Dawidek default: 84*de503941SPawel Jakub Dawidek if (waitpid(pid, NULL, 0) == -1) 85*de503941SPawel Jakub Dawidek err(1, "waitpid() failed"); 86*de503941SPawel Jakub Dawidek } 87*de503941SPawel Jakub Dawidek 88*de503941SPawel Jakub Dawidek /* If parent is not in capability mode, child after pdfork() also won't be. */ 89*de503941SPawel Jakub Dawidek pid = pdfork(&pfd, 0); 90*de503941SPawel Jakub Dawidek switch (pid) { 91*de503941SPawel Jakub Dawidek case -1: 92*de503941SPawel Jakub Dawidek err(1, "pdfork() failed"); 93*de503941SPawel Jakub Dawidek case 0: 94*de503941SPawel Jakub Dawidek mode = 666; 95*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 96*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 97*de503941SPawel Jakub Dawidek CHECK(mode != 666); 98*de503941SPawel Jakub Dawidek /* We are not in capability mode. */ 99*de503941SPawel Jakub Dawidek CHECK(mode == 0); 100*de503941SPawel Jakub Dawidek exit(0); 101*de503941SPawel Jakub Dawidek default: 102*de503941SPawel Jakub Dawidek if (pdwait(pfd) == -1) 103*de503941SPawel Jakub Dawidek err(1, "pdwait() failed"); 104*de503941SPawel Jakub Dawidek close(pfd); 105*de503941SPawel Jakub Dawidek } 106*de503941SPawel Jakub Dawidek 107*de503941SPawel Jakub Dawidek /* In capability mode... */ 108*de503941SPawel Jakub Dawidek 109*de503941SPawel Jakub Dawidek CHECK(cap_enter() == 0); 110*de503941SPawel Jakub Dawidek 111*de503941SPawel Jakub Dawidek mode = 666; 112*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 113*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 114*de503941SPawel Jakub Dawidek CHECK(mode != 666); 115*de503941SPawel Jakub Dawidek /* We are in capability mode. */ 116*de503941SPawel Jakub Dawidek CHECK(mode == 1); 117*de503941SPawel Jakub Dawidek 118*de503941SPawel Jakub Dawidek /* Expect EFAULT. */ 119*de503941SPawel Jakub Dawidek errno = 0; 120*de503941SPawel Jakub Dawidek CHECK(cap_getmode(NULL) == -1); 121*de503941SPawel Jakub Dawidek CHECK(errno == EFAULT); 122*de503941SPawel Jakub Dawidek errno = 0; 123*de503941SPawel Jakub Dawidek CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1); 124*de503941SPawel Jakub Dawidek CHECK(errno == EFAULT); 125*de503941SPawel Jakub Dawidek 126*de503941SPawel Jakub Dawidek /* If parent is in capability mode, child after fork() also will be. */ 127*de503941SPawel Jakub Dawidek pid = fork(); 128*de503941SPawel Jakub Dawidek switch (pid) { 129*de503941SPawel Jakub Dawidek case -1: 130*de503941SPawel Jakub Dawidek err(1, "fork() failed"); 131*de503941SPawel Jakub Dawidek case 0: 132*de503941SPawel Jakub Dawidek mode = 666; 133*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 134*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 135*de503941SPawel Jakub Dawidek CHECK(mode != 666); 136*de503941SPawel Jakub Dawidek /* We are in capability mode. */ 137*de503941SPawel Jakub Dawidek CHECK(mode == 1); 138*de503941SPawel Jakub Dawidek exit(0); 139*de503941SPawel Jakub Dawidek default: 140*de503941SPawel Jakub Dawidek /* 141*de503941SPawel Jakub Dawidek * wait(2) and friends are not permitted in the capability mode, 142*de503941SPawel Jakub Dawidek * so we can only just wait for a while. 143*de503941SPawel Jakub Dawidek */ 144*de503941SPawel Jakub Dawidek sleep(1); 145*de503941SPawel Jakub Dawidek } 146*de503941SPawel Jakub Dawidek 147*de503941SPawel Jakub Dawidek /* If parent is in capability mode, child after pdfork() also will be. */ 148*de503941SPawel Jakub Dawidek pid = pdfork(&pfd, 0); 149*de503941SPawel Jakub Dawidek switch (pid) { 150*de503941SPawel Jakub Dawidek case -1: 151*de503941SPawel Jakub Dawidek err(1, "pdfork() failed"); 152*de503941SPawel Jakub Dawidek case 0: 153*de503941SPawel Jakub Dawidek mode = 666; 154*de503941SPawel Jakub Dawidek CHECK(cap_getmode(&mode) == 0); 155*de503941SPawel Jakub Dawidek /* If cap_getmode() succeeded mode should be modified. */ 156*de503941SPawel Jakub Dawidek CHECK(mode != 666); 157*de503941SPawel Jakub Dawidek /* We are in capability mode. */ 158*de503941SPawel Jakub Dawidek CHECK(mode == 1); 159*de503941SPawel Jakub Dawidek exit(0); 160*de503941SPawel Jakub Dawidek default: 161*de503941SPawel Jakub Dawidek if (pdwait(pfd) == -1) 162*de503941SPawel Jakub Dawidek err(1, "pdwait() failed"); 163*de503941SPawel Jakub Dawidek close(pfd); 164*de503941SPawel Jakub Dawidek } 165*de503941SPawel Jakub Dawidek 166*de503941SPawel Jakub Dawidek exit(0); 167*de503941SPawel Jakub Dawidek } 168