Path: blob/main/tools/regression/security/cap_test/cap_test_pdkill.c
48266 views
/*-1* Copyright (c) 2009-2011 Robert N. M. Watson2* Copyright (c) 2011 Jonathan Anderson3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* Test routines to make sure a variety of system calls are or are not29* available in capability mode. The goal is not to see if they work, just30* whether or not they return the expected ECAPMODE.31*/3233#include <sys/types.h>3435#include <sys/capsicum.h>36#include <sys/errno.h>37#include <sys/procdesc.h>38#include <sys/resource.h>39#include <sys/wait.h>4041#include <err.h>42#include <signal.h>43#include <stdlib.h>44#include <string.h>45#include <unistd.h>4647#include <stdio.h>4849#include "cap_test.h"5051void handle_signal(int);52void handle_signal(int sig) {53exit(PASSED);54}5556int57test_pdkill(void)58{59int success = PASSED;60int pd, error;61pid_t pid;6263//cap_enter();6465error = pdfork(&pd, 0);66if (error < 0)67err(-1, "pdfork");6869else if (error == 0) {70signal(SIGINT, handle_signal);71sleep(3600);72exit(FAILED);73}7475/* Parent process; find the child's PID (we'll need it later). */76error = pdgetpid(pd, &pid);77if (error != 0)78FAIL("pdgetpid");7980/* Kill the child! */81usleep(100);82error = pdkill(pd, SIGINT);83if (error != 0)84FAIL("pdkill");8586/* Make sure the child finished properly. */87int status;88while (waitpid(pid, &status, 0) != pid) {}89if ((success == PASSED) && WIFEXITED(status))90success = WEXITSTATUS(status);91else92success = FAILED;9394return (success);95}969798