Path: blob/main/tools/regression/security/cap_test/cap_test_pdfork.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/capsium.h>36#include <sys/errno.h>37#include <sys/procdesc.h>38#include <sys/resource.h>39#include <sys/stat.h>40#include <sys/wait.h>4142#include <err.h>43#include <stdlib.h>44#include <string.h>45#include <unistd.h>4647#include <stdio.h>48#include <time.h>4950#include "cap_test.h"5152int53test_pdfork(void)54{55struct stat stat;56int success = PASSED;57int pd, error;58pid_t pid;59time_t now;6061//cap_enter();6263pid = pdfork(&pd, 0);64if (pid < 0)65err(-1, "pdfork");6667else if (pid == 0) {68/*69* Child process.70*71* pd should not be a valid process descriptor.72*/73error = pdgetpid(pd, &pid);74if (error != -1)75FAILX("pdgetpid succeeded");76else if (errno != EBADF)77FAIL("pdgetpid failed, but errno != EBADF");7879exit(success);80}8182/* Parent process. Ensure that [acm]times have been set correctly. */83REQUIRE(fstat(pd, &stat));8485now = time(NULL);86CHECK(now != (time_t)-1);8788CHECK(now >= stat.st_birthtime);89CHECK((now - stat.st_birthtime) < 2);90CHECK(stat.st_birthtime == stat.st_atime);91CHECK(stat.st_atime == stat.st_ctime);92CHECK(stat.st_ctime == stat.st_mtime);9394/* Wait for the child to finish. */95error = pdgetpid(pd, &pid);96CHECK(error == 0);97CHECK(pid > 0);9899int status;100while (waitpid(pid, &status, 0) != pid) {}101if ((success == PASSED) && WIFEXITED(status))102success = WEXITSTATUS(status);103else104success = FAILED;105106return (success);107}108109110