2 * Copyright (c) 2007 Bjoern A. Zeeb
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
18 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Confirm that privilege is required to open a pfkey socket, and that this
29 * is not allowed in jail.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <net/pfkeyv2.h>
35 #include <netinet/in.h>
36 #include <netipsec/ipsec.h>
45 static char policy_bypass[] = "in bypass";
46 static char policy_entrust[] = "in entrust";
47 static char *bypassbuf = NULL;
48 static char *entrustbuf = NULL;
53 priv_netinet_ipsec_policy_bypass_setup_af(int asroot, int injail,
54 struct test *test, int af)
57 bypassbuf = ipsec_set_policy(policy_bypass, sizeof(policy_bypass) - 1);
58 if (bypassbuf == NULL) {
59 warn("%s: ipsec_set_policy(NULL)", __func__);
64 sd = socket(AF_INET, SOCK_DGRAM, 0);
66 warn("%s: socket4", __func__);
72 sd = socket(AF_INET6, SOCK_DGRAM, 0);
74 warn("%s: socket6", __func__);
80 warnx("%s: unexpected address family", __func__);
87 priv_netinet_ipsec_policy4_bypass_setup(int asroot, int injail,
91 return (priv_netinet_ipsec_policy_bypass_setup_af(asroot, injail, test,
97 priv_netinet_ipsec_policy6_bypass_setup(int asroot, int injail,
101 return (priv_netinet_ipsec_policy_bypass_setup_af(asroot, injail, test,
108 priv_netinet_ipsec_policy_entrust_setup_af(int asroot, int injail,
109 struct test *test, int af)
112 entrustbuf = ipsec_set_policy(policy_entrust, sizeof(policy_entrust)-1);
113 if (entrustbuf == NULL) {
114 warn("%s: ipsec_set_policy(NULL)", __func__);
119 sd = socket(AF_INET, SOCK_DGRAM, 0);
121 warn("%s: socket4", __func__);
127 sd = socket(AF_INET6, SOCK_DGRAM, 0);
129 warn("%s: socket6", __func__);
135 warnx("%s: unexpected address family", __func__);
142 priv_netinet_ipsec_policy4_entrust_setup(int asroot, int injail,
146 return (priv_netinet_ipsec_policy_entrust_setup_af(asroot, injail, test,
152 priv_netinet_ipsec_policy6_entrust_setup(int asroot, int injail,
156 return (priv_netinet_ipsec_policy_entrust_setup_af(asroot, injail, test,
162 priv_netinet_ipsec_pfkey(int asroot, int injail, struct test *test)
166 fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
172 * The injail checks are not really priv checks but making sure
173 * sys/kern/uipc_socket.c:socreate cred checks are working correctly.
175 if (asroot && injail)
176 expect("priv_netinet_ipsec_pfkey(asroot, injail)", error,
177 -1, EPROTONOSUPPORT);
178 if (asroot && !injail)
179 expect("priv_netinet_ipsec_pfkey(asroot, !injail)", error,
181 if (!asroot && injail)
182 expect("priv_netinet_ipsec_pfkey(!asroot, injail)", error,
183 -1, EPROTONOSUPPORT);
184 if (!asroot && !injail)
185 expect("priv_netinet_ipsec_pfkey(!asroot, !injail)", error,
193 priv_netinet_ipsec_policy_bypass_af(int asroot, int injail, struct test *test,
196 int error, level, optname;
201 optname = IP_IPSEC_POLICY;
205 level = IPPROTO_IPV6;
206 optname = IPV6_IPSEC_POLICY;
210 warnx("%s: unexpected address family", __func__);
213 error = setsockopt(sd, level, optname,
214 bypassbuf, ipsec_get_policylen(bypassbuf));
215 if (asroot && injail)
216 expect("priv_netinet_ipsec_policy_bypass(asroot, injail)",
217 error, -1, EACCES); /* see ipsec_set_policy */
218 if (asroot && !injail)
219 expect("priv_netinet_ipsec_policy_bypass(asroot, !injail)",
221 if (!asroot && injail)
222 expect("priv_netinet_ipsec_policy_bypass(!asroot, injail)",
223 error, -1, EACCES); /* see ipsec_set_policy */
224 if (!asroot && !injail)
225 expect("priv_netinet_ipsec_policy_bypass(!asroot, !injail)",
226 error, -1, EACCES); /* see ipsec_set_policy */
230 priv_netinet_ipsec_policy4_bypass(int asroot, int injail, struct test *test)
233 priv_netinet_ipsec_policy_bypass_af(asroot, injail, test, AF_INET);
238 priv_netinet_ipsec_policy6_bypass(int asroot, int injail, struct test *test)
241 priv_netinet_ipsec_policy_bypass_af(asroot, injail, test, AF_INET6);
246 priv_netinet_ipsec_policy_entrust_af(int asroot, int injail, struct test *test,
249 int error, level, optname;
254 optname = IP_IPSEC_POLICY;
258 level = IPPROTO_IPV6;
259 optname = IPV6_IPSEC_POLICY;
263 warnx("%s: unexpected address family", __func__);
266 error = setsockopt(sd, level, optname,
267 entrustbuf, ipsec_get_policylen(entrustbuf));
268 if (asroot && injail)
269 expect("priv_netinet_ipsec_policy_entrust(asroot, injail)",
270 error, 0, 0); /* XXX ipsec_set_policy */
271 if (asroot && !injail)
272 expect("priv_netinet_ipsec_policy_entrust(asroot, !injail)",
274 if (!asroot && injail)
275 expect("priv_netinet_ipsec_policy_entrust(!asroot, injail)",
276 error, 0, 0); /* XXX ipsec_set_policy */
277 if (!asroot && !injail)
278 expect("priv_netinet_ipsec_policy_entrust(!asroot, !injail)",
279 error, 0, 0); /* XXX ipsec_set_policy */
283 priv_netinet_ipsec_policy4_entrust(int asroot, int injail, struct test *test)
286 priv_netinet_ipsec_policy_entrust_af(asroot, injail, test, AF_INET);
291 priv_netinet_ipsec_policy6_entrust(int asroot, int injail, struct test *test)
294 priv_netinet_ipsec_policy_entrust_af(asroot, injail, test, AF_INET6);
299 priv_netinet_ipsec_policy_bypass_cleanup(int asroot, int injail,
303 if (bypassbuf != NULL) {
314 priv_netinet_ipsec_policy_entrust_cleanup(int asroot, int injail,
318 if (entrustbuf != NULL) {