#!/bin/bash # shellcheck disable=SC2034,SC2016 DESCRIPTION="security: what an agent in the sandbox can't do, and what root runs" IMAGE=installed TIER="nightly" # Step 3 of the security checks: the agent sandbox. 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. # # Step 5, below the sandbox's: what root runs is root's alone, nothing root runs # is in a user's home, and a security key or fingerprint for sudo keeps the # password working. Steps 4 (network exposure, which needs a second machine) # and 6 (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 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" } # --- Step 5: what root runs, and root-only secrets --------------------------- # A user who has to type a password for sudo. tester's sudo needs none, and # then PAM's auth step, which is also where Arch's system-auth runs pam_env, # never runs. PAM_USER=pamtest PAM_PASSWORD=Smoketest-pw-1 # Files CoreDesktop installs that root runs or reads, on every installed machine ROOT_FILES=( /usr/local/lib/coredesktop/pacman-guard /etc/pacman.d/hooks/00-coredesktop-guard.hook /etc/NetworkManager/dispatcher.d/90-coredesktop-captive-portal /etc/ssh/sshd_config.d/10-coredesktop-keys-only.conf /etc/ssh/ssh_config.d/20-coredesktop-keepalive.conf /etc/systemd/system.conf.d/10-coredesktop-timeout.conf /etc/systemd/user.conf.d/10-coredesktop-timeout.conf /etc/systemd/user/app.slice.d/10-coredesktop-oomd.conf /etc/security/pam_env.conf /etc/security/faillock.conf /etc/ufw/after.rules /etc/ufw/user.rules /etc/ufw/user6.rules /etc/xdg/reflector/reflector.conf /etc/pacman.conf /etc/fstab /etc/avahi/services/ssh.service /etc/nsswitch.conf ) # And those configure security-key and configure fingerprint add PAM_FILES=( /usr/local/lib/coredesktop/lid-closed /etc/pam.d/sudo /etc/pam.d/polkit-1 /etc/u2f_mappings ) # VM-side helpers, put in front of the scripts that use them ROOT_LIB=' # root_only PATH: PATH, or the nearest folder above it that exists, and every # folder above that up to /, are owned by root and not writable by whoever runs # this. Both the path as named and, if a link leads elsewhere, where it leads. root_only() { local p="$1" real while [ ! -e "$p" ] && [ ! -L "$p" ] && [ "$p" != / ]; do p="$(dirname "$p")"; done root_only_chain "$p" || return 1 real="$(realpath -e "$p")" || { echo "$p: a link to nowhere"; return 1; } [ "$real" = "$p" ] || root_only_chain "$real" } root_only_chain() { local p="$1" while :; do if [ "$(stat -c %u "$p")" != 0 ] || { [ ! -L "$p" ] && [ -w "$p" ]; }; then echo "$p: owned by $(stat -c %U "$p"), mode $(stat -c %a "$p"), writable by $(id -un): $([ -w "$p" ] && echo yes || echo no)" return 1 fi [ "$p" = / ] && return 0 p="$(dirname "$p")" done } # path_root_only PATH-VALUE: every folder in a PATH is root_only path_root_only() { local dir status=0 IFS=: echo "PATH: $1" for dir in $1; do [ -n "$dir" ] || { echo "PATH has an empty entry, which means the current folder"; status=1; continue; } case "$dir" in /*) ;; *) echo "PATH has a relative entry: $dir"; status=1; continue ;; esac root_only "$dir" || status=1 done return $status } ' # as_pam_user SCRIPT: a VM script that runs SCRIPT as PAM_USER as_pam_user() { echo "sudo -u $PAM_USER -i bash -c $(q "$1")" } # pam_sudo PASSWORD: a VM script in which PAM_USER runs sudo true with PASSWORD pam_sudo() { as_pam_user "echo $(q "$1") | timeout 60 sudo -S -k true" } setup_root_checks() { assert_vm "make a user who types a password for sudo" "set -e id $PAM_USER > /dev/null 2>&1 || sudo useradd -m $PAM_USER echo $PAM_USER:$PAM_PASSWORD | sudo chpasswd echo '$PAM_USER ALL=(ALL) ALL' | sudo install -m 440 /dev/stdin /etc/sudoers.d/50-smoketest-$PAM_USER sudo visudo -c > /dev/null" } # Turn on configure security-key and configure fingerprint the way they do it, # with no key or reader: PAM_USER has a key registered, but it isn't plugged in use_security_key_and_fingerprint() { assert_vm "keep the PAM files as they were" "set -e sudo install -d -m 700 /root/smoketest-pam sudo cp /etc/pam.d/sudo /root/smoketest-pam/sudo if [ -f /etc/pam.d/polkit-1 ]; then sudo cp /etc/pam.d/polkit-1 /root/smoketest-pam/polkit-1 else sudo cp /usr/lib/pam.d/polkit-1 /root/smoketest-pam/polkit-1; fi" assert_vm "turn on the security key and fingerprint for sudo and polkit" "set -e export COREDESKTOP=\$HOME/.local/share/coredesktop sudo pacman -S --needed --noconfirm pam-u2f fprintd > /dev/null source \$COREDESKTOP/configure.d/70-security-key.sh use_keys echo '$PAM_USER:smoketest-handle,smoketest-key,es256,+presence' | sudo tee -a \$U2F_MAPPINGS > /dev/null sudo chmod 644 \$U2F_MAPPINGS source \$COREDESKTOP/configure.d/71-fingerprint.sh use_fingerprint grep -q pam_u2f.so /etc/pam.d/sudo && grep -q pam_fprintd.so /etc/pam.d/polkit-1" } root_checks() { local file service setup_root_checks # --- Files root runs --- for file in "${ROOT_FILES[@]}"; do check "root: $file and the folders above it are root's alone" "$ROOT_LIB [ -e $file ] || { echo 'missing: $file'; exit 1; } root_only $file" done # /etc/coredesktop/smb isn't here: only configure storage makes it, which # needs an SMB server. It writes it with install -m 600 -o root. # Anything in the system's folders the user can write, CoreDesktop's or not check "root: nothing in /etc, /usr, /boot or /opt is the user's to write" " found=\$(find /etc /usr /boot /opt -xdev \\( -writable -o -user \$(id -u) \\) ! -type l -print 2> /dev/null | head -n 20) [ -z \"\$found\" ] || { echo \"\$found\"; exit 1; }" # --- Nothing root runs is in a user's home --- # System units, pacman hooks, NetworkManager's scripts, PAM, sudo, udev and # polkit rules: what root runs on its own or for anyone. (fstab's davfs line # and pam_env's PATH name home folders for the user's own use; PATH is # checked below.) check "root: nothing root runs names a path in a home folder" " found=\$(sudo grep -rnIE '/home/|~/|\\\$HOME|@\\{HOME\\}' /etc/systemd/system /etc/pacman.d/hooks \ /etc/NetworkManager/dispatcher.d /etc/pam.d /etc/udev/rules.d /etc/sudoers /etc/sudoers.d \ /etc/polkit-1/rules.d /usr/local 2> /dev/null) [ -z \"\$found\" ] || { echo \"\$found\"; exit 1; }" # pam_env.conf gives logins @{HOME}/.local/bin and mise's shims. Root must # get neither, whoever runs sudo, or the user could put a sudo command there. check "root: sudo's PATH (tester, no password) has only root's folders" "$ROOT_LIB path_root_only \"\$(sudo sh -c 'echo \$PATH')\"" check "root: sudo -i's PATH (tester) has only root's folders" "$ROOT_LIB path_root_only \"\$(sudo -i sh -c 'echo \$PATH')\"" check "root: sudo's PATH ($PAM_USER, with a password) has only root's folders" "$ROOT_LIB path=\$($(as_pam_user "echo $(q "$PAM_PASSWORD") | sudo -S -k sh -c 'echo \$PATH' 2> /dev/null")) path_root_only \"\$path\"" check "root: systemd's PATH for system services has only root's folders" "$ROOT_LIB path_root_only \"\$(systemctl show-environment | sed -n 's/^PATH=//p')\"" # --- PAM: a security key and a fingerprint add a way in, and take none away --- check "pam: before: $PAM_USER's password works for sudo" "$(pam_sudo "$PAM_PASSWORD")" check "pam: before: a wrong password doesn't" "! $(pam_sudo wrong-password)" use_security_key_and_fingerprint for file in "${PAM_FILES[@]}"; do check "root: $file and the folders above it are root's alone" "$ROOT_LIB [ -e $file ] || { echo 'missing: $file'; exit 1; } root_only $file" done for service in sudo polkit-1; do check "pam: $service's stack only gained the key and fingerprint lines" " diff <(sudo cat /root/smoketest-pam/$service) \ <(grep -vE 'pam_u2f\\.so|pam_fprintd\\.so|/usr/local/lib/coredesktop/lid-closed' /etc/pam.d/$service)" done assert_vm "reset $PAM_USER's failed logins" "sudo faillock --user $PAM_USER --reset" # Without the key or a finger, it falls through to the password, as before check "pam: after: $PAM_USER's password still works for sudo" "$(pam_sudo "$PAM_PASSWORD")" check "pam: after: a wrong password still doesn't" "! $(pam_sudo wrong-password)" check "pam: after: no password at all doesn't" "! $(as_pam_user 'timeout 60 sudo -S -k true < /dev/null')" } sandbox_checks() { 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' } scenario() { sandbox_checks root_checks if [ ${#FAILED[@]} -gt 0 ]; then printf 'failed: %s\n' "${FAILED[@]}" fail "${#FAILED[@]} security checks failed" fi }