Compare commits
2
Commits
master
...
571f43457d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
571f43457d | ||
|
|
0737b28897 |
No files matched your search
@@ -0,0 +1,26 @@
|
||||
name: images
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [develop, master]
|
||||
tags: ['v*']
|
||||
|
||||
env:
|
||||
REGISTRY: git.security-command.org/securitycommand
|
||||
|
||||
jobs:
|
||||
sandbox:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Log in to the registry
|
||||
run: >
|
||||
echo "${{ secrets.REGISTRY_TOKEN }}"
|
||||
| docker login "${REGISTRY%%/*}" -u "${{ secrets.REGISTRY_USER }}" --password-stdin
|
||||
|
||||
- name: Build and push
|
||||
run: |
|
||||
image="$REGISTRY/homepage-sandbox"
|
||||
docker build -t "$image:${{ gitea.sha }}" -t "$image:${{ gitea.ref_name }}" sandbox/
|
||||
docker push --all-tags "$image"
|
||||
@@ -5,3 +5,4 @@
|
||||
*.md
|
||||
!README.md
|
||||
|
||||
/runtime/
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
# Deploys the terminal homepage on this host. Clone the repository into
|
||||
# /srv/security-command.org and run this script for the first setup and for
|
||||
# every update.
|
||||
#
|
||||
# Usage: sudo ./deploy.sh [--no-pull]
|
||||
# --no-pull deploy the checked out commit without pulling (allows local changes)
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$(readlink -f "$0")")"
|
||||
|
||||
pull=1
|
||||
for arg in "$@"; do
|
||||
case $arg in
|
||||
--no-pull) pull=0 ;;
|
||||
*) echo "unknown option: $arg" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "run as root" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Git refuses repositories owned by another user, so it runs as the owner.
|
||||
repo_owner=$(stat -c %U .)
|
||||
as_owner() { sudo -u "$repo_owner" "$@"; }
|
||||
|
||||
if [[ $pull == 1 ]]; then
|
||||
if [[ -n $(as_owner git status --porcelain) ]]; then
|
||||
echo "the working tree has local changes; commit them or use --no-pull" >&2
|
||||
exit 1
|
||||
fi
|
||||
as_owner git pull --ff-only
|
||||
fi
|
||||
|
||||
if [[ ! -f .env ]]; then
|
||||
as_owner cp deploy/env.example .env
|
||||
read -rp "Absolute path of the content directory: " content_dir
|
||||
as_owner sed -i "s|^CONTENT_DIR=.*|CONTENT_DIR=${content_dir}|" .env
|
||||
fi
|
||||
set -a
|
||||
source .env
|
||||
set +a
|
||||
|
||||
if [[ ! -d ${CONTENT_DIR:-} || ${CONTENT_DIR} != /* ]]; then
|
||||
echo "CONTENT_DIR in .env must be an absolute path to a directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
registry_host=${REGISTRY%%/*}
|
||||
if ! grep -qs "\"${registry_host}\"" ~/.docker/config.json; then
|
||||
echo "log in to the registry first: docker login ${registry_host} (token with read:package)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
deploy/host/install-sandbox-host.sh
|
||||
|
||||
sandbox_docker() { DOCKER_HOST=unix:///run/docker-sandbox/docker.sock docker "$@"; }
|
||||
|
||||
# Images are tagged with the commit they were built from. Right after a push,
|
||||
# CI may still be building them, so pulling is retried for a while.
|
||||
pull_image() {
|
||||
local engine=$1 image=$2 deadline=$((SECONDS + 900))
|
||||
until "$engine" pull --quiet "$image" >/dev/null; do
|
||||
if ((SECONDS >= deadline)); then
|
||||
echo "gave up waiting for ${image}" >&2
|
||||
return 1
|
||||
fi
|
||||
echo "waiting for CI to publish ${image}"
|
||||
sleep 20
|
||||
done
|
||||
}
|
||||
|
||||
commit=$(as_owner git rev-parse HEAD)
|
||||
sandbox_tag="${REGISTRY}/homepage-sandbox:${commit}"
|
||||
pull_image sandbox_docker "$sandbox_tag"
|
||||
sandbox_image=$(sandbox_docker image inspect --format '{{index .RepoDigests 0}}' "$sandbox_tag")
|
||||
|
||||
install -d -m 0755 runtime
|
||||
cat > runtime/deploy.env <<EOF
|
||||
SANDBOX_IMAGE=${sandbox_image}
|
||||
SANDBOX_GROUP_ID=$(getent group sandbox-spawner | cut -d: -f3)
|
||||
EOF
|
||||
|
||||
IMAGE=$sandbox_image deploy/host/verify-sandbox.sh
|
||||
|
||||
echo "deployed ${commit}"
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Absolute host path of the homepage content. It is mounted read only.
|
||||
CONTENT_DIR=
|
||||
|
||||
# Container registry of the SecurityCommand organization.
|
||||
REGISTRY=git.security-command.org/securitycommand
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
# Prepares the host for sandbox sessions: installs gVisor, creates the
|
||||
# spawner group and runs the dedicated sandbox Docker daemon.
|
||||
# Called by deploy.sh as root. Safe to run repeatedly: the sandbox daemon
|
||||
# only restarts when its configuration changed, so live sessions survive.
|
||||
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
|
||||
|
||||
changed=0
|
||||
install_if_changed() {
|
||||
if ! cmp -s "$1" "$2"; then
|
||||
install -D -m 0644 "$1" "$2"
|
||||
changed=1
|
||||
fi
|
||||
}
|
||||
|
||||
install_if_changed "$repo_root/deploy/docker-sandbox/daemon.json" /etc/docker-sandbox/daemon.json
|
||||
install_if_changed "$repo_root/deploy/systemd/sandbox.slice" /etc/systemd/system/sandbox.slice
|
||||
install_if_changed "$repo_root/deploy/systemd/docker-sandbox.service" /etc/systemd/system/docker-sandbox.service
|
||||
|
||||
if [[ $changed == 1 ]]; then
|
||||
systemctl daemon-reload
|
||||
fi
|
||||
systemctl start sandbox.slice
|
||||
systemctl enable --quiet docker-sandbox.service
|
||||
if [[ $changed == 1 ]]; then
|
||||
systemctl restart docker-sandbox.service
|
||||
else
|
||||
systemctl start docker-sandbox.service
|
||||
fi
|
||||
@@ -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