#!/bin/bash # shellcheck disable=SC2034,SC2016 DESCRIPTION="security: what an agent in the sandbox can't read, write, reach or see" IMAGE=installed TIER="nightly" # Each check is made twice. Outside the sandbox it must work, so the thing is # really there. Inside it must fail, while a marker shows that the sandbox did # start, so a wrapper that fails to start doesn't count as a pass. # # An agent is played by a fake one, first on PATH, that runs bash with the # wrapper's arguments. sandbox/agent finds it as the agent's real command and # runs it exactly as it would run opencode, Hermes or Cline Desktop: same # config, same srt, same proxy. # # Steps 4 to 6 of the security checks (network exposure, what root runs, the # install path) are still to come. AGENTS=(opencode hermes cline-desktop) FAKE_BIN="\$HOME/smoketest-agents" WRAPPERS="\$HOME/.local/share/coredesktop/sandbox/bin" # Where opencode and Hermes are started; Cline Desktop always starts in ~/Projects PROJECT="\$HOME/Projects/smoketest-project" # A listener on the VM's own LAN address, the way an Ollama server on another # machine would be, and one on loopback, the way one on this machine would be LAN_PORT=18080 LAN_PORT_OTHER=18081 LOOPBACK_PORT=18082 USER_SANDBOX_CONF="\$HOME/.config/coredesktop/sandbox/base.json" # The sandbox checks that failed. A failure doesn't stop the others, so one # run shows every gap; the scenario fails at the end if there were any. FAILED=() # check DESCRIPTION SCRIPT: like assert_vm, but records a failure and goes on check() { ( assert_vm "$1" "$2" ) || FAILED+=("$1") } # in_sandbox AGENT SCRIPT: a VM script that runs SCRIPT in AGENT's sandbox in_sandbox() { local dir="$PROJECT" [ "$1" = cline-desktop ] && dir="\$HOME/Projects" echo "cd $dir && PATH=$FAKE_BIN:\$PATH $WRAPPERS/$1 -c $(q "echo SANDBOX-STARTED; $2")" } # sandbox_runs AGENT DESCRIPTION SCRIPT: SCRIPT must work in AGENT's sandbox sandbox_runs() { check "$1: $2" "out=\$($(in_sandbox "$1" "$3 && echo SCRIPT-WORKED") 2>&1) printf '%s\n' \"\$out\" [[ \$out == *SANDBOX-STARTED* ]] || { echo \"$1's sandbox didn't start\"; exit 1; } [[ \$out == *SCRIPT-WORKED* ]]" } # sandbox_blocks AGENT DESCRIPTION SCRIPT: SCRIPT must fail in AGENT's sandbox sandbox_blocks() { check "$1: $2" "out=\$($(in_sandbox "$1" "$3 && echo SCRIPT-WORKED") 2>&1) printf '%s\n' \"\$out\" [[ \$out == *SANDBOX-STARTED* ]] || { echo \"$1's sandbox didn't start\"; exit 1; } [[ \$out != *SCRIPT-WORKED* ]]" } # blocked DESCRIPTION SCRIPT: SCRIPT works outside the sandbox and in no # agent's sandbox blocked() { local agent check "outside the sandbox: $1" "cd $PROJECT && $2" for agent in "${AGENTS[@]}"; do sandbox_blocks "$agent" "$1" "$2" done } # write_blocked DESCRIPTION PATH: the user may write PATH, and no agent's # write reaches it. The write itself may seem to work in the sandbox, e.g. # into the empty folder srt mounts over a hidden one, so each agent appends a # token and the check is that the real PATH doesn't have it. Outside, the # check only looks at permissions, so it changes nothing. write_blocked() { local agent token check "outside the sandbox: $1" "[ -w \"\$(dirname $2)\" ] && { [ ! -e $2 ] || [ -w $2 ]; }" for agent in "${AGENTS[@]}"; do token="smoketest-escape-$agent" check "$agent: $1" "out=\$($(in_sandbox "$agent" "mkdir -p \"\$(dirname $2)\"; echo $token >> $2") 2>&1) printf '%s\n' \"\$out\" [[ \$out == *SANDBOX-STARTED* ]] || { echo \"$agent's sandbox didn't start\"; exit 1; } ! grep -qs $token $2" done } # Secret files the sandbox must hide. Each gets a canary, so reading it can # succeed outside. SECRETS=( .ssh .gnupg .local/share/keyrings .config/Bitwarden .var/app/com.bitwarden.desktop .config/coredesktop .winboat ) setup_vm() { local secret dirs="" for secret in "${SECRETS[@]}"; do dirs+=" \"\$HOME\"/$(q "$secret")" done assert_vm "set up the fake agents, a project and canaries" "set -e mkdir -p $FAKE_BIN $PROJECT \$HOME/smoketest-other \$HOME/.local/bin \ \$HOME/.config/systemd/user \$HOME/.config/autostart \$(dirname $USER_SANDBOX_CONF) for agent in ${AGENTS[*]}; do printf '#!/bin/bash\nexec bash \"\$@\"\n' > $FAKE_BIN/\$agent chmod +x $FAKE_BIN/\$agent done git -C $PROJECT init -q for dir in $dirs; do mkdir -p \"\$dir\" chmod 700 \"\$dir\" echo canary > \"\$dir/smoketest-canary\" done # Hidden unless configure storage has made it; the user can't read it # either, only root [ -f /etc/coredesktop/smb ] || { sudo mkdir -p /etc/coredesktop echo 'password=canary' | sudo install -m 600 -o root -g root /dev/stdin /etc/coredesktop/smb }" assert_vm "start listeners on the LAN address and on loopback" "set -e ip=\$(ip -4 route get 1.1.1.1 | sed -n 's/.* src \\([0-9.]*\\).*/\\1/p') [ -n \"\$ip\" ] echo \"\$ip\" > \$HOME/smoketest-lan-ip # socat is there for srt; it answers each connection with this script printf '#!/bin/sh\nprintf \"HTTP/1.0 200 OK\\\\r\\\\n\\\\r\\\\nlistener\\\\n\"\n' > \$HOME/smoketest-respond chmod +x \$HOME/smoketest-respond for listen in \$ip:$LAN_PORT \$ip:$LAN_PORT_OTHER 127.0.0.1:$LOOPBACK_PORT; do setsid socat TCP-LISTEN:\${listen#*:},bind=\${listen%:*},fork,reuseaddr \ EXEC:\$HOME/smoketest-respond > /dev/null 2>&1 < /dev/null & done for _ in \$(seq 50); do curl -sf http://\$ip:$LAN_PORT_OTHER/ > /dev/null && curl -sf http://127.0.0.1:$LOOPBACK_PORT/ > /dev/null && exit 0 sleep 0.1 done exit 1" } scenario() { local agent secret lan="http://\$(cat \$HOME/smoketest-lan-ip)" setup_vm # What every agent must be able to do, or the rest proves nothing for agent in "${AGENTS[@]}"; do sandbox_runs "$agent" "writes in the folder it started in" 'echo x > ./smoketest-probe && rm ./smoketest-probe' sandbox_runs "$agent" "writes in /tmp" 'echo x > /tmp/smoketest-probe-$$ && rm /tmp/smoketest-probe-$$' sandbox_runs "$agent" "reaches an allowed domain through its proxy" 'curl -sf -o /dev/null -m 20 https://github.com/' done sandbox_runs opencode "writes its own state" 'touch ~/.local/share/opencode/smoketest-probe' sandbox_runs hermes "writes its own state" 'touch ~/.hermes/smoketest-probe' # --- Reading secrets --- for secret in "${SECRETS[@]}"; do blocked "reads ~/$secret" "cat ~/$(q "$secret")/smoketest-canary" done # Root-only: the user reaches it through sudo, which the sandbox must stop blocked "reads /etc/coredesktop/smb through sudo" 'sudo -n cat /etc/coredesktop/smb' # --- Writing outside the folder it started in --- write_blocked "writes in the home folder" '$HOME/smoketest-escaped' write_blocked "writes in another project" '$HOME/smoketest-other/escaped' write_blocked "writes ~/.bashrc" '$HOME/.bashrc' write_blocked "writes in ~/.local/bin, which is on PATH" '$HOME/.local/bin/smoketest-escaped' write_blocked "writes a systemd user unit" '$HOME/.config/systemd/user/smoketest.service' write_blocked "writes an autostart entry" '$HOME/.config/autostart/smoketest.desktop' write_blocked "changes CoreDesktop's sandbox config" '$HOME/.local/share/coredesktop/sandbox/config/base.json' write_blocked "changes the user's sandbox config" "$USER_SANDBOX_CONF" write_blocked "changes the settings srt was started with" '$XDG_RUNTIME_DIR/coredesktop-sandbox/smoketest' # In a project, where Cline Desktop may write, but run outside the sandbox # the next time the user commits write_blocked "writes a git hook in its project" "$PROJECT/.git/hooks/pre-commit" write_blocked "changes its project's git config" "$PROJECT/.git/config" # --- Network --- blocked "reaches a domain that isn't allowed" 'curl -sf -o /dev/null -m 20 https://example.com/' blocked "reaches an allowed domain without its proxy" 'curl -sf -o /dev/null -m 20 --noproxy "*" https://github.com/' blocked "reaches a LAN address that isn't allowed" "curl -sf -m 10 $lan:$LAN_PORT/" blocked "reaches a LAN address that isn't allowed, without its proxy" "curl -sf -m 10 --noproxy '*' $lan:$LAN_PORT/" # Its own loopback is its own; this machine's servers aren't on it blocked "reaches a server on this machine's loopback" "curl -sf -m 10 http://127.0.0.1:$LOOPBACK_PORT/" # The way configure ai-servers allows a server: that host and port, no other assert_vm "allow one LAN server, as configure ai-servers does" "set -e mkdir -p \$(dirname $USER_SANDBOX_CONF) # The write check above may have left it empty [ -s $USER_SANDBOX_CONF ] || echo '{}' > $USER_SANDBOX_CONF jq --arg s \"\$(cat \$HOME/smoketest-lan-ip):$LAN_PORT\" '.network.allowedDomains += [\$s]' \ $USER_SANDBOX_CONF > $USER_SANDBOX_CONF.new mv $USER_SANDBOX_CONF.new $USER_SANDBOX_CONF" for agent in "${AGENTS[@]}"; do sandbox_runs "$agent" "reaches the allowed LAN server" "curl -sSv --fail-with-body -m 10 $lan:$LAN_PORT/" done blocked "reaches another port on the allowed LAN server" "curl -sf -m 10 $lan:$LAN_PORT_OTHER/" # --- Processes and D-Bus --- blocked "sees other users' processes" '[ -n "$(ps -e -o user= | grep -vx "$USER")" ]' # The session's own processes, whose /proc/PID/environ the user can read blocked "sees GNOME Shell" 'pgrep -x gnome-shell' # GNOME and systemd --user run things outside the sandbox when asked # here, e.g. systemd-run --user blocked "talks to the D-Bus session bus" \ 'gdbus call --session --timeout 5 --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus --method org.freedesktop.DBus.ListNames' blocked "talks to systemd --user" 'systemctl --user show-environment > /dev/null' if [ ${#FAILED[@]} -gt 0 ]; then printf 'failed: %s\n' "${FAILED[@]}" fail "${#FAILED[@]} sandbox checks failed" fi }