xref: /freebsd-src/tools/regression/security/cap_test/cap_test_capmode.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1ceb42a13SJonathan Anderson /*-
2ceb42a13SJonathan Anderson  * Copyright (c) 2008-2009 Robert N. M. Watson
3f8c6c2cfSJonathan Anderson  * Copyright (c) 2011 Jonathan Anderson
4ceb42a13SJonathan Anderson  * All rights reserved.
5ceb42a13SJonathan Anderson  *
6ceb42a13SJonathan Anderson  * Redistribution and use in source and binary forms, with or without
7ceb42a13SJonathan Anderson  * modification, are permitted provided that the following conditions
8ceb42a13SJonathan Anderson  * are met:
9ceb42a13SJonathan Anderson  * 1. Redistributions of source code must retain the above copyright
10ceb42a13SJonathan Anderson  *    notice, this list of conditions and the following disclaimer.
11ceb42a13SJonathan Anderson  * 2. Redistributions in binary form must reproduce the above copyright
12ceb42a13SJonathan Anderson  *    notice, this list of conditions and the following disclaimer in the
13ceb42a13SJonathan Anderson  *    documentation and/or other materials provided with the distribution.
14ceb42a13SJonathan Anderson  *
15ceb42a13SJonathan Anderson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ceb42a13SJonathan Anderson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ceb42a13SJonathan Anderson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ceb42a13SJonathan Anderson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ceb42a13SJonathan Anderson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ceb42a13SJonathan Anderson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ceb42a13SJonathan Anderson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ceb42a13SJonathan Anderson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ceb42a13SJonathan Anderson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ceb42a13SJonathan Anderson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ceb42a13SJonathan Anderson  * SUCH DAMAGE.
26ceb42a13SJonathan Anderson  */
27ceb42a13SJonathan Anderson 
28ceb42a13SJonathan Anderson /*
29ceb42a13SJonathan Anderson  * Test routines to make sure a variety of system calls are or are not
30ceb42a13SJonathan Anderson  * available in capability mode.  The goal is not to see if they work, just
31ceb42a13SJonathan Anderson  * whether or not they return the expected ECAPMODE.
32ceb42a13SJonathan Anderson  */
33ceb42a13SJonathan Anderson 
34ceb42a13SJonathan Anderson #include <sys/param.h>
35*b881b8beSRobert Watson #include <sys/capsicum.h>
36f8c6c2cfSJonathan Anderson #include <sys/errno.h>
37ceb42a13SJonathan Anderson #include <sys/mman.h>
38ceb42a13SJonathan Anderson #include <sys/mount.h>
39ceb42a13SJonathan Anderson #include <sys/socket.h>
40ceb42a13SJonathan Anderson #include <sys/stat.h>
41ceb42a13SJonathan Anderson #include <sys/wait.h>
42ceb42a13SJonathan Anderson 
43ceb42a13SJonathan Anderson #include <machine/sysarch.h>
44ceb42a13SJonathan Anderson 
45ceb42a13SJonathan Anderson #include <err.h>
46ceb42a13SJonathan Anderson #include <fcntl.h>
47ceb42a13SJonathan Anderson #include <stdlib.h>
48ceb42a13SJonathan Anderson #include <string.h>
49ceb42a13SJonathan Anderson #include <unistd.h>
50ceb42a13SJonathan Anderson 
51ceb42a13SJonathan Anderson #include "cap_test.h"
52ceb42a13SJonathan Anderson 
53f8c6c2cfSJonathan Anderson #define	CHECK_SYSCALL_VOID_NOT_ECAPMODE(syscall, ...)	do {		\
54f8c6c2cfSJonathan Anderson 	errno = 0;							\
55f8c6c2cfSJonathan Anderson 	(void)syscall(__VA_ARGS__);					\
56f8c6c2cfSJonathan Anderson 	if (errno == ECAPMODE)						\
57f8c6c2cfSJonathan Anderson 		FAIL("capmode: %s failed with ECAPMODE", #syscall);	\
58f8c6c2cfSJonathan Anderson } while (0)
59f8c6c2cfSJonathan Anderson 
60f8c6c2cfSJonathan Anderson int
test_capmode(void)61ceb42a13SJonathan Anderson test_capmode(void)
62ceb42a13SJonathan Anderson {
63ceb42a13SJonathan Anderson 	struct statfs statfs;
64ceb42a13SJonathan Anderson 	struct stat sb;
65ceb42a13SJonathan Anderson 	long sysarch_arg = 0;
66f8c6c2cfSJonathan Anderson 	int fd_close, fd_dir, fd_file, fd_socket, fd2[2];
67f8c6c2cfSJonathan Anderson 	int success = PASSED;
68ceb42a13SJonathan Anderson 	pid_t pid, wpid;
69ceb42a13SJonathan Anderson 	char ch;
70ceb42a13SJonathan Anderson 
71f8c6c2cfSJonathan Anderson 	/* Open some files to play with. */
72f8c6c2cfSJonathan Anderson 	REQUIRE(fd_file = open("/tmp/cap_capmode", O_RDWR|O_CREAT, 0644));
73f8c6c2cfSJonathan Anderson 	REQUIRE(fd_close = open("/dev/null", O_RDWR));
74f8c6c2cfSJonathan Anderson 	REQUIRE(fd_dir = open("/tmp", O_RDONLY));
75f8c6c2cfSJonathan Anderson 	REQUIRE(fd_socket = socket(PF_INET, SOCK_DGRAM, 0));
76ceb42a13SJonathan Anderson 
77f8c6c2cfSJonathan Anderson 	/* Enter capability mode. */
78f8c6c2cfSJonathan Anderson 	REQUIRE(cap_enter());
79ceb42a13SJonathan Anderson 
80ceb42a13SJonathan Anderson 	/*
81f8c6c2cfSJonathan Anderson 	 * System calls that are not permitted in capability mode.
82ceb42a13SJonathan Anderson 	 */
83f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(access, "/tmp/cap_capmode_access", F_OK);
84f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(acct, "/tmp/cap_capmode_acct");
85f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(bind, PF_INET, NULL, 0);
86f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(chdir, "/tmp/cap_capmode_chdir");
87f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(chflags, "/tmp/cap_capmode_chflags", UF_NODUMP);
88f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(chmod, "/tmp/cap_capmode_chmod", 0644);
89f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(chown, "/tmp/cap_capmode_chown", -1, -1);
90f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(chroot, "/tmp/cap_capmode_chroot");
91f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(connect, PF_INET, NULL, 0);
92f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(creat, "/tmp/cap_capmode_creat", 0644);
93f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(fchdir, fd_dir);
94f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(getfsstat, &statfs, sizeof(statfs), MNT_NOWAIT);
95f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(link, "/tmp/foo", "/tmp/bar");
96f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(lstat, "/tmp/cap_capmode_lstat", &sb);
97f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(mknod, "/tmp/capmode_mknod", 06440, 0);
98f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(mount, "procfs", "/not_mounted", 0, NULL);
99f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(open, "/dev/null", O_RDWR);
100f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(readlink, "/tmp/cap_capmode_readlink", NULL, 0);
101f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(revoke, "/tmp/cap_capmode_revoke");
102f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(stat, "/tmp/cap_capmode_stat", &sb);
103f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(symlink,
104f8c6c2cfSJonathan Anderson 	    "/tmp/cap_capmode_symlink_from",
105f8c6c2cfSJonathan Anderson 	    "/tmp/cap_capmode_symlink_to");
106f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(unlink, "/tmp/cap_capmode_unlink");
107f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(unmount, "/not_mounted", 0);
108ceb42a13SJonathan Anderson 
109f8c6c2cfSJonathan Anderson 	/*
110f8c6c2cfSJonathan Anderson 	 * System calls that are permitted in capability mode.
111f8c6c2cfSJonathan Anderson 	 */
112f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(close, fd_close);
113f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(dup, fd_file);
114f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(fstat, fd_file, &sb);
115bb729f85SChristian Brueffer 	CHECK_SYSCALL_SUCCEEDS(lseek, fd_file, 0, SEEK_SET);
116f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(msync, &fd_file, 8192, MS_ASYNC);
117f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(profil, NULL, 0, 0, 0);
118f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(read, fd_file, &ch, sizeof(ch));
119f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(recvfrom, fd_socket, NULL, 0, 0, NULL, NULL);
120f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(setuid, getuid());
121f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_SUCCEEDS(write, fd_file, &ch, sizeof(ch));
122ceb42a13SJonathan Anderson 
123f8c6c2cfSJonathan Anderson 	/*
124f8c6c2cfSJonathan Anderson 	 * These calls will fail for lack of e.g. a proper name to send to,
125f8c6c2cfSJonathan Anderson 	 * but they are allowed in capability mode, so errno != ECAPMODE.
126f8c6c2cfSJonathan Anderson 	 */
127f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(accept, fd_socket, NULL, NULL);
128f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(getpeername, fd_socket, NULL, NULL);
129f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(getsockname, fd_socket, NULL, NULL);
130f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(fchflags, fd_file, UF_NODUMP);
131f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(recvmsg, fd_socket, NULL, 0);
132f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(sendmsg, fd_socket, NULL, 0);
133f8c6c2cfSJonathan Anderson 	CHECK_NOT_CAPMODE(sendto, fd_socket, NULL, 0, 0, NULL, 0);
134ceb42a13SJonathan Anderson 
135f8c6c2cfSJonathan Anderson 	/*
136f8c6c2cfSJonathan Anderson 	 * System calls which should be allowed in capability mode, but which
137f8c6c2cfSJonathan Anderson 	 * don't return errors, and are thus difficult to check.
138f8c6c2cfSJonathan Anderson 	 *
139f8c6c2cfSJonathan Anderson 	 * We will try anyway, by checking errno.
140f8c6c2cfSJonathan Anderson 	 */
141f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(getegid);
142f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(geteuid);
143f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(getgid);
144f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(getpid);
145f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(getppid);
146f8c6c2cfSJonathan Anderson 	CHECK_SYSCALL_VOID_NOT_ECAPMODE(getuid);
147ceb42a13SJonathan Anderson 
148f8c6c2cfSJonathan Anderson 	/*
149f8c6c2cfSJonathan Anderson 	 * Finally, tests for system calls that don't fit the pattern very well.
150f8c6c2cfSJonathan Anderson 	 */
151ceb42a13SJonathan Anderson 	pid = fork();
152ceb42a13SJonathan Anderson 	if (pid >= 0) {
153ceb42a13SJonathan Anderson 		if (pid == 0) {
154ceb42a13SJonathan Anderson 			exit(0);
155ceb42a13SJonathan Anderson 		} else if (pid > 0) {
156ceb42a13SJonathan Anderson 			wpid = waitpid(pid, NULL, 0);
157ceb42a13SJonathan Anderson 			if (wpid < 0) {
158ceb42a13SJonathan Anderson 				if (errno != ECAPMODE)
159f8c6c2cfSJonathan Anderson 					FAIL("capmode:waitpid");
160ceb42a13SJonathan Anderson 			} else
161f8c6c2cfSJonathan Anderson 				FAIL("capmode:waitpid succeeded");
162ceb42a13SJonathan Anderson 		}
163ceb42a13SJonathan Anderson 	} else
164f8c6c2cfSJonathan Anderson 		FAIL("capmode:fork");
165ceb42a13SJonathan Anderson 
166ceb42a13SJonathan Anderson 	if (getlogin() == NULL)
167f8c6c2cfSJonathan Anderson 		FAIL("test_sycalls:getlogin %d", errno);
168ceb42a13SJonathan Anderson 
169ceb42a13SJonathan Anderson 	if (getsockname(fd_socket, NULL, NULL) < 0) {
170ceb42a13SJonathan Anderson 		if (errno == ECAPMODE)
171f8c6c2cfSJonathan Anderson 			FAIL("capmode:getsockname");
172ceb42a13SJonathan Anderson 	}
173ceb42a13SJonathan Anderson 
174ceb42a13SJonathan Anderson 	/* XXXRW: ktrace */
175ceb42a13SJonathan Anderson 
176ceb42a13SJonathan Anderson 	if (pipe(fd2) == 0) {
177ceb42a13SJonathan Anderson 		close(fd2[0]);
178ceb42a13SJonathan Anderson 		close(fd2[1]);
179ceb42a13SJonathan Anderson 	} else if (errno == ECAPMODE)
180f8c6c2cfSJonathan Anderson 		FAIL("capmode:pipe");
181ceb42a13SJonathan Anderson 
182ceb42a13SJonathan Anderson 	/* XXXRW: ptrace. */
183ceb42a13SJonathan Anderson 
184ceb42a13SJonathan Anderson 	/* sysarch() is, by definition, architecture-dependent */
185f8c6c2cfSJonathan Anderson #if defined (__amd64__) || defined (__i386__)
186f8c6c2cfSJonathan Anderson 	CHECK_CAPMODE(sysarch, I386_SET_IOPERM, &sysarch_arg);
187f8c6c2cfSJonathan Anderson #else
188ceb42a13SJonathan Anderson 	/* XXXJA: write a test for arm */
189f8c6c2cfSJonathan Anderson 	FAIL("capmode:no sysarch() test for current architecture");
190f8c6c2cfSJonathan Anderson #endif
191ceb42a13SJonathan Anderson 
192ceb42a13SJonathan Anderson 	/* XXXRW: No error return from sync(2) to test. */
193ceb42a13SJonathan Anderson 
194f8c6c2cfSJonathan Anderson 	return (success);
195ceb42a13SJonathan Anderson }
196