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 29*215bab79SShivank Garg. $(atf_get_srcdir)/utils.subr 30*215bab79SShivank Garg 31*215bab79SShivank Gargatf_test_case "ipacl_v4" "cleanup" 32*215bab79SShivank Garg 33*215bab79SShivank Gargipacl_v4_head() 34*215bab79SShivank Garg{ 35*215bab79SShivank Garg atf_set descr 'basic test for ipacl on IPv4 addresses' 36*215bab79SShivank Garg atf_set require.user root 37*215bab79SShivank Garg} 38*215bab79SShivank Garg 39*215bab79SShivank Gargipacl_v4_body() 40*215bab79SShivank Garg{ 41*215bab79SShivank Garg ipacl_test_init 42*215bab79SShivank Garg 43*215bab79SShivank Garg epairA=$(vnet_mkepair) 44*215bab79SShivank Garg epairB=$(vnet_mkepair) 45*215bab79SShivank Garg epairC=$(vnet_mkepair) 46*215bab79SShivank Garg 47*215bab79SShivank Garg vnet_mkjail A ${epairA}b 48*215bab79SShivank Garg vnet_mkjail B ${epairB}b ${epairC}b 49*215bab79SShivank Garg 50*215bab79SShivank Garg jidA=$(jls -j A -s jid | grep -o -E '[0-9]+') 51*215bab79SShivank Garg jidB=$(jls -j B -s jid | grep -o -E '[0-9]+') 52*215bab79SShivank Garg 53*215bab79SShivank Garg # The ipacl policy module is not enforced for IPv4. 54*215bab79SShivank Garg sysctl security.mac.ipacl.ipv4=0 55*215bab79SShivank Garg 56*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 57*215bab79SShivank Garg jexec A ifconfig ${epairA}b 192.0.2.2/24 up 58*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 59*215bab79SShivank Garg jexec A ifconfig ${epairA}b 203.0.113.254/24 up 60*215bab79SShivank Garg 61*215bab79SShivank Garg # The ipacl policy module is enforced for IPv4 and prevent all 62*215bab79SShivank Garg # jails from setting their IPv4 address. 63*215bab79SShivank Garg sysctl security.mac.ipacl.ipv4=1 64*215bab79SShivank Garg sysctl security.mac.ipacl.rules= 65*215bab79SShivank Garg 66*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 67*215bab79SShivank Garg jexec A ifconfig ${epairA}b 192.0.2.2/24 up 68*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 69*215bab79SShivank Garg jexec A ifconfig ${epairA}b 203.0.113.254/24 up 70*215bab79SShivank Garg 71*215bab79SShivank Garg rule="${jidA},1,${epairA}b,AF_INET,192.0.2.42/-1@" 72*215bab79SShivank Garg rule="${rule}${jidB},1,${epairB}b,AF_INET,198.51.100.12/-1@" 73*215bab79SShivank Garg rule="${rule}${jidB},1,,AF_INET,203.0.113.1/24@" 74*215bab79SShivank Garg rule="${rule}${jidB},0,,AF_INET,203.0.113.9/-1" 75*215bab79SShivank Garg sysctl security.mac.ipacl.rules="${rule}" 76*215bab79SShivank Garg 77*215bab79SShivank Garg # Verify if it allows jail to set only certain IPv4 address. 78*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 79*215bab79SShivank Garg jexec A ifconfig ${epairA}b 192.0.2.42/24 up 80*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 81*215bab79SShivank Garg jexec A ifconfig ${epairA}b 192.0.2.43/24 up 82*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 83*215bab79SShivank Garg jexec B ifconfig ${epairB}b 198.51.100.12/24 up 84*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 85*215bab79SShivank Garg jexec B ifconfig ${epairC}b 198.51.100.12/24 up 86*215bab79SShivank Garg 87*215bab79SShivank Garg # Verify if the module allow jail to set any address in subnet. 88*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 89*215bab79SShivank Garg jexec B ifconfig ${epairB}b 203.0.113.19/24 up 90*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 91*215bab79SShivank Garg jexec B ifconfig ${epairB}b 203.0.113.241/24 up 92*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 93*215bab79SShivank Garg jexec B ifconfig ${epairB}b 198.18.0.1/24 up 94*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 95*215bab79SShivank Garg jexec B ifconfig ${epairB}b 203.0.113.9/24 up 96*215bab79SShivank Garg 97*215bab79SShivank Garg # Check wildcard for interfaces. 98*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 99*215bab79SShivank Garg jexec B ifconfig ${epairC}b 203.0.113.20/24 up 100*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 101*215bab79SShivank Garg jexec B ifconfig ${epairC}b 203.0.113.242/24 up 102*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 103*215bab79SShivank Garg jexec B ifconfig ${epairC}b 198.18.0.1/24 up 104*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 105*215bab79SShivank Garg jexec B ifconfig ${epairC}b 203.0.113.9/24 up 106*215bab79SShivank Garg 107*215bab79SShivank Garg rule="${jidA},1,,AF_INET,198.18.0.0/15@" 108*215bab79SShivank Garg rule="${rule}${jidA},0,,AF_INET,198.18.23.0/24@" 109*215bab79SShivank Garg rule="${rule}${jidA},1,,AF_INET,198.18.23.1/-1@" 110*215bab79SShivank Garg rule="${rule}${jidA},1,,AF_INET,198.51.100.0/24@" 111*215bab79SShivank Garg rule="${rule}${jidA},0,,AF_INET,198.51.100.100/-1" 112*215bab79SShivank Garg sysctl security.mac.ipacl.rules="${rule}" 113*215bab79SShivank Garg 114*215bab79SShivank Garg # Tests from Benchamarking and Documentation(TEST-NET-3). 115*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 116*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.18.0.1/24 up 117*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 118*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.18.23.2/24 up 119*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 120*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.18.23.1/22 up 121*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 122*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.18.23.3/24 up 123*215bab79SShivank Garg 124*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 125*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.51.100.001/24 up 126*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 127*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.51.100.254/24 up 128*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 129*215bab79SShivank Garg jexec A ifconfig ${epairA}b 198.51.100.100/24 up 130*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 131*215bab79SShivank Garg jexec A ifconfig ${epairA}b 203.0.113.1/24 up 132*215bab79SShivank Garg 133*215bab79SShivank Garg # Reset rules OID. 134*215bab79SShivank Garg sysctl security.mac.ipacl.rules= 135*215bab79SShivank Garg} 136*215bab79SShivank Garg 137*215bab79SShivank Gargipacl_v4_cleanup() 138*215bab79SShivank Garg{ 139*215bab79SShivank Garg ipacl_test_cleanup 140*215bab79SShivank Garg} 141*215bab79SShivank Garg 142*215bab79SShivank Gargatf_test_case "ipacl_v6" "cleanup" 143*215bab79SShivank Garg 144*215bab79SShivank Gargipacl_v6_head() 145*215bab79SShivank Garg{ 146*215bab79SShivank Garg atf_set descr 'basic test for ipacl on IPv6 addresses' 147*215bab79SShivank Garg atf_set require.user root 148*215bab79SShivank Garg} 149*215bab79SShivank Garg 150*215bab79SShivank Gargipacl_v6_body() 151*215bab79SShivank Garg{ 152*215bab79SShivank Garg ipacl_test_init 153*215bab79SShivank Garg 154*215bab79SShivank Garg epairA=$(vnet_mkepair) 155*215bab79SShivank Garg epairB=$(vnet_mkepair) 156*215bab79SShivank Garg epairC=$(vnet_mkepair) 157*215bab79SShivank Garg 158*215bab79SShivank Garg vnet_mkjail A ${epairA}b 159*215bab79SShivank Garg vnet_mkjail B ${epairB}b ${epairC}b 160*215bab79SShivank Garg 161*215bab79SShivank Garg jidA=$(jls -j A -s jid | grep -o -E '[0-9]+') 162*215bab79SShivank Garg jidB=$(jls -j B -s jid | grep -o -E '[0-9]+') 163*215bab79SShivank Garg 164*215bab79SShivank Garg # The ipacl policy module is not enforced for IPv6. 165*215bab79SShivank Garg sysctl security.mac.ipacl.ipv6=0 166*215bab79SShivank Garg 167*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 168*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:2::abcd/48 up 169*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 170*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:2::5ea:11/48 up 171*215bab79SShivank Garg 172*215bab79SShivank Garg # The ipacl policy module is enforced for IPv6 and prevent all 173*215bab79SShivank Garg # jails from setting their IPv6 address. 174*215bab79SShivank Garg sysctl security.mac.ipacl.ipv6=1 175*215bab79SShivank Garg sysctl security.mac.ipacl.rules= 176*215bab79SShivank Garg 177*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 178*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:2::abcd/48 up 179*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 180*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:2::5ea:11/48 up 181*215bab79SShivank Garg 182*215bab79SShivank Garg rule="${jidA},1,${epairA}b,AF_INET6,2001:db8::1111/-1@" 183*215bab79SShivank Garg rule="${rule}${jidB},1,${epairB}b,AF_INET6,2001:2::1234:1234/-1@" 184*215bab79SShivank Garg rule="${rule}${jidB},1,,AF_INET6,fe80::/32@" 185*215bab79SShivank Garg rule="${rule}${jidB},0,,AF_INET6,fe80::abcd/-1" 186*215bab79SShivank Garg sysctl security.mac.ipacl.rules="${rule}" 187*215bab79SShivank Garg 188*215bab79SShivank Garg # Verify if it allows jail to set only certain IPv6 address. 189*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 190*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:db8::1111/64 up 191*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 192*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:db8::1112/64 up 193*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 194*215bab79SShivank Garg jexec B ifconfig ${epairB}b inet6 2001:2::1234:1234/48 up 195*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 196*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 2001:2::1234:1234/48 up 197*215bab79SShivank Garg 198*215bab79SShivank Garg # Verify if the module allow jail set any address in subnet. 199*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 200*215bab79SShivank Garg jexec B ifconfig ${epairB}b inet6 FE80::1101:1221/15 up 201*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 202*215bab79SShivank Garg jexec B ifconfig ${epairB}b inet6 FE80::abab/15 up 203*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 204*215bab79SShivank Garg jexec B ifconfig ${epairB}b inet6 FE80::1/64 up 205*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 206*215bab79SShivank Garg jexec B ifconfig ${epairB}b inet6 FE80::abcd/15 up 207*215bab79SShivank Garg 208*215bab79SShivank Garg # Check wildcard for interfaces. 209*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 210*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 FE80::1101:1221/15 up 211*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 212*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 FE80::abab/32 up 213*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 214*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 FE81::1/64 up 215*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 216*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 FE80::abcd/32 up 217*215bab79SShivank Garg 218*215bab79SShivank Garg rule="${jidB},1,,AF_INET6,2001:2::/48@" 219*215bab79SShivank Garg rule="${rule}${jidB},1,,AF_INET6,2001:3::/32" 220*215bab79SShivank Garg sysctl security.mac.ipacl.rules="${rule}" 221*215bab79SShivank Garg 222*215bab79SShivank Garg # Tests when subnet is allowed. 223*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 224*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 2001:2:0001::1/64 up 225*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 226*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 2001:2:1000::1/32 up 227*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 228*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 2001:3:0001::1/64 up 229*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 230*215bab79SShivank Garg jexec B ifconfig ${epairC}b inet6 2001:4::1/64 up 231*215bab79SShivank Garg 232*215bab79SShivank Garg # More tests of ULA address space. 233*215bab79SShivank Garg rule="${jidA},1,,AF_INET6,fc00::/7@" 234*215bab79SShivank Garg rule="${rule}${jidA},0,,AF_INET6,fc00::1111:2200/120@" 235*215bab79SShivank Garg rule="${rule}${jidA},1,,AF_INET6,fc00::1111:2299/-1@" 236*215bab79SShivank Garg rule="${rule}${jidA},1,,AF_INET6,2001:db8::/32@" 237*215bab79SShivank Garg rule="${rule}${jidA},0,,AF_INET6,2001:db8::abcd/-1" 238*215bab79SShivank Garg sysctl security.mac.ipacl.rules="${rule}" 239*215bab79SShivank Garg 240*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 241*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::0000:1234/48 up 242*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 243*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::0000:1234/48 up 244*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 245*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 f800::2222:2200/48 up 246*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 247*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 f800::2222:22ff/48 up 248*215bab79SShivank Garg 249*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 250*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::1111:2111/64 up 251*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 252*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::1111:2211/64 up 253*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore \ 254*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::1111:22aa/48 up 255*215bab79SShivank Garg atf_check -s exit:0 -e ignore \ 256*215bab79SShivank Garg jexec A ifconfig ${epairA}b inet6 fc00::1111:2299/48 up 257*215bab79SShivank Garg 258*215bab79SShivank Garg # More tests from IPv6 documentation range. 259*215bab79SShivank Garg atf_check -s exit:0 -e ignore jexec A ifconfig \ 260*215bab79SShivank Garg ${epairA}b inet6 2001:db8:abcd:bcde:cdef:def1:ef12:f123/32 up 261*215bab79SShivank Garg atf_check -s exit:0 -e ignore jexec A ifconfig \ 262*215bab79SShivank Garg ${epairA}b inet6 2001:db8:1111:2222:3333:4444:5555:6666/32 up 263*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore jexec A ifconfig \ 264*215bab79SShivank Garg ${epairA}b inet6 2001:ab9:1111:2222:3333:4444:5555:6666/32 up 265*215bab79SShivank Garg atf_check -s not-exit:0 -e ignore jexec A ifconfig \ 266*215bab79SShivank Garg ${epairA}b inet6 2001:db8::abcd/32 up 267*215bab79SShivank Garg 268*215bab79SShivank Garg # Reset rules OID. 269*215bab79SShivank Garg sysctl security.mac.ipacl.rules= 270*215bab79SShivank Garg} 271*215bab79SShivank Garg 272*215bab79SShivank Gargipacl_v6_cleanup() 273*215bab79SShivank Garg{ 274*215bab79SShivank Garg ipacl_test_cleanup 275*215bab79SShivank Garg} 276*215bab79SShivank Garg 277*215bab79SShivank Gargatf_init_test_cases() 278*215bab79SShivank Garg{ 279*215bab79SShivank Garg atf_add_test_case "ipacl_v4" 280*215bab79SShivank Garg atf_add_test_case "ipacl_v6" 281*215bab79SShivank Garg} 282