Skip to content

eBPF-based security platform

image: build operator images with Bash and veritysetup

KP Singh7b742f996ae3
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 153685bf..e0411046 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -522,6 +522,12 @@ install-operator: install shirudo-build $(VMLINUX_H) $(BPF_LIBBPF_HEADERS) $(BPF
$(Q)install -d $(DESTDIR)$(libexecdir)/shirudo
$(Q)install -m 0755 tools/shirudo-build/shirudo-build \
$(DESTDIR)$(libexecdir)/shirudo/shirudo-build
+ $(Q)install -d $(DESTDIR)$(libexecdir)/shirudo/gen-image
+ $(Q)install -m 0755 tools/gen-image/shirudo-gen-* \
+ $(DESTDIR)$(libexecdir)/shirudo/gen-image/
+ $(Q)install -m 0644 tools/gen-image/common.sh \
+ tools/gen-image/runtime-service.conf.in \
+ $(DESTDIR)$(libexecdir)/shirudo/gen-image/
@printf ' INSTALL $(DESTDIR)$(sharedir)/shirudo/vmlinux.h\n'
$(Q)install -d $(DESTDIR)$(sharedir)/shirudo
$(Q)install -m 0644 $(VMLINUX_H) \
diff --git a/packaging/rpm/buildrpm.sh b/packaging/rpm/buildrpm.sh
index f4e16315..937c3512 100755
--- a/packaging/rpm/buildrpm.sh
+++ b/packaging/rpm/buildrpm.sh
@@ -86,7 +86,7 @@ dirs=$(cd "$stage" && find . -mindepth 1 -type d -path '*/shirudo*' | sed 's#^\.
printf 'Conflicts: %s\n' "$other"
if [ "$role" = operator ]; then
# Operator-only compilers and the standard image format/verify tool.
- printf 'Requires: clang, llvm, cryptsetup\n'
+ printf 'Requires: bash, coreutils, clang, llvm, cryptsetup\n'
fi
printf '\n%%description\n'
if [ "$role" = target ]; then
diff --git a/tools/gen-image/common.sh b/tools/gen-image/common.sh
new file mode 100644
index 00000000..5fcf8dc2
--- /dev/null
+++ b/tools/gen-image/common.sh
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+# shellcheck shell=bash
+
+die()
+{
+ printf '%s: %s\n' "${0##*/}" "$*" >&2
+ exit 1
+}
+
+require_command()
+{
+ command -v "$1" >/dev/null || die "$2"
+}
+
+require_new_output()
+{
+ [ ! -e "$1" ] && [ ! -L "$1" ] ||
+ die "${2:-output} already exists: $1"
+}
+
+check_image_options()
+{
+ local output=$1 keys_dir=$2 size=$3 suffix
+
+ [ -n "$output" ] || die '--output is required'
+ [ -n "$keys_dir" ] && [ -d "$keys_dir" ] ||
+ die '--keys-dir needs a directory'
+ case "$size" in ''|*[!0-9]*) die '--size must be an integer in MiB' ;; esac
+ [ "${#size}" -le 7 ] && [ "$size" -ge 8 ] && [ "$size" -le 1048576 ] ||
+ die '--size must be between 8 and 1048576 MiB'
+ for suffix in .img .verity .roothash .p7s; do
+ require_new_output "$output$suffix"
+ done
+}
+
+escape_sed_replacement()
+{
+ printf '%s' "$1" | sed 's/[\\&|]/\\&/g'
+}
diff --git a/tools/gen-image/shirudo-gen-image b/tools/gen-image/shirudo-gen-image
index f0e7074a..6515e6c9 100755
--- a/tools/gen-image/shirudo-gen-image
+++ b/tools/gen-image/shirudo-gen-image
@@ -3,6 +3,8 @@
# Copyright (c) 2026 Authors of shirudo
set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+. "$here/common.sh"
usage()
{
@@ -18,10 +20,71 @@ No existing output file is replaced. The private key never enters the image.
EOF
}
-die()
+parse_args()
{
- printf 'shirudo-gen-image: %s\n' "$*" >&2
- exit 1
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ --source) source_dir=${2:?}; shift 2 ;;
+ --output) output=${2:?}; shift 2 ;;
+ --keys-dir) keys_dir=${2:?}; shift 2 ;;
+ --kind) kind=${2:?}; shift 2 ;;
+ --size) size=${2:?}; shift 2 ;;
+ --builder) builder=${2:?}; shift 2 ;;
+ -h|--help) usage; exit 0 ;;
+ *) usage >&2; die "unknown option: $1" ;;
+ esac
+ done
+}
+
+check_inputs()
+{
+ [ -n "$source_dir" ] && [ -d "$source_dir" ] ||
+ die '--source needs a directory'
+ check_image_options "$output" "$keys_dir" "$size"
+ case "$kind" in config|runtime) ;; *) die '--kind must be config or runtime' ;; esac
+ require_command veritysetup 'install veritysetup (cryptsetup-bin or cryptsetup)'
+ require_command mkfs.ext4 'install e2fsprogs (mkfs.ext4)'
+ require_command "$builder" 'shirudo-build not found; use --builder'
+ source_dir=$(realpath "$source_dir")
+ keys_dir=$(realpath "$keys_dir")
+}
+
+check_source()
+{
+ case "$keys_dir/" in "$source_dir/"*) die 'operator keys must be outside the image source' ;; esac
+ if find "$source_dir" -type f -name signing_key.pem -print -quit | grep -q .; then
+ die 'the image source contains a private signing key'
+ fi
+ if [ "$kind" = config ] && find "$source_dir" -type l -print -quit | grep -q .; then
+ die 'config images must not contain symbolic links'
+ fi
+ if find "$source_dir" ! -type d ! -type f ! -type l -print -quit | grep -q .; then
+ die 'the image source contains a special file'
+ fi
+}
+
+build_filesystem()
+{
+ truncate -s "${size}M" "$work/filesystem.img"
+ mkfs.ext4 -q -F -b 4096 -O '^has_journal' -E root_owner=0:0 \
+ -d "$source_dir" "$work/filesystem.img"
+}
+
+build_signed_image()
+{
+ "$here/shirudo-gen-verity" --builder "$builder" "$work/filesystem.img" -o "$output" \
+ --kind "$kind" --keys-dir "$keys_dir"
+}
+
+main()
+{
+ parse_args "$@"
+ check_inputs
+ check_source
+ work=$(mktemp -d)
+ trap 'rm -rf "$work"' EXIT
+ build_filesystem
+ build_signed_image
}
source_dir=
@@ -30,53 +93,4 @@ keys_dir=
kind=config
size=64
builder=${SHIRUDO_BUILD:-shirudo-build}
-
-while [ $# -gt 0 ]; do
- case "$1" in
- --source) source_dir=${2:?}; shift 2 ;;
- --output) output=${2:?}; shift 2 ;;
- --keys-dir) keys_dir=${2:?}; shift 2 ;;
- --kind) kind=${2:?}; shift 2 ;;
- --size) size=${2:?}; shift 2 ;;
- --builder) builder=${2:?}; shift 2 ;;
- -h|--help) usage; exit 0 ;;
- *) usage >&2; die "unknown option: $1" ;;
- esac
-done
-
-[ -n "$source_dir" ] && [ -d "$source_dir" ] || die '--source needs a directory'
-[ -n "$output" ] || die '--output is required'
-[ -n "$keys_dir" ] && [ -d "$keys_dir" ] || die '--keys-dir needs a directory'
-case "$kind" in config|runtime) ;; *) die '--kind must be config or runtime' ;; esac
-case "$size" in ''|*[!0-9]*) die '--size must be an integer in MiB' ;; esac
-[ "${#size}" -le 7 ] && [ "$size" -ge 8 ] && [ "$size" -le 1048576 ] ||
- die '--size must be between 8 and 1048576 MiB'
-command -v veritysetup >/dev/null || die 'install veritysetup (cryptsetup-bin or cryptsetup)'
-command -v mkfs.ext4 >/dev/null || die 'install e2fsprogs (mkfs.ext4)'
-command -v "$builder" >/dev/null || die 'shirudo-build not found; use --builder'
-
-source_dir=$(realpath "$source_dir")
-keys_dir=$(realpath "$keys_dir")
-case "$keys_dir/" in "$source_dir/"*) die 'operator keys must be outside the image source' ;; esac
-for suffix in .img .verity .roothash .p7s; do
- [ ! -e "$output$suffix" ] && [ ! -L "$output$suffix" ] ||
- die "output already exists: $output$suffix"
-done
-if find "$source_dir" -type f -name signing_key.pem -print -quit | grep -q .; then
- die 'the image source contains a private signing key'
-fi
-if [ "$kind" = config ] && find "$source_dir" -type l -print -quit | grep -q .; then
- die 'config images must not contain symbolic links'
-fi
-if find "$source_dir" ! -type d ! -type f ! -type l -print -quit | grep -q .; then
- die 'the image source contains a special file'
-fi
-
-work=$(mktemp -d)
-trap 'rm -rf "$work"' EXIT
-truncate -s "${size}M" "$work/filesystem.img"
-mkfs.ext4 -q -F -b 4096 -O '^has_journal' -E root_owner=0:0 \
- -d "$source_dir" "$work/filesystem.img"
-"$builder" image build "$work/filesystem.img" -o "$output" \
- --kind "$kind" --keys-dir "$keys_dir"
-"$builder" image verify "$output" --cert "$keys_dir/cert.der" >/dev/null
+main "$@"
diff --git a/tools/gen-image/shirudo-gen-runtime b/tools/gen-image/shirudo-gen-runtime
index b9198da5..cbcec774 100755
--- a/tools/gen-image/shirudo-gen-runtime
+++ b/tools/gen-image/shirudo-gen-runtime
@@ -3,6 +3,8 @@
# Copyright (c) 2026 Authors of shirudo
set -euo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+. "$here/common.sh"
usage()
{
@@ -19,10 +21,137 @@ The input binary must be trusted: ldd invokes its dynamic loader on the operator
EOF
}
-die()
+parse_args()
+{
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ --binary) binary=${2:?}; shift 2 ;;
+ --bootstrap-conf) conf=${2:?}; shift 2 ;;
+ --cert) cert=${2:?}; shift 2 ;;
+ --config-cert) config_cert=${2:?}; shift 2 ;;
+ --output) output=${2:?}; shift 2 ;;
+ --keys-dir) keys_dir=${2:?}; shift 2 ;;
+ --size) size=${2:?}; shift 2 ;;
+ --builder) builder=${2:?}; shift 2 ;;
+ --unit-output) unit_output=${2:?}; shift 2 ;;
+ --extra-binary) extra_binaries+=("${2:?}"); shift 2 ;;
+ -h|--help) usage; exit 0 ;;
+ *) usage >&2; die "unknown option: $1" ;;
+ esac
+ done
+}
+
+elf_interpreter()
+{
+ LC_ALL=C readelf -l "$1" |
+ sed -n 's/.*Requesting program interpreter: \([^]]*\)].*/\1/p'
+}
+
+check_inputs()
+{
+ local file
+
+ for file in "$binary" "$conf" "$cert" "$config_cert"; do
+ [ -n "$file" ] && [ -f "$file" ] ||
+ die 'binary, bootstrap config and both public certificates are required'
+ done
+ check_image_options "$output" "$keys_dir" "$size"
+ require_command readelf 'install binutils (readelf)'
+ require_command ldd 'ldd is required on the operator'
+ interpreter=$(elf_interpreter "$binary")
+ [ -n "$interpreter" ] && [ "${interpreter:0:1}" = / ] ||
+ die 'expected a dynamically linked target ELF with an absolute interpreter'
+ [ -f "$interpreter" ] || die "interpreter not found: $interpreter"
+}
+
+stage_files()
+{
+ mkdir -p "$work/root/usr/bin" "$work/root/etc/shirudo" "$work/root/keys"
+ install -m 0755 "$binary" "$work/root/usr/bin/shirudo"
+ install -m 0644 "$conf" "$work/root/etc/shirudo/shirudo.conf"
+ install -m 0644 "$cert" "$work/root/keys/cert.der"
+ install -m 0644 "$config_cert" "$work/root/etc/shirudo/config-cert.der"
+ LC_ALL=C ldd "$binary" >"$work/ldd" || die 'could not resolve target libraries'
+ printf '%s\n' "$interpreter" >"$work/interpreters"
+}
+
+stage_extra_binaries()
+{
+ local extra name extra_interpreter
+
+ for extra in "${extra_binaries[@]}"; do
+ [ -f "$extra" ] && [ -x "$extra" ] || die "extra binary not executable: $extra"
+ name=$(basename "$extra")
+ [ ! -e "$work/root/usr/bin/$name" ] || die "duplicate executable name: $name"
+ install -m 0755 "$extra" "$work/root/usr/bin/$name"
+ LC_ALL=C readelf -h "$extra" >/dev/null || die "extra binary is not ELF: $extra"
+ extra_interpreter=$(elf_interpreter "$extra")
+ if [ -n "$extra_interpreter" ]; then
+ printf '%s\n' "$extra_interpreter" >>"$work/interpreters"
+ fi
+ if LC_ALL=C readelf -d "$extra" | grep '(NEEDED)' >/dev/null; then
+ LC_ALL=C ldd "$extra" >>"$work/ldd" || die "could not resolve libraries: $extra"
+ fi
+ done
+}
+
+stage_libraries()
{
- printf 'shirudo-gen-runtime: %s\n' "$*" >&2
- exit 1
+ local lib directory
+
+ ! grep -q 'not found' "$work/ldd" || die 'target libraries are missing'
+ cp "$work/interpreters" "$work/libraries"
+ awk '/^[[:space:]]*\// { print $1 } /=> \// { print $3 }' \
+ "$work/ldd" >>"$work/libraries"
+ sort -u "$work/libraries" -o "$work/libraries"
+ while IFS= read -r lib; do
+ [ -f "$lib" ] && [ "${lib:0:1}" = / ] || die "bad library path: $lib"
+ mkdir -p "$work/root$(dirname "$lib")"
+ cp -L "$lib" "$work/root$lib"
+ directory="/run/shirudo/runtime$(dirname "$lib")"
+ case ":$library_path:" in *":$directory:"*) ;;
+ *) library_path=${library_path:+$library_path:}$directory ;; esac
+ done <"$work/libraries"
+}
+
+build_runtime_image()
+{
+ "$here/shirudo-gen-image" --source "$work/root" --output "$output" \
+ --keys-dir "$keys_dir" --kind runtime --size "$size" --builder "$builder"
+ printf 'Runtime invocation (after the authenticated image is mounted):\n' >&2
+ printf '%q --inhibit-cache --library-path %q %q init\n' \
+ "/run/shirudo/runtime$interpreter" "$library_path" \
+ /run/shirudo/runtime/usr/bin/shirudo >&2
+}
+
+write_service()
+{
+ local interpreter_escaped library_path_escaped
+
+ [ -n "$unit_output" ] || return 0
+ require_new_output "$unit_output" 'unit output'
+ case "$interpreter$library_path" in *[[:space:]\\\"%]*) die 'runtime paths cannot be encoded in a systemd ExecStart' ;; esac
+ interpreter_escaped=$(escape_sed_replacement "$interpreter")
+ library_path_escaped=$(escape_sed_replacement "$library_path")
+ (
+ set -o noclobber
+ sed -e "s|@interpreter@|$interpreter_escaped|g" \
+ -e "s|@library_path@|$library_path_escaped|g" \
+ "$here/runtime-service.conf.in" >"$unit_output"
+ )
+}
+
+main()
+{
+ parse_args "$@"
+ check_inputs
+ work=$(mktemp -d)
+ trap 'rm -rf "$work"' EXIT
+ stage_files
+ stage_extra_binaries
+ stage_libraries
+ build_runtime_image
+ write_service
}
binary=
@@ -34,91 +163,6 @@ keys_dir=
size=128
builder=${SHIRUDO_BUILD:-shirudo-build}
unit_output=
-extra_binaries=()
-while [ $# -gt 0 ]; do
- case "$1" in
- --binary) binary=${2:?}; shift 2 ;;
- --bootstrap-conf) conf=${2:?}; shift 2 ;;
- --cert) cert=${2:?}; shift 2 ;;
- --config-cert) config_cert=${2:?}; shift 2 ;;
- --output) output=${2:?}; shift 2 ;;
- --keys-dir) keys_dir=${2:?}; shift 2 ;;
- --size) size=${2:?}; shift 2 ;;
- --builder) builder=${2:?}; shift 2 ;;
- --unit-output) unit_output=${2:?}; shift 2 ;;
- --extra-binary) extra_binaries+=("${2:?}"); shift 2 ;;
- -h|--help) usage; exit 0 ;;
- *) usage >&2; die "unknown option: $1" ;;
- esac
-done
-for file in "$binary" "$conf" "$cert" "$config_cert"; do
- [ -n "$file" ] && [ -f "$file" ] || die 'binary, bootstrap config and both public certificates are required'
-done
-[ -n "$output" ] && [ -n "$keys_dir" ] || die '--output and --keys-dir are required'
-command -v readelf >/dev/null || die 'install binutils (readelf)'
-command -v ldd >/dev/null || die 'ldd is required on the operator'
-interpreter=$(LC_ALL=C readelf -l "$binary" |
- sed -n 's/.*Requesting program interpreter: \([^]]*\)].*/\1/p')
-[ -n "$interpreter" ] && [ "${interpreter:0:1}" = / ] ||
- die 'expected a dynamically linked target ELF with an absolute interpreter'
-[ -f "$interpreter" ] || die "interpreter not found: $interpreter"
-
-work=$(mktemp -d)
-trap 'rm -rf "$work"' EXIT
-mkdir -p "$work/root/usr/bin" "$work/root/etc/shirudo" "$work/root/keys"
-install -m 0755 "$binary" "$work/root/usr/bin/shirudo"
-install -m 0644 "$conf" "$work/root/etc/shirudo/shirudo.conf"
-install -m 0644 "$cert" "$work/root/keys/cert.der"
-install -m 0644 "$config_cert" "$work/root/etc/shirudo/config-cert.der"
-LC_ALL=C ldd "$binary" >"$work/ldd" || die 'could not resolve target libraries'
-printf '%s\n' "$interpreter" >"$work/interpreters"
-for extra in "${extra_binaries[@]}"; do
- [ -f "$extra" ] && [ -x "$extra" ] || die "extra binary not executable: $extra"
- name=$(basename "$extra")
- [ ! -e "$work/root/usr/bin/$name" ] || die "duplicate executable name: $name"
- install -m 0755 "$extra" "$work/root/usr/bin/$name"
- LC_ALL=C readelf -h "$extra" >/dev/null || die "extra binary is not ELF: $extra"
- extra_interpreter=$(LC_ALL=C readelf -l "$extra" |
- sed -n 's/.*Requesting program interpreter: \([^]]*\)\].*/\1/p')
- if [ -n "$extra_interpreter" ]; then
- printf '%s\n' "$extra_interpreter" >>"$work/interpreters"
- fi
- if LC_ALL=C readelf -d "$extra" | grep '(NEEDED)' >/dev/null; then
- LC_ALL=C ldd "$extra" >>"$work/ldd" || die "could not resolve libraries: $extra"
- fi
-done
-! grep -q 'not found' "$work/ldd" || die 'target libraries are missing'
-cp "$work/interpreters" "$work/libraries"
-awk '/^[[:space:]]*\// { print $1 } /=> \// { print $3 }' \
- "$work/ldd" >>"$work/libraries"
-sort -u "$work/libraries" -o "$work/libraries"
library_path=
-while IFS= read -r lib; do
- [ -f "$lib" ] && [ "${lib:0:1}" = / ] || die "bad library path: $lib"
- mkdir -p "$work/root$(dirname "$lib")"
- cp -L "$lib" "$work/root$lib"
- directory="/run/shirudo/runtime$(dirname "$lib")"
- case ":$library_path:" in *":$directory:"*) ;;
- *) library_path=${library_path:+$library_path:}$directory ;; esac
-done <"$work/libraries"
-
-here=$(cd "$(dirname "$0")" && pwd)
-"$here/shirudo-gen-image" --source "$work/root" --output "$output" \
- --keys-dir "$keys_dir" --kind runtime --size "$size" --builder "$builder"
-printf 'Runtime invocation (after the authenticated image is mounted):\n' >&2
-printf '%q --inhibit-cache --library-path %q %q init\n' \
- "/run/shirudo/runtime$interpreter" "$library_path" \
- /run/shirudo/runtime/usr/bin/shirudo >&2
-
-if [ -n "$unit_output" ]; then
- [ ! -e "$unit_output" ] && [ ! -L "$unit_output" ] || die "unit output already exists: $unit_output"
- case "$interpreter$library_path" in *[[:space:]\\\"%]*) die 'runtime paths cannot be encoded in a systemd ExecStart' ;; esac
- (
- set -o noclobber
- interpreter_escaped=$(printf '%s' "$interpreter" | sed 's/[\&|]/\\&/g')
- library_path_escaped=$(printf '%s' "$library_path" | sed 's/[\&|]/\\&/g')
- sed -e "s|@interpreter@|$interpreter_escaped|g" \
- -e "s|@library_path@|$library_path_escaped|g" \
- "$here/runtime-service.conf.in" >"$unit_output"
- )
-fi
+extra_binaries=()
+main "$@"
diff --git a/tools/gen-image/shirudo-gen-verity b/tools/gen-image/shirudo-gen-verity
new file mode 100755
index 00000000..cddd951d
--- /dev/null
+++ b/tools/gen-image/shirudo-gen-verity
@@ -0,0 +1,151 @@
+#!/bin/bash
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+set -euo pipefail
+here=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
+. "$here/common.sh"
+
+usage()
+{
+ cat <<'EOF'
+usage: shirudo image build <filesystem.img> -o <prefix>
+ [--kind config|runtime] [--keys-dir <dir>]
+ [--key <private.pem>] [--cert <cert.der>] [--salt <64-hex>]
+
+Use veritysetup to build the hash tree, then sign its root with shirudo-build.
+Writes <prefix>.img, <prefix>.verity, <prefix>.roothash and <prefix>.p7s.
+Existing output files are never replaced.
+EOF
+}
+
+parse_args()
+{
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ -o|--output) output=${2:?}; shift 2 ;;
+ --keys-dir) keys_dir=${2:?}; shift 2 ;;
+ --key) key=${2:?}; shift 2 ;;
+ --cert) cert=${2:?}; shift 2 ;;
+ --salt) salt=${2:?}; shift 2 ;;
+ --kind) kind=${2:?}; shift 2 ;;
+ --builder) builder=${2:?}; shift 2 ;;
+ -h|--help) usage; exit 0 ;;
+ -*) usage >&2; die "unknown option: $1" ;;
+ *) [ -z "$input" ] || die 'only one filesystem image is allowed'
+ input=$1; shift ;;
+ esac
+ done
+}
+
+check_inputs()
+{
+ local suffix
+
+ require_command veritysetup 'install veritysetup (cryptsetup-bin or cryptsetup)'
+ require_command "$builder" 'shirudo-build not found; use --builder'
+ [ -n "$input" ] && [ -f "$input" ] && [ ! -L "$input" ] ||
+ die 'input must be a regular filesystem image, not a symbolic link'
+ [ -n "$output" ] || die '--output is required'
+ case "$kind" in config|runtime) ;; *) die '--kind must be config or runtime' ;; esac
+ if [ -n "$salt" ] && [[ ! $salt =~ ^[0-9a-fA-F]{64}$ ]]; then
+ die '--salt needs exactly 64 hex characters'
+ fi
+ for suffix in .img .verity .roothash .p7s; do
+ require_new_output "$output$suffix"
+ done
+ key=${key:-$keys_dir/signing_key.pem}
+ cert=${cert:-$keys_dir/cert.der}
+ check_filesystem_size "$input"
+}
+
+check_filesystem_size()
+{
+ local size
+
+ size=$(stat -c %s -- "$1")
+ [ "$size" -gt 0 ] && [ "$size" -le 1099511627776 ] &&
+ [ "$((size % 4096))" -eq 0 ] ||
+ die 'input must be nonempty, 4096-byte aligned and at most 1 TiB'
+}
+
+cleanup()
+{
+ local file
+
+ if [ "$complete" = no ]; then
+ for file in "${published[@]}"; do
+ if [ "$file" -ef "$work/image${file#"$output"}" ]; then
+ rm -f -- "$file"
+ fi
+ done
+ fi
+ rm -rf -- "$work"
+}
+
+format_and_sign()
+{
+ local root
+ local salt_option=()
+
+ cp -P --reflink=auto --sparse=always -- "$input" "$work/image.img"
+ [ -f "$work/image.img" ] && [ ! -L "$work/image.img" ] ||
+ die 'input changed while copying'
+ check_filesystem_size "$work/image.img"
+ [ -z "$salt" ] || salt_option=(--salt="$salt")
+ veritysetup format --format=1 --hash=sha256 \
+ --data-block-size=4096 --hash-block-size=4096 --hash-offset=0 \
+ "${salt_option[@]}" --root-hash-file="$work/image.roothash" \
+ "$work/image.img" "$work/image.verity" >/dev/null
+ root=$(<"$work/image.roothash")
+ [[ $root =~ ^[0-9a-f]{64}$ ]] || die 'veritysetup returned an invalid root hash'
+ printf '%s' "$root" >"$work/image.roothash"
+ "$builder" image sign-root "$work/image.roothash" -o "$work/image.p7s" \
+ --key "$key" --cert "$cert"
+ "$builder" image verify "$work/image" --cert "$cert" >"$work/verified-root"
+}
+
+publish_image()
+{
+ local suffix mode
+
+ mode=$(printf '%04o' "$((0644 & ~$(umask)))")
+ for suffix in .img .verity .roothash .p7s; do
+ chmod "$mode" "$work/image$suffix"
+ done
+ sync -- "$work/image.img" "$work/image.verity" \
+ "$work/image.roothash" "$work/image.p7s"
+ for suffix in .img .verity .roothash .p7s; do
+ ln -T -- "$work/image$suffix" "$output$suffix"
+ published+=("$output$suffix")
+ done
+ complete=yes
+ cat "$work/verified-root"
+}
+
+main()
+{
+ local parent
+
+ parse_args "$@"
+ check_inputs
+ parent=${output%/*}
+ [ "$parent" != "$output" ] || parent=.
+ work=$(mktemp -d -- "$parent/.shirudo-image.XXXXXX")
+ trap cleanup EXIT
+ trap 'exit 1' HUP INT TERM
+ format_and_sign
+ publish_image
+}
+
+input=
+output=
+key=
+cert=
+salt=
+kind=config
+keys_dir=${SHIRUDO_KEYS_DIR:-/var/lib/shirudo/keys}
+builder=${SHIRUDO_BUILD:-shirudo-build}
+complete=no
+published=()
+main "$@"
diff --git a/tools/shirudo-build/image.c b/tools/shirudo-build/image.c
index e47b8db8..c3adc45d 100644
--- a/tools/shirudo-build/image.c
+++ b/tools/shirudo-build/image.c
@@ -10,13 +10,10 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
-#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
-#include <openssl/rand.h>
-
#include "bpm.h"
#include "config_image.h"
#include "crypto.h"
@@ -24,6 +21,7 @@
#include "image.h"
#include "io.h"
#include "log.h"
+#include "malloc.h"
#include "paths.h"
#include "sign.h"
@@ -41,86 +39,64 @@ static void image_usage(FILE *fp)
"Build and offline verification require veritysetup.\n");
}
-static int image_output_path(char *path, size_t cap, const char *prefix,
- const char *suffix)
-{
- int n = snprintf(path, cap, "%s%s", prefix, suffix);
-
- return n < 0 || (size_t)n >= cap ? -ENAMETOOLONG : 0;
-}
-
-static int copy_image(int input_fd, int output_fd, __u64 size)
+static int build_image(int argc, char **argv)
{
- __u8 buf[65536];
- __u64 done = 0;
- size_t want;
- ssize_t n;
- int rc;
+ char executable[PATH_MAX], script[PATH_MAX], directory[PATH_MAX];
+ char **command;
+ ssize_t size;
+ char *slash;
+ int i, n;
- while (done < size) {
- want = size - done < sizeof(buf) ? size - done : sizeof(buf);
- n = read(input_fd, buf, want);
- if (n < 0) {
- if (errno == EINTR)
- continue;
- return -errno;
- }
- if (!n)
- return -EIO;
- rc = write_complete(output_fd, buf, n);
- if (rc)
- return rc;
- done += n;
+ size = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
+ if (size < 0 || (size_t)size >= sizeof(executable) - 1)
+ return 1;
+ executable[size] = '\0';
+ memcpy(directory, executable, size + 1);
+ slash = strrchr(directory, '/');
+ if (!slash)
+ return 1;
+ *slash = '\0';
+ n = snprintf(script, sizeof(script),
+ "%s/../gen-image/shirudo-gen-verity", directory);
+ if (n < 0 || (size_t)n >= sizeof(script))
+ return 1;
+ if (access(script, X_OK)) {
+ n = snprintf(script, sizeof(script),
+ "%s/gen-image/shirudo-gen-verity", directory);
+ if (n < 0 || (size_t)n >= sizeof(script))
+ return 1;
}
- do {
- n = read(input_fd, buf, 1);
- } while (n < 0 && errno == EINTR);
- return n < 0 ? -errno : n ? -EFBIG : 0;
+ command = xcalloc((size_t)argc + 2, sizeof(*command));
+ if (!command)
+ return 1;
+ command[0] = script;
+ command[1] = "--builder";
+ command[2] = executable;
+ for (i = 2; i < argc; i++)
+ command[i + 1] = argv[i];
+ execv(script, command);
+ pr_err("image build: cannot run %s: %s", script, strerror(errno));
+ xfree(command, ((size_t)argc + 2) * sizeof(*command));
+ return 1;
}
-/* Only the operator invokes veritysetup; inherited FDs pin its inputs. */
-static int run_veritysetup(int data_fd, int hash_fd, int root_fd,
- const struct shirudo_config_image *image)
+/* Inherited descriptors keep verification on the files we authenticated. */
+static int verify_verity(int data_fd, int hash_fd, const char *root)
{
- char root_path[80], data_path[40], hash_path[40], blocks[48];
- char salt[72], root[65];
+ char data_path[40], hash_path[40];
pid_t pid;
int status;
snprintf(data_path, sizeof(data_path), "/proc/self/fd/%d", data_fd);
snprintf(hash_path, sizeof(hash_path), "/proc/self/fd/%d", hash_fd);
- snprintf(root_path, sizeof(root_path),
- "--root-hash-file=/proc/self/fd/%d", root_fd);
- snprintf(blocks, sizeof(blocks), "--data-blocks=%llu",
- (__u64)image->data_blocks);
- memcpy(salt, "--salt=", 7);
- hex_encode(image->salt, sizeof(image->salt), salt + 7);
- hex_encode(image->root_hash, sizeof(image->root_hash), root);
pid = fork();
if (pid < 0)
return -errno;
if (!pid) {
- const char *const format_args[] = {
- "veritysetup", "format", "--format=1", "--hash=sha256",
- "--data-block-size=4096", "--hash-block-size=4096",
- "--hash-offset=0", blocks, salt,
- data_path, hash_path, root_path, NULL,
- };
- const char *const verify_args[] = {
- "veritysetup", "verify", "--hash-offset=0",
- data_path, hash_path, root, NULL,
- };
- int null_fd;
-
- if (fcntl(data_fd, F_SETFD, 0) || fcntl(hash_fd, F_SETFD, 0) ||
- (root_fd >= 0 && fcntl(root_fd, F_SETFD, 0)))
- _exit(126);
- null_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
- if (null_fd < 0 || dup2(null_fd, STDOUT_FILENO) < 0)
+ if (fcntl(data_fd, F_SETFD, 0) || fcntl(hash_fd, F_SETFD, 0))
_exit(126);
- close(null_fd);
- execvp("veritysetup", (char *const *)(root_fd >= 0 ?
- format_args : verify_args));
+ execlp("veritysetup", "veritysetup", "verify", "--hash-offset=0",
+ data_path, hash_path, root, NULL);
pr_err("veritysetup: %s; install the operator veritysetup tool",
strerror(errno));
_exit(127);
@@ -135,45 +111,6 @@ static int run_veritysetup(int data_fd, int hash_fd, int root_fd,
return WEXITSTATUS(status) ? -EIO : 0;
}
-static int format_image(int data_fd, int hash_fd, __u64 size,
- const __u8 salt[32],
- struct shirudo_config_image *image)
-{
- char root[66];
- ssize_t n;
- int fd, rc;
-
- *image = (struct shirudo_config_image) {
- .data_blocks = size / SHIRUDO_CONFIG_IMAGE_BLOCK_SIZE,
- };
- memcpy(image->salt, salt, sizeof(image->salt));
- fd = memfd_create("shirudo-verity-root", MFD_CLOEXEC);
- if (fd < 0)
- return -errno;
- rc = run_veritysetup(data_fd, hash_fd, fd, image);
- if (rc)
- goto out;
- do {
- n = pread(fd, root, sizeof(root), 0);
- } while (n < 0 && errno == EINTR);
- if (n != 64 && (n != 65 || root[64] != '\n')) {
- rc = n < 0 ? -errno : -EINVAL;
- goto out;
- }
- rc = shirudo_config_image_read_header(hash_fd, image);
- if (rc)
- goto out;
- root[64] = 0;
- rc = hex_decode(root, image->root_hash,
- sizeof(image->root_hash));
- if (rc)
- goto out;
- rc = shirudo_config_image_check_files(data_fd, hash_fd, image);
-out:
- close(fd);
- return rc;
-}
-
static int verify_root_signature(const char root[65], const __u8 *signature,
size_t size, const char *path)
{
@@ -231,8 +168,7 @@ static int verify_image(const char *prefix, const char *cert)
root);
rc = verify_root_signature(root, signature, size, cert);
if (!rc)
- rc = run_veritysetup(image.data_fd, image.hash_fd, -1,
- &image.image);
+ rc = verify_verity(image.data_fd, image.hash_fd, root);
if (!rc)
puts(root);
out:
@@ -240,56 +176,43 @@ out:
return rc;
}
-static int build_image(const char *input, const char *prefix, const char *pkey,
- const char *cert, const __u8 salt[32])
+static int sign_root(const char *input, const char *output, const char *pkey,
+ const char *cert)
{
- static const char *const suffix[] = {".img", ".verity", ".roothash",
- ".p7s"};
__u8 signature[SHIRUDO_CONFIG_IMAGE_SIG_MAX];
- struct shirudo_config_image image;
struct shirudo_sign_io sign = {};
- char paths[4][PATH_MAX], root[65];
- int fds[4] = {-1, -1, -1, -1};
+ char root[65] = {};
+ size_t done = 0;
struct stat st;
- int in = -1, rc;
- __u32 i;
+ ssize_t size;
+ int fd, rc;
if (shirudo_build_check_keys(pkey, cert))
return -EKEYREJECTED;
- for (i = 0; i < 4; i++) {
- rc = image_output_path(paths[i], sizeof(paths[i]), prefix,
- suffix[i]);
- if (rc)
- return rc;
- }
- in = open(input, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
- if (in < 0)
+ fd = open(input, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
+ if (fd < 0)
return -errno;
- if (fstat(in, &st)) {
- rc = -errno;
- goto out;
- }
- if (!S_ISREG(st.st_mode) || st.st_size <= 0 ||
- (__u64)st.st_size > SHIRUDO_CONFIG_IMAGE_MAX_BYTES ||
- st.st_size % SHIRUDO_CONFIG_IMAGE_BLOCK_SIZE) {
+ rc = fstat(fd, &st) ? -errno : 0;
+ if (!rc && (!S_ISREG(st.st_mode) || st.st_size != 64))
rc = -EINVAL;
- goto out;
- }
- for (i = 0; i < 4; i++) {
- fds[i] = open(paths[i], O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC,
- 0644);
- if (fds[i] < 0) {
- rc = -errno;
- goto out;
+ while (!rc && done < 64) {
+ size = read(fd, root + done, 64 - done);
+ if (size < 0) {
+ if (errno != EINTR)
+ rc = -errno;
+ } else if (!size) {
+ rc = -EIO;
+ } else {
+ done += size;
}
}
- rc = copy_image(in, fds[0], st.st_size);
- if (!rc)
- rc = format_image(fds[0], fds[1], st.st_size, salt,
- &image);
+ close(fd);
if (rc)
- goto out;
- hex_encode(image.root_hash, sizeof(image.root_hash), root);
+ return rc;
+ for (done = 0; done < 64; done++)
+ if (!((root[done] >= '0' && root[done] <= '9') ||
+ (root[done] >= 'a' && root[done] <= 'f')))
+ return -EINVAL;
sign.insns = root;
sign.insns_sz = 64;
sign.signature = signature;
@@ -298,39 +221,32 @@ static int build_image(const char *input, const char *prefix, const char *pkey,
if (!rc)
rc = verify_root_signature(root, signature, sign.signature_sz,
cert);
- if (!rc)
- rc = write_complete(fds[2], root, 64);
- if (!rc)
- rc = write_complete(fds[3], signature, sign.signature_sz);
- for (i = 0; !rc && i < 4; i++)
- if (fsync(fds[i]))
- rc = -errno;
- if (!rc)
- puts(root);
-out:
- if (in >= 0)
- close(in);
- for (i = 0; i < 4; i++) {
- if (fds[i] < 0)
- continue;
- close(fds[i]);
- if (rc)
- unlink(paths[i]);
- }
+ if (rc)
+ return rc;
+ fd = open(output, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644);
+ if (fd < 0)
+ return -errno;
+ rc = write_complete(fd, signature, sign.signature_sz);
+ if (!rc && fsync(fd))
+ rc = -errno;
+ close(fd);
+ if (rc)
+ unlink(output);
return rc;
}
int shirudo_build_image(int argc, char **argv)
{
- const char *pkey = SHIRUDO_SIGNING_KEY_PATH;
- const char *cert = SHIRUDO_CERT_PATH;
- const char *keys_dir = NULL, *salt_hex = NULL;
- const char *input = NULL, *prefix = NULL;
bool explicit_key = false, explicit_cert = false;
char pkey_path[PATH_MAX], cert_path[PATH_MAX];
- __u8 salt[SHIRUDO_CONFIG_IMAGE_SALT_SIZE];
+ const char *pkey = SHIRUDO_SIGNING_KEY_PATH;
+ const char *input = NULL, *prefix = NULL;
+ const char *cert = SHIRUDO_CERT_PATH;
+ const char *keys_dir = NULL;
int i, rc;
+ if (argc >= 2 && !strcmp(argv[1], "build"))
+ return build_image(argc, argv);
if (argc == 2 && !strcmp(argv[1], "--help")) {
image_usage(stdout);
return 0;
@@ -342,7 +258,7 @@ int shirudo_build_image(int argc, char **argv)
pr_err("image verify: %s", strerror(-rc));
return rc ? 1 : 0;
}
- if (argc < 2 || strcmp(argv[1], "build")) {
+ if (argc < 2 || strcmp(argv[1], "sign-root")) {
image_usage(stderr);
return 2;
}
@@ -363,12 +279,6 @@ int shirudo_build_image(int argc, char **argv)
} else if (!strcmp(arg, "--cert") && i + 1 < argc) {
cert = argv[++i];
explicit_cert = true;
- } else if (!strcmp(arg, "--salt") && i + 1 < argc) {
- salt_hex = argv[++i];
- } else if (!strcmp(arg, "--kind") && i + 1 < argc) {
- arg = argv[++i];
- if (strcmp(arg, "config") && strcmp(arg, "runtime"))
- return 2;
} else if (arg[0] != '-' && !input) {
input = arg;
} else {
@@ -389,15 +299,8 @@ int shirudo_build_image(int argc, char **argv)
if (!explicit_cert)
cert = cert_path;
}
- if (salt_hex) {
- if (strlen(salt_hex) != 64 ||
- hex_decode(salt_hex, salt, sizeof(salt)))
- return 2;
- } else if (RAND_bytes(salt, sizeof(salt)) != 1) {
- return 1;
- }
- rc = build_image(input, prefix, pkey, cert, salt);
+ rc = sign_root(input, prefix, pkey, cert);
if (rc)
- pr_err("image build: %s", strerror(-rc));
+ pr_err("image sign-root: %s", strerror(-rc));
return rc ? 1 : 0;
}