Test that what root runs is root's alone

60-security.sh now also checks, in the smoketest VM, that each file
CoreDesktop installs for root (the pacman guard and hook, the captive
portal script, sshd, ufw and systemd settings, and after configure
security-key and fingerprint, the PAM files, the lid check and the key
list), and every folder above it, is root's and not writable by the
user; that nothing in /etc, /usr, /boot or /opt is the user's to write;
and that nothing root runs names a path in a home folder.

pam_env gives logins ~/.local/bin on PATH, so it checks that root's
PATH under sudo, sudo -i and systemd has only root's folders, also for
a user who types a password: tester's sudo needs none, and then PAM's
auth step, where Arch runs pam_env, never runs. With a security key
registered but not plugged in and fingerprints turned on, the password
still works for sudo and a wrong or missing one doesn't.

It found nothing to fix. Each check was shown to fail on a machine
broken the way it looks for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 17:07:44 -07:00
co-authored by Claude Opus 5.5
parent e991264ddc
commit d5925fa551
2 changed files with 216 additions and 34 deletions
+189 -7
View File
@@ -1,10 +1,10 @@
#!/bin/bash
# shellcheck disable=SC2034,SC2016
DESCRIPTION="security: what an agent in the sandbox can't read, write, reach or see"
DESCRIPTION="security: what an agent in the sandbox can't do, and what root runs"
IMAGE=installed
TIER="nightly"
# Each check is made twice. Outside the sandbox it must work, so the thing is
# 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.
#
@@ -13,8 +13,10 @@ TIER="nightly"
# 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.
# 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"
@@ -28,7 +30,7 @@ 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
# 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=()
@@ -142,7 +144,182 @@ setup_vm() {
exit 1"
}
scenario() {
# --- 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
@@ -208,9 +385,14 @@ scenario() {
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[@]} sandbox checks failed"
fail "${#FAILED[@]} security checks failed"
fi
}