Upgrades the static-policy scaffold to real kernel isolation. sandbox-container.sh runs the command in a locked-down container where the KERNEL neutralises escapes: --network=none (no egress), --read-only rootfs (no out-of-workspace writes), --pids-limit (fork bombs capped), --memory/--cpus, --cap-drop=ALL + no-new-privileges, and ONLY the workspace bind-mounted (host $HOME/.ssh unreachable). Wall-clock timeout goes through the portable tool-exec.sh (macOS has no coreutils `timeout`); a timed-out container is force-removed. sandbox-run.sh delegates here when CASAN_SANDBOX_MODE=container (default stays the static scaffold → phase2 C6 + baseline unchanged); falls back if Docker absent. phase-c6-sandbox-tests.sh: 6 checks (skip-aware) proving egress/host-read/ out-of-workspace-write fail INSIDE the sandbox while benign workspace writes land on the host — validated live against Docker (busybox). Baselines: run-casan4 35/35, adversarial 44/44, phase2 C6 29/29 unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
2.9 KiB
Bash
Executable File
69 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -uo pipefail
|
|
|
|
# CASAN Track C — TRUE runtime isolation via container (C6 / V22, production form).
|
|
#
|
|
# Upgrades the static-policy scaffold (sandbox-run.sh) to real kernel isolation:
|
|
# the command runs inside a locked-down container where the KERNEL — not a grep —
|
|
# neutralises escapes:
|
|
# --network=none → no egress at all
|
|
# --read-only → root filesystem is immutable (can't write outside workspace)
|
|
# --pids-limit → fork bombs are capped
|
|
# --memory/--cpus → resource abuse is bounded
|
|
# -v <ws>:/work:rw → ONLY the workspace is writable; host $HOME/.ssh is NOT mounted
|
|
# --cap-drop=ALL --security-opt=no-new-privileges → no privilege escalation
|
|
#
|
|
# Usage:
|
|
# sandbox-container.sh --workspace <dir> [--image busybox] [--timeout 20]
|
|
# [--memory 256m] [--pids 128] [--cpus 1] -- <command...>
|
|
# Exit: command's exit code · 124 timeout · 2 policy/setup error · 127 no docker.
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
IMAGE="${CASAN_SANDBOX_IMAGE:-busybox}"
|
|
WORKSPACE="$PWD"; TIMEOUT="${CASAN_SANDBOX_TIMEOUT:-20}"
|
|
MEMORY="${CASAN_SANDBOX_MEMORY:-256m}"; PIDS="${CASAN_SANDBOX_PIDS:-128}"; CPUS="${CASAN_SANDBOX_CPUS:-1}"
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
--workspace) WORKSPACE="${2:-}"; shift 2 ;;
|
|
--image) IMAGE="${2:-}"; shift 2 ;;
|
|
--timeout) TIMEOUT="${2:-}"; shift 2 ;;
|
|
--memory) MEMORY="${2:-}"; shift 2 ;;
|
|
--pids) PIDS="${2:-}"; shift 2 ;;
|
|
--cpus) CPUS="${2:-}"; shift 2 ;;
|
|
--) shift; break ;;
|
|
*) echo "sandbox-container: unknown arg $1" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
[[ "$#" -ge 1 ]] || { echo "Usage: sandbox-container.sh --workspace <dir> -- <command...>" >&2; exit 2; }
|
|
|
|
command -v docker >/dev/null 2>&1 || { echo "SANDBOX_CONTAINER_NO_DOCKER" >&2; exit 127; }
|
|
docker info >/dev/null 2>&1 || { echo "SANDBOX_CONTAINER_DOCKER_DOWN" >&2; exit 127; }
|
|
|
|
WS_ABS="$(cd "$WORKSPACE" 2>/dev/null && pwd)" || { echo "SANDBOX_CONTAINER_BAD_WORKSPACE" >&2; exit 2; }
|
|
|
|
# Join the command into a single shell string to run inside the container.
|
|
CMD="$*"
|
|
|
|
# Hardened container. --init reaps zombies; tmpfs gives a small writable /tmp
|
|
# without a writable rootfs. The wall-clock timeout goes through tool-exec.sh
|
|
# (portable: `timeout` if present, else a perl alarm — macOS has no coreutils
|
|
# `timeout`). Container name is tracked so a timed-out container is force-removed.
|
|
CID="casan-sbx-$$-${RANDOM}"
|
|
set +e
|
|
bash "$SCRIPT_DIR/tool-exec.sh" "$TIMEOUT" -- \
|
|
docker run --rm --init --name "$CID" \
|
|
--network=none --read-only \
|
|
--pids-limit="$PIDS" --memory="$MEMORY" --cpus="$CPUS" \
|
|
--cap-drop=ALL --security-opt=no-new-privileges \
|
|
--tmpfs /tmp:rw,size=16m \
|
|
-v "$WS_ABS":/work:rw -w /work \
|
|
"$IMAGE" sh -c "$CMD"
|
|
rc=$?
|
|
set -e
|
|
if [[ "$rc" -eq 124 ]]; then
|
|
docker rm -f "$CID" >/dev/null 2>&1 || true
|
|
echo "SANDBOX_CONTAINER_TIMEOUT after ${TIMEOUT}s" >&2
|
|
exit 124
|
|
fi
|
|
exit "$rc"
|