xref: /freebsd-src/tools/regression/security/cap_test/cap_test_pdkill.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1b465884fSJonathan Anderson /*-
2b465884fSJonathan Anderson  * Copyright (c) 2009-2011 Robert N. M. Watson
3b465884fSJonathan Anderson  * Copyright (c) 2011 Jonathan Anderson
4b465884fSJonathan Anderson  * All rights reserved.
5b465884fSJonathan Anderson  *
6b465884fSJonathan Anderson  * Redistribution and use in source and binary forms, with or without
7b465884fSJonathan Anderson  * modification, are permitted provided that the following conditions
8b465884fSJonathan Anderson  * are met:
9b465884fSJonathan Anderson  * 1. Redistributions of source code must retain the above copyright
10b465884fSJonathan Anderson  *    notice, this list of conditions and the following disclaimer.
11b465884fSJonathan Anderson  * 2. Redistributions in binary form must reproduce the above copyright
12b465884fSJonathan Anderson  *    notice, this list of conditions and the following disclaimer in the
13b465884fSJonathan Anderson  *    documentation and/or other materials provided with the distribution.
14b465884fSJonathan Anderson  *
15b465884fSJonathan Anderson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b465884fSJonathan Anderson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b465884fSJonathan Anderson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b465884fSJonathan Anderson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b465884fSJonathan Anderson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b465884fSJonathan Anderson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b465884fSJonathan Anderson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b465884fSJonathan Anderson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b465884fSJonathan Anderson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b465884fSJonathan Anderson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b465884fSJonathan Anderson  * SUCH DAMAGE.
26b465884fSJonathan Anderson  */
27b465884fSJonathan Anderson 
28b465884fSJonathan Anderson /*
29b465884fSJonathan Anderson  * Test routines to make sure a variety of system calls are or are not
30b465884fSJonathan Anderson  * available in capability mode.  The goal is not to see if they work, just
31b465884fSJonathan Anderson  * whether or not they return the expected ECAPMODE.
32b465884fSJonathan Anderson  */
33b465884fSJonathan Anderson 
34b465884fSJonathan Anderson #include <sys/types.h>
35b465884fSJonathan Anderson 
36*b881b8beSRobert Watson #include <sys/capsicum.h>
37b465884fSJonathan Anderson #include <sys/errno.h>
38b465884fSJonathan Anderson #include <sys/procdesc.h>
39b465884fSJonathan Anderson #include <sys/resource.h>
40b465884fSJonathan Anderson #include <sys/wait.h>
41b465884fSJonathan Anderson 
42b465884fSJonathan Anderson #include <err.h>
43b465884fSJonathan Anderson #include <signal.h>
44b465884fSJonathan Anderson #include <stdlib.h>
45b465884fSJonathan Anderson #include <string.h>
46b465884fSJonathan Anderson #include <unistd.h>
47b465884fSJonathan Anderson 
48b465884fSJonathan Anderson #include <stdio.h>
49b465884fSJonathan Anderson 
50b465884fSJonathan Anderson #include "cap_test.h"
51b465884fSJonathan Anderson 
52b465884fSJonathan Anderson void handle_signal(int);
handle_signal(int sig)53b465884fSJonathan Anderson void handle_signal(int sig) {
54b465884fSJonathan Anderson 	exit(PASSED);
55b465884fSJonathan Anderson }
56b465884fSJonathan Anderson 
57b465884fSJonathan Anderson int
test_pdkill(void)58b465884fSJonathan Anderson test_pdkill(void)
59b465884fSJonathan Anderson {
60b465884fSJonathan Anderson 	int success = PASSED;
61b465884fSJonathan Anderson 	int pd, error;
62b465884fSJonathan Anderson 	pid_t pid;
63b465884fSJonathan Anderson 
64b465884fSJonathan Anderson 	//cap_enter();
65b465884fSJonathan Anderson 
66b465884fSJonathan Anderson 	error = pdfork(&pd, 0);
67b465884fSJonathan Anderson 	if (error < 0)
68b465884fSJonathan Anderson 		err(-1, "pdfork");
69b465884fSJonathan Anderson 
70b465884fSJonathan Anderson 	else if (error == 0) {
71b465884fSJonathan Anderson 		signal(SIGINT, handle_signal);
72b465884fSJonathan Anderson 		sleep(3600);
73b465884fSJonathan Anderson 		exit(FAILED);
74b465884fSJonathan Anderson 	}
75b465884fSJonathan Anderson 
76b465884fSJonathan Anderson 	/* Parent process; find the child's PID (we'll need it later). */
77b465884fSJonathan Anderson 	error = pdgetpid(pd, &pid);
78b465884fSJonathan Anderson 	if (error != 0)
79b465884fSJonathan Anderson 		FAIL("pdgetpid");
80b465884fSJonathan Anderson 
81b465884fSJonathan Anderson 	/* Kill the child! */
82b465884fSJonathan Anderson 	usleep(100);
83b465884fSJonathan Anderson 	error = pdkill(pd, SIGINT);
84b465884fSJonathan Anderson 	if (error != 0)
85b465884fSJonathan Anderson 		FAIL("pdkill");
86b465884fSJonathan Anderson 
87b465884fSJonathan Anderson 	/* Make sure the child finished properly. */
88b465884fSJonathan Anderson 	int status;
89b465884fSJonathan Anderson 	while (waitpid(pid, &status, 0) != pid) {}
90b465884fSJonathan Anderson 	if ((success == PASSED) && WIFEXITED(status))
91b465884fSJonathan Anderson 		success = WEXITSTATUS(status);
92b465884fSJonathan Anderson 	else
93b465884fSJonathan Anderson 		success = FAILED;
94b465884fSJonathan Anderson 
95b465884fSJonathan Anderson 	return (success);
96b465884fSJonathan Anderson }
97