1224989Sjonathan/*- 2224989Sjonathan * Copyright (c) 2009-2011 Robert N. M. Watson 3224989Sjonathan * Copyright (c) 2011 Jonathan Anderson 4224989Sjonathan * All rights reserved. 5224989Sjonathan * 6224989Sjonathan * Redistribution and use in source and binary forms, with or without 7224989Sjonathan * modification, are permitted provided that the following conditions 8224989Sjonathan * are met: 9224989Sjonathan * 1. Redistributions of source code must retain the above copyright 10224989Sjonathan * notice, this list of conditions and the following disclaimer. 11224989Sjonathan * 2. Redistributions in binary form must reproduce the above copyright 12224989Sjonathan * notice, this list of conditions and the following disclaimer in the 13224989Sjonathan * documentation and/or other materials provided with the distribution. 14224989Sjonathan * 15224989Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16224989Sjonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17224989Sjonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18224989Sjonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19224989Sjonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20224989Sjonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21224989Sjonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22224989Sjonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23224989Sjonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24224989Sjonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25224989Sjonathan * SUCH DAMAGE. 26224989Sjonathan * 27224989Sjonathan * $FreeBSD$ 28224989Sjonathan */ 29224989Sjonathan 30224989Sjonathan/* 31224989Sjonathan * Test routines to make sure a variety of system calls are or are not 32224989Sjonathan * available in capability mode. The goal is not to see if they work, just 33224989Sjonathan * whether or not they return the expected ECAPMODE. 34224989Sjonathan */ 35224989Sjonathan 36224989Sjonathan#include <sys/cdefs.h> 37224989Sjonathan__FBSDID("$FreeBSD$"); 38224989Sjonathan 39224989Sjonathan#include <sys/types.h> 40224989Sjonathan 41263234Srwatson#include <sys/capsicum.h> 42224989Sjonathan#include <sys/errno.h> 43224989Sjonathan#include <sys/procdesc.h> 44224989Sjonathan#include <sys/resource.h> 45224989Sjonathan#include <sys/wait.h> 46224989Sjonathan 47224989Sjonathan#include <err.h> 48224989Sjonathan#include <signal.h> 49224989Sjonathan#include <stdlib.h> 50224989Sjonathan#include <string.h> 51224989Sjonathan#include <unistd.h> 52224989Sjonathan 53224989Sjonathan#include <stdio.h> 54224989Sjonathan 55224989Sjonathan#include "cap_test.h" 56224989Sjonathan 57224989Sjonathanvoid handle_signal(int); 58224989Sjonathanvoid handle_signal(int sig) { 59224989Sjonathan exit(PASSED); 60224989Sjonathan} 61224989Sjonathan 62224989Sjonathanint 63224989Sjonathantest_pdkill(void) 64224989Sjonathan{ 65224989Sjonathan int success = PASSED; 66224989Sjonathan int pd, error; 67224989Sjonathan pid_t pid; 68224989Sjonathan 69224989Sjonathan //cap_enter(); 70224989Sjonathan 71224989Sjonathan error = pdfork(&pd, 0); 72224989Sjonathan if (error < 0) 73224989Sjonathan err(-1, "pdfork"); 74224989Sjonathan 75224989Sjonathan else if (error == 0) { 76224989Sjonathan signal(SIGINT, handle_signal); 77224989Sjonathan sleep(3600); 78224989Sjonathan exit(FAILED); 79224989Sjonathan } 80224989Sjonathan 81224989Sjonathan /* Parent process; find the child's PID (we'll need it later). */ 82224989Sjonathan error = pdgetpid(pd, &pid); 83224989Sjonathan if (error != 0) 84224989Sjonathan FAIL("pdgetpid"); 85224989Sjonathan 86224989Sjonathan /* Kill the child! */ 87224989Sjonathan usleep(100); 88224989Sjonathan error = pdkill(pd, SIGINT); 89224989Sjonathan if (error != 0) 90224989Sjonathan FAIL("pdkill"); 91224989Sjonathan 92224989Sjonathan /* Make sure the child finished properly. */ 93224989Sjonathan int status; 94224989Sjonathan while (waitpid(pid, &status, 0) != pid) {} 95224989Sjonathan if ((success == PASSED) && WIFEXITED(status)) 96224989Sjonathan success = WEXITSTATUS(status); 97224989Sjonathan else 98224989Sjonathan success = FAILED; 99224989Sjonathan 100224989Sjonathan return (success); 101224989Sjonathan} 102