#!/bin/bash # shellcheck disable=SC2154 # SPDX-Version: 3.0 # SPDX-CreationInfo: 2025-10-11; WEIDNER, Marc S.; # SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.git # SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency # SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; # SPDX-FileType: SOURCE # SPDX-License-Identifier: LicenseRef-CNCL-1.1 OR LicenseRef-CCLA-1.1 # SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework. # SPDX-PackageName: CISS.debian.live.builder # SPDX-Security-Contact: security@coresecret.eu set -Ceuo pipefail # Final live-build binary hook for encrypted root filesystem packaging. It creates and signs a deterministic attestation # manifest for the final filesystem.squashfs byte stream, preallocates a LUKS2 container, formats it with the generated build # secret, copies the generated filesystem.squashfs into the opened encrypted mapping, then closes the container, shreds the # temporary LUKS secret, and removes the plaintext SquashFS from the ISO payload. printf "\e[95m🧪 '%s' starting ... \e[0m\n" "${0}" __umask=$(umask) umask 0077 ####################################### # Pre allocates space for LUKS container. # Globals: # None # Arguments: # 1: LUKS Container # 2: LUKS Container Size # Returns: # 0: on success # 42: on failure ####################################### preallocate() { declare file="$1" size="$2" declare -i blocksize=$((8*1024*1024)) declare -i blockcounter=$(( (size + blocksize - 1) / blocksize )) if fallocate -l "${size}" -- "${file}" 2>/dev/null; then printf "\e[92m✅ [fallocate -l %s -- %s] successful. \e[0m\n" "${size}" "${file}" return 0 else printf "\e[91m❌ [fallocate -l %s -- %s] NOT successful. \e[0m\n" "${size}" "${file}" fi if dd if=/dev/zero of="${file}" bs="${blocksize}" count="${blockcounter}" status=progress conv=fsync; then printf "\e[92m✅ [dd if=/dev/zero of=%s bs=%s count=%s status=progress conv=fsync] successful. \e[0m\n" "${file}" "${blocksize}" "${blockcounter}" return 0 else printf "\e[91m❌ [dd if=/dev/zero of=%s bs=%s count=%s status=progress conv=fsync] NOT successful. \e[0m\n" "${file}" "${blocksize}" "${blockcounter}" return 42 fi } ### Prevents accidental 'unset -f'. # shellcheck disable=SC2034 readonly -f preallocate ####################################### # Create and sign the rootfs attestation manifest for the exact SquashFS payload copied into the LUKS mapper. # Globals: # VAR_SIGNING_KEY_FPR # VAR_SIGNING_KEY_PASSFILE # VAR_VERIFY_KEYRING # Arguments: # 1: Final SquashFS payload file # 2: Manifest path below binary/live # Returns: # 0: on success # 42: on failure ####################################### create_attestation() { declare rootfs_file="${1}" declare rootfs_attestation="${2}" declare rootfs_hash="" declare rootfs_size="" rootfs_size="$(stat -c%s -- "${rootfs_file}")" rootfs_hash="$(LC_ALL=C sha512sum "${rootfs_file}")" rootfs_hash="${rootfs_hash%% *}" if printf '%s %s\n' "${rootfs_hash}" "${rootfs_file}" | LC_ALL=C sha512sum -c --strict --quiet; then printf "\e[92m✅ [LC_ALL=C sha512sum -c --strict --quiet of %s] successful. \e[0m\n" "${rootfs_file}" else printf "\e[91m❌ [LC_ALL=C sha512sum -c --strict --quiet of %s] NOT successful. \e[0m\n" "${rootfs_file}" return 42 fi # The attested boundary is the final SquashFS byte stream before LUKS wrapping. The boot verifier reads exactly this many # bytes from the decrypted mapper and intentionally excludes the LUKS allocation slack after the SquashFS payload. cat << EOF >| "${rootfs_attestation}" # CISS.debian.live.builder Master ${VAR_VERSION} # Attestation file for filesystem.squashfs Version 1.0.0 # Boundary : Final filesystem.squashfs byte stream copied into /dev/mapper/crypt_liveiso # Bytes : Final filesystem.squashfs ${rootfs_size} ${rootfs_hash} filesystem.squashfs EOF chmod 0444 "${rootfs_attestation}" if gpg --batch --yes --pinentry-mode loopback --passphrase-file "${VAR_SIGNING_KEY_PASSFILE}" --local-user "${VAR_SIGNING_KEY_FPR}" \ --detach-sign --output "${rootfs_attestation}.sig" "${rootfs_attestation}"; then printf "\e[92m✅ [gpg of %s] successful. \e[0m\n" "${rootfs_attestation}" else printf "\e[91m❌ [gpg of %s] NOT successful. \e[0m\n" "${rootfs_attestation}" return 42 fi chmod 0444 "${rootfs_attestation}.sig" if gpgv --keyring "${VAR_VERIFY_KEYRING}" "${rootfs_attestation}.sig" "${rootfs_attestation}"; then printf "\e[92m✅ [gpgv of %s] successful. \e[0m\n" "${rootfs_attestation}.sig" else printf "\e[91m❌ [gpgv of %s] NOT successful. \e[0m\n" "${rootfs_attestation}.sig" return 42 fi return 0 } ### Prevents accidental 'unset -f'. # shellcheck disable=SC2034 readonly -f create_attestation declare LUKSFS="${VAR_HANDLER_BUILD_DIR}/binary/live/ciss_rootfs.crypt" declare ROOTFS="${VAR_HANDLER_BUILD_DIR}/binary/live/filesystem.squashfs" declare ROOTFS_ATTESTATION="${VAR_HANDLER_BUILD_DIR}/binary/live/filesystem.squashfs.sha512sum.txt" declare DM_LAB="crypt_liveiso" declare DEVMAP="/dev/mapper/${DM_LAB}" declare LUKS_KEY_FILE="${VAR_TMP_SECRET}/${VAR_LUKS_KEY:-luks.txt}" declare KEYFD="" # Keep Argon2 keyslot memory and parallel costs bounded for later initramfs unlocks on smaller systems. declare -i LUKS_PBKDF_MEMORY_KIB=262144 declare -i LUKS_PBKDF_PARALLEL=1 # shellcheck disable=SC2155 declare -i VAR_ROOTFS_SIZE="$(stat -c%s -- "${ROOTFS}")" ### Attestation Boundary # - The attested boundary is the final SquashFS byte stream before LUKS wrapping. # - The boot verifier reads exactly this many bytes from the decrypted mapper and intentionally excludes the LUKS allocation # slack after the SquashFS payload. printf "\e[95m🧪 Attestation of filesystem.squashfs ... \e[0m\n" create_attestation "${ROOTFS}" "${ROOTFS_ATTESTATION}" printf "\e[92m✅ Attestation of filesystem.squashfs successful. \e[0m\n" ### Safety margin: # - LUKS2-Header and Metadata # - dm-integrity Overhead (Tags and Journal) # - Filesystem-Slack declare -i OVERHEAD_FIXED=$((64 * 1024 * 1024)) declare -i OVERHEAD_PCT=2 declare -i ALIGN_BYTES=$(( 4096 * 1024 )) declare -i BASE_SIZE=$(( VAR_ROOTFS_SIZE + OVERHEAD_FIXED + (VAR_ROOTFS_SIZE * OVERHEAD_PCT / 100) )) declare -i VAR_LUKSFS_SIZE=$(( ( (BASE_SIZE + ALIGN_BYTES - 1) / ALIGN_BYTES ) * ALIGN_BYTES )) preallocate "${LUKSFS}" "${VAR_LUKSFS_SIZE}" exec {KEYFD}<"${LUKS_KEY_FILE}" if [[ "${VAR_CDLB_INSIDE_RUNNER}" == "false" ]]; then cryptsetup luksFormat \ --batch-mode \ --cipher aes-xts-plain64 \ --integrity hmac-sha512 \ --iter-time 1000 \ --key-file "/proc/$$/fd/${KEYFD}" \ --key-size 512 \ --label "${DM_LAB}" \ --luks2-keyslots-size 16777216 \ --luks2-metadata-size 4194304 \ --pbkdf argon2id \ --pbkdf-memory "${LUKS_PBKDF_MEMORY_KIB}" \ --pbkdf-parallel "${LUKS_PBKDF_PARALLEL}" \ --sector-size 4096 \ --type luks2 \ --use-random \ --verbose \ "${LUKSFS}" elif [[ "${VAR_CDLB_INSIDE_RUNNER}" == "true" ]]; then cryptsetup luksFormat \ --batch-mode \ --cipher aes-xts-plain64 \ --integrity hmac-sha512 \ --iter-time 1000 \ --key-file "/proc/$$/fd/${KEYFD}" \ --key-size 512 \ --label "${DM_LAB}" \ --luks2-keyslots-size 16777216 \ --luks2-metadata-size 4194304 \ --pbkdf argon2id \ --pbkdf-memory "${LUKS_PBKDF_MEMORY_KIB}" \ --pbkdf-parallel "${LUKS_PBKDF_PARALLEL}" \ --sector-size 4096 \ --type luks2 \ --use-random \ --verbose \ "${LUKSFS}" fi cryptsetup open --key-file "/proc/$$/fd/${KEYFD}" "${LUKSFS}" "${DM_LAB}" # shellcheck disable=SC2155 declare -i LUKS_FREE=$(blockdev --getsize64 "${DEVMAP}") declare -i SQUASH_FS="${VAR_ROOTFS_SIZE}" if (( LUKS_FREE >= SQUASH_FS )); then printf "\e[92m✅ LUKS_FREE '%s' >= SQUASH_FS '%s' \e[0m\n" "${LUKS_FREE}" "${SQUASH_FS}" else printf "\e[91m❌ LUKS_FREE '%s' <= SQUASH_FS '%s' \e[0m\n" "${LUKS_FREE}" "${SQUASH_FS}" >&2 exit 42 fi dd if="${ROOTFS}" of="${DEVMAP}" bs=8M status=progress conv=fsync sync cryptsetup close "${DM_LAB}" exec {KEYFD}<&- shred -fzu -n 5 -- "${LUKS_KEY_FILE}" rm -f -- "${ROOTFS}" umask "${__umask}" __umask="" printf "\e[92m✅ '%s' applied successfully. \e[0m\n" "${0}" exit 0 # vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh