cap_test_pdfork.c revision 267654
198184Sgordon/*- 298184Sgordon * Copyright (c) 2009-2011 Robert N. M. Watson 398184Sgordon * Copyright (c) 2011 Jonathan Anderson 498184Sgordon * All rights reserved. 598184Sgordon * 6151806Syar * Redistribution and use in source and binary forms, with or without 7113676Smtm * modification, are permitted provided that the following conditions 8136224Smtm * are met: 998184Sgordon * 1. Redistributions of source code must retain the above copyright 1098184Sgordon * notice, this list of conditions and the following disclaimer. 1198184Sgordon * 2. Redistributions in binary form must reproduce the above copyright 1298184Sgordon * notice, this list of conditions and the following disclaimer in the 13138889Sbrian * documentation and/or other materials provided with the distribution. 14151807Syar * 15168119Smtm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16168119Smtm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1798184Sgordon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18168119Smtm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1998184Sgordon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2098184Sgordon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2198184Sgordon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2298184Sgordon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23104980Sschweikh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24104980Sschweikh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25104980Sschweikh * SUCH DAMAGE. 2698184Sgordon * 2798184Sgordon * $FreeBSD: releng/9.3/tools/regression/security/cap_test/cap_test_pdfork.c 224989 2011-08-18 23:15:04Z jonathan $ 28138889Sbrian */ 2998184Sgordon 3098184Sgordon/* 3198184Sgordon * Test routines to make sure a variety of system calls are or are not 3298184Sgordon * available in capability mode. The goal is not to see if they work, just 3398184Sgordon * whether or not they return the expected ECAPMODE. 34138889Sbrian */ 3598184Sgordon 3698184Sgordon#include <sys/cdefs.h> 3798184Sgordon__FBSDID("$FreeBSD: releng/9.3/tools/regression/security/cap_test/cap_test_pdfork.c 224989 2011-08-18 23:15:04Z jonathan $"); 38138889Sbrian 39138889Sbrian#include <sys/types.h> 4098184Sgordon 41168119Smtm#include <sys/capability.h> 42138889Sbrian#include <sys/errno.h> 43163696Savatar#include <sys/procdesc.h> 4498184Sgordon#include <sys/resource.h> 4598184Sgordon#include <sys/stat.h> 46163696Savatar#include <sys/wait.h> 4798184Sgordon 4898184Sgordon#include <err.h> 4998184Sgordon#include <stdlib.h> 5098184Sgordon#include <string.h> 51#include <unistd.h> 52 53#include <stdio.h> 54#include <time.h> 55 56#include "cap_test.h" 57 58int 59test_pdfork(void) 60{ 61 struct stat stat; 62 int success = PASSED; 63 int pd, error; 64 pid_t pid; 65 time_t now; 66 67 //cap_enter(); 68 69 pid = pdfork(&pd, 0); 70 if (pid < 0) 71 err(-1, "pdfork"); 72 73 else if (pid == 0) { 74 /* 75 * Child process. 76 * 77 * pd should not be a valid process descriptor. 78 */ 79 error = pdgetpid(pd, &pid); 80 if (error != -1) 81 FAILX("pdgetpid succeeded"); 82 else if (errno != EBADF) 83 FAIL("pdgetpid failed, but errno != EBADF"); 84 85 exit(success); 86 } 87 88 /* Parent process. Ensure that [acm]times have been set correctly. */ 89 REQUIRE(fstat(pd, &stat)); 90 91 now = time(NULL); 92 CHECK(now != (time_t)-1); 93 94 CHECK(now >= stat.st_birthtime); 95 CHECK((now - stat.st_birthtime) < 2); 96 CHECK(stat.st_birthtime == stat.st_atime); 97 CHECK(stat.st_atime == stat.st_ctime); 98 CHECK(stat.st_ctime == stat.st_mtime); 99 100 /* Wait for the child to finish. */ 101 error = pdgetpid(pd, &pid); 102 CHECK(error == 0); 103 CHECK(pid > 0); 104 105 int status; 106 while (waitpid(pid, &status, 0) != pid) {} 107 if ((success == PASSED) && WIFEXITED(status)) 108 success = WEXITSTATUS(status); 109 else 110 success = FAILED; 111 112 return (success); 113} 114