shellcheck / shellcheck (push) Failing after 20s
60-security.sh now also checks the way in: federated.sh sends wget to install.sh on Gitea over HTTPS at every hop, plain HTTP goes to HTTPS without serving the script, and a browser goes to the Install page. For the [federated] repo, it checks that pacman.conf needs signatures on packages and the database, gets them from Gitea over HTTPS, and lists the repo after core and extra; that pacman trusts the pinned key; and that Gitea's repository.key is that key alone. Then it serves a package from a stand-in repo with [federated]'s settings and pacman's keyring: pacman refuses it unsigned, signed by a key it doesn't have or doesn't trust, or in a signed database but unsigned itself, and installs it when its key is trusted. It found nothing to fix. Each repo check was shown to fail with [federated] weakened, moved above core, or its key deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
543 lines
26 KiB
Bash
543 lines
26 KiB
Bash
#!/bin/bash
|
|
# shellcheck disable=SC2034,SC2016,SC2028
|
|
DESCRIPTION="security: the agent sandbox, 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) needs a second machine and is 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')"
|
|
}
|
|
|
|
# --- 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)"
|
|
}
|
|
|
|
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
|
|
install_path_checks
|
|
|
|
if [ ${#FAILED[@]} -gt 0 ]; then
|
|
printf 'failed: %s\n' "${FAILED[@]}"
|
|
fail "${#FAILED[@]} security checks failed"
|
|
fi
|
|
}
|