shellcheck / shellcheck (push) Failing after 19s
60-security.sh now checks network exposure (step 4), from two other machines made of network namespaces in the VM: one on private addresses, like a neighbour on the same Wi-Fi, and one on public-looking ones. From each, over IPv4 and IPv6: only SSH and LocalSend are open, a container's published port can't be reached, containers still reach both machines, and SSH takes keys only. It also checks ufw's rules and sshd -T. It found that ufw-docker's rules let any private address (10/8, 172.16/12, 192.168/16) reach published ports, so anyone on a café's or hotel's Wi-Fi could. system/docker-firewall.sh lets only Docker's own bridges (docker0, br-+) past instead; firewall.sh runs it on a new install and migration 1794878760 on existing machines. To let other machines reach a container's port: sudo ufw-docker allow <container> <port>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
719 lines
35 KiB
Bash
719 lines
35 KiB
Bash
#!/bin/bash
|
|
# shellcheck disable=SC2034,SC2016,SC2028
|
|
DESCRIPTION="security: the agent sandbox, network exposure, what root runs, and the install path"
|
|
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. Step 6, last: federated.sh hands out install.sh over HTTPS
|
|
# only, and pacman takes [federated]'s packages only when signed by a key it
|
|
# trusts. Step 4, network exposure, is seen from two other machines: network
|
|
# namespaces in the VM, one on private addresses and one on public-looking ones.
|
|
|
|
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')"
|
|
}
|
|
|
|
# --- Step 6: the install path ------------------------------------------------
|
|
|
|
# What README.md tells people to run is wget -qO- https://federated.sh | bash
|
|
WGET_UA="Wget/1.25.0"
|
|
BROWSER_UA="Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0"
|
|
INSTALL_SH_URL="https://gitea.federated.computer/Federated_Computer/coredesktop/raw/branch/main/install.sh"
|
|
# Stand-in [federated] repos, served from files
|
|
FAKE_REPOS="$SMOKETEST_VM_DIR/federated-repos"
|
|
|
|
# The pinned key and the repo's URL, from the installed utils.sh
|
|
FEDERATED_VARS='
|
|
utils=$HOME/.local/share/coredesktop/utils.sh
|
|
key=$(sed -n "s/^FEDERATED_REPO_KEY=\"\(.*\)\"$/\1/p" "$utils")
|
|
url=$(sed -n "s/^FEDERATED_REPO_URL=\"\(.*\)\"$/\1/p" "$utils")
|
|
[ -n "$key" ] && [ -n "$url" ] || { echo "no FEDERATED_REPO_KEY or URL in $utils"; exit 1; }
|
|
'
|
|
|
|
# Build a package and the stand-in repos:
|
|
# unsigned nothing signed
|
|
# signed package and database signed by a throwaway key
|
|
# unsigned-pkg database signed by that key, package not
|
|
# The throwaway key's public half is in $FAKE_REPOS/key.asc
|
|
setup_fake_repos() {
|
|
assert_vm "build a package and stand-in [federated] repos" "set -e
|
|
rm -rf $FAKE_REPOS && mkdir -p $FAKE_REPOS/build $FAKE_REPOS/gnupg
|
|
chmod 700 $FAKE_REPOS/gnupg
|
|
export GNUPGHOME=$FAKE_REPOS/gnupg
|
|
gpg --batch --quiet --passphrase '' --quick-gen-key 'Smoketest Not Federated <smoketest@localhost>' ed25519 sign never
|
|
fpr=\$(gpg --with-colons --list-keys | awk -F: '\$1 == \"fpr\" { print \$10; exit }')
|
|
echo \"\$fpr\" > $FAKE_REPOS/key.fpr
|
|
gpg --armor --export \"\$fpr\" > $FAKE_REPOS/key.asc
|
|
cd $FAKE_REPOS/build
|
|
printf '%s\n' 'pkgname=smoketest-fake' 'pkgver=1' 'pkgrel=1' 'arch=(any)' 'options=(!debug)' \
|
|
'package() { install -Dm644 /dev/null \"\$pkgdir/usr/share/smoketest-fake\"; }' > PKGBUILD
|
|
makepkg --nodeps --force > /dev/null 2>&1
|
|
pkg=\$(ls smoketest-fake-1-1-any.pkg.tar.*)
|
|
for repo in unsigned signed unsigned-pkg; do
|
|
mkdir -p $FAKE_REPOS/\$repo && cp \"\$pkg\" $FAKE_REPOS/\$repo/
|
|
done
|
|
gpg --batch --quiet --detach-sign --no-armor -u \"\$fpr\" -o $FAKE_REPOS/signed/\$pkg.sig \"\$pkg\"
|
|
repo-add -q $FAKE_REPOS/unsigned/federated.db.tar.gz $FAKE_REPOS/unsigned/\$pkg
|
|
repo-add -q --sign --key \"\$fpr\" $FAKE_REPOS/signed/federated.db.tar.gz $FAKE_REPOS/signed/\$pkg
|
|
repo-add -q --sign --key \"\$fpr\" $FAKE_REPOS/unsigned-pkg/federated.db.tar.gz $FAKE_REPOS/unsigned-pkg/\$pkg
|
|
chmod -R a+rX $FAKE_REPOS
|
|
chmod 700 $FAKE_REPOS/gnupg
|
|
# Keyrings for the control and for a key that's there but not trusted: copies
|
|
# of pacman's, so the real one isn't changed
|
|
for ring in trusted untrusted; do
|
|
sudo rm -rf $FAKE_REPOS/keyring-\$ring
|
|
sudo cp -a /etc/pacman.d/gnupg $FAKE_REPOS/keyring-\$ring
|
|
sudo pacman-key --gpgdir $FAKE_REPOS/keyring-\$ring --add $FAKE_REPOS/key.asc > /dev/null 2>&1
|
|
done
|
|
sudo pacman-key --gpgdir $FAKE_REPOS/keyring-trusted --lsign-key \"\$fpr\" > /dev/null 2>&1"
|
|
}
|
|
|
|
# install_from REPO [KEYRING]: a VM script that installs smoketest-fake from
|
|
# the stand-in REPO into a throwaway root, with [federated]'s settings from
|
|
# /etc/pacman.conf and pacman's keyring (or KEYRING, a copy of it). Prints
|
|
# pacman's output; succeeds only if the package was installed.
|
|
install_from() {
|
|
local gpgdir=""
|
|
[ -n "${2:-}" ] && gpgdir="--gpgdir $FAKE_REPOS/keyring-$2"
|
|
echo "set -o pipefail
|
|
root=\$(mktemp -d)
|
|
sudo mkdir -p \$root/var/lib/pacman \$root/cache \$root/hooks
|
|
# [options] and [federated] as they are, but served from the stand-in.
|
|
# DownloadUser would download as alpm, which can't write the throwaway
|
|
# cache; that's the download sandbox, not signature checking.
|
|
awk '/^\\[/ { section = \$0 } section == \"[options]\" || section == \"[federated]\"' /etc/pacman.conf \
|
|
| sed -e 's#^Server *=.*#Server = file://$FAKE_REPOS/$1#' -e '/^DownloadUser/d' > \$root.conf
|
|
grep -q '^\\[federated\\]' \$root.conf || { echo 'no [federated] in /etc/pacman.conf'; exit 1; }
|
|
sudo timeout 120 pacman --config \$root.conf --root \$root --dbpath \$root/var/lib/pacman \
|
|
--cachedir \$root/cache --hookdir \$root/hooks --disable-sandbox $gpgdir \
|
|
--noconfirm -Sy smoketest-fake 2>&1
|
|
[ -e \$root/usr/share/smoketest-fake ]"
|
|
}
|
|
|
|
# refused REPO [KEYRING]: installing from REPO fails, and for a signature's
|
|
# sake: a bad one, or a missing .sig file, which Required asks for
|
|
refused() {
|
|
echo "out=\$($(install_from "$@"))
|
|
status=\$?
|
|
printf '%s\n' \"\$out\"
|
|
[ \$status -ne 0 ] || { echo 'pacman installed it'; exit 1; }
|
|
grep -qiE 'signature|\\.sig|PGP|key' <<< \"\$out\" || { echo 'pacman failed, but not over a signature'; exit 1; }"
|
|
}
|
|
|
|
install_path_checks() {
|
|
# --- federated.sh ---
|
|
check "install: federated.sh sends wget to install.sh on Gitea, over HTTPS" "
|
|
read -r code location < <(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' -A $(q "$WGET_UA") https://federated.sh/)
|
|
echo \"\$code \$location\"
|
|
[[ \$code == 30[1278] ]] && [ \"\$location\" = $(q "$INSTALL_SH_URL") ]"
|
|
check "install: every hop from federated.sh to the script is HTTPS, and it's a bash script" "
|
|
first=\$(curl --proto =https --proto-redir =https -fsSL -A $(q "$WGET_UA") https://federated.sh/ 2> /dev/null | head -n 1)
|
|
echo \"\$first\"
|
|
[[ \$first == '#!/bin/bash'* ]]"
|
|
check "install: wget itself gets the script" "
|
|
command -v wget > /dev/null || { echo 'no wget in the VM'; exit 1; }
|
|
first=\$(wget -qO- https://federated.sh | head -n 1)
|
|
echo \"\$first\"
|
|
[[ \$first == '#!/bin/bash'* ]]"
|
|
check "install: plain HTTP to federated.sh goes to HTTPS and doesn't serve the script" "
|
|
body=\$(mktemp)
|
|
read -r code location < <(curl -sS -o \$body -w '%{http_code} %{redirect_url}' -A $(q "$WGET_UA") http://federated.sh/)
|
|
echo \"\$code \$location\"
|
|
[[ \$code == 30[1278] && \$location == https://* ]] && ! grep -q '^#!' \$body"
|
|
check "install: a browser goes to the Install page, over HTTPS" "
|
|
read -r code location < <(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' -A $(q "$BROWSER_UA") https://federated.sh/)
|
|
echo \"\$code \$location\"
|
|
[[ \$code == 30[1278] && \$location == https://coredesktop.computer/* ]]"
|
|
|
|
# --- The [federated] repo ---
|
|
check "install: [federated] needs signatures on packages and database, from Gitea over HTTPS" "$FEDERATED_VARS
|
|
section=\$(awk '/^\\[/ { f = (\$0 == \"[federated]\") ; next } f' /etc/pacman.conf | grep -v '^[[:space:]]*\\(#\\|\$\\)')
|
|
printf '%s\n' \"\$section\"
|
|
[ \"\$section\" = \"\$(printf 'SigLevel = Required DatabaseRequired\nServer = %s/\$repo/\$arch' \"\$url\")\" ] \
|
|
&& [[ \$url == https://* ]]"
|
|
check "install: [federated] comes after core and extra, so it can't stand in for Arch's packages" "
|
|
line() { grep -n \"^\\[\$1\\]\" /etc/pacman.conf | cut -d: -f1; }
|
|
f=\$(line federated); c=\$(line core); e=\$(line extra)
|
|
echo \"core \$c, extra \$e, federated \$f\"
|
|
[ -n \"\$f\" ] && [ \"\$f\" -gt \"\$c\" ] && [ \"\$f\" -gt \"\$e\" ]"
|
|
check "install: pacman trusts the pinned key" "$FEDERATED_VARS
|
|
validity=\$(sudo gpg --homedir /etc/pacman.d/gnupg --with-colons --list-keys \"\$key\" 2> /dev/null | awk -F: '\$1 == \"pub\" { print \$2 }')
|
|
echo \"\$key: validity \$validity\"
|
|
[[ \$validity == [fu] ]]"
|
|
check "install: Gitea's repository.key is the pinned key and no other" "$FEDERATED_VARS
|
|
home=\$(mktemp -d)
|
|
keys=\$(curl -fsSL \"\$url/repository.key\" | gpg --homedir \$home --with-colons --show-keys 2> /dev/null \
|
|
| awk -F: '\$1 == \"pub\" { getline; print \$10 }')
|
|
echo \"\$keys\"
|
|
[ \"\$keys\" = \"\$key\" ]"
|
|
|
|
setup_fake_repos
|
|
check "install: control: pacman installs from a repo signed by a key it trusts" "$(install_from signed trusted)"
|
|
check "install: pacman refuses a repo with nothing signed" "$(refused unsigned)"
|
|
check "install: pacman refuses a repo signed by a key it doesn't have" "$(refused signed)"
|
|
check "install: pacman refuses a repo signed by a key it has but doesn't trust" "$(refused signed untrusted)"
|
|
check "install: pacman refuses an unsigned package from a signed repo" "$(refused unsigned-pkg trusted)"
|
|
}
|
|
|
|
# --- Step 4: network exposure ------------------------------------------------
|
|
|
|
# Two other machines, each a network namespace joined to this one by a veth
|
|
# pair. What they send arrives on an ordinary interface, not loopback, so ufw
|
|
# and Docker treat it as they would a machine on the network. One has private
|
|
# addresses, like a neighbour on the same Wi-Fi; the other public-looking ones
|
|
# (TEST-NET-2 and 2001:db8::/32, the documentation ranges), like the internet.
|
|
# name this machine's side the other machine
|
|
OUTSIDE=(
|
|
"lan 192.168.77.1 fd77:1::1 192.168.77.2 fd77:1::2"
|
|
"wan 198.51.100.1 2001:db8:77::1 198.51.100.2 2001:db8:77::2"
|
|
)
|
|
# What system/firewall.sh lets in; tailscale0 isn't here (it needs a Tailscale
|
|
# login, so it's not checked)
|
|
OPEN_TCP="22 53317"
|
|
# Listeners started for the checks: one on every address, and a container's
|
|
TEST_TCP=18090
|
|
TEST_UDP=18091
|
|
LOCALSEND=53317
|
|
DOCKER_PORT=18095
|
|
# A server on each other machine, for containers to reach
|
|
OUTSIDE_PORT=18096
|
|
|
|
# udp_to ADDRESS PORT: socat's address for a UDP port on ADDRESS
|
|
udp_to() {
|
|
if [[ $1 == *:* ]]; then echo "UDP6:[$1]:$2"; else echo "UDP4:$1:$2"; fi
|
|
}
|
|
|
|
# url_host ADDRESS: ADDRESS as it goes in a URL
|
|
url_host() {
|
|
if [[ $1 == *:* ]]; then echo "[$1]"; else echo "$1"; fi
|
|
}
|
|
|
|
# outside NAME SCRIPT: a VM script that runs SCRIPT as root on machine NAME
|
|
outside() {
|
|
echo "sudo ip netns exec smoketest-$1 bash -c $(q "$2")"
|
|
}
|
|
|
|
setup_outside() {
|
|
local machine name here here6 there there6
|
|
for machine in "${OUTSIDE[@]}"; do
|
|
read -r name here here6 there there6 <<< "$machine"
|
|
assert_vm "make the other machine '$name' ($there, $there6)" "set -e
|
|
sudo ip netns add smoketest-$name
|
|
sudo ip link add sm-$name type veth peer name sm-$name-out
|
|
sudo ip link set sm-$name-out netns smoketest-$name
|
|
sudo ip addr add $here/24 dev sm-$name
|
|
sudo ip -6 addr add $here6/64 dev sm-$name nodad
|
|
sudo ip link set sm-$name up
|
|
sudo ip netns exec smoketest-$name sh -c '
|
|
ip link set lo up
|
|
ip addr add $there/24 dev sm-$name-out
|
|
ip -6 addr add $there6/64 dev sm-$name-out nodad
|
|
ip link set sm-$name-out up'
|
|
sudo ip netns exec smoketest-$name ping -c 1 -W 5 $here > /dev/null"
|
|
done
|
|
|
|
assert_vm "install nmap" "sudo pacman -S --needed --noconfirm nmap > /dev/null"
|
|
assert_vm "start test listeners on every address, TCP $TEST_TCP and UDP $TEST_UDP and $LOCALSEND" "set -e
|
|
printf '#!/bin/sh\nprintf \"HTTP/1.0 200 OK\\\\r\\\\n\\\\r\\\\nlistener\\\\n\"\n' > \$HOME/smoketest-respond
|
|
chmod +x \$HOME/smoketest-respond
|
|
# The other machines run it as root, from here
|
|
cp \$HOME/smoketest-respond $SMOKETEST_VM_DIR/
|
|
setsid socat TCP6-LISTEN:$TEST_TCP,ipv6only=0,fork,reuseaddr EXEC:\$HOME/smoketest-respond > /dev/null 2>&1 < /dev/null &
|
|
for port in $TEST_UDP $LOCALSEND; do
|
|
ss -Hlun \"sport = :\$port\" | grep -q . && { echo \"UDP \$port is taken\"; exit 1; }
|
|
# One packet each, then out after a second idle: socat with fork
|
|
# answered only the first sender
|
|
setsid bash -c \"while :; do socat -T 1 UDP6-RECVFROM:\$port,ipv6only=0,reuseaddr EXEC:cat; done\" \
|
|
> /dev/null 2>&1 < /dev/null &
|
|
done
|
|
sleep 1
|
|
curl -sf http://127.0.0.1:$TEST_TCP/ > /dev/null
|
|
[ \"\$(echo ping | socat -t 3 - UDP4:127.0.0.1:$TEST_UDP)\" = ping ]
|
|
sleep 2
|
|
[ \"\$(echo ping | socat -t 3 - UDP6:[::1]:$TEST_UDP)\" = ping ]"
|
|
for machine in "${OUTSIDE[@]}"; do
|
|
read -r name _ <<< "$machine"
|
|
assert_vm "start a server on '$name', port $OUTSIDE_PORT" "
|
|
$(outside "$name" "setsid socat TCP-LISTEN:$OUTSIDE_PORT,fork,reuseaddr EXEC:$SMOKETEST_VM_DIR/smoketest-respond > /dev/null 2>&1 < /dev/null &")
|
|
sleep 1"
|
|
done
|
|
assert_vm "make a Docker network of its own (a br- bridge)" "sudo docker network create smoketest-net > /dev/null"
|
|
assert_vm "start a container that publishes port $DOCKER_PORT" "set -e
|
|
sudo docker run -d --rm --name smoketest-web -p $DOCKER_PORT:8080 busybox httpd -f -p 8080 > /dev/null
|
|
for _ in \$(seq 50); do
|
|
[ \"\$(curl -s -o /dev/null -w '%{http_code}' -m 2 http://127.0.0.1:$DOCKER_PORT/)\" != 000 ] && exit 0
|
|
sleep 0.2
|
|
done
|
|
exit 1"
|
|
}
|
|
|
|
network_checks() {
|
|
local machine name here here6 there there6 address
|
|
|
|
setup_outside
|
|
|
|
# --- The firewall's own rules ---
|
|
check "net: ufw is on, denies incoming by default, and allows only SSH, mDNS, LocalSend and Tailscale" "
|
|
status=\$(sudo ufw status verbose)
|
|
printf '%s\n' \"\$status\"
|
|
grep -qx 'Status: active' <<< \"\$status\" && grep -q '^Default: deny (incoming)' <<< \"\$status\" || exit 1
|
|
added=\$(sudo ufw show added | grep '^ufw ' | sort)
|
|
expected=\$(printf '%s\n' \"ufw allow 22/tcp comment 'SSH'\" \"ufw allow 5353/udp comment 'mDNS'\" \
|
|
\"ufw allow 53317 comment 'LocalSend'\" \"ufw allow in on tailscale0 comment 'Tailscale'\" | sort)
|
|
printf 'rules:\n%s\n' \"\$added\"
|
|
[ \"\$added\" = \"\$expected\" ]"
|
|
|
|
for machine in "${OUTSIDE[@]}"; do
|
|
read -r name here here6 there there6 <<< "$machine"
|
|
|
|
# --- What's open, seen from the other machine ---
|
|
for address in "$here" "$here6"; do
|
|
check "net: from $name, the only TCP ports open on $address are $OPEN_TCP" "
|
|
out=\$($(outside "$name" "nmap -n -Pn -sS -p- --min-rate 5000 --max-retries 1 \
|
|
$([[ $address == *:* ]] && echo -6) -oG - $address"))
|
|
open=\$(grep -o '[0-9]*/open/tcp' <<< \"\$out\" | cut -d/ -f1 | sort -n | tr '\n' ' ')
|
|
echo \"open: \$open\"
|
|
for port in \$open; do
|
|
case ' $OPEN_TCP ' in *\" \$port \"*) ;; *) echo \"\$port shouldn't be open\"; bad=1 ;; esac
|
|
done
|
|
# nmap saw the machine at all: SSH answered
|
|
[[ \" \$open \" == *' 22 '* ]] && [ -z \"\${bad:-}\" ]"
|
|
check "net: from $name, UDP $TEST_UDP on $address doesn't answer" "
|
|
reply=\$($(outside "$name" "echo ping | socat -t 3 - $(udp_to "$address" $TEST_UDP)"))
|
|
[ \"\$reply\" != ping ]"
|
|
check "net: from $name, UDP $LOCALSEND (LocalSend's) on $address answers" "
|
|
reply=\$($(outside "$name" "echo ping | socat -t 3 - $(udp_to "$address" $LOCALSEND)"))
|
|
[ \"\$reply\" = ping ]"
|
|
# ufw-docker's job: Docker's own rules publish ports around ufw
|
|
check "net: from $name, the container's port $DOCKER_PORT on $address can't be reached" "
|
|
code=\$($(outside "$name" "curl -s -o /dev/null -w '%{http_code}' -m 5 http://$(url_host "$address"):$DOCKER_PORT/"))
|
|
echo \"HTTP \$code\"
|
|
[ \"\$code\" = 000 ] || { sudo sed -n '/BEGIN UFW AND DOCKER/,/END UFW AND DOCKER/p' /etc/ufw/after.rules; exit 1; }"
|
|
done
|
|
|
|
# Containers still reach other machines, from Docker's default bridge and
|
|
# from a network of their own
|
|
for network in bridge smoketest-net; do
|
|
check "net: a container on Docker's $network network reaches $name ($there)" "
|
|
sudo docker run --rm --network $network busybox wget -q -T 10 -O - http://$there:$OUTSIDE_PORT/ | grep -qx listener"
|
|
done
|
|
|
|
# --- SSH: keys only ---
|
|
check "net: from $name, SSH offers only public keys" "
|
|
out=\$($(outside "$name" "ssh -o BatchMode=yes -o PubkeyAuthentication=no -o StrictHostKeyChecking=no \
|
|
-o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 tester@$here true 2>&1"))
|
|
echo \"\$out\"
|
|
[[ \$out == *'Permission denied (publickey).'* ]]"
|
|
check "net: from $name, tester's real password doesn't get in over SSH" "
|
|
out=\$($(outside "$name" "printf '#!/bin/sh\necho tester\n' > /tmp/smoketest-askpass; chmod +x /tmp/smoketest-askpass
|
|
SSH_ASKPASS=/tmp/smoketest-askpass SSH_ASKPASS_REQUIRE=force DISPLAY=none setsid -w ssh \
|
|
-o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive \
|
|
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 \
|
|
-o NumberOfPasswordPrompts=1 tester@$here 'echo LOGGED-IN' 2>&1 < /dev/null"))
|
|
echo \"\$out\"
|
|
[[ \$out != *LOGGED-IN* && \$out == *'Permission denied'* ]]"
|
|
done
|
|
# The way to let other machines reach a container, as docker-firewall.sh says
|
|
read -r name here _ <<< "${OUTSIDE[0]}"
|
|
check "net: after ufw-docker allow, $name reaches the container's port" "
|
|
sudo ufw-docker allow smoketest-web 8080/tcp > /dev/null
|
|
code=\$($(outside "$name" "curl -s -o /dev/null -w '%{http_code}' -m 5 http://$here:$DOCKER_PORT/"))
|
|
echo \"HTTP \$code\"
|
|
[ \"\$code\" != 000 ]"
|
|
check "net: sshd allows no passwords, no keyboard-interactive and no root login" "
|
|
sudo sshd -T > /tmp/smoketest-sshd-T 2>&1 || { cat /tmp/smoketest-sshd-T; exit 1; }
|
|
# Newer OpenSSH prints the names in CamelCase
|
|
conf=\$(grep -iE '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) ' /tmp/smoketest-sshd-T \
|
|
| tr '[:upper:]' '[:lower:]' | sort)
|
|
echo \"\$conf\"
|
|
[ \"\$conf\" = \"\$(printf '%s\n' 'kbdinteractiveauthentication no' 'passwordauthentication no' 'permitrootlogin no')\" ]"
|
|
}
|
|
|
|
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
|
|
network_checks
|
|
root_checks
|
|
install_path_checks
|
|
|
|
if [ ${#FAILED[@]} -gt 0 ]; then
|
|
printf 'failed: %s\n' "${FAILED[@]}"
|
|
fail "${#FAILED[@]} security checks failed"
|
|
fi
|
|
}
|