Initial sandbox infrastructure setup.
This commit is contained in:
1 parent
6ac68a7025
commit
0737b28897
10 files changed
+262
No files matched your search
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"hosts": ["unix:///run/docker-sandbox/docker.sock"],
|
||||||
|
"group": "sandbox-spawner",
|
||||||
|
"data-root": "/var/lib/docker-sandbox",
|
||||||
|
"exec-root": "/run/docker-sandbox/exec",
|
||||||
|
"pidfile": "/run/docker-sandbox/docker.pid",
|
||||||
|
"containerd-namespace": "sandbox",
|
||||||
|
"containerd-plugins-namespace": "sandbox-plugins",
|
||||||
|
"bridge": "none",
|
||||||
|
"iptables": false,
|
||||||
|
"ip6tables": false,
|
||||||
|
"ip-forward": false,
|
||||||
|
"ip-masq": false,
|
||||||
|
"userland-proxy": false,
|
||||||
|
"exec-opts": ["native.cgroupdriver=systemd"],
|
||||||
|
"cgroup-parent": "sandbox.slice",
|
||||||
|
"default-runtime": "runsc",
|
||||||
|
"runtimes": {
|
||||||
|
"runsc": {
|
||||||
|
"path": "/usr/bin/runsc",
|
||||||
|
"runtimeArgs": ["--systemd-cgroup"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"log-driver": "none",
|
||||||
|
"no-new-privileges": true,
|
||||||
|
"live-restore": false
|
||||||
|
}
|
||||||
Executable
+38
@@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Prepares the host for sandbox sessions: installs gVisor, creates the
|
||||||
|
# spawner account and starts the dedicated sandbox Docker daemon.
|
||||||
|
# Run as root from the repository root. Safe to run repeatedly.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [[ $EUID -ne 0 ]]; then
|
||||||
|
echo "run as root" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
repo_root=$(cd "$(dirname "$0")/../.." && pwd)
|
||||||
|
|
||||||
|
if ! command -v runsc >/dev/null; then
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y curl gnupg
|
||||||
|
curl -fsSL https://gvisor.dev/archive.key \
|
||||||
|
| gpg --dearmor --yes -o /usr/share/keyrings/gvisor-archive-keyring.gpg
|
||||||
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" \
|
||||||
|
> /etc/apt/sources.list.d/gvisor.list
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y runsc
|
||||||
|
fi
|
||||||
|
|
||||||
|
getent group sandbox-spawner >/dev/null || groupadd --system sandbox-spawner
|
||||||
|
id sandbox-spawner >/dev/null 2>&1 \
|
||||||
|
|| useradd --system --gid sandbox-spawner --no-create-home --shell /usr/sbin/nologin sandbox-spawner
|
||||||
|
|
||||||
|
install -D -m 0644 "$repo_root/deploy/docker-sandbox/daemon.json" /etc/docker-sandbox/daemon.json
|
||||||
|
install -m 0644 "$repo_root/deploy/systemd/sandbox.slice" /etc/systemd/system/sandbox.slice
|
||||||
|
install -m 0644 "$repo_root/deploy/systemd/docker-sandbox.service" /etc/systemd/system/docker-sandbox.service
|
||||||
|
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl start sandbox.slice
|
||||||
|
systemctl enable docker-sandbox.service
|
||||||
|
systemctl restart docker-sandbox.service
|
||||||
|
|
||||||
|
echo "sandbox daemon is up: DOCKER_HOST=unix:///run/docker-sandbox/docker.sock docker info"
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# Container flags for sandbox sessions, shared by the host and local tools.
|
||||||
|
# They mirror the configuration hardcoded in the spawner. Keep them in sync.
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
SANDBOX_FLAGS=(
|
||||||
|
--read-only --network none --cap-drop ALL
|
||||||
|
--security-opt no-new-privileges --user guest
|
||||||
|
--memory 80m --memory-swap 80m --cpus 0.5 --pids-limit 50
|
||||||
|
--ulimit nofile=256:256
|
||||||
|
--tmpfs /home/guest:size=8m,mode=1777,noexec,nosuid
|
||||||
|
--tmpfs /tmp:size=8m,noexec,nosuid
|
||||||
|
--log-driver none
|
||||||
|
--hostname sandbox
|
||||||
|
)
|
||||||
Executable
+93
@@ -0,0 +1,93 @@
|
|||||||
|
#!/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 writable" 'echo ok > ~/probe && cat ~/probe' '^ok$'
|
||||||
|
check "home is noexec" 'cp /bin/true ~/t && ~/t && echo executed' 'Permission denied'
|
||||||
|
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"
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Docker daemon dedicated to sandbox sessions (gVisor)
|
||||||
|
After=network-online.target containerd.service sandbox.slice
|
||||||
|
Wants=network-online.target sandbox.slice
|
||||||
|
Requires=containerd.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=notify
|
||||||
|
ExecStart=/usr/bin/dockerd --config-file /etc/docker-sandbox/daemon.json
|
||||||
|
ExecReload=/bin/kill -s HUP $MAINPID
|
||||||
|
RuntimeDirectory=docker-sandbox
|
||||||
|
RuntimeDirectoryMode=0755
|
||||||
|
TimeoutStartSec=0
|
||||||
|
Restart=always
|
||||||
|
RestartSec=2
|
||||||
|
LimitNOFILE=infinity
|
||||||
|
LimitNPROC=infinity
|
||||||
|
LimitCORE=infinity
|
||||||
|
Delegate=yes
|
||||||
|
KillMode=process
|
||||||
|
OOMScoreAdjust=-500
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Ceiling for all sandbox session containers
|
||||||
|
|
||||||
|
[Slice]
|
||||||
|
MemoryMax=1600M
|
||||||
|
MemorySwapMax=0
|
||||||
|
CPUQuota=150%
|
||||||
|
CPUWeight=20
|
||||||
|
TasksMax=800
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
FROM debian:trixie-slim
|
||||||
|
|
||||||
|
ENV LANG=C.UTF-8
|
||||||
|
|
||||||
|
# Slim images exclude man pages through a dpkg path filter. Removing it and
|
||||||
|
# reinstalling the preinstalled essentials restores their man pages.
|
||||||
|
RUN rm -f /etc/dpkg/dpkg.cfg.d/docker \
|
||||||
|
&& apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends --reinstall \
|
||||||
|
bash coreutils findutils grep sed \
|
||||||
|
&& apt-get install -y --no-install-recommends \
|
||||||
|
gawk less tree file vim-tiny nano man-db manpages jq procps htop \
|
||||||
|
figlet cowsay fortune-mod fortunes-min sl curl wget \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Setuid binaries are useless under no-new-privileges, so they are stripped
|
||||||
|
# to keep the attack surface minimal even if that option is ever dropped.
|
||||||
|
RUN useradd --uid 1000 --create-home --shell /bin/bash guest \
|
||||||
|
&& find / -xdev -perm /6000 -type f -exec chmod a-s {} +
|
||||||
|
|
||||||
|
COPY etc/ /etc/
|
||||||
|
COPY --chmod=0755 bin/status /usr/local/bin/status
|
||||||
|
|
||||||
|
ENV PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
|
||||||
|
USER guest
|
||||||
|
WORKDIR /home/guest
|
||||||
|
CMD ["/bin/bash", "-l"]
|
||||||
Executable
+15
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [ -r /var/status/status ]; then
|
||||||
|
cat /var/status/status
|
||||||
|
else
|
||||||
|
echo "server status unavailable"
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -n "$SESSION_ID" ] && echo "session ${SESSION_ID}"
|
||||||
|
|
||||||
|
if [ -n "$SESSION_EXPIRES_AT" ]; then
|
||||||
|
remaining=$((SESSION_EXPIRES_AT - $(date +%s)))
|
||||||
|
[ "$remaining" -lt 0 ] && remaining=0
|
||||||
|
printf 'expires in %d:%02d\n' $((remaining / 60)) $((remaining % 60))
|
||||||
|
fi
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[ -z "$PS1" ] && return
|
||||||
|
|
||||||
|
shopt -s checkwinsize histappend
|
||||||
|
HISTSIZE=1000
|
||||||
|
HISTCONTROL=ignoredups
|
||||||
|
|
||||||
|
PS1='\[\e[1;32m\]guest@security-command\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '
|
||||||
|
|
||||||
|
alias ls='ls --color=auto'
|
||||||
|
alias ll='ls -lah --color=auto'
|
||||||
|
alias grep='grep --color=auto'
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
if [ -r /srv/www/motd ]; then
|
||||||
|
cat /srv/www/motd
|
||||||
|
else
|
||||||
|
printf '%s\n' "Welcome. The content lives in /srv/www, and 'status' shows your session."
|
||||||
|
fi
|
||||||
Reference in new issue
Block a user