IMA: Add test for selinux measurement
Subsystems (2)
PATCH 1/1 IMA: Add test for selinux measurement
New functionality has been added in IMA to measure data that is critical to the integrity of the system. SELinux uses this feature to measure the hash of the SELinux policy loaded in kernel memory, and the current state of various SELinux configuration. This new functionality needs test automation in LTP.
Add test cases which verify that the IMA subsystem correctly measures the data provided by SELinux.
---
This patch is based on commit c7c4cd5e7f3b ("tst_security.sh: Add SELinux helpers")
in https://github.com/pevik/ltp/commits/ima/selinux.v2.draft
in branch ima/selinux.v2.draft.
runtest/ima | 1 + .../kernel/security/integrity/ima/README.md | 19 ++ .../security/integrity/ima/datafiles/Makefile | 2 +- .../ima/datafiles/ima_selinux/Makefile | 11 ++ .../ima/datafiles/ima_selinux/selinux.policy | 1 + .../integrity/ima/tests/ima_selinux.sh | 180 ++++++++++++++++++ 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 testcases/kernel/security/integrity/ima/datafiles/ima_selinux/Makefile create mode 100644 testcases/kernel/security/integrity/ima/datafiles/ima_selinux/selinux.policy create mode 100755 testcases/kernel/security/integrity/ima/tests/ima_selinux.sh
runtest/ima +1 -0
@@ -5,4 +5,5 @@ ima_tpm ima_tpm.sh ima_violations ima_violations.sh ima_keys ima_keys.sh ima_kexec ima_kexec.sh +ima_selinux ima_selinux.sh evm_overlay evm_overlay.sh
testcases/kernel/security/integrity/ima/README.md +19 -0
@@ -37,6 +37,25 @@ see example in `kexec.policy`. The test attempts to kexec the existing running kernel image. To kexec a different kernel image export `IMA_KEXEC_IMAGE=<pathname>`. +### IMA SELinux test + +To enable IMA to measure SELinux state and policy, `ima_selinux.sh` +requires a readable IMA policy, as well as a loaded measure policy with +`measure func=CRITICAL_DATA label=selinux`, +see example in `selinux.policy`. + +As well as what's required for the IMA tests, SELinux tests require reading +the IMA policy allowed in the kernel configuration: +``` +CONFIG_IMA_READ_POLICY=y +``` + +The following kernel configuration is also required. It enables compiling +the Linux Security Module (LSM) namely SELinux. +``` +CONFIG_SECURITY_SELINUX=y +``` + ## EVM tests `evm_overlay.sh` requires a builtin IMA appraise tcb policy (e.g. `ima_policy=appraise_tcb`
testcases/kernel/security/integrity/ima/datafiles/Makefile +1 -1
@@ -26,6 +26,6 @@ top_srcdir ?= ../../../../../.. include $(top_srcdir)/include/mk/env_pre.mk -SUBDIRS := ima_kexec ima_keys ima_policy +SUBDIRS := ima_kexec ima_keys ima_policy ima_selinux include $(top_srcdir)/include/mk/generic_trunk_target.mk
testcases/kernel/security/integrity/ima/datafiles/ima_selinux/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) Linux Test Project, 2021 + +top_srcdir ?= ../../../../../../.. + +include $(top_srcdir)/include/mk/env_pre.mk + +INSTALL_DIR := testcases/data/ima_selinux +INSTALL_TARGETS := *.policy + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/security/integrity/ima/datafiles/ima_selinux/selinux.policy new file +1 -0
@@ -0,0 +1 @@ +measure func=CRITICAL_DATA label=selinux
testcases/kernel/security/integrity/ima/tests/ima_selinux.sh new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
2.30.0
Hi Lakshmi,
Quoted 1 line
+++
b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh
...
Quoted 6 lines
(quoted diff) +6
-0+validate_policy_capabilities() +{ + local measured_cap measured_value expected_value + local result=1 + local inx=7 +
Review
Quoted 10 lines
(quoted diff) +10
-0+ # Policy capabilities flags start from "network_peer_controls" + # in the measured SELinux state at offset 7 for 'awk' + while [ $inx -lt 20 ]; do + measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + inx=$(( $inx + 1 )) + + measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") + if [ "$measured_value" != "$expected_value" ];then + tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest"
We rarely use TWARN in the tests, only when the error is not related to the test result. Otherwise we use TFAIL.
The rest LGTM.
I did few formatting and style changes:
https://github.com/pevik/ltp/commits/ima/selinux.v2.fixes
(branch ima/selinux.v2.fixes), see diff below.
As we discuss, I'm going tom merge test when patchset is merged in maintainers tree, please ping me. And ideally we should mention kernel commit hash as a comment in the test.
Thanks a lot!
Kind regards,
Petr
kernel/security/integrity/ima/tests/ima_selinux.sh +11 -16
@@ -13,16 +13,14 @@ TST_SETUP="setup" . ima_setup.sh FUNC_CRITICAL_DATA='func=CRITICAL_DATA' -REQUIRED_POLICY="^measure.*($FUNC_CRITICAL_DATA)" +REQUIRED_POLICY="^measure.*$FUNC_CRITICAL_DATA" setup() { - SELINUX_DIR=$(tst_get_selinux_dir) - if [ -z "$SELINUX_DIR" ]; then - tst_brk TCONF "SELinux is not enabled" - return - fi + tst_require_selinux_enabled require_ima_policy_content "$REQUIRED_POLICY" '-E' > $TST_TMPDIR/policy.txt + + SELINUX_DIR=$(tst_get_selinux_dir) } # Format of the measured SELinux state data.
@@ -41,16 +39,16 @@ validate_policy_capabilities() # in the measured SELinux state at offset 7 for 'awk' while [ $inx -lt 20 ]; do measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') - inx=$(( $inx + 1 )) + inx=$(($inx + 1)) measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") - if [ "$measured_value" != "$expected_value" ];then + if [ "$measured_value" != "$expected_value" ]; then tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest" result=0 fi - inx=$(( $inx + 1 )) + inx=$(($inx + 1)) done return $result
@@ -109,7 +107,6 @@ test2() local initialized_value local enforced_value expected_enforced_value local checkreqprot_value expected_checkreqprot_value - local result tst_res TINFO "verifying SELinux state measurement"
@@ -149,27 +146,25 @@ test2() measured_data=$(cat $state_file) enforced_value=$(echo $measured_data | awk -F'[=;]' '{print $4}') expected_enforced_value=$(cat $SELINUX_DIR/enforce) - if [ "$expected_enforced_value" != "$enforced_value" ];then + if [ "$expected_enforced_value" != "$enforced_value" ]; then tst_res TFAIL "enforce: expected: $expected_enforced_value, got: $enforced_value" return fi checkreqprot_value=$(echo $measured_data | awk -F'[=;]' '{print $6}') expected_checkreqprot_value=$(cat $SELINUX_DIR/checkreqprot) - if [ "$expected_checkreqprot_value" != "$checkreqprot_value" ];then + if [ "$expected_checkreqprot_value" != "$checkreqprot_value" ]; then tst_res TFAIL "checkreqprot: expected: $expected_checkreqprot_value, got: $checkreqprot_value" return fi initialized_value=$(echo $measured_data | awk -F'[=;]' '{print $2}') - if [ "$initialized_value" != "1" ];then + if [ "$initialized_value" != "1" ]; then tst_res TFAIL "initialized: expected 1, got: $initialized_value" return fi - validate_policy_capabilities $measured_data - result=$? - if [ $result = 0 ]; then + if validate_policy_capabilities $measured_data; then tst_res TFAIL "policy capabilities did not match" return fi
Quoted 13 lines · Petr Vorel
Quoted 2 lines
+++
b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh...
Quoted 10 lines
(quoted diff) +10
-0+validate_policy_capabilities() +{ + local measured_cap measured_value expected_value + local result=1 + local inx=7 + + # Policy capabilities flags start from "network_peer_controls" + # in the measured SELinux state at offset 7 for 'awk' + while [ $inx -lt 20 ]; do + measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}')
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ inx=$(( $inx + 1 )) + + measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") + if [ "$measured_value" != "$expected_value" ];then + tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest"We rarely use TWARN in the tests, only when the error is not related to the test result. Otherwise we use TFAIL.
ok - I will change it to TFAIL.
Quoted 7 lines
The rest LGTM.
Reviewed-by Petr Vorel <[email protected]>I did few formatting and style changes:
https://github.com/pevik/ltp/commits/ima/selinux.v2.fixes
(branch ima/selinux.v2.fixes), see diff below.
The changes look. Thanks Petr.
I do have one comment - please see below.
Quoted 4 lines
As we discuss, I'm going tom merge test when patchset is merged in maintainers tree, please ping me. And ideally we should mention kernel commit hash as a comment in the test.
Will do. Thank you.
Quoted 15 lines
Thanks a lot!
Kind regards,
Petrkernel/security/integrity/ima/tests/ima_selinux.sh +0
-1@@ -13,16 +13,14 @@ TST_SETUP="setup" . ima_setup.sh FUNC_CRITICAL_DATA='func=CRITICAL_DATA' -REQUIRED_POLICY="^measure.*($FUNC_CRITICAL_DATA)"
kernel/security/integrity/ima/tests/ima_selinux.sh TST_SETUP="setup" · line 20
Quoted 10 lines
kernel/security/integrity/ima/tests/ima_selinux.sh +2
-5+REQUIRED_POLICY="^measure.*$FUNC_CRITICAL_DATA" setup() { - SELINUX_DIR=$(tst_get_selinux_dir) - if [ -z "$SELINUX_DIR" ]; then - tst_brk TCONF "SELinux is not enabled" - return - fi + tst_require_selinux_enabled
Please correct me if I have misunderstood this one:
tst_require_selinux_enabled is checking if SELinux is enabled in "enforce" mode. Would this check fail if SELinux is enabled in "permissive" mode?
For running the test, we just need SELinux to be enabled. I verify that by checking for the presence of SELINUX_DIR.
thanks,
-lakshmi
Quoted 67 lines
kernel/security/integrity/ima/tests/ima_selinux.sh +9
-10require_ima_policy_content "$REQUIRED_POLICY" '-E' > $TST_TMPDIR/policy.txt + + SELINUX_DIR=$(tst_get_selinux_dir) } # Format of the measured SELinux state data.@@ -41,16 +39,16 @@ validate_policy_capabilities() # in the measured SELinux state at offset 7 for 'awk' while [ $inx -lt 20 ]; do measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') - inx=$(( $inx + 1 )) + inx=$(($inx + 1)) measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") - if [ "$measured_value" != "$expected_value" ];then + if [ "$measured_value" != "$expected_value" ]; then tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest" result=0 fi - inx=$(( $inx + 1 )) + inx=$(($inx + 1)) done return $result@@ -109,7 +107,6 @@ test2() local initialized_value local enforced_value expected_enforced_value local checkreqprot_value expected_checkreqprot_value - local result tst_res TINFO "verifying SELinux state measurement"@@ -149,27 +146,25 @@ test2() measured_data=$(cat $state_file) enforced_value=$(echo $measured_data | awk -F'[=;]' '{print $4}') expected_enforced_value=$(cat $SELINUX_DIR/enforce) - if [ "$expected_enforced_value" != "$enforced_value" ];then + if [ "$expected_enforced_value" != "$enforced_value" ]; then tst_res TFAIL "enforce: expected: $expected_enforced_value, got: $enforced_value" return fi checkreqprot_value=$(echo $measured_data | awk -F'[=;]' '{print $6}') expected_checkreqprot_value=$(cat $SELINUX_DIR/checkreqprot) - if [ "$expected_checkreqprot_value" != "$checkreqprot_value" ];then + if [ "$expected_checkreqprot_value" != "$checkreqprot_value" ]; then tst_res TFAIL "checkreqprot: expected: $expected_checkreqprot_value, got: $checkreqprot_value" return fi initialized_value=$(echo $measured_data | awk -F'[=;]' '{print $2}') - if [ "$initialized_value" != "1" ];then + if [ "$initialized_value" != "1" ]; then tst_res TFAIL "initialized: expected 1, got: $initialized_value" return fi - validate_policy_capabilities $measured_data - result=$? - if [ $result = 0 ]; then + if validate_policy_capabilities $measured_data; then tst_res TFAIL "policy capabilities did not match" return fi
Quoted 23 lines · Petr Vorel
Quoted 14 lines
Quoted 3 lines
+++
b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh...
Quoted 10 lines
(quoted diff) +10
-0+validate_policy_capabilities() +{ + local measured_cap measured_value expected_value + local result=1 + local inx=7 + + # Policy capabilities flags start from "network_peer_controls" + # in the measured SELinux state at offset 7 for 'awk' + while [ $inx -lt 20 ]; do + measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}')Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ inx=$(( $inx + 1 )) + + measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") + if [ "$measured_value" != "$expected_value" ];then + tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest"We rarely use TWARN in the tests, only when the error is not related to the test result. Otherwise we use TFAIL.
Quoted 1 line
ok - I will change it to TFAIL.
Thanks!
But I've noticed that this error is handled twice, first in validate_policy_capabilities() as TWARN (or TFAIL) and then in test2(). Let's use TPASS/TFAIL in validate_policy_capabilities():
validate_policy_capabilities()
{
local measured_cap measured_value expected_value
local inx=7
# Policy capabilities flags start from "network_peer_controls"
# in the measured SELinux state at offset 7 for 'awk'
while [ $inx -lt 20 ]; do
measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}')
inx=$(($inx + 1))
measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}')
expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap")
if [ "$measured_value" != "$expected_value" ]; then
tst_res TFAIL "$measured_cap: expected: $expected_value, got: $digest"
return
fi
inx=$(($inx + 1))
done
tst_res TPASS "SELinux state measured correctly"
}test2()
{
...
validate_policy_capabilities $measured_data
}...
Quoted 4 lines
Quoted 3 lines
As we discuss, I'm going tom merge test when patchset is merged in maintainers tree, please ping me. And ideally we should mention kernel commit hash as a comment in the test.
Will do. Thank you.
Thanks!
...
Quoted 16 lines
Quoted 4 lines
+++
testcases/kernel/security/integrity/ima/tests/ima_selinux.shkernel/security/integrity/ima/tests/ima_selinux.sh +0
-0@@ -13,16 +13,14 @@ TST_SETUP="setup" . ima_setup.sh FUNC_CRITICAL_DATA='func=CRITICAL_DATA'kernel/security/integrity/ima/tests/ima_selinux.sh TST_SETUP="setup" · line 18
Quoted 10 lines
kernel/security/integrity/ima/tests/ima_selinux.sh +2
-6-REQUIRED_POLICY="^measure.*($FUNC_CRITICAL_DATA)" +REQUIRED_POLICY="^measure.*$FUNC_CRITICAL_DATA" setup() { - SELINUX_DIR=$(tst_get_selinux_dir) - if [ -z "$SELINUX_DIR" ]; then - tst_brk TCONF "SELinux is not enabled" - return - fi + tst_require_selinux_enabledPlease correct me if I have misunderstood this one:
Quoted 5 lines
tst_require_selinux_enabledis checking if SELinux is enabled in "enforce" mode. Would this check fail if SELinux is enabled in "permissive" mode?For running the test, we just need SELinux to be enabled. I verify that by checking for the presence of
SELINUX_DIR.
Good catch. Your original version is correct (put it back into ima/selinux.v2.fixes). I didn't put a helper for it, because you need $SELINUX_DIR anyway. Thus removed tst_require_selinux_enabled() as not needed.
I renamed tst_selinux_enabled() to tst_selinux_enforced() to make the purpose clearer (commit 82b598ea1 IMA: Add test for selinux measurement).
I've updated branch ima/selinux.v2.fixes with all mentioned changes
https://github.com/pevik/ltp/commits/ima/selinux.v2.fixes
Kind regards,
Petr
Quoted 2 lines
thanks,
-lakshmi
Quoted 23 lines · Petr Vorel
Quoted 23 lines · Petr Vorel
Quoted 22 lines
Quoted 3 lines
+++
b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh...
Quoted 6 lines
(quoted diff) +6
-0+validate_policy_capabilities() +{ + local measured_cap measured_value expected_value + local result=1 + local inx=7 +Review
Quoted 10 lines
(quoted diff) +10
-0+ # Policy capabilities flags start from "network_peer_controls" + # in the measured SELinux state at offset 7 for 'awk' + while [ $inx -lt 20 ]; do + measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + inx=$(( $inx + 1 )) + + measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') + expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") + if [ "$measured_value" != "$expected_value" ];then + tst_res TWARN "$measured_cap: expected: $expected_value, got: $digest"We rarely use TWARN in the tests, only when the error is not related to the test result. Otherwise we use TFAIL.
Quoted 5 lines
Quoted 1 line
ok - I will change it to TFAIL.
Thanks!
But I've noticed that this error is handled twice, first invalidate_policy_capabilities()as TWARN (or TFAIL) and then intest2(). Let's use TPASS/TFAIL invalidate_policy_capabilities():
Sure - will make that change.
Quoted 58 lines
validate_policy_capabilities(){ local measured_cap measured_value expected_value local inx=7 # Policy capabilities flags start from "network_peer_controls" # in the measured SELinux state at offset 7 for 'awk' while [ $inx -lt 20 ]; do measured_cap=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') inx=$(($inx + 1)) measured_value=$(echo $1 | awk -F'[=;]' -v inx="$inx" '{print $inx}') expected_value=$(cat "$SELINUX_DIR/policy_capabilities/$measured_cap") if [ "$measured_value" != "$expected_value" ]; then tst_res TFAIL "$measured_cap: expected: $expected_value, got: $digest" return fi inx=$(($inx + 1)) done tst_res TPASS "SELinux state measured correctly" }
test2(){ ... validate_policy_capabilities $measured_data }...
Quoted 4 lines
Quoted 3 lines
As we discuss, I'm going tom merge test when patchset is merged in maintainers tree, please ping me. And ideally we should mention kernel commit hash as a comment in the test.
Will do. Thank you.
Thanks!
...
Quoted 18 lines
Quoted 4 lines
+++
testcases/kernel/security/integrity/ima/tests/ima_selinux.shkernel/security/integrity/ima/tests/ima_selinux.sh +0
-0@@ -13,16 +13,14 @@ TST_SETUP="setup" . ima_setup.sh FUNC_CRITICAL_DATA='func=CRITICAL_DATA'kernel/security/integrity/ima/tests/ima_selinux.sh TST_SETUP="setup" · line 18
Quoted 10 lines
kernel/security/integrity/ima/tests/ima_selinux.sh +2
-6-REQUIRED_POLICY="^measure.*($FUNC_CRITICAL_DATA)" +REQUIRED_POLICY="^measure.*$FUNC_CRITICAL_DATA" setup() { - SELINUX_DIR=$(tst_get_selinux_dir) - if [ -z "$SELINUX_DIR" ]; then - tst_brk TCONF "SELinux is not enabled" - return - fi + tst_require_selinux_enabledPlease correct me if I have misunderstood this one:
tst_require_selinux_enabledis checking if SELinux is enabled in "enforce" mode. Would this check fail if SELinux is enabled in "permissive" mode?
Quoted 6 lines
Quoted 2 lines
For running the test, we just need SELinux to be enabled. I verify that by checking for the presence of
SELINUX_DIR.Good catch. Your original version is correct (put it back into ima/selinux.v2.fixes). I didn't put a helper for it, because you need $SELINUX_DIR anyway. Thus removed
tst_require_selinux_enabled()as not needed.
Thanks
Quoted 7 lines
I renamed
tst_selinux_enabled()totst_selinux_enforced()to make the purpose clearer (commit82b598ea1IMA: Add test for selinux measurement).I've updated branch ima/selinux.v2.fixes with all mentioned changes
https://github.com/pevik/ltp/commits/ima/selinux.v2.fixes
Thanks again. I'll make my changes in this branch and post the patches later this week.
-lakshmi
Hi Lakshmi, Mimi,
Quoted 8 lines
New functionality has been added in IMA to measure data that is critical to the integrity of the system. SELinux uses this feature to measure the hash of the SELinux policy loaded in kernel memory, and the current state of various SELinux configuration. This new functionality needs test automation in LTP.
Add test cases which verify that the IMA subsystem correctly measures the data provided by SELinux.
I finally merged this patchset, noted both kernel commits. Thanks for this test!
Kind regards,
Petr
* fdd1ffe8a812 ("selinux: include a consumer of the new IMA critical data hook") (v5.12-rc1-dontuse)
* 2554a48f4437 ("selinux: measure state and policy capabilities") (to be in v5.13