aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/mac/do/consistency.sh
blob: 6a64917edb6d2a20044c125f3c5728f03b303367 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright (c) 2026 The FreeBSD Foundation
#
# SPDX-License-Identifier: BSD-2-Clause
#
# This software was developed by Olivier Certner <olce@FreeBSD.org> at
# Kumacom SARL under sponsorship from the FreeBSD Foundation.

SJ_JID_FILE=sj.jid

atf_test_case concurrent_rules_exec_paths_changes
concurrent_rules_exec_paths_changes_head()
{
    atf_set descr "Consistency of rules and exec paths changes on same jail"
}
concurrent_rules_exec_paths_changes_body()
{
    local rules exec_paths rules_es exec_paths_es

    for I in $(jot - 1 1000); do
        sysctl_set_and_check_rules "uid=$I>uid=1001"
    done &
    rules=$!

    for I in $(jot - 1 1000); do
        sysctl_set_and_check_exec_paths /nowhere/nonexistent$I
    done &
    exec_paths=$!

    wait $rules
    rules_es=$?

    wait $exec_paths
    exec_paths_es=$?

    # atf_check called in the asynchronous AND-OR lists above causes exit of the
    # subshells and also a write to the ATF result file.  These writes are
    # concurrent and may cause the result file to be malformed.  Consequently,
    # it is important that, once execution becomes sequential again, atf_fail() is
    # called again (and not just exit()).
    if [ $rules_es -ne 0 ] || [ $exec_paths_es -ne 0 ]; then
        atf_fail "Rules exit status: $rules_es, \
exec paths exit status: $exec_paths_es"
    fi
}

atf_test_case inheritance cleanup
inheritance_head()
{
    atf_set descr "Simple inheritance test (values propagated to child jail)"
}
inheritance_body()
{
    local sj rules exec_paths

    # For the sake of not running the test under Kyua
    mac_do_ensure_disabled

    sj=$(launch_subjail)
    echo $sj > "${SJ_JID_FILE}"

    jail -m jid=$sj ${ROOT_JAIL_PARAM}=inherit
    JEXEC="jexec $sj"
    mac_do_check_disabled
    JEXEC=

    rules="uid=1001>uid=0"
    sysctl_set_and_check_rules $rules
    JEXEC="jexec $sj"
    sysctl_check_rules $rules
    JEXEC=

    rules="gid=1001>uid=0"
    sysctl_set_and_check_rules $rules
    JEXEC="jexec $sj"
    sysctl_check_rules $rules
    JEXEC=

    # Not really necessary, just to keep mac_do(4) disabled
    sysctl_set_and_check_rules ""

    exec_paths="/nowhere/nonexistent"
    sysctl_set_and_check_exec_paths $exec_paths
    JEXEC="jexec $sj"
    sysctl_check_exec_paths $exec_paths
    JEXEC=

    exec_paths="$MDO"
    sysctl_set_and_check_exec_paths $exec_paths
    JEXEC="jexec $sj"
    sysctl_check_exec_paths $exec_paths
    JEXEC=
}
inheritance_cleanup()
{
    # We clean up our subjail manually just for the sake of launching this test
    # with atf-sh.  Kyua is informed that these tests should run in a jail, and
    # kills it automatically after the test, which kills all subjails.  It is
    # annoying that atf-sh does not offer a more practical way to pass
    # information from the body to the cleanup part than a file.
    jail -r $(cat "${SJ_JID_FILE}")
    rm -f "${SJ_JID_FILE}"
}

atf_test_case inheritance_relax_parent_jail cleanup
inheritance_relax_parent_jail_head()
{
    atf_set descr \
            "Test sequential consistency in a \"relax parent rules\" scenario"
}
inheritance_relax_parent_jail_body()
{
    local sj rules exec_paths subproc

    sj=$(launch_subjail)
    echo $sj > "${SJ_JID_FILE}"

    jail -m jid=$sj ${ROOT_JAIL_PARAM}=inherit
    rules="uid=1001>uid=0"
    sysctl_set_and_check_rules $rules
    # Additional inheritance sanity check
    JEXEC="jexec $sj"
    sysctl_check_rules $rules
    JEXEC=
    exec_paths="$MDO"
    sysctl_set_and_check_exec_paths $exec_paths
    # Additional inheritance sanity check
    JEXEC="jexec $sj"
    sysctl_check_exec_paths $exec_paths
    JEXEC=

    # Launch a process that tries to become 'root' from user 1002, and verify
    # that this always fails.
    { for I in $(jot - 1 1000); do
          jexec $sj "$MDO" -u 1002 -g 1002 -G 1002 "$MDO" -i true 2>/dev/null &&
              exit 1
      done; true; } &
    subproc=$!

    # Decouple the subjail from the parent jail, copying its parameters
    jail -m jid=$sj ${ROOT_JAIL_PARAM}=new
    # Allow user 1002 to become 'root' on the parent jail
    sysctl_set_and_check_rules "$rules;uid=1002>uid=0"
    JEXEC="jexec $sj"
    # Additional sanity check (that rules of the subjail are now independent)
    [ "$(sysctl_rules)" == $rules ] || atf_fail "Rules not copied"
    [ "$(sysctl_exec_paths)" == $exec_paths ] ||
        atf_fail "Exec paths not copied"
    JEXEC=

    wait $subproc || atf_fail "A transition wrongly succeeded in the subjail!"
}
inheritance_relax_parent_jail_cleanup()
{
    # See inheritance_cleanup() for explanations
    jail -r $(cat "${SJ_JID_FILE}")
    rm -f "${SJ_JID_FILE}"
}

atf_test_case same_knob_and_jail_parameter cleanup
same_knob_and_jail_parameter_head()
{
    atf_set descr \
            "Corresponding sysctl knobs and jail parameters have same value"
}
same_knob_and_jail_parameter_body()
{
    local sj rules exec_paths subproc

    sj=$(launch_subjail)
    echo $sj > "${SJ_JID_FILE}"

    # Set sysctl knobs, observe parameters
    rules="uid=19999>uid=21700"
    exec_paths="/improbable/path/he"
    JEXEC="jexec $sj"
    sysctl_set_and_check_rules $rules
    sysctl_set_and_check_exec_paths $exec_paths
    JEXEC=
    atf_check -o inline:"$rules\n" jls -j $sj ${RULES_JAIL_PARAM}
    atf_check -o inline:"${exec_paths}\n" jls -j $sj ${EXEC_PATHS_JAIL_PARAM}

    # Set parameters, observe knobs
    rules="uid=128000>uid=-1"
    exec_paths="/hello/i_ve/changed"
    jail -m jid=$sj ${RULES_JAIL_PARAM}=$rules \
         ${EXEC_PATHS_JAIL_PARAM}=${exec_paths}
    JEXEC="jexec $sj"
    sysctl_check_rules $rules
    sysctl_check_exec_paths $exec_paths
    JEXEC=
}
same_knob_and_jail_parameter_cleanup()
{
    # See inheritance_cleanup() for explanations
    jail -r $(cat "${SJ_JID_FILE}")
    rm -f "${SJ_JID_FILE}"
}


atf_init_test_cases()
{
    . $(atf_get_srcdir)/common.sh
    atf_require_prog jot
    # Needs an absolute path for mdo(1), to set it in exec_paths
    atf_require_prog "$MDO"

    atf_add_test_case concurrent_rules_exec_paths_changes
    atf_add_test_case inheritance
    atf_add_test_case inheritance_relax_parent_jail
    atf_add_test_case same_knob_and_jail_parameter
}