168 lines
8.5 KiB
Bash
168 lines
8.5 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
runner_die() { printf 'ERROR: %s\n' "$*" >&2; return 1; }
|
|
|
|
runner_ensure_account() {
|
|
local user=$1 home=$2 entry uid passwd_home shell groups uid_min
|
|
if ! entry=$(getent passwd "$user"); then
|
|
[[ ! -e $home && ! -L $home ]] || { runner_die "runner home already exists without its account: $home"; return 1; }
|
|
useradd --system --create-home --home-dir "$home" --shell /usr/sbin/nologin "$user" || return 1
|
|
return
|
|
fi
|
|
IFS=: read -r _ _ uid _ _ passwd_home shell <<<"$entry"
|
|
uid_min=$(awk '$1 == "UID_MIN" && $2 ~ /^[0-9]+$/ {print $2; exit}' /etc/login.defs)
|
|
[[ $uid =~ ^[0-9]+$ && $uid -gt 0 && $uid_min =~ ^[0-9]+$ && $uid -lt $uid_min && $passwd_home == "$home" && $shell == /usr/sbin/nologin ]] || { runner_die "existing $user account has an unexpected UID, home, or shell"; return 1; }
|
|
[[ $(id -gn "$user") == "$user" ]] || { runner_die "existing $user account has an unexpected primary group"; return 1; }
|
|
groups=$(id -nG "$user")
|
|
[[ $groups == "$user" ]] || { runner_die "existing $user account has unexpected supplementary groups: $groups"; return 1; }
|
|
[[ -d $home && ! -L $home ]] || { runner_die "existing $user home is missing or has unexpected type"; return 1; }
|
|
case $(stat -c %U "$home") in
|
|
"$user") ;;
|
|
root) [[ $(stat -c '%G:%a' "$home") == root:711 ]] || { runner_die "root-owned $user home must be mode 0711"; return 1; } ;;
|
|
*) runner_die "existing $user home has unexpected ownership"; return 1 ;;
|
|
esac
|
|
}
|
|
|
|
runner_lock_home() {
|
|
local home=$1 parent_mode
|
|
[[ -d $home && ! -L $home && $(stat -c %U /var/lib) == root ]] || { runner_die "runner home or its parent is unsafe: $home"; return 1; }
|
|
parent_mode=$(stat -c %a /var/lib)
|
|
(( (8#$parent_mode & 022) == 0 )) || { runner_die '/var/lib must not be group/world-writable'; return 1; }
|
|
chown root:root "$home" && chmod 0711 "$home" || return 1
|
|
[[ $(stat -c '%U:%G:%a' "$home") == root:root:711 ]] || { runner_die 'runner home could not be locked from runner writes'; return 1; }
|
|
}
|
|
|
|
runner_ensure_image() {
|
|
local image=$1 bytes=$2 parent_uid=$3 parent_mode=$4
|
|
python3 "$(dirname "${BASH_SOURCE[0]}")/secure-runner-image.py" "$image" "$bytes" "$parent_uid" "$parent_mode" || { runner_die "runner image validation or initialization failed: $image"; return 1; }
|
|
}
|
|
|
|
runner_install_file() {
|
|
local source=$1 destination=$2 owner=$3 group=$4 mode=${5#0}
|
|
if [[ -e $destination || -L $destination ]]; then
|
|
[[ -f $destination && ! -L $destination ]] && cmp -s "$source" "$destination" || { runner_die "existing runner file differs from reviewed configuration: $destination"; return 1; }
|
|
[[ $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { runner_die "existing runner file has unexpected ownership or permissions: $destination"; return 1; }
|
|
return
|
|
fi
|
|
install -o "$owner" -g "$group" -m "0$mode" "$source" "$destination"
|
|
}
|
|
|
|
runner_install_content() {
|
|
local destination=$1 owner=$2 group=$3 mode=$4 temporary
|
|
temporary=$(mktemp)
|
|
cat >"$temporary"
|
|
if ! runner_install_file "$temporary" "$destination" "$owner" "$group" "$mode"; then
|
|
rm -f "$temporary"
|
|
return 1
|
|
fi
|
|
rm -f "$temporary"
|
|
}
|
|
|
|
runner_ensure_private_directory() {
|
|
local directory=$1 owner=$2 group=$3 current= component index
|
|
local -a components
|
|
[[ $directory == /* ]] || { runner_die "runner directory path is not absolute: $directory"; return 1; }
|
|
IFS=/ read -r -a components <<<"${directory#/}"
|
|
for index in "${!components[@]}"; do
|
|
component=${components[index]}
|
|
[[ -n $component && $component != . && $component != .. ]] || { runner_die "runner directory path has an unsafe component: $directory"; return 1; }
|
|
current+="/$component"
|
|
[[ ! -L $current ]] || { runner_die "runner directory path contains a symbolic link: $current"; return 1; }
|
|
if [[ ! -e $current ]]; then
|
|
((index == ${#components[@]} - 1)) || { runner_die "runner directory parent is missing: $current"; return 1; }
|
|
break
|
|
fi
|
|
[[ -d $current ]] || { runner_die "runner directory path component is not a directory: $current"; return 1; }
|
|
if [[ $current == /var/lib/akyldash-runner ]]; then
|
|
[[ $(stat -c '%U:%G:%a' "$current") == root:root:711 ]] || { runner_die 'runner home must be root-owned mode 0711'; return 1; }
|
|
elif [[ $current == /var/lib/akyldash-runner/* ]]; then
|
|
[[ $(stat -c '%U:%G' "$current") == "$owner:$group" ]] || { runner_die "runner directory ancestor has unexpected ownership: $current"; return 1; }
|
|
fi
|
|
done
|
|
# These paths are runner-writable; make changes as that user so a path swap
|
|
# cannot turn a root operation into a write outside its home.
|
|
if [[ $directory == /var/lib/akyldash-runner/.config ]]; then
|
|
install -d -o "$owner" -g "$group" -m 0700 "$directory" || return 1
|
|
else
|
|
runuser -u "$owner" -- install -d -m 0700 "$directory" || return 1
|
|
fi
|
|
[[ -d $directory && ! -L $directory && $(stat -c '%U:%G:%a' "$directory") == "$owner:$group:700" ]] || { runner_die "runner directory failed validation: $directory"; return 1; }
|
|
}
|
|
|
|
runner_install_user_content() {
|
|
local destination=$1 owner=$2 group=$3 mode=${4#0} temporary
|
|
temporary=$(mktemp)
|
|
cat >"$temporary"
|
|
if [[ -e $destination || -L $destination ]]; then
|
|
[[ -f $destination && ! -L $destination ]] && cmp -s "$temporary" "$destination" || { rm -f "$temporary"; runner_die "existing runner file differs from reviewed configuration: $destination"; return 1; }
|
|
[[ $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { rm -f "$temporary"; runner_die "existing runner file has unexpected ownership or permissions: $destination"; return 1; }
|
|
rm -f "$temporary"
|
|
return
|
|
fi
|
|
chown "$owner:$group" "$temporary" && chmod "0$mode" "$temporary" || { rm -f "$temporary"; return 1; }
|
|
local result=0
|
|
runuser -u "$owner" -- install -m "0$mode" "$temporary" "$destination" || result=$?
|
|
rm -f "$temporary"
|
|
((result == 0)) || return 1
|
|
[[ -f $destination && ! -L $destination && $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { runner_die "runner file failed validation: $destination"; return 1; }
|
|
}
|
|
|
|
runner_validate_mount() {
|
|
local image=$1 store=$2 target filesystem source options backing mount_info
|
|
mount_info=$(findmnt --noheadings --output TARGET,FSTYPE,SOURCE,OPTIONS --mountpoint "$store") || { runner_die 'runner store is not mounted'; return 1; }
|
|
read -r target filesystem source options <<<"$mount_info" || { runner_die 'runner store mount information is empty'; return 1; }
|
|
[[ $target == "$store" && $filesystem == ext4 && $source == /dev/loop* ]] || { runner_die 'runner store is mounted from an unexpected target, filesystem, or device'; return 1; }
|
|
case ",$options," in *,nosuid,*nodev,*|*,nodev,*nosuid,*) ;; *) runner_die 'runner store mount is missing nosuid or nodev'; return 1 ;; esac
|
|
backing=$(losetup --noheadings --output BACK-FILE "$source") || { runner_die 'cannot identify the runner loop backing file'; return 1; }
|
|
[[ $(realpath -e "$backing") == "$(realpath -e "$image")" ]] || { runner_die 'runner store is backed by a different image'; return 1; }
|
|
}
|
|
|
|
runner_ensure_mount() {
|
|
local image=$1 store=$2 unit_name=$3 unit_file=$4 unit_tmp mounted=0
|
|
[[ -d $store && ! -L $store ]] || { runner_die "runner mount point is missing or is not a directory: $store"; return 1; }
|
|
if mountpoint -q "$store"; then
|
|
runner_validate_mount "$image" "$store" || return 1
|
|
mounted=1
|
|
fi
|
|
unit_tmp=$(mktemp)
|
|
if ! cat >"$unit_tmp" <<EOF
|
|
[Unit]
|
|
Description=Bounded storage for the AkylDash CI runner
|
|
Before=local-fs.target
|
|
|
|
[Mount]
|
|
What=$image
|
|
Where=$store
|
|
Type=ext4
|
|
Options=loop,nosuid,nodev
|
|
|
|
[Install]
|
|
WantedBy=local-fs.target
|
|
EOF
|
|
then
|
|
rm -f "$unit_tmp"
|
|
runner_die 'failed to prepare runner mount unit'
|
|
return 1
|
|
fi
|
|
if [[ -e $unit_file || -L $unit_file ]]; then
|
|
if [[ ! -f $unit_file || -L $unit_file ]] || ! cmp -s "$unit_tmp" "$unit_file"; then
|
|
rm -f "$unit_tmp"
|
|
runner_die "existing runner mount unit differs from the reviewed configuration: $unit_file"
|
|
return 1
|
|
fi
|
|
[[ $(stat -c '%u:%g:%a' "$unit_file") == "$(id -u):$(id -g):644" ]] || { rm -f "$unit_tmp"; runner_die 'existing runner mount unit has unexpected ownership or permissions'; return 1; }
|
|
else
|
|
install -m 0644 "$unit_tmp" "$unit_file" || { rm -f "$unit_tmp"; return 1; }
|
|
fi
|
|
rm -f "$unit_tmp"
|
|
|
|
systemctl daemon-reload || return 1
|
|
if ((mounted)); then
|
|
systemctl enable "$unit_name" || return 1
|
|
else
|
|
systemctl enable "$unit_name" || return 1
|
|
systemctl start "$unit_name" || return 1
|
|
runner_validate_mount "$image" "$store" || return 1
|
|
fi
|
|
}
|