Path: blob/main/tools/regression/capsicum/syscalls/cap_getmode.c
48254 views
/*-1* Copyright (c) 2012 The FreeBSD Foundation2*3* This software was developed by Pawel Jakub Dawidek under sponsorship from4* the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>29#include <sys/capsicum.h>30#include <sys/procdesc.h>31#include <sys/wait.h>3233#include <err.h>34#include <errno.h>35#include <stdio.h>36#include <stdlib.h>37#include <unistd.h>3839#include "misc.h"4041int42main(void)43{44unsigned int mode;45pid_t pid;46int pfd;4748printf("1..27\n");4950mode = 666;51CHECK(cap_getmode(&mode) == 0);52/* If cap_getmode() succeeded mode should be modified. */53CHECK(mode != 666);54/* We are not in capability mode. */55CHECK(mode == 0);5657/* Expect EFAULT. */58errno = 0;59CHECK(cap_getmode(NULL) == -1);60CHECK(errno == EFAULT);61errno = 0;62CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);63CHECK(errno == EFAULT);6465/* If parent is not in capability mode, child after fork() also won't be. */66pid = fork();67switch (pid) {68case -1:69err(1, "fork() failed");70case 0:71mode = 666;72CHECK(cap_getmode(&mode) == 0);73/* If cap_getmode() succeeded mode should be modified. */74CHECK(mode != 666);75/* We are not in capability mode. */76CHECK(mode == 0);77exit(0);78default:79if (waitpid(pid, NULL, 0) == -1)80err(1, "waitpid() failed");81}8283/* If parent is not in capability mode, child after pdfork() also won't be. */84pid = pdfork(&pfd, 0);85switch (pid) {86case -1:87err(1, "pdfork() failed");88case 0:89mode = 666;90CHECK(cap_getmode(&mode) == 0);91/* If cap_getmode() succeeded mode should be modified. */92CHECK(mode != 666);93/* We are not in capability mode. */94CHECK(mode == 0);95exit(0);96default:97if (pdwait(pfd) == -1)98err(1, "pdwait() failed");99close(pfd);100}101102/* In capability mode... */103104CHECK(cap_enter() == 0);105106mode = 666;107CHECK(cap_getmode(&mode) == 0);108/* If cap_getmode() succeeded mode should be modified. */109CHECK(mode != 666);110/* We are in capability mode. */111CHECK(mode == 1);112113/* Expect EFAULT. */114errno = 0;115CHECK(cap_getmode(NULL) == -1);116CHECK(errno == EFAULT);117errno = 0;118CHECK(cap_getmode((void *)(uintptr_t)0xdeadc0de) == -1);119CHECK(errno == EFAULT);120121/* If parent is in capability mode, child after fork() also will be. */122pid = fork();123switch (pid) {124case -1:125err(1, "fork() failed");126case 0:127mode = 666;128CHECK(cap_getmode(&mode) == 0);129/* If cap_getmode() succeeded mode should be modified. */130CHECK(mode != 666);131/* We are in capability mode. */132CHECK(mode == 1);133exit(0);134default:135/*136* wait(2) and friends are not permitted in the capability mode,137* so we can only just wait for a while.138*/139sleep(1);140}141142/* If parent is in capability mode, child after pdfork() also will be. */143pid = pdfork(&pfd, 0);144switch (pid) {145case -1:146err(1, "pdfork() failed");147case 0:148mode = 666;149CHECK(cap_getmode(&mode) == 0);150/* If cap_getmode() succeeded mode should be modified. */151CHECK(mode != 666);152/* We are in capability mode. */153CHECK(mode == 1);154exit(0);155default:156if (pdwait(pfd) == -1)157err(1, "pdwait() failed");158close(pfd);159}160161exit(0);162}163164165