diff --git a/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh b/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh index e9ddb39ef5..ffe4357bd1 100755 --- a/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh +++ b/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh @@ -14,18 +14,26 @@ # no audit records, so --input-logs is not optional: without it the search returns 0 # records and every answer is "clean". # -# audit 4.0.3 takes a bare HH:MM:SS and rejects the output of date "+%x %T". It prints -# nothing and exits 0, so a window that holds records reads as empty. Measured on an -# enforcing el10 node: "-ts 10/21/2026 20:59:08" returned 0 records, "-ts 20:59:08" -# returned 22 and "-ts today" returned 4006. A window with no audit record at all is not a -# credible answer on a running node, so SEARCHED=0 is a usage error and not a clean result. +# A time window cannot say whether an empty answer means a quiet node or a broken +# search, and -ts has one second of granularity, so a denial in the second a case starts +# lands in that case's window. A checkpoint file is an offset: nothing since the +# checkpoint has one meaning, there is no format to reject, and two cases cannot share a +# record. Prefer --init and --since. The -ts form stays for the keywords boot and today, +# where the window is not a case boundary. +# +# audit 4.0.3 rejects the output of date "+%x %T" and names the format on stderr, which +# the pipeline below discards, so a window that holds records reads as empty. Measured on +# an enforcing el10 node: "-ts 10/21/2026 20:59:08" returned 0 records, "-ts 20:59:08" +# returned 22 and "-ts today" returned 4006. # # Usage: -# selinux_avc_check.sh search and report +# selinux_avc_check.sh --init start a window, print the records it read +# selinux_avc_check.sh --since report the denials after that window +# selinux_avc_check.sh search a time window and report # selinux_avc_check.sh --classify read raw records on stdin # -# Prints: SEARCHED= AVC= XCAT_AVC= -# then the records it kept. +# Prints: BASELINE= for --init. AVC= XCAT_AVC= for a +# report, then the records it kept. The -ts form adds SEARCHED=. # Exits: 0 when it kept none, 1 when it kept any, 2 on a usage error. # Subjects: the programs xCAT runs. Objects: the directories xCAT owns. @@ -37,6 +45,55 @@ selinux_avc_classify() grep -E "(comm|exe)=\"[^\"]*(${XCAT_AVC_COMM})[^\"]*\"|(path|name)=\"[^\"]*(${XCAT_AVC_PATH})" } +# Report the denials ausearch answered with. An error message is not a clean window: +# ausearch prints the records, or '', or nothing at all when it has read to +# the end of the log, and anything else is a failure of the search. +selinux_avc_count() +{ + denials=$1 + noise=$(printf '%s\n' "$denials" | grep -v -e '^type=' -e '^$' -e '^$') + if [ -n "$noise" ]; then + echo "AVC=? XCAT_AVC=?" + echo "ausearch answered neither a record nor '': $noise" >&2 + return 2 + fi + + all=$(printf '%s\n' "$denials" | grep -c '^type=') + kept=$(printf '%s\n' "$denials" | selinux_avc_classify) + ours=$(printf '%s\n' "$kept" | grep -c '^type=') + + echo "AVC=$all XCAT_AVC=$ours" + [ "$ours" -eq 0 ] && return 0 + printf '%s\n' "$kept" + return 1 +} + +# Consume every record written so far, so the checkpoint names this moment. BASELINE +# counts them: on a running node a zero means the search reads nothing, not that the node +# is quiet, and the checkpoint file is then not written. +selinux_avc_init() +{ + checkpoint=$1 + [ -n "$checkpoint" ] || return 2 + + rm -f "$checkpoint" + records=$(ausearch --input-logs --checkpoint "$checkpoint" -ts today --raw 2>&1 \ + | grep -c '^type=') + echo "BASELINE=$records" + return 0 +} + +selinux_avc_since() +{ + checkpoint=$1 + [ -n "$checkpoint" ] || return 2 + + selinux_avc_count "$(ausearch --input-logs -m AVC,USER_AVC \ + --checkpoint "$checkpoint" --raw 2>&1)" +} + +# The -ts form keeps the two-query discriminator: an empty narrow window with records +# today is a quiet node, an empty window in both is a search that reads nothing. selinux_avc_report() { since=$1 @@ -44,26 +101,25 @@ selinux_avc_report() searched=$(ausearch --input-logs -ts "$since" --raw 2>/dev/null | grep -c '^type=') if [ "$searched" -eq 0 ]; then - echo "SEARCHED=0 AVC=0 XCAT_AVC=0" - echo "no audit record since '$since'. ausearch takes a bare HH:MM:SS." >&2 - return 2 + today=$(ausearch --input-logs -ts today --raw 2>/dev/null | grep -c '^type=') + if [ "$today" -eq 0 ]; then + echo "SEARCHED=0 AVC=0 XCAT_AVC=0" + echo "no audit record since '$since' and none today. ausearch takes a bare HH:MM:SS." >&2 + return 2 + fi fi - denials=$(ausearch --input-logs -m AVC,USER_AVC -ts "$since" --raw 2>/dev/null) - all=$(printf '%s\n' "$denials" | grep -c '^type=') - kept=$(printf '%s\n' "$denials" | selinux_avc_classify) - ours=$(printf '%s\n' "$kept" | grep -c '^type=') - echo "SEARCHED=$searched AVC=$all XCAT_AVC=$ours" - [ "$ours" -eq 0 ] && return 0 - printf '%s\n' "$kept" - return 1 + echo "SEARCHED=$searched" + selinux_avc_count "$(ausearch --input-logs -m AVC,USER_AVC -ts "$since" --raw 2>&1)" } # Sourced by the bats test, which calls the functions directly. if [ "$(basename "$0")" = selinux_avc_check.sh ]; then case $1 in --classify) selinux_avc_classify ;; - '') echo "usage: $0 " >&2; exit 2 ;; + --init) selinux_avc_init "$2" ;; + --since) selinux_avc_since "$2" ;; + '') echo "usage: $0 --init|--since | " >&2; exit 2 ;; *) selinux_avc_report "$1" ;; esac fi diff --git a/xCAT-test/autotest/testcase/selinux/cases0 b/xCAT-test/autotest/testcase/selinux/cases0 index 8f74f09e82..ebf95660ea 100644 --- a/xCAT-test/autotest/testcase/selinux/cases0 +++ b/xCAT-test/autotest/testcase/selinux/cases0 @@ -58,8 +58,11 @@ label:mn_only,selinux cmd:getenforce check:rc==0 check:output=~^Enforcing$ -cmd:sh -c 'rm -f /tmp/selinux_mn_labels.ck; ausearch --input-logs -m AVC,USER_AVC --checkpoint /tmp/selinux_mn_labels.ck -ts today > /dev/null 2>&1; date "+%H:%M:%S" > /tmp/selinux_mn_labels.start; true' +cmd:sh -c 'rm -f /tmp/selinux_mn_labels.ck; ausearch --input-logs -m AVC,USER_AVC --checkpoint /tmp/selinux_mn_labels.ck -ts today > /dev/null 2>&1; true' check:rc==0 +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --init /tmp/selinux_mn_labels.avcck' +check:rc==0 +check:output=~BASELINE=[1-9] cmd:rpm -q xCAT-selinux check:rc==0 cmd:sh -c 'semodule -l | grep -x -E "xcat([[:space:]].*)?"' @@ -96,15 +99,18 @@ check:rc==0 cmd:sh -c 'grep -F "permissive=0" /tmp/selinux_mn_labels.avc | wc -l' check:rc==0 check:output=~^0$ -cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_mn_labels.start)"' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --since /tmp/selinux_mn_labels.avcck' check:rc==0 check:output=~XCAT_AVC=0 -cmd:sh -c 'date "+%H:%M:%S" > /tmp/selinux_mn_control.start; sleep 1; chcon -t shadow_t /install/postscripts/xcatdsklspost' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --init /tmp/selinux_mn_control.avcck' +check:rc==0 +check:output=~BASELINE=[1-9] +cmd:chcon -t shadow_t /install/postscripts/xcatdsklspost check:rc==0 cmd:sh -c 'curl -s -o /dev/null -w "%{http_code}" http://localhost/install/postscripts/xcatdsklspost' check:rc==0 check:output=~403 -cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_mn_control.start)"' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --since /tmp/selinux_mn_control.avcck' check:rc==1 check:output=~XCAT_AVC=[1-9] cmd:restorecon -F /install/postscripts/xcatdsklspost @@ -114,7 +120,7 @@ check:rc==0 check:output=~:public_content_t: cmd:sh -c 'curl -s -f -o /dev/null http://localhost/install/postscripts/xcatdsklspost' check:rc==0 -cmd:rm -f /tmp/selinux_mn_labels.start /tmp/selinux_mn_control.start /tmp/selinux_mn_labels.ck /tmp/selinux_mn_labels.avc +cmd:rm -f /tmp/selinux_mn_labels.ck /tmp/selinux_mn_labels.avc /tmp/selinux_mn_labels.avcck /tmp/selinux_mn_control.avcck end start:selinux_restapi_avc @@ -123,8 +129,11 @@ label:mn_only,selinux,restapi cmd:getenforce check:rc==0 check:output=~^Enforcing$ -cmd:sh -c 'rm -f /tmp/selinux_restapi_avc.ck; ausearch --input-logs -m AVC,USER_AVC --checkpoint /tmp/selinux_restapi_avc.ck -ts today > /dev/null 2>&1; date "+%H:%M:%S" > /tmp/selinux_restapi_avc.start; true' +cmd:sh -c 'rm -f /tmp/selinux_restapi_avc.ck; ausearch --input-logs -m AVC,USER_AVC --checkpoint /tmp/selinux_restapi_avc.ck -ts today > /dev/null 2>&1; true' check:rc==0 +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --init /tmp/selinux_restapi_avc.avcck' +check:rc==0 +check:output=~BASELINE=[1-9] cmd:sh -c 'rpm -q mod_ssl || dnf install -y mod_ssl' check:rc==0 cmd:systemctl restart httpd @@ -139,8 +148,8 @@ check:rc==0 cmd:sh -c 'grep -F "permissive=0" /tmp/selinux_restapi_avc.avc | wc -l' check:rc==0 check:output=~^0$ -cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_restapi_avc.start)"' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --since /tmp/selinux_restapi_avc.avcck' check:rc==0 check:output=~XCAT_AVC=0 -cmd:rm -f /tmp/selinux_restapi_avc.start /tmp/selinux_restapi_avc.ck /tmp/selinux_restapi_avc.avc +cmd:rm -f /tmp/selinux_restapi_avc.ck /tmp/selinux_restapi_avc.avc /tmp/selinux_restapi_avc.avcck end diff --git a/xCAT-test/autotest/testcase/selinux/cases1 b/xCAT-test/autotest/testcase/selinux/cases1 index a6c9a3ed23..defdf7c18c 100644 --- a/xCAT-test/autotest/testcase/selinux/cases1 +++ b/xCAT-test/autotest/testcase/selinux/cases1 @@ -1,12 +1,13 @@ start:selinux_cn_diskful_enforcing -description:on an enforcing management node, a diskful compute node with noderes.selinux=enforcing boots enforcing, with correct labels and no denial +description:on an enforcing management node, a diskful compute node with noderes.selinux=enforcing boots enforcing, with correct labels and no denial. The labels of /xcatpost are reported and not asserted: the kickstart relabel runs in %post, the xCAT post scripts write that tree at the first boot, and the node carries no xCAT-selinux label:selinux,provision os:Linux cmd:getenforce check:rc==0 check:output=~^Enforcing$ -cmd:sh -c 'date "+%H:%M:%S" > /tmp/selinux_cn_diskful.start' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --init /tmp/selinux_cn_diskful.avcck' check:rc==0 +check:output=~BASELINE=[1-9] cmd:chdef -t node -o $$CN selinux=enforcing check:rc==0 cmd:/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/retry_install.sh $$CN __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-install-compute @@ -23,9 +24,11 @@ check:rc==0 check:output=~ssh_home_t check:output!~default_t check:output!~unlabeled_t -cmd:xdsh $$CN "restorecon -Rnv /xcatpost /opt/xcat /root /etc/ssh /var/log/xcat" +cmd:xdsh $$CN "restorecon -Rnv /opt/xcat /root /etc/ssh /var/log/xcat" check:rc==0 check:output!~Would relabel +cmd:xdsh $$CN "restorecon -Rnv /xcatpost; true" +check:rc==0 cmd:xdcp $$CN /opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh /tmp/ check:rc==0 cmd:xdsh $$CN "sh /tmp/selinux_avc_check.sh boot" @@ -33,12 +36,12 @@ check:rc==0 check:output=~XCAT_AVC=0 cmd:xdsh $$CN "rm -f /tmp/selinux_avc_check.sh" check:rc==0 -cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_cn_diskful.start)"' +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh --since /tmp/selinux_cn_diskful.avcck' check:rc==0 check:output=~XCAT_AVC=0 cmd:chdef -t node -o $$CN selinux= check:rc==0 -cmd:rm -f /tmp/selinux_cn_diskful.start +cmd:rm -f /tmp/selinux_cn_diskful.avcck end start:selinux_cn_diskless_disabled_by_design @@ -106,7 +109,7 @@ cmd:rm -f /tmp/selinux_site.orig end start:selinux_sn_enforcing -description:in a hierarchical cluster, the service node runs SELinux enforcing with the xCAT labels in place and no xCAT denial since boot +description:in a hierarchical cluster, the service node runs SELinux enforcing with the xCAT labels in place and no xCAT denial since boot. The labels of /xcatpost are reported and not asserted, as on a compute node label:selinux,sn_diskful os:rhels cmd:getenforce @@ -126,9 +129,11 @@ cmd:xdsh $$SN "ls -dZ /install /tftpboot /opt/xcat /xcatpost" check:rc==0 check:output!~unlabeled_t check:output!~default_t -cmd:xdsh $$SN "restorecon -Rnv /xcatpost /opt/xcat /etc/xcat /var/log/xcat" +cmd:xdsh $$SN "restorecon -Rnv /opt/xcat /etc/xcat /var/log/xcat" check:rc==0 check:output!~Would relabel +cmd:xdsh $$SN "restorecon -Rnv /xcatpost; true" +check:rc==0 cmd:xdsh $$SN "systemctl is-active xcatd" check:rc==0 check:output=~active