mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
install.sh and both image builds run WROLPi only from a signed checkout (#909)
Only upgrade.sh checked the release key's signature. install.sh and the Pi and ISO builds cloned by branch or tag name and ran what GitHub returned, so a forged TLS certificate (as the eMudhra key compromise allowed) could change what a fresh install or a published image runs. verify_wrolpi_commit.sh requires a VALIDSIG from the pinned release-key fingerprint. The image builds verify their clone with the verifier and key from the build checkout; install.sh carries an identical copy of the function, since it runs before any verified checkout exists. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
70e915f610
commit
fb3b959fff
8 changed files with 184 additions and 1 deletions
|
|
@ -107,6 +107,10 @@ rsync -a "${SCRIPT_DIR}/config" "${BUILD_DIR}/"
|
|||
# Write branch config for hook script.
|
||||
mkdir -p "${BUILD_DIR}/config/includes.chroot/opt/wrolpi-blobs"
|
||||
echo "${BRANCH}" > "${BUILD_DIR}/config/includes.chroot/opt/wrolpi-branch"
|
||||
# The hook verifies its WROLPi clone with the verifier and key from this (build) checkout, not from the clone.
|
||||
mkdir -p "${BUILD_DIR}/config/includes.chroot/opt/wrolpi-verify"
|
||||
cp "${SCRIPT_DIR}/../wrolpi/scripts/verify_wrolpi_commit.sh" "${SCRIPT_DIR}/../wrolpi/roland@learningselfreliance.com.gpg" \
|
||||
"${BUILD_DIR}/config/includes.chroot/opt/wrolpi-verify/"
|
||||
|
||||
# Cache directory for downloaded blobs (persists across builds).
|
||||
BLOB_CACHE="${SCRIPT_DIR}/files"
|
||||
|
|
|
|||
|
|
@ -48,6 +48,8 @@ git --version
|
|||
WROLPI_BRANCH="release"
|
||||
[ -f /opt/wrolpi-branch ] && WROLPI_BRANCH=$(cat /opt/wrolpi-branch)
|
||||
git clone -b "${WROLPI_BRANCH}" https://github.com/lrnselfreliance/wrolpi.git /opt/wrolpi
|
||||
bash /opt/wrolpi-verify/verify_wrolpi_commit.sh /opt/wrolpi HEAD /opt/wrolpi-verify/roland@learningselfreliance.com.gpg
|
||||
rm -rf /opt/wrolpi-verify
|
||||
rm -f /opt/wrolpi-branch
|
||||
|
||||
# Install webapp.
|
||||
|
|
|
|||
26
install.sh
26
install.sh
|
|
@ -30,6 +30,25 @@ while getopts ":hb:" option; do
|
|||
esac
|
||||
done
|
||||
|
||||
# Verifies the WROLPi checkout before anything in it runs. This is a copy of wrolpi/scripts/verify_wrolpi_commit.sh
|
||||
# (install.sh runs before any verified checkout exists); a test keeps the two the same.
|
||||
verify_wrolpi_commit() {
|
||||
local repo=$1 rev=$2 key=$3
|
||||
local fingerprint=${WROLPI_SIGNING_FINGERPRINT:-A74A9634A86C65F3C13949AD1BA620CE6C9A4680}
|
||||
local gnupghome status rc=1
|
||||
# A short path: gpg's agent socket lives in GNUPGHOME and its path is limited to ~108 characters.
|
||||
gnupghome=$(mktemp -d /tmp/wgpg.XXXXXX) || return 1
|
||||
if GNUPGHOME="${gnupghome}" gpg --batch --quiet --import "${key}" 2>/dev/null; then
|
||||
# VALIDSIG <signing key fingerprint> ... <primary key fingerprint>
|
||||
status=$(GNUPGHOME="${gnupghome}" git -C "${repo}" verify-commit --raw "${rev}" 2>&1) &&
|
||||
awk -v fpr="${fingerprint}" '$2 == "VALIDSIG" && ($3 == fpr || $NF == fpr) {found = 1} END {exit !found}' \
|
||||
<<<"${status}" && rc=0
|
||||
fi
|
||||
rm -rf "${gnupghome}"
|
||||
[ ${rc} -eq 0 ] || echo "ERROR: ${rev} in ${repo} is not signed by the WROLPi key ${fingerprint}" >&2
|
||||
return ${rc}
|
||||
}
|
||||
|
||||
rpi=false
|
||||
if (grep 'Raspberry Pi' /proc/cpuinfo >/dev/null); then
|
||||
rpi=true
|
||||
|
|
@ -60,12 +79,17 @@ set -e
|
|||
[ -d /opt/wrolpi ] && git config --global --add safe.directory /opt/wrolpi
|
||||
|
||||
# Get the latest WROLPi code. Use the branch requested.
|
||||
apt install -y git
|
||||
apt install -y git gnupg
|
||||
git --version
|
||||
git clone https://github.com/lrnselfreliance/wrolpi.git /opt/wrolpi || :
|
||||
# Use `checkout -B <branch> origin/<branch>` so the ref can never be mistaken for a path. A tracked `release/`
|
||||
# directory otherwise collides with the `release` branch name, making a bare `git checkout release` ambiguous.
|
||||
(cd /opt/wrolpi && git fetch && git checkout -B "${BRANCH}" "origin/${BRANCH}" && git reset --hard origin/"${BRANCH}") || exit 4
|
||||
# The key comes from the clone, so it is only trusted because its fingerprint is pinned in verify_wrolpi_commit.
|
||||
verify_wrolpi_commit /opt/wrolpi HEAD /opt/wrolpi/wrolpi/roland@learningselfreliance.com.gpg || {
|
||||
echo "Refusing to install: the WROLPi code on ${BRANCH} is not signed by the WROLPi release key."
|
||||
exit 5
|
||||
}
|
||||
|
||||
if [ ${rpi} == true ]; then
|
||||
/opt/wrolpi/scripts/install_raspberrypios.sh 2>&1 | tee /opt/wrolpi/install.log
|
||||
|
|
|
|||
|
|
@ -122,6 +122,9 @@ git init -q "${BUILD_DIR}" &&
|
|||
cp "${SCRIPT_DIR}/config.txt" "${BUILD_DIR}/config.txt"
|
||||
echo "export WROLPI_BRANCH=${BRANCH}" >> "${BUILD_DIR}/config.txt"
|
||||
rsync -a "${SCRIPT_DIR}"/stage2/* "${BUILD_DIR}/stage2/"
|
||||
# The chroot verifies its WROLPi clone with the verifier and key from this (build) checkout, not from the clone.
|
||||
cp "${SCRIPT_DIR}/../wrolpi/scripts/verify_wrolpi_commit.sh" "${SCRIPT_DIR}/../wrolpi/roland@learningselfreliance.com.gpg" \
|
||||
"${BUILD_DIR}/stage2/04-wrolpi/files/"
|
||||
|
||||
# We only need to build the Lite and Desktop images.
|
||||
rm -f "${BUILD_DIR}"/stage*/EXPORT*
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ gcc-doc
|
|||
git
|
||||
git-doc
|
||||
gnome-disk-utility
|
||||
gnupg
|
||||
gnupg-agent
|
||||
gpsd
|
||||
gpsd-tools
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ deno --version
|
|||
|
||||
# Put the latest WROLPi in /opt/wrolpi.
|
||||
git clone -b "${WROLPI_BRANCH:-release}" https://github.com/lrnselfreliance/wrolpi.git /opt/wrolpi
|
||||
bash /opt/wrolpi-blobs/verify_wrolpi_commit.sh /opt/wrolpi HEAD /opt/wrolpi-blobs/roland@learningselfreliance.com.gpg
|
||||
rm /opt/wrolpi-blobs/verify_wrolpi_commit.sh /opt/wrolpi-blobs/roland@learningselfreliance.com.gpg
|
||||
# `git config --global` needs HOME, which is empty when the build runs under
|
||||
# systemd (the release poller service has no HOME); set it to root's home.
|
||||
export HOME=/root
|
||||
|
|
|
|||
30
wrolpi/scripts/verify_wrolpi_commit.sh
Executable file
30
wrolpi/scripts/verify_wrolpi_commit.sh
Executable file
|
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env bash
|
||||
# Usage: verify_wrolpi_commit.sh REPO REV [KEY_FILE]
|
||||
#
|
||||
# Exits non-zero unless REV in the git checkout REPO is signed by the WROLPi release key. KEY_FILE defaults to the
|
||||
# key committed beside this script; the image builds pass the key from the build checkout, never from the clone being
|
||||
# verified.
|
||||
|
||||
# The key is imported into a throwaway keyring, and the signature must come from the pinned fingerprint, so a key file
|
||||
# swapped along with the code is rejected. install.sh carries an identical copy of this function because it runs
|
||||
# before any verified checkout exists; a test keeps the two the same.
|
||||
verify_wrolpi_commit() {
|
||||
local repo=$1 rev=$2 key=$3
|
||||
local fingerprint=${WROLPI_SIGNING_FINGERPRINT:-A74A9634A86C65F3C13949AD1BA620CE6C9A4680}
|
||||
local gnupghome status rc=1
|
||||
# A short path: gpg's agent socket lives in GNUPGHOME and its path is limited to ~108 characters.
|
||||
gnupghome=$(mktemp -d /tmp/wgpg.XXXXXX) || return 1
|
||||
if GNUPGHOME="${gnupghome}" gpg --batch --quiet --import "${key}" 2>/dev/null; then
|
||||
# VALIDSIG <signing key fingerprint> ... <primary key fingerprint>
|
||||
status=$(GNUPGHOME="${gnupghome}" git -C "${repo}" verify-commit --raw "${rev}" 2>&1) &&
|
||||
awk -v fpr="${fingerprint}" '$2 == "VALIDSIG" && ($3 == fpr || $NF == fpr) {found = 1} END {exit !found}' \
|
||||
<<<"${status}" && rc=0
|
||||
fi
|
||||
rm -rf "${gnupghome}"
|
||||
[ ${rc} -eq 0 ] || echo "ERROR: ${rev} in ${repo} is not signed by the WROLPi key ${fingerprint}" >&2
|
||||
return ${rc}
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
|
||||
verify_wrolpi_commit "$1" "$2" "${3:-$(dirname "$(realpath "$0")")/../roland@learningselfreliance.com.gpg}"
|
||||
fi
|
||||
117
wrolpi/test/test_verify_wrolpi_commit.py
Normal file
117
wrolpi/test/test_verify_wrolpi_commit.py
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
"""
|
||||
Tests that every path which installs WROLPi checks the checkout is signed by the WROLPi release key.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from wrolpi.test.test_common import gpg_test_key # noqa: F401 (fixture)
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
||||
VERIFIER = PROJECT_DIR / 'wrolpi/scripts/verify_wrolpi_commit.sh'
|
||||
WROLPI_KEY = PROJECT_DIR / 'wrolpi/roland@learningselfreliance.com.gpg'
|
||||
INSTALL_SH = PROJECT_DIR / 'install.sh'
|
||||
PI_GEN_CHROOT = PROJECT_DIR / 'pi-gen/stage2/04-wrolpi/03-run-chroot.sh'
|
||||
ISO_HOOK = PROJECT_DIR / 'debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot'
|
||||
|
||||
|
||||
def _fingerprint(key_path, env=None) -> str:
|
||||
output = subprocess.run(['gpg', '--batch', '--show-keys', '--with-colons', str(key_path)],
|
||||
env=env, capture_output=True, text=True, check=True).stdout
|
||||
return next(line.split(':')[9] for line in output.splitlines() if line.startswith('fpr:'))
|
||||
|
||||
|
||||
def _function(path) -> str:
|
||||
"""The text of `verify_wrolpi_commit() { ... }` in a script."""
|
||||
match = re.search(r'^verify_wrolpi_commit\(\) \{\n.*?^\}\n', path.read_text(), re.MULTILINE | re.DOTALL)
|
||||
assert match, f'verify_wrolpi_commit is not defined in {path}'
|
||||
return match.group(0)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def signed_repo(tmp_path, gpg_test_key):
|
||||
"""A git repository whose `signed` commit is signed by the test key, and whose `unsigned` commit is not."""
|
||||
env, test_pubkey, _ = gpg_test_key
|
||||
repo = tmp_path / 'repo'
|
||||
repo.mkdir()
|
||||
|
||||
def git(*args):
|
||||
return subprocess.run(['git', '-C', str(repo), *args], env=env, capture_output=True, text=True,
|
||||
check=True).stdout.strip()
|
||||
|
||||
git('init', '-q')
|
||||
git('config', 'user.email', 'test@test.com')
|
||||
git('config', 'user.name', 'Test')
|
||||
git('config', 'user.signingkey', 'test@test.com')
|
||||
(repo / 'a.txt').write_text('a')
|
||||
git('add', 'a.txt')
|
||||
git('commit', '-q', '-S', '-m', 'signed')
|
||||
git('tag', 'signed')
|
||||
(repo / 'b.txt').write_text('b')
|
||||
git('add', 'b.txt')
|
||||
git('commit', '-q', '--no-gpg-sign', '-m', 'unsigned')
|
||||
git('tag', 'unsigned')
|
||||
yield repo, test_pubkey, _fingerprint(test_pubkey, env)
|
||||
|
||||
|
||||
def _verify(repo, rev, key, fingerprint=None):
|
||||
env = {k: v for k, v in os.environ.items() if k != 'GNUPGHOME'}
|
||||
if fingerprint:
|
||||
env['WROLPI_SIGNING_FINGERPRINT'] = fingerprint
|
||||
return subprocess.run(['bash', str(VERIFIER), str(repo), rev, str(key)], env=env, capture_output=True, text=True)
|
||||
|
||||
|
||||
def test_verify_signed_commit(signed_repo):
|
||||
"""A commit signed by the trusted key, from the pinned fingerprint, verifies."""
|
||||
repo, key, fingerprint = signed_repo
|
||||
result = _verify(repo, 'signed', key, fingerprint)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
|
||||
|
||||
def test_verify_unsigned_commit(signed_repo):
|
||||
repo, key, fingerprint = signed_repo
|
||||
assert _verify(repo, 'unsigned', key, fingerprint).returncode != 0
|
||||
|
||||
|
||||
def test_verify_commit_signed_by_another_key(signed_repo, tmp_path):
|
||||
"""The signature must come from the key file given; the WROLPi key cannot vouch for the test signature."""
|
||||
repo, _, fingerprint = signed_repo
|
||||
assert _verify(repo, 'signed', WROLPI_KEY, fingerprint).returncode != 0
|
||||
|
||||
|
||||
def test_verify_swapped_key_file(signed_repo):
|
||||
"""A key file swapped along with the code is rejected because its fingerprint is not the pinned one."""
|
||||
repo, key, _ = signed_repo
|
||||
assert _verify(repo, 'signed', key).returncode != 0
|
||||
|
||||
|
||||
def test_pinned_fingerprint_is_the_wrolpi_key():
|
||||
"""The fingerprint the verifier pins is the committed WROLPi key's."""
|
||||
fingerprint = _fingerprint(WROLPI_KEY)
|
||||
assert f':-{fingerprint}}}' in _function(VERIFIER)
|
||||
|
||||
|
||||
def test_install_sh_has_the_same_verifier():
|
||||
"""install.sh runs before any verified checkout exists, so it carries a copy of the function; they must match."""
|
||||
assert _function(INSTALL_SH) == _function(VERIFIER)
|
||||
|
||||
|
||||
def test_install_sh_verifies_before_running_the_checkout():
|
||||
text = INSTALL_SH.read_text()
|
||||
verify = text.index('verify_wrolpi_commit /opt/wrolpi HEAD')
|
||||
assert text.index('git reset --hard') < verify < text.index('scripts/install_debian.sh')
|
||||
assert re.search(r'^apt install -y git gnupg$', text, re.MULTILINE)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', [PI_GEN_CHROOT, ISO_HOOK])
|
||||
def test_image_builds_verify_the_clone(path):
|
||||
"""Each image build verifies the clone with the key from the build checkout, before using it."""
|
||||
lines = path.read_text().splitlines()
|
||||
clone = next(i for i, line in enumerate(lines) if 'git clone' in line and 'lrnselfreliance/wrolpi' in line)
|
||||
verify = next(i for i, line in enumerate(lines) if 'verify_wrolpi_commit.sh /opt/wrolpi HEAD' in line)
|
||||
assert clone < verify
|
||||
# Nothing from the clone runs before it is verified.
|
||||
assert not any('/opt/wrolpi/' in line and not line.lstrip().startswith('#') and 'verify' not in line
|
||||
for line in lines[clone + 1:verify]), lines[clone + 1:verify]
|
||||
Loading…
Add table
Add a link
Reference in a new issue