diff --git a/scripts/tests/httpd-exec-surface-gate.test.sh b/scripts/tests/httpd-exec-surface-gate.test.sh index 7e354a9..42bdcbd 100755 --- a/scripts/tests/httpd-exec-surface-gate.test.sh +++ b/scripts/tests/httpd-exec-surface-gate.test.sh @@ -6,9 +6,11 @@ ## starting point of Dockerfile.shared-httpd) and checks: ## - `check` FAILS on the unhardened image (negative control) ## - `remove` then `check` PASSES, and `httpd -M` has none of the banned modules -## - each regression is caught: lua / include / cgid LoadModule back, the global -## INCLUDES filter, a cgi-bin ScriptAlias, `Options +ExecCGI`, and a REQUIRED -## module going missing (positive control) +## - each regression is caught FOR ITS OWN REASON (the failure message is +## asserted): mod_lua back, include loaded again, a prefork-guarded cgi +## LoadModule line, the global INCLUDES filter, a cgi-bin ScriptAlias, +## `Options +ExecCGI`, a leftover .so, and a REQUIRED module going missing +## (positive control, incl. the balancer used by multi-FPM sites) ## Needs docker and network (dnf). Exits 0 only if every case behaves. ## Env: BASE (default almalinux/10-base). set -u @@ -28,9 +30,14 @@ docker exec "$N" sh -c 'dnf install -y httpd mod_ssl mod_lua openssl >/dev/null docker exec "$N" sh -c 'mkdir -p /scripts; [ -e /etc/pki/tls/certs/localhost.crt ] || openssl req -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/localhost.key -x509 -days 2 -subj /CN=localhost -out /etc/pki/tls/certs/localhost.crt 2>/dev/null' docker cp "$GATE" "$N:/scripts/gate.sh" g() { docker exec "$N" /scripts/gate.sh "$@" 2>&1; } -# Snapshot of the hardened state so each mutation starts clean. +# Snapshot of the hardened state so each mutation starts clean. The RPM database +# is not in the snapshot, so restore also uninstalls mod_lua: a case that +# reinstalls it must not leave every later case failing on "RPM mod_lua is +# installed" instead of on the check it is meant to exercise. snap() { docker exec "$N" sh -c 'rm -rf /tmp/snap; mkdir /tmp/snap; cp -a /etc/httpd /tmp/snap/etc; cp -a /usr/lib64/httpd /tmp/snap/lib'; } -restore() { docker exec "$N" sh -c 'rm -rf /etc/httpd /usr/lib64/httpd; cp -a /tmp/snap/etc /etc/httpd; cp -a /tmp/snap/lib /usr/lib64/httpd'; } +restore() { docker exec "$N" sh -c 'if rpm -q mod_lua >/dev/null 2>&1; then rpm -e mod_lua; fi; rm -rf /etc/httpd /usr/lib64/httpd; cp -a /tmp/snap/etc /etc/httpd; cp -a /tmp/snap/lib /usr/lib64/httpd'; } +# Unhardened module set, so a case can put a real banned .so back. +docker exec "$N" sh -c 'cp -a /usr/lib64/httpd/modules /tmp/pristine-mods' echo "== negative control: unhardened image" if out=$(g check); then bad "check passed on an unhardened image: $out"; else ok "check FAILS on the unhardened image"; fi @@ -43,22 +50,32 @@ if docker exec "$N" rpm -q mod_lua >/dev/null 2>&1; then bad "mod_lua RPM still if out=$(g remove) && out2=$(g check); then ok "remove is idempotent"; else bad "second remove/check: $out $out2"; fi snap -expect_fail() { # label, shell snippet run in the container to introduce the regression +expect_fail() { # label, ERE the gate output must match (the reason), shell snippet + # run in the container to introduce the regression. The reason is asserted so a + # case cannot pass on some other, unrelated failure. + local out restore - docker exec "$N" sh -c "$2" >/dev/null 2>&1 || { bad "$1: could not set up the mutation"; return; } - if g check >/dev/null; then bad "$1: gate did NOT fail"; else ok "$1: gate fails"; fi + docker exec "$N" sh -c "$3" >/dev/null 2>&1 || { bad "$1: could not set up the mutation"; return; } + if out=$(g check); then bad "$1: gate did NOT fail"; return; fi + if printf '%s\n' "$out" | grep -qE "$2"; then ok "$1: gate fails ($2)"; else bad "$1: gate failed for another reason: $out"; fi } -echo "== regressions must be caught" -expect_fail "lua_module LoadModule back (RPM)" 'dnf install -y mod_lua' -expect_fail "include_module LoadModule line back" 'echo "LoadModule include_module modules/mod_include.so" >> /etc/httpd/conf.modules.d/00-base.conf' -expect_fail "cgid LoadModule line back" 'echo "LoadModule cgid_module modules/mod_cgid.so" >> /etc/httpd/conf.modules.d/01-cgi.conf' -expect_fail "global AddOutputFilter INCLUDES .shtml" 'echo "AddOutputFilter INCLUDES .shtml" >> /etc/httpd/conf/httpd.conf' -expect_fail "ScriptAlias /cgi-bin/" 'echo "ScriptAlias /cgi-bin/ /var/www/cgi-bin/" >> /etc/httpd/conf/httpd.conf' -expect_fail "Options +ExecCGI in conf.d" 'printf "\nOptions +ExecCGI\n\n" > /etc/httpd/conf.d/x.conf' -expect_fail "leftover mod_userdir.so on disk" 'touch /usr/lib64/httpd/modules/mod_userdir.so' -expect_fail "required module missing (rewrite)" 'sed -i "/rewrite_module/d" /etc/httpd/conf.modules.d/00-base.conf' -expect_fail "required module missing (proxy_fcgi)" 'sed -i "/proxy_fcgi_module/d" /etc/httpd/conf.modules.d/00-proxy.conf' -expect_fail "httpd -t failure" 'echo "BogusDirective on" >> /etc/httpd/conf/httpd.conf' +echo "== regressions must be caught, each for its own reason" +expect_fail "mod_lua RPM back" 'RPM mod_lua is installed' 'dnf install -y mod_lua' +expect_fail "include .so + LoadModule back (loads)" 'exec-capable module is still loaded' \ + 'cp /tmp/pristine-mods/mod_include.so /usr/lib64/httpd/modules/ && echo "LoadModule include_module modules/mod_include.so" >> /etc/httpd/conf.modules.d/00-base.conf' +# Stock 01-cgi.conf shape: under event MPM this line never loads, so only the +# LoadModule scan can see it. +expect_fail "cgi LoadModule line (prefork-guarded)" 'LoadModule line for a banned module' \ + 'printf "\n LoadModule cgi_module modules/mod_cgi.so\n\n" > /etc/httpd/conf.modules.d/01-cgi.conf' +expect_fail "cgid LoadModule line, .so gone" 'httpd -M failed' 'echo "LoadModule cgid_module modules/mod_cgid.so" >> /etc/httpd/conf.modules.d/01-cgi.conf' +expect_fail "global AddOutputFilter INCLUDES .shtml" 'global INCLUDES filter' 'echo "AddOutputFilter INCLUDES .shtml" >> /etc/httpd/conf/httpd.conf' +expect_fail "ScriptAlias /cgi-bin/" 'global INCLUDES filter' 'echo "ScriptAlias /cgi-bin/ /var/www/cgi-bin/" >> /etc/httpd/conf/httpd.conf' +expect_fail "Options +ExecCGI in conf.d" 'global INCLUDES filter' 'printf "\nOptions +ExecCGI\n\n" > /etc/httpd/conf.d/x.conf' +expect_fail "leftover mod_userdir.so on disk" 'mod_userdir.so still exists' 'touch /usr/lib64/httpd/modules/mod_userdir.so' +expect_fail "required module missing (rewrite)" 'required module rewrite_module' 'sed -i "/rewrite_module/d" /etc/httpd/conf.modules.d/00-base.conf' +expect_fail "required module missing (proxy_fcgi)" 'required module proxy_fcgi_module' 'sed -i "/proxy_fcgi_module/d" /etc/httpd/conf.modules.d/00-proxy.conf' +expect_fail "required module missing (balancer)" 'required module proxy_balancer_module' 'sed -i "/proxy_balancer_module/d" /etc/httpd/conf.modules.d/00-proxy.conf' +expect_fail "config does not parse" 'httpd -M failed' 'echo "BogusDirective on" >> /etc/httpd/conf/httpd.conf' echo "$pass passed, $fail failed" [ "$fail" -eq 0 ]