diff --git a/xCAT-test/autotest/bundle/selinux.bundle b/xCAT-test/autotest/bundle/selinux.bundle new file mode 100644 index 0000000000..c911b45964 --- /dev/null +++ b/xCAT-test/autotest/bundle/selinux.bundle @@ -0,0 +1,6 @@ +selinux_xcatd_domain +selinux_mn_labels +selinux_restapi_avc +selinux_cn_diskful_enforcing +selinux_cn_diskless_disabled_by_design +selinux_cn_diskless_site_disabled diff --git a/xCAT-test/autotest/bundle/selinux_mn.bundle b/xCAT-test/autotest/bundle/selinux_mn.bundle new file mode 100644 index 0000000000..bebb26dbce --- /dev/null +++ b/xCAT-test/autotest/bundle/selinux_mn.bundle @@ -0,0 +1,3 @@ +selinux_xcatd_domain +selinux_mn_labels +selinux_restapi_avc diff --git a/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh b/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh new file mode 100755 index 0000000000..bc3bbd7809 --- /dev/null +++ b/xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# Report the SELinux denials that xCAT is answerable for, out of the audit records since +# a start time. +# +# A cluster denies things xCAT has nothing to do with. On an idle enforcing management +# node httpd asks the kernel for net_admin on itself every ten seconds, and the base +# policy carries no dontaudit rule for that shape, so a two hour window holds hundreds of +# denials that no change to xCAT can remove. 1034 denials were counted on one node in one +# day, of which 817 were that one shape. "The window holds no denial" is therefore not a +# test xCAT can pass. This script keeps the denials whose subject program or object path +# belongs to xCAT, and counts those. +# +# ausearch reads the journal unless it is told otherwise, and on el10 the journal carries +# no audit records, so --input-logs is not optional: without it the search returns 0 +# records and every answer is "clean". +# +# Usage: +# selinux_avc_check.sh search and report +# selinux_avc_check.sh --classify read raw records on stdin +# +# Prints: SEARCHED= AVC= XCAT_AVC= +# then the records it kept. +# 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. +XCAT_AVC_COMM='xcatd|genimage|packimage|nodeset|mknb|makedhcp|makedns|makehosts|makeknownhosts|copycds|liteimg|xdsh|xdcp|xcatprobe|xcatconfig' +XCAT_AVC_PATH='/opt/xcat|/xcatpost|/tftpboot|/install|/var/log/xcat|/etc/xcat|/var/lib/xcat' + +selinux_avc_classify() +{ + grep -E "(comm|exe)=\"[^\"]*(${XCAT_AVC_COMM})[^\"]*\"|(path|name)=\"[^\"]*(${XCAT_AVC_PATH})" +} + +selinux_avc_report() +{ + since=$1 + [ -n "$since" ] || return 2 + + searched=$(ausearch --input-logs -ts "$since" --raw 2>/dev/null | grep -c '^type=') + 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 +} + +# 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 ;; + *) selinux_avc_report "$1" ;; + esac +fi diff --git a/xCAT-test/autotest/testcase/selinux/cases0 b/xCAT-test/autotest/testcase/selinux/cases0 index 364ac84ab1..eef22eb219 100644 --- a/xCAT-test/autotest/testcase/selinux/cases0 +++ b/xCAT-test/autotest/testcase/selinux/cases0 @@ -58,7 +58,7 @@ 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; 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; date "+%x %T" > /tmp/selinux_mn_labels.start; true' check:rc==0 cmd:rpm -q xCAT-selinux check:rc==0 @@ -96,7 +96,25 @@ check:rc==0 cmd:sh -c 'grep -F "permissive=0" /tmp/selinux_mn_labels.avc | wc -l' check:rc==0 check:output=~^0$ -cmd:rm -f /tmp/selinux_mn_labels.ck /tmp/selinux_mn_labels.avc +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_mn_labels.start)"' +check:rc==0 +check:output=~XCAT_AVC=0 +cmd:sh -c 'date "+%x %T" > /tmp/selinux_mn_control.start; sleep 1; 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)"' +check:rc==1 +check:output=~XCAT_AVC=[1-9] +cmd:restorecon -F /install/postscripts/xcatdsklspost +check:rc==0 +cmd:ls -Z /install/postscripts/xcatdsklspost +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 end start:selinux_restapi_avc @@ -105,7 +123,7 @@ 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; 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; date "+%x %T" > /tmp/selinux_restapi_avc.start; true' check:rc==0 cmd:sh -c 'rpm -q mod_ssl || dnf install -y mod_ssl' check:rc==0 @@ -121,5 +139,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:rm -f /tmp/selinux_restapi_avc.ck /tmp/selinux_restapi_avc.avc +cmd:sh -c '/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/selinux_avc_check.sh "$(cat /tmp/selinux_restapi_avc.start)"' +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 end diff --git a/xCAT-test/autotest/testcase/selinux/cases1 b/xCAT-test/autotest/testcase/selinux/cases1 index 81d5168b16..932188687a 100644 --- a/xCAT-test/autotest/testcase/selinux/cases1 +++ b/xCAT-test/autotest/testcase/selinux/cases1 @@ -23,15 +23,19 @@ check:rc==0 check:output=~ssh_home_t check:output!~default_t check:output!~unlabeled_t -cmd:xdsh $$CN "restorecon -RnvF /xcatpost /opt/xcat /root /etc/ssh /var/log/xcat" +cmd:xdsh $$CN "restorecon -Rnv /xcatpost /opt/xcat /root /etc/ssh /var/log/xcat" check:rc==0 check:output!~Would relabel -cmd:xdsh $$CN "ausearch --input-logs -m AVC,USER_AVC -ts boot" -check:output=~ -check:output!~type=AVC -cmd:sh -c 'ausearch --input-logs -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_diskful.start)' -check:rc==1 -check:output=~ +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" +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)"' +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 @@ -71,15 +75,19 @@ check:output=~ssh_home_t check:output!~tmpfs_t check:output!~default_t check:output!~unlabeled_t -cmd:xdsh $$CN "restorecon -RnvF /xcatpost /opt/xcat /root /etc/ssh /usr/sbin" +cmd:xdsh $$CN "restorecon -Rnv /xcatpost /opt/xcat /root /etc/ssh /usr/sbin" check:rc==0 check:output!~Would relabel -cmd:xdsh $$CN "ausearch --input-logs -m AVC,USER_AVC -ts boot" -check:output=~ -check:output!~type=AVC -cmd:sh -c 'ausearch --input-logs -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_diskless.start)' -check:rc==1 -check:output=~ +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" +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_diskless.start)"' +check:rc==0 +check:output=~XCAT_AVC=0 cmd:chdef -t node -o $$CN selinux= check:rc==0 cmd:rm -f /tmp/selinux_cn_diskless.start @@ -121,15 +129,19 @@ check:output=~bin_t check:output!~tmp_t check:output!~default_t check:output!~unlabeled_t -cmd:xdsh $$CN "restorecon -RnvF /usr/bin /usr/sbin /etc/ssh /xcatpost /opt/xcat" +cmd:xdsh $$CN "restorecon -Rnv /usr/bin /usr/sbin /etc/ssh /xcatpost /opt/xcat" check:rc==0 check:output!~Would relabel -cmd:xdsh $$CN "ausearch --input-logs -m AVC,USER_AVC -ts boot" -check:output=~ -check:output!~type=AVC -cmd:sh -c 'ausearch --input-logs -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_squashfs.start)' -check:rc==1 -check:output=~ +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" +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_squashfs.start)"' +check:rc==0 +check:output=~XCAT_AVC=0 cmd:chdef -t node -o $$CN selinux= check:rc==0 cmd:rm -f /tmp/selinux_cn_squashfs.start diff --git a/xCAT-test/bats/selinux_avc_check.bats b/xCAT-test/bats/selinux_avc_check.bats new file mode 100644 index 0000000000..8decd6a85e --- /dev/null +++ b/xCAT-test/bats/selinux_avc_check.bats @@ -0,0 +1,78 @@ +#!/usr/bin/env bats +# +# The classifier that decides which SELinux denials xCAT is answerable for. The records +# below are real: they were captured on an enforcing AlmaLinux 10 management node while a +# compute node was provisioned. + +load 'helpers/shell_source' + +setup() +{ + . "$(repo_path 'xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh')" +} + +classify() +{ + printf '%s\n' "$@" | selinux_avc_classify +} + +count_kept() +{ + printf '%s\n' "$@" | selinux_avc_classify | grep -c '^type=' +} + +# httpd serving /tftpboot through the xcat.conf AliasMatch. This is the one the xcat +# policy module exists for, so a denial here is xCAT's. +XCAT_RECORD='type=AVC msg=audit(1759000000.111:222): avc: denied { getattr } for pid=1234 comm="httpd" path="/tftpboot/xcat/xnba.kpxe" dev="vda2" ino=55 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:shadow_t:s0 tclass=file permissive=0' + +# httpd asking for net_admin on itself, every ten seconds, on an idle node. 817 of 1034 +# denials on one node in one day were this shape. +TIMER_RECORD='type=AVC msg=audit(1759000001.222:333): avc: denied { net_admin } for pid=2345 comm="httpd" scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:system_r:httpd_t:s0 tclass=capability permissive=0' + +# An ordinary ssh login. cluster-test.pl makes one on every step. +SSH_RECORD='type=AVC msg=audit(1759000002.333:444): avc: denied { siginh } for pid=3456 comm="sshd" scontext=system_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:system_r:sshd_session_t:s0-s0:c0.c1023 tclass=process permissive=0' + +@test "a denial on an xCAT directory is kept" { + run classify "$XCAT_RECORD" + [ "$status" -eq 0 ] + [[ "$output" == *'/tftpboot/xcat/xnba.kpxe'* ]] +} + +@test "the httpd net_admin timer is not kept" { + run classify "$TIMER_RECORD" + [ -z "$output" ] +} + +@test "an ssh login denial is not kept" { + run classify "$SSH_RECORD" + [ -z "$output" ] +} + +@test "one xCAT denial among the noise is found and counted once" { + run count_kept "$TIMER_RECORD" "$XCAT_RECORD" "$SSH_RECORD" "$TIMER_RECORD" + [ "$status" -eq 0 ] + [ "$output" = "1" ] +} + +@test "a window of noise alone is kept empty" { + run classify "$TIMER_RECORD" "$SSH_RECORD" + [ -z "$output" ] +} + +@test "a denial by an xCAT program is kept whatever the path" { + local record='type=AVC msg=audit(1759000003.444:555): avc: denied { read } for pid=4567 comm="genimage" name="dracut.conf" scontext=system_u:system_r:unconfined_service_t:s0 tcontext=system_u:object_r:etc_t:s0 tclass=file permissive=0' + run classify "$record" + [ "$status" -eq 0 ] + [[ "$output" == *'comm="genimage"'* ]] +} + +@test "a program whose name merely contains a kept word is not matched by accident" { + local record='type=AVC msg=audit(1759000004.555:666): avc: denied { execute } for pid=5678 comm="systemd-udevd" scontext=system_u:system_r:udev_t:s0 tcontext=system_u:object_r:bin_t:s0 tclass=file permissive=0' + run classify "$record" + [ -z "$output" ] +} + +@test "the report refuses a call with no start time" { + run "$(repo_path 'xCAT-test/autotest/testcase/commoncmd/selinux_avc_check.sh')" + [ "$status" -eq 2 ] +} diff --git a/xCAT-test/unit/selinux_bundle.t b/xCAT-test/unit/selinux_bundle.t new file mode 100644 index 0000000000..dc7a7e721c --- /dev/null +++ b/xCAT-test/unit/selinux_bundle.t @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use File::Slurper qw(read_lines); +use Test::More; + +# A case that no bundle names is a case no run selects. The six SELinux cases were in no +# bundle and in no pipeline conf, so none of them had ever run, and eight checks that +# could never pass went unnoticed for that reason. +my $root = "$FindBin::Bin/../.."; +my $dir = "$root/xCAT-test/autotest/testcase/selinux"; + +my @cases; +foreach my $file (sort glob("$dir/cases*")) { + push @cases, map { /^start:(\S+)/ ? $1 : () } read_lines($file); +} +ok(scalar(@cases) >= 6, "the selinux testcase files declare @{[scalar @cases]} cases"); + +my %bundled; +foreach my $bundle (sort glob("$root/xCAT-test/autotest/bundle/*.bundle")) { + $bundled{$_} = 1 for grep { /\S/ } read_lines($bundle); +} +ok(scalar(keys %bundled) > 100, "the bundles name @{[scalar keys %bundled]} cases in total"); + +my @orphans = grep { !$bundled{$_} } @cases; +is_deeply(\@orphans, [], 'every selinux case is named by a bundle'); + +my @mn = grep { /\S/ } read_lines("$root/xCAT-test/autotest/bundle/selinux_mn.bundle"); +my %mn = map { $_ => 1 } @mn; +foreach my $case (@cases) { + next unless $case =~ /_cn_/; + ok(!$mn{$case}, "$case needs a compute node, so the management node bundle leaves it out"); +} + +done_testing(); diff --git a/xCAT-test/unit/selinux_kickstart_template.t b/xCAT-test/unit/selinux_kickstart_template.t index 3ca8a8d8dd..0d7bd71b2b 100644 --- a/xCAT-test/unit/selinux_kickstart_template.t +++ b/xCAT-test/unit/selinux_kickstart_template.t @@ -94,8 +94,18 @@ foreach my $case (@templates) { unlike($ks, $relabel, "$template does not relabel a $mode node"); } else { like($post, $relabel, "$template relabels the xCAT files in %post for a $mode node"); - like($post, qr/$relabel.*\n\}\s*&>>\/var\/log\/xcat\/xcat\.log\s*\n%end\z/s, - "$template relabels after the xCAT post scripts ran"); + + # The relabel has to run after the xCAT post scripts wrote their files, so + # compare where each one lands. A regex that only spans from the relabel to + # the end of %post passes wherever the relabel sits. + my @post_lines = split(/\n/, $post); + my ($relabel_at) = grep { $post_lines[$_] =~ $relabel } 0 .. $#post_lines; + my @xcat_at = grep { + $_ != $relabel_at && $post_lines[$_] =~ /mypostscript|xcatdsklspost|updateflag/ + } 0 .. $#post_lines; + ok(scalar(@xcat_at) > 0, "$template runs the xCAT post scripts in %post"); + ok(defined $relabel_at && $relabel_at > $xcat_at[-1], + "$template relabels at line $relabel_at, after the last xCAT post script at line $xcat_at[-1]"); } } }