94 lines
3.4 KiB
Bash
Executable File
94 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Verifies the sandbox hardening and measures cold start and memory overhead.
|
|
#
|
|
# On the server (as root, after install-sandbox-host.sh):
|
|
# deploy/host/verify-sandbox.sh
|
|
# Locally with Podman, where gVisor and the slice are not available:
|
|
# ENGINE=podman EXPECT_GVISOR=0 deploy/host/verify-sandbox.sh
|
|
#
|
|
set -uo pipefail
|
|
|
|
ENGINE=${ENGINE:-docker}
|
|
IMAGE=${IMAGE:-security-command/sandbox:dev}
|
|
EXPECT_GVISOR=${EXPECT_GVISOR:-1}
|
|
if [[ $ENGINE == docker ]]; then
|
|
export DOCKER_HOST=${DOCKER_HOST:-unix:///run/docker-sandbox/docker.sock}
|
|
fi
|
|
|
|
source "$(dirname "$0")/sandbox-flags.sh"
|
|
flags=(--rm "${SANDBOX_FLAGS[@]}")
|
|
|
|
failures=0
|
|
pass() { printf '\e[32mPASS\e[0m %s\n' "$1"; }
|
|
fail() { printf '\e[31mFAIL\e[0m %s\n' "$1"; failures=$((failures + 1)); }
|
|
info() { printf '\e[34mINFO\e[0m %s\n' "$1"; }
|
|
|
|
sandbox() { "$ENGINE" run "${flags[@]}" "$IMAGE" bash -c "$1" 2>&1; }
|
|
|
|
check() {
|
|
local name=$1 script=$2 pattern=$3 output
|
|
output=$(sandbox "$script")
|
|
if grep -Eq -- "$pattern" <<<"$output"; then
|
|
pass "$name"
|
|
else
|
|
fail "$name"
|
|
sed 's/^/ /' <<<"$output" | head -n 10
|
|
fi
|
|
}
|
|
|
|
if [[ $EXPECT_GVISOR == 1 ]]; then
|
|
check "runtime is gVisor" 'dmesg' 'gVisor'
|
|
fi
|
|
|
|
check "rootfs is read only" 'touch /x && echo writable' 'Read-only file system'
|
|
check "home is read only" 'touch ~/x && echo writable' 'Read-only file system'
|
|
check "tmp is writable" 'echo ok > /tmp/probe && cat /tmp/probe' '^ok$'
|
|
check "tmp is noexec" 'cp /bin/true /tmp/t && /tmp/t && echo executed' 'Permission denied'
|
|
check "no network interfaces besides lo" \
|
|
"tail -n +3 /proc/net/dev | awk '{print \$1}' | grep -v '^lo:' | wc -l" '^0$'
|
|
check "curl cannot reach anything" \
|
|
'curl -sS --max-time 3 http://1.1.1.1 >/dev/null && echo reachable' 'curl: \([0-9]+\)'
|
|
check "runs as guest" 'id' 'uid=1000\(guest\)'
|
|
check "no effective capabilities" 'grep CapEff /proc/self/status' 'CapEff:\s+0+$'
|
|
check "no new privileges" 'grep NoNewPrivs /proc/self/status' 'NoNewPrivs:\s+1'
|
|
check "no setuid binaries" 'find / -xdev -perm /6000 -type f 2>/dev/null | wc -l' '^0$'
|
|
check "pids limit stops forks" \
|
|
'for i in $(seq 80); do sleep 5 & done; wait' 'Resource temporarily unavailable|retry'
|
|
check "man pages exist" 'man -w ls' 'ls\.1'
|
|
check "fun tools on PATH" 'command -v figlet cowsay fortune sl | wc -l' '^4$'
|
|
|
|
"$ENGINE" run "${flags[@]}" "$IMAGE" bash -c 'tail /dev/zero' >/dev/null 2>&1
|
|
code=$?
|
|
if [[ $code == 137 ]]; then
|
|
pass "memory limit kills the session (exit 137)"
|
|
else
|
|
fail "memory limit kills the session (exit $code)"
|
|
fi
|
|
|
|
start=$(date +%s%N)
|
|
for _ in 1 2 3 4 5; do "$ENGINE" run "${flags[@]}" "$IMAGE" true >/dev/null 2>&1; done
|
|
info "cold start: $(( ($(date +%s%N) - start) / 5000000 )) ms on average"
|
|
|
|
id=$("$ENGINE" run -d "${flags[@]}" "$IMAGE" bash -c 'sleep 30')
|
|
sleep 3
|
|
info "idle session memory: $("$ENGINE" stats --no-stream --format '{{.MemUsage}}' "$id")"
|
|
if [[ $ENGINE == docker ]]; then
|
|
pid=$("$ENGINE" inspect -f '{{.State.Pid}}' "$id")
|
|
if grep -q 'sandbox.slice' "/proc/$pid/cgroup" 2>/dev/null; then
|
|
pass "session runs inside sandbox.slice"
|
|
else
|
|
fail "session runs inside sandbox.slice ($(cat "/proc/$pid/cgroup" 2>/dev/null))"
|
|
fi
|
|
fi
|
|
"$ENGINE" kill "$id" >/dev/null 2>&1
|
|
|
|
if [[ $EXPECT_GVISOR == 1 ]]; then
|
|
info "slice: $(systemctl show sandbox.slice -p MemoryMax -p CPUQuotaPerSecUSec -p TasksMax | tr '\n' ' ')"
|
|
fi
|
|
|
|
if [[ $failures -gt 0 ]]; then
|
|
echo "$failures check(s) failed"
|
|
exit 1
|
|
fi
|
|
echo "all checks passed"
|