2017-10-20 05:47:15 +08:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
|
|
load helpers
|
|
|
|
|
|
|
|
|
|
@test "selinux test" {
|
2018-04-28 02:09:32 +08:00
|
|
|
if ! which selinuxenabled > /dev/null 2> /dev/null ; then
|
2018-07-20 02:41:02 +08:00
|
|
|
skip 'selinuxenabled command not found in $PATH'
|
2018-04-28 02:09:32 +08:00
|
|
|
elif ! selinuxenabled ; then
|
|
|
|
|
skip "selinux is disabled"
|
2017-10-20 05:47:15 +08:00
|
|
|
fi
|
2018-04-28 02:09:32 +08:00
|
|
|
|
2017-10-20 05:47:15 +08:00
|
|
|
image=alpine
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch $image
|
2018-04-28 02:09:32 +08:00
|
|
|
|
|
|
|
|
# Create a container and read its context as a baseline.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from --quiet --quiet $WITH_POLICY_JSON $image
|
2019-12-12 03:11:08 +08:00
|
|
|
cid=$output
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $cid sh -c 'tr \\0 \\n < /proc/self/attr/current'
|
2023-01-05 21:42:11 +08:00
|
|
|
assert "$output" != "" "/proc/self/attr/current cannot be empty"
|
2018-04-28 02:09:32 +08:00
|
|
|
firstlabel="$output"
|
2017-10-20 05:47:15 +08:00
|
|
|
|
2018-04-28 02:09:32 +08:00
|
|
|
# Ensure that we label the same container consistently across multiple "run" instructions.
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $cid sh -c 'tr \\0 \\n < /proc/self/attr/current'
|
2019-04-05 23:59:54 +08:00
|
|
|
expect_output "$firstlabel" "label of second container == first"
|
2017-10-20 05:47:15 +08:00
|
|
|
|
2018-04-28 02:09:32 +08:00
|
|
|
# Ensure that different containers get different labels.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from --quiet --quiet $WITH_POLICY_JSON $image
|
2019-12-12 03:11:08 +08:00
|
|
|
cid1=$output
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $cid1 sh -c 'tr \\0 \\n < /proc/self/attr/current'
|
2021-05-14 05:08:35 +08:00
|
|
|
assert "$output" != "$firstlabel" \
|
|
|
|
|
"Second container has the same label as first (both '$output')"
|
2017-10-20 05:47:15 +08:00
|
|
|
}
|
2018-08-02 02:35:10 +08:00
|
|
|
|
|
|
|
|
@test "selinux spc" {
|
|
|
|
|
if ! which selinuxenabled > /dev/null 2> /dev/null ; then
|
|
|
|
|
skip "No selinuxenabled"
|
|
|
|
|
elif ! selinuxenabled ; then
|
|
|
|
|
skip "selinux is disabled"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
image=alpine
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch $image
|
2018-08-02 02:35:10 +08:00
|
|
|
|
|
|
|
|
# Create a container and read its context as a baseline.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from --quiet --security-opt label=disable --quiet $WITH_POLICY_JSON $image
|
2019-12-12 03:11:08 +08:00
|
|
|
cid=$output
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $cid sh -c 'tr \\0 \\n < /proc/self/attr/current'
|
2020-01-31 03:19:58 +08:00
|
|
|
context=$output
|
2023-05-31 02:30:49 +08:00
|
|
|
run id -Z
|
|
|
|
|
crole=$(secon -r $output)
|
2020-01-31 03:19:58 +08:00
|
|
|
# Role and Type should always be constant. (We don't check user)
|
|
|
|
|
role=$(awk -F: '{print $2}' <<<$context)
|
2023-05-31 02:30:49 +08:00
|
|
|
expect_output --from="$role" "${crole}" "SELinux role"
|
2020-01-31 03:19:58 +08:00
|
|
|
|
|
|
|
|
type=$(awk -F: '{print $3}' <<<$context)
|
|
|
|
|
expect_output --from="$type" "spc_t" "SELinux type"
|
|
|
|
|
|
|
|
|
|
# Range should match that of the invoking process
|
|
|
|
|
my_range=$(id -Z |awk -F: '{print $4 ":" $5}')
|
|
|
|
|
container_range=$(awk -F: '{print $4 ":" $5}' <<<$context)
|
|
|
|
|
expect_output --from="$container_range" "$my_range" "SELinux range: container matches process"
|
2018-08-02 02:35:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@test "selinux specific level" {
|
|
|
|
|
if ! which selinuxenabled > /dev/null 2> /dev/null ; then
|
|
|
|
|
skip "No selinuxenabled"
|
|
|
|
|
elif ! selinuxenabled ; then
|
|
|
|
|
skip "selinux is disabled"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
image=alpine
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch $image
|
2018-08-02 02:35:10 +08:00
|
|
|
|
|
|
|
|
firstlabel="system_u:system_r:container_t:s0:c1,c2"
|
|
|
|
|
# Create a container and read its context as a baseline.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from --quiet --security-opt label="level:s0:c1,c2" --quiet $WITH_POLICY_JSON $image
|
2019-12-12 03:11:08 +08:00
|
|
|
cid=$output
|
2019-04-05 09:32:54 +08:00
|
|
|
|
|
|
|
|
# Inspect image
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah inspect --format '{{.ProcessLabel}}' $cid
|
2019-04-05 23:59:54 +08:00
|
|
|
expect_output "$firstlabel"
|
2019-04-05 09:32:54 +08:00
|
|
|
|
|
|
|
|
# Check actual running context
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $cid sh -c 'tr \\0 \\n < /proc/self/attr/current'
|
2019-04-05 23:59:54 +08:00
|
|
|
expect_output "$firstlabel" "running container context"
|
2018-08-02 02:35:10 +08:00
|
|
|
}
|