#!/usr/bin/env bash ## httpd-exec-surface-gate.test.sh - proves scripts/httpd-exec-surface-gate.sh can FAIL. ## ## A gate that always says "OK" is worse than none. This runs the gate inside a ## throwaway container on stock AlmaLinux 10 + httpd + mod_ssl + mod_lua (the exact ## 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 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); and a ## banned status / info / proxy_http / proxy_wstunnel / cache module back ## (loaded, or only left on disk), which no config can neutralise because a ## tenant `RewriteRule [H=server-status]` / `[P] http://` reaches it ## Needs docker and network (dnf). Exits 0 only if every case behaves. ## Env: BASE (default almalinux/10-base). set -u HERE=$(cd "$(dirname "$0")/.." && pwd) GATE="$HERE/httpd-exec-surface-gate.sh" BASE=${BASE:-almalinux/10-base} N="exec-gate-test-$$" fail=0; pass=0 ok() { pass=$((pass + 1)); echo " ok $*"; } bad() { fail=$((fail + 1)); echo " FAIL $*"; } command -v docker >/dev/null 2>&1 || { echo "SKIP-FAIL: docker not installed" >&2; exit 1; } trap 'docker rm -f "$N" >/dev/null 2>&1' EXIT docker run -d --name "$N" "$BASE" sleep 3600 >/dev/null || { echo "cannot start $BASE" >&2; exit 1; } docker exec "$N" sh -c 'dnf install -y httpd mod_ssl mod_lua openssl >/dev/null 2>&1' \ || { echo "dnf install failed (network?)" >&2; exit 1; } 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. 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 '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 echo "== remove + check" g remove >/dev/null || bad "remove failed" if out=$(g check); then ok "remove then check passes"; else bad "check after remove: $out"; fi if docker exec "$N" sh -c "httpd -M 2>/dev/null | grep -E '^ (cgid|cgi|lua|include|actions|dav|dav_fs|dav_lock|userdir|status|info|proxy_(http|ajp|ftp|connect|scgi|uwsgi|wstunnel|fdpass|express|hcheck)|lbmethod_(bybusyness|bytraffic|heartbeat)|suexec|dbd|authn_dbd|authz_dbd|cache|cache_disk|cache_socache|socache_(dbm|memcache|redis))_module '"; then bad "banned module in httpd -M"; else ok "httpd -M has none of the banned modules (exec, dav, userdir, status, info, unused proxy_*, suexec, dbd, cache, socache)"; fi if docker exec "$N" rpm -q mod_lua >/dev/null 2>&1; then bad "mod_lua RPM still installed"; else ok "mod_lua RPM gone"; fi if out=$(g remove) && out2=$(g check); then ok "remove is idempotent"; else bad "second remove/check: $out $out2"; fi snap 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 "$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, 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' # Handlers/[P] schemes a tenant can reach by RewriteRule no matter what config says. addmod() { echo "cp /tmp/pristine-mods/mod_$1.so /usr/lib64/httpd/modules/ && echo 'LoadModule $1_module modules/mod_$1.so' >> /etc/httpd/conf.modules.d/$2"; } expect_fail "mod_status back (server-status)" 'exec-capable module is still loaded' "$(addmod status 00-base.conf)" expect_fail "mod_info back (server-info)" 'exec-capable module is still loaded' "$(addmod info 00-base.conf)" expect_fail "mod_proxy_http back ([P] http:// SSRF)" 'exec-capable module is still loaded' "$(addmod proxy_http 00-proxy.conf)" expect_fail "mod_proxy_wstunnel back ([P] ws://)" 'exec-capable module is still loaded' "$(addmod proxy_wstunnel 00-proxy.conf)" expect_fail "mod_proxy_ajp back" 'exec-capable module is still loaded' "$(addmod proxy_ajp 00-proxy.conf)" expect_fail "mod_cache back" 'exec-capable module is still loaded' "$(addmod cache 00-base.conf)" expect_fail "leftover mod_status.so on disk" 'mod_status.so still exists' 'touch /usr/lib64/httpd/modules/mod_status.so' expect_fail "leftover mod_proxy_http.so on disk" 'mod_proxy_http.so still exists' 'touch /usr/lib64/httpd/modules/mod_proxy_http.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 (lbmethod_byrequests)" 'required module lbmethod_byrequests_module' 'sed -i "/lbmethod_byrequests_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 ]