xref: /freebsd/tests/sys/capsicum/ioctls_test.c (revision 1d386b48)
1 /*-
2  * Copyright (c) 2018 John Baldwin <jhb@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <sys/capsicum.h>
28 #include <sys/filio.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <netinet/in.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 
36 #include <atf-c.h>
37 
38 #include "freebsd_test_suite/macros.h"
39 
40 /*
41  * A variant of ATF_REQUIRE that is suitable for use in child
42  * processes.  This only works if the parent process is tripped up by
43  * the early exit and fails some requirement itself.
44  */
45 #define	CHILD_REQUIRE(exp) do {						\
46 		if (!(exp))						\
47 			child_fail_require(__FILE__, __LINE__,		\
48 			    #exp " not met");				\
49 	} while (0)
50 
51 static __dead2 void
52 child_fail_require(const char *file, int line, const char *str)
53 {
54 	char buf[128];
55 
56 	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
57 	write(2, buf, strlen(buf));
58 	_exit(32);
59 }
60 
61 /*
62  * Exercise the edge case of a custom ioctl list being copied from a
63  * listen socket to an accepted socket.
64  */
65 ATF_TC_WITHOUT_HEAD(cap_ioctls__listen_copy);
66 ATF_TC_BODY(cap_ioctls__listen_copy, tc)
67 {
68 	struct sockaddr_in sin;
69 	cap_rights_t rights;
70 	u_long cmds[] = { FIONREAD };
71 	socklen_t len;
72 	pid_t pid;
73 	char dummy;
74 	int s[2], status;
75 
76 	ATF_REQUIRE_FEATURE("security_capabilities");
77 
78 	s[0] = socket(AF_INET, SOCK_STREAM, 0);
79 	ATF_REQUIRE(s[0] > 0);
80 
81 	/* Bind to an arbitrary unused port. */
82 	memset(&sin, 0, sizeof(sin));
83 	sin.sin_len = sizeof(sin);
84 	sin.sin_family = AF_INET;
85 	sin.sin_port = 0;
86 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
87 	ATF_REQUIRE(bind(s[0], (struct sockaddr *)&sin, sizeof(sin)) == 0);
88 
89 	CHILD_REQUIRE(listen(s[0], 1) == 0);
90 
91 	len = sizeof(sin);
92 	ATF_REQUIRE(getsockname(s[0], (struct sockaddr *)&sin, &len) == 0);
93 	ATF_REQUIRE(len == sizeof(sin));
94 
95 	cap_rights_init(&rights, CAP_ACCEPT, CAP_IOCTL);
96 	ATF_REQUIRE(cap_rights_limit(s[0], &rights) == 0);
97 	ATF_REQUIRE(cap_ioctls_limit(s[0], cmds, nitems(cmds)) == 0);
98 
99 	pid = fork();
100 	if (pid == 0) {
101 		s[1] = accept(s[0], NULL, NULL);
102 		CHILD_REQUIRE(s[1] > 0);
103 
104 		/* Close both sockets during exit(). */
105 		exit(0);
106 	}
107 
108 	ATF_REQUIRE(pid > 0);
109 
110 	ATF_REQUIRE(close(s[0]) == 0);
111 	s[1] = socket(AF_INET, SOCK_STREAM, 0);
112 	ATF_REQUIRE(s[1] > 0);
113 	ATF_REQUIRE(connect(s[1], (struct sockaddr *)&sin, sizeof(sin)) == 0);
114 	ATF_REQUIRE(read(s[1], &dummy, sizeof(dummy)) == 0);
115 	ATF_REQUIRE(close(s[1]) == 0);
116 
117 	ATF_REQUIRE(wait(&status) == pid);
118 	ATF_REQUIRE(WIFEXITED(status));
119 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
120 }
121 
122 ATF_TP_ADD_TCS(tp)
123 {
124 
125 	ATF_TP_ADD_TC(tp, cap_ioctls__listen_copy);
126 
127 	return (atf_no_error());
128 }
129