xref: /freebsd/tests/sys/mac/ipacl/ipacl_test.sh (revision 215bab7924f6c8e133a96431b3e2176d5fae1eff)
1*215bab79SShivank Garg#-
2*215bab79SShivank Garg# Copyright (c) 2019, 2023 Shivank Garg <shivank@FreeBSD.org>
3*215bab79SShivank Garg#
4*215bab79SShivank Garg# This code was developed as a Google Summer of Code 2019 project
5*215bab79SShivank Garg# under the guidance of Bjoern A. Zeeb.
6*215bab79SShivank Garg#
7*215bab79SShivank Garg# Redistribution and use in source and binary forms, with or without
8*215bab79SShivank Garg# modification, are permitted provided that the following conditions
9*215bab79SShivank Garg# are met:
10*215bab79SShivank Garg# 1. Redistributions of source code must retain the above copyright
11*215bab79SShivank Garg#    notice, this list of conditions and the following disclaimer.
12*215bab79SShivank Garg# 2. Redistributions in binary form must reproduce the above copyright
13*215bab79SShivank Garg#    notice, this list of conditions and the following disclaimer in the
14*215bab79SShivank Garg#    documentation and/or other materials provided with the distribution.
15*215bab79SShivank Garg#
16*215bab79SShivank Garg# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17*215bab79SShivank Garg# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*215bab79SShivank Garg# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*215bab79SShivank Garg# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20*215bab79SShivank Garg# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*215bab79SShivank Garg# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*215bab79SShivank Garg# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*215bab79SShivank Garg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*215bab79SShivank Garg# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*215bab79SShivank Garg# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*215bab79SShivank Garg# SUCH DAMAGE.
27*215bab79SShivank Garg#
28*215bab79SShivank Garg# $FreeBSD$
29*215bab79SShivank Garg
30*215bab79SShivank Garg. $(atf_get_srcdir)/utils.subr
31*215bab79SShivank Garg
32*215bab79SShivank Gargatf_test_case "ipacl_v4" "cleanup"
33*215bab79SShivank Garg
34*215bab79SShivank Gargipacl_v4_head()
35*215bab79SShivank Garg{
36*215bab79SShivank Garg	atf_set descr 'basic test for ipacl on IPv4 addresses'
37*215bab79SShivank Garg	atf_set require.user root
38*215bab79SShivank Garg}
39*215bab79SShivank Garg
40*215bab79SShivank Gargipacl_v4_body()
41*215bab79SShivank Garg{
42*215bab79SShivank Garg	ipacl_test_init
43*215bab79SShivank Garg
44*215bab79SShivank Garg	epairA=$(vnet_mkepair)
45*215bab79SShivank Garg	epairB=$(vnet_mkepair)
46*215bab79SShivank Garg	epairC=$(vnet_mkepair)
47*215bab79SShivank Garg
48*215bab79SShivank Garg	vnet_mkjail A ${epairA}b
49*215bab79SShivank Garg	vnet_mkjail B ${epairB}b ${epairC}b
50*215bab79SShivank Garg
51*215bab79SShivank Garg	jidA=$(jls -j A -s jid | grep -o -E '[0-9]+')
52*215bab79SShivank Garg	jidB=$(jls -j B -s jid | grep -o -E '[0-9]+')
53*215bab79SShivank Garg
54*215bab79SShivank Garg	# The ipacl policy module is not enforced for IPv4.
55*215bab79SShivank Garg	sysctl security.mac.ipacl.ipv4=0
56*215bab79SShivank Garg
57*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
58*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 192.0.2.2/24 up
59*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
60*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 203.0.113.254/24 up
61*215bab79SShivank Garg
62*215bab79SShivank Garg	# The ipacl policy module is enforced for IPv4 and prevent all
63*215bab79SShivank Garg	# jails from setting their IPv4 address.
64*215bab79SShivank Garg	sysctl security.mac.ipacl.ipv4=1
65*215bab79SShivank Garg	sysctl security.mac.ipacl.rules=
66*215bab79SShivank Garg
67*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
68*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 192.0.2.2/24 up
69*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
70*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 203.0.113.254/24 up
71*215bab79SShivank Garg
72*215bab79SShivank Garg	rule="${jidA},1,${epairA}b,AF_INET,192.0.2.42/-1@"
73*215bab79SShivank Garg	rule="${rule}${jidB},1,${epairB}b,AF_INET,198.51.100.12/-1@"
74*215bab79SShivank Garg	rule="${rule}${jidB},1,,AF_INET,203.0.113.1/24@"
75*215bab79SShivank Garg	rule="${rule}${jidB},0,,AF_INET,203.0.113.9/-1"
76*215bab79SShivank Garg	sysctl security.mac.ipacl.rules="${rule}"
77*215bab79SShivank Garg
78*215bab79SShivank Garg	# Verify if it allows jail to set only certain IPv4 address.
79*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
80*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 192.0.2.42/24 up
81*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
82*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 192.0.2.43/24 up
83*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
84*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b 198.51.100.12/24 up
85*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
86*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b 198.51.100.12/24 up
87*215bab79SShivank Garg
88*215bab79SShivank Garg	# Verify if the module allow jail to set any address in subnet.
89*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
90*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b 203.0.113.19/24 up
91*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
92*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b 203.0.113.241/24 up
93*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
94*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b 198.18.0.1/24 up
95*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
96*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b 203.0.113.9/24 up
97*215bab79SShivank Garg
98*215bab79SShivank Garg	# Check wildcard for interfaces.
99*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
100*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b 203.0.113.20/24 up
101*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
102*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b 203.0.113.242/24 up
103*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
104*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b 198.18.0.1/24 up
105*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
106*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b 203.0.113.9/24 up
107*215bab79SShivank Garg
108*215bab79SShivank Garg	rule="${jidA},1,,AF_INET,198.18.0.0/15@"
109*215bab79SShivank Garg	rule="${rule}${jidA},0,,AF_INET,198.18.23.0/24@"
110*215bab79SShivank Garg	rule="${rule}${jidA},1,,AF_INET,198.18.23.1/-1@"
111*215bab79SShivank Garg	rule="${rule}${jidA},1,,AF_INET,198.51.100.0/24@"
112*215bab79SShivank Garg	rule="${rule}${jidA},0,,AF_INET,198.51.100.100/-1"
113*215bab79SShivank Garg	sysctl security.mac.ipacl.rules="${rule}"
114*215bab79SShivank Garg
115*215bab79SShivank Garg	# Tests from Benchamarking and Documentation(TEST-NET-3).
116*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
117*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.18.0.1/24 up
118*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
119*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.18.23.2/24 up
120*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
121*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.18.23.1/22 up
122*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
123*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.18.23.3/24 up
124*215bab79SShivank Garg
125*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
126*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.51.100.001/24 up
127*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
128*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.51.100.254/24 up
129*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
130*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 198.51.100.100/24 up
131*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
132*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b 203.0.113.1/24 up
133*215bab79SShivank Garg
134*215bab79SShivank Garg	# Reset rules OID.
135*215bab79SShivank Garg	sysctl security.mac.ipacl.rules=
136*215bab79SShivank Garg}
137*215bab79SShivank Garg
138*215bab79SShivank Gargipacl_v4_cleanup()
139*215bab79SShivank Garg{
140*215bab79SShivank Garg	ipacl_test_cleanup
141*215bab79SShivank Garg}
142*215bab79SShivank Garg
143*215bab79SShivank Gargatf_test_case "ipacl_v6" "cleanup"
144*215bab79SShivank Garg
145*215bab79SShivank Gargipacl_v6_head()
146*215bab79SShivank Garg{
147*215bab79SShivank Garg	atf_set descr 'basic test for ipacl on IPv6 addresses'
148*215bab79SShivank Garg	atf_set require.user root
149*215bab79SShivank Garg}
150*215bab79SShivank Garg
151*215bab79SShivank Gargipacl_v6_body()
152*215bab79SShivank Garg{
153*215bab79SShivank Garg	ipacl_test_init
154*215bab79SShivank Garg
155*215bab79SShivank Garg	epairA=$(vnet_mkepair)
156*215bab79SShivank Garg	epairB=$(vnet_mkepair)
157*215bab79SShivank Garg	epairC=$(vnet_mkepair)
158*215bab79SShivank Garg
159*215bab79SShivank Garg	vnet_mkjail A ${epairA}b
160*215bab79SShivank Garg	vnet_mkjail B ${epairB}b ${epairC}b
161*215bab79SShivank Garg
162*215bab79SShivank Garg	jidA=$(jls -j A -s jid | grep -o -E '[0-9]+')
163*215bab79SShivank Garg	jidB=$(jls -j B -s jid | grep -o -E '[0-9]+')
164*215bab79SShivank Garg
165*215bab79SShivank Garg	# The ipacl policy module is not enforced for IPv6.
166*215bab79SShivank Garg	sysctl security.mac.ipacl.ipv6=0
167*215bab79SShivank Garg
168*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
169*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:2::abcd/48 up
170*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
171*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:2::5ea:11/48 up
172*215bab79SShivank Garg
173*215bab79SShivank Garg	# The ipacl policy module is enforced for IPv6 and prevent all
174*215bab79SShivank Garg	# jails from setting their IPv6 address.
175*215bab79SShivank Garg	sysctl security.mac.ipacl.ipv6=1
176*215bab79SShivank Garg	sysctl security.mac.ipacl.rules=
177*215bab79SShivank Garg
178*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
179*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:2::abcd/48 up
180*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
181*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:2::5ea:11/48 up
182*215bab79SShivank Garg
183*215bab79SShivank Garg	rule="${jidA},1,${epairA}b,AF_INET6,2001:db8::1111/-1@"
184*215bab79SShivank Garg	rule="${rule}${jidB},1,${epairB}b,AF_INET6,2001:2::1234:1234/-1@"
185*215bab79SShivank Garg	rule="${rule}${jidB},1,,AF_INET6,fe80::/32@"
186*215bab79SShivank Garg	rule="${rule}${jidB},0,,AF_INET6,fe80::abcd/-1"
187*215bab79SShivank Garg	sysctl security.mac.ipacl.rules="${rule}"
188*215bab79SShivank Garg
189*215bab79SShivank Garg	# Verify if it allows jail to set only certain IPv6 address.
190*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
191*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:db8::1111/64 up
192*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
193*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:db8::1112/64 up
194*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
195*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b inet6 2001:2::1234:1234/48 up
196*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
197*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 2001:2::1234:1234/48 up
198*215bab79SShivank Garg
199*215bab79SShivank Garg	# Verify if the module allow jail set any address in subnet.
200*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
201*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b inet6 FE80::1101:1221/15 up
202*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
203*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b inet6 FE80::abab/15 up
204*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
205*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b inet6 FE80::1/64 up
206*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
207*215bab79SShivank Garg	    jexec B ifconfig ${epairB}b inet6 FE80::abcd/15 up
208*215bab79SShivank Garg
209*215bab79SShivank Garg	# Check wildcard for interfaces.
210*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
211*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 FE80::1101:1221/15 up
212*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
213*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 FE80::abab/32 up
214*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
215*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 FE81::1/64 up
216*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
217*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 FE80::abcd/32 up
218*215bab79SShivank Garg
219*215bab79SShivank Garg	rule="${jidB},1,,AF_INET6,2001:2::/48@"
220*215bab79SShivank Garg	rule="${rule}${jidB},1,,AF_INET6,2001:3::/32"
221*215bab79SShivank Garg	sysctl security.mac.ipacl.rules="${rule}"
222*215bab79SShivank Garg
223*215bab79SShivank Garg	# Tests when subnet is allowed.
224*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
225*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 2001:2:0001::1/64 up
226*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
227*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 2001:2:1000::1/32 up
228*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
229*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 2001:3:0001::1/64 up
230*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
231*215bab79SShivank Garg	    jexec B ifconfig ${epairC}b inet6 2001:4::1/64 up
232*215bab79SShivank Garg
233*215bab79SShivank Garg	# More tests of ULA address space.
234*215bab79SShivank Garg	rule="${jidA},1,,AF_INET6,fc00::/7@"
235*215bab79SShivank Garg	rule="${rule}${jidA},0,,AF_INET6,fc00::1111:2200/120@"
236*215bab79SShivank Garg	rule="${rule}${jidA},1,,AF_INET6,fc00::1111:2299/-1@"
237*215bab79SShivank Garg	rule="${rule}${jidA},1,,AF_INET6,2001:db8::/32@"
238*215bab79SShivank Garg	rule="${rule}${jidA},0,,AF_INET6,2001:db8::abcd/-1"
239*215bab79SShivank Garg	sysctl security.mac.ipacl.rules="${rule}"
240*215bab79SShivank Garg
241*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
242*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::0000:1234/48 up
243*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
244*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::0000:1234/48 up
245*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
246*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 f800::2222:2200/48 up
247*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
248*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 f800::2222:22ff/48 up
249*215bab79SShivank Garg
250*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
251*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::1111:2111/64 up
252*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
253*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::1111:2211/64 up
254*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore \
255*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::1111:22aa/48 up
256*215bab79SShivank Garg	atf_check -s exit:0 -e ignore \
257*215bab79SShivank Garg	    jexec A ifconfig ${epairA}b inet6 fc00::1111:2299/48 up
258*215bab79SShivank Garg
259*215bab79SShivank Garg	# More tests from IPv6 documentation range.
260*215bab79SShivank Garg	atf_check -s exit:0 -e ignore jexec A ifconfig \
261*215bab79SShivank Garg	    ${epairA}b inet6 2001:db8:abcd:bcde:cdef:def1:ef12:f123/32 up
262*215bab79SShivank Garg	atf_check -s exit:0 -e ignore jexec A ifconfig \
263*215bab79SShivank Garg	    ${epairA}b inet6 2001:db8:1111:2222:3333:4444:5555:6666/32 up
264*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore jexec A ifconfig \
265*215bab79SShivank Garg	    ${epairA}b inet6 2001:ab9:1111:2222:3333:4444:5555:6666/32 up
266*215bab79SShivank Garg	atf_check -s not-exit:0 -e ignore jexec A ifconfig \
267*215bab79SShivank Garg	    ${epairA}b inet6 2001:db8::abcd/32 up
268*215bab79SShivank Garg
269*215bab79SShivank Garg	# Reset rules OID.
270*215bab79SShivank Garg	sysctl security.mac.ipacl.rules=
271*215bab79SShivank Garg}
272*215bab79SShivank Garg
273*215bab79SShivank Gargipacl_v6_cleanup()
274*215bab79SShivank Garg{
275*215bab79SShivank Garg	ipacl_test_cleanup
276*215bab79SShivank Garg}
277*215bab79SShivank Garg
278*215bab79SShivank Gargatf_init_test_cases()
279*215bab79SShivank Garg{
280*215bab79SShivank Garg	atf_add_test_case "ipacl_v4"
281*215bab79SShivank Garg	atf_add_test_case "ipacl_v6"
282*215bab79SShivank Garg}
283