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.
30 * Confirm that privilege is required to open a pfkey socket, and that this
31 * is not allowed in jail.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <net/pfkeyv2.h>
37 #include <netinet/in.h>
38 #include <netipsec/ipsec.h>
47 static char policy_bypass[] = "in bypass";
48 static char policy_entrust[] = "in entrust";
49 static char *bypassbuf = NULL;
50 static char *entrustbuf = NULL;
55 priv_netinet_ipsec_policy_bypass_setup_af(int asroot, int injail,
56 struct test *test, int af)
59 bypassbuf = ipsec_set_policy(policy_bypass, sizeof(policy_bypass) - 1);
60 if (bypassbuf == NULL) {
61 warn("%s: ipsec_set_policy(NULL)", __func__);
66 sd = socket(AF_INET, SOCK_DGRAM, 0);
68 warn("%s: socket4", __func__);
74 sd = socket(AF_INET6, SOCK_DGRAM, 0);
76 warn("%s: socket6", __func__);
82 warnx("%s: unexpected address family", __func__);
89 priv_netinet_ipsec_policy4_bypass_setup(int asroot, int injail,
93 return (priv_netinet_ipsec_policy_bypass_setup_af(asroot, injail, test,
99 priv_netinet_ipsec_policy6_bypass_setup(int asroot, int injail,
103 return (priv_netinet_ipsec_policy_bypass_setup_af(asroot, injail, test,
110 priv_netinet_ipsec_policy_entrust_setup_af(int asroot, int injail,
111 struct test *test, int af)
114 entrustbuf = ipsec_set_policy(policy_entrust, sizeof(policy_entrust)-1);
115 if (entrustbuf == NULL) {
116 warn("%s: ipsec_set_policy(NULL)", __func__);
121 sd = socket(AF_INET, SOCK_DGRAM, 0);
123 warn("%s: socket4", __func__);
129 sd = socket(AF_INET6, SOCK_DGRAM, 0);
131 warn("%s: socket6", __func__);
137 warnx("%s: unexpected address family", __func__);
144 priv_netinet_ipsec_policy4_entrust_setup(int asroot, int injail,
148 return (priv_netinet_ipsec_policy_entrust_setup_af(asroot, injail, test,
154 priv_netinet_ipsec_policy6_entrust_setup(int asroot, int injail,
158 return (priv_netinet_ipsec_policy_entrust_setup_af(asroot, injail, test,
164 priv_netinet_ipsec_pfkey(int asroot, int injail, struct test *test)
168 fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
174 * The injail checks are not really priv checks but making sure
175 * sys/kern/uipc_socket.c:socreate cred checks are working correctly.
177 if (asroot && injail)
178 expect("priv_netinet_ipsec_pfkey(asroot, injail)", error,
179 -1, EPROTONOSUPPORT);
180 if (asroot && !injail)
181 expect("priv_netinet_ipsec_pfkey(asroot, !injail)", error,
183 if (!asroot && injail)
184 expect("priv_netinet_ipsec_pfkey(!asroot, injail)", error,
185 -1, EPROTONOSUPPORT);
186 if (!asroot && !injail)
187 expect("priv_netinet_ipsec_pfkey(!asroot, !injail)", error,
195 priv_netinet_ipsec_policy_bypass_af(int asroot, int injail, struct test *test,
198 int error, level, optname;
203 optname = IP_IPSEC_POLICY;
207 level = IPPROTO_IPV6;
208 optname = IPV6_IPSEC_POLICY;
212 warnx("%s: unexpected address family", __func__);
215 error = setsockopt(sd, level, optname,
216 bypassbuf, ipsec_get_policylen(bypassbuf));
217 if (asroot && injail)
218 expect("priv_netinet_ipsec_policy_bypass(asroot, injail)",
219 error, -1, EACCES); /* see ipsec_set_policy */
220 if (asroot && !injail)
221 expect("priv_netinet_ipsec_policy_bypass(asroot, !injail)",
223 if (!asroot && injail)
224 expect("priv_netinet_ipsec_policy_bypass(!asroot, injail)",
225 error, -1, EACCES); /* see ipsec_set_policy */
226 if (!asroot && !injail)
227 expect("priv_netinet_ipsec_policy_bypass(!asroot, !injail)",
228 error, -1, EACCES); /* see ipsec_set_policy */
232 priv_netinet_ipsec_policy4_bypass(int asroot, int injail, struct test *test)
235 priv_netinet_ipsec_policy_bypass_af(asroot, injail, test, AF_INET);
240 priv_netinet_ipsec_policy6_bypass(int asroot, int injail, struct test *test)
243 priv_netinet_ipsec_policy_bypass_af(asroot, injail, test, AF_INET6);
248 priv_netinet_ipsec_policy_entrust_af(int asroot, int injail, struct test *test,
251 int error, level, optname;
256 optname = IP_IPSEC_POLICY;
260 level = IPPROTO_IPV6;
261 optname = IPV6_IPSEC_POLICY;
265 warnx("%s: unexpected address family", __func__);
268 error = setsockopt(sd, level, optname,
269 entrustbuf, ipsec_get_policylen(entrustbuf));
270 if (asroot && injail)
271 expect("priv_netinet_ipsec_policy_entrust(asroot, injail)",
272 error, 0, 0); /* XXX ipsec_set_policy */
273 if (asroot && !injail)
274 expect("priv_netinet_ipsec_policy_entrust(asroot, !injail)",
276 if (!asroot && injail)
277 expect("priv_netinet_ipsec_policy_entrust(!asroot, injail)",
278 error, 0, 0); /* XXX ipsec_set_policy */
279 if (!asroot && !injail)
280 expect("priv_netinet_ipsec_policy_entrust(!asroot, !injail)",
281 error, 0, 0); /* XXX ipsec_set_policy */
285 priv_netinet_ipsec_policy4_entrust(int asroot, int injail, struct test *test)
288 priv_netinet_ipsec_policy_entrust_af(asroot, injail, test, AF_INET);
293 priv_netinet_ipsec_policy6_entrust(int asroot, int injail, struct test *test)
296 priv_netinet_ipsec_policy_entrust_af(asroot, injail, test, AF_INET6);
301 priv_netinet_ipsec_policy_bypass_cleanup(int asroot, int injail,
305 if (bypassbuf != NULL) {
316 priv_netinet_ipsec_policy_entrust_cleanup(int asroot, int injail,
320 if (entrustbuf != NULL) {