xref: /freebsd/tests/sys/kern/sysctl_security_jail_children.sh (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 Igor Ostapenko <pm@igoro.pro>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27atf_test_case "max_cur" "cleanup"
28max_cur_head()
29{
30	atf_set descr 'Test maximum and current number of child jails'
31	atf_set require.user root
32	atf_set execenv jail
33}
34max_cur_body()
35{
36	origin_max=$(sysctl -n security.jail.children.max)
37	origin_cur=$(sysctl -n security.jail.children.cur)
38
39	# Magic numbers reasoning:
40	# 3 stands for:
41	#   - the test creates three jails: childfree, maxallowed, maxallowed.family
42	# 6 stands for:
43	#   - maxallowed.family wants to set children.max=4
44	#   - it means that its parent (maxallowed) should have at least children.max=5
45	#   - it makes the origin (parent of maxallowed) provide children.max=6 minimum
46	#
47	test $origin_cur -le $origin_max || atf_fail "Abnormal cur=$origin_cur > max=$origin_max."
48	test $((origin_max - origin_cur)) -ge 3 || atf_skip "Not enough child jails are allowed for the test."
49	test $origin_max -ge 6 || atf_skip "Not high enough children.max limit for the test."
50
51	jail -c name=childfree persist
52	atf_check_equal "$((origin_cur + 1))" "$(sysctl -n security.jail.children.cur)"
53	atf_check_equal "0" "$(jexec childfree sysctl -n security.jail.children.max)"
54	atf_check_equal "0" "$(jexec childfree sysctl -n security.jail.children.cur)"
55
56	jail -c name=maxallowed children.max=$((origin_max - 1)) persist
57	atf_check_equal "$((origin_cur + 2))" "$(sysctl -n security.jail.children.cur)"
58	atf_check_equal "$((origin_max - 1))" "$(jexec maxallowed sysctl -n security.jail.children.max)"
59	atf_check_equal "0" "$(jexec maxallowed sysctl -n security.jail.children.cur)"
60
61	jexec maxallowed jail -c name=family children.max=4 persist
62	atf_check_equal "$((origin_cur + 3))" "$(sysctl -n security.jail.children.cur)"
63	atf_check_equal "1" "$(jexec maxallowed sysctl -n security.jail.children.cur)"
64	atf_check_equal "4" "$(jexec maxallowed.family sysctl -n security.jail.children.max)"
65	atf_check_equal "0" "$(jexec maxallowed.family sysctl -n security.jail.children.cur)"
66}
67max_cur_cleanup()
68{
69	jail -r maxallowed
70	jail -r childfree
71	return 0
72}
73
74atf_init_test_cases()
75{
76	atf_add_test_case "max_cur"
77}
78