From efd662c63a18192b1c82597b17822c61dc28c00d Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sat, 29 Apr 2017 13:40:34 -0400 Subject: [PATCH 01/10] adds a USB boot option with basic parsing to kexec Supports booting from USB media using either the root device or a signed ISO as the boot device. Boot options are parsed with quick/dirty shell scripts to infer kexec params. Closes #195 and begins to address #196 --- config/purism13v1-qubes.config | 2 + config/x230-qubes.config | 2 + initrd/bin/usb-boot | 73 ++++++++++++++ initrd/bin/usb-init | 65 +++++++++++++ initrd/bin/usb-iso-init | 27 ++++++ initrd/bin/usb-parse-boot | 171 +++++++++++++++++++++++++++++++++ initrd/bin/usb-select-boot | 59 ++++++++++++ initrd/etc/functions | 35 +++++++ initrd/init | 10 +- 9 files changed, 443 insertions(+), 1 deletion(-) create mode 100755 initrd/bin/usb-boot create mode 100755 initrd/bin/usb-init create mode 100755 initrd/bin/usb-iso-init create mode 100755 initrd/bin/usb-parse-boot create mode 100755 initrd/bin/usb-select-boot diff --git a/config/purism13v1-qubes.config b/config/purism13v1-qubes.config index 5fc4584c15..21b248063b 100644 --- a/config/purism13v1-qubes.config +++ b/config/purism13v1-qubes.config @@ -19,6 +19,8 @@ CONFIG_LINUX_USB=y CONFIG_BOOTSCRIPT=/bin/qubes-init +CONFIG_USB_BOOT_DEV="/dev/sdb1" + # Disks encrypted by the TPM LUKS key CONFIG_QUBES_BOOT_DEV="/dev/sda1" CONFIG_QUBES_VG="qubes_dom0" diff --git a/config/x230-qubes.config b/config/x230-qubes.config index 29b2a0f85e..5323ba3cb7 100644 --- a/config/x230-qubes.config +++ b/config/x230-qubes.config @@ -20,6 +20,8 @@ CONFIG_LINUX_E1000E=y CONFIG_BOOTSCRIPT=/bin/qubes-init +CONFIG_USB_BOOT_DEV="/dev/sdb1" + # Disks encrypted by the TPM LUKS key CONFIG_QUBES_BOOT_DEV="/dev/sda1" CONFIG_QUBES_VG="qubes_dom0" diff --git a/initrd/bin/usb-boot b/initrd/bin/usb-boot new file mode 100755 index 0000000000..2b4b646bc4 --- /dev/null +++ b/initrd/bin/usb-boot @@ -0,0 +1,73 @@ +#!/bin/sh +. /etc/functions + +while getopts "b:e:r:a:" arg; do + case $arg in + b) bootdir="$OPTARG" ;; + e) entry="$OPTARG" ;; + r) cmdremove="$OPTARG" ;; + a) cmdadd="$OPTARG" ;; + esac +done + +kexectype=`echo $entry | cut -d\| -f2` +kexecparams=`echo $entry | cut -d\| -f3- | tr '|' '\n'` +kexeccmd="kexec" + +fix_file_path() { + filepath=`find $bootdir -path "*$firstval" | tail -1` + if ! [ -r $filepath ]; then + die "failed to find file $firstval" + fi +} + +adjust_cmd_line() { + if [ -n "$cmdremove" ]; then + for i in $cmdremove; do + cmdline="${cmdline//$i/}" + done + fi + + if [ -n "$cmdadd" ]; then + cmdline="$cmdline $cmdadd" + fi +} + +while read line +do + key=`echo $line | cut -d\ -f1` + firstval=`echo $line | cut -d\ -f2` + restval=`echo $line | cut -d\ -f3-` + if [ "$key" = "kernel" ]; then + fix_file_path + kexeccmd="$kexeccmd -l $filepath" + if [ "$kexectype" = "multiboot" ]; then + cmdline="$restval" + adjust_cmd_line + kexeccmd="$kexeccmd --command-line \"$cmdline\"" + fi + fi + if [ "$key" = "module" ]; then + fix_file_path + kexeccmd="$kexeccmd --module \"$filepath $restval\"" + fi + if [ "$key" = "initrd" ]; then + fix_file_path + kexeccmd="$kexeccmd --initrd=$filepath" + fi + if [ "$key" = "append" ]; then + cmdline="$firstval $restval" + adjust_cmd_line + kexeccmd="$kexeccmd --append=\"$cmdline\"" + fi +done << EOF +$kexecparams +EOF + +echo "Loading the new kernel:" +echo "$kexeccmd" +eval "$kexeccmd" \ +|| die "Failed to load the new kernel" + +echo "Starting the new kernel" +exec kexec -e diff --git a/initrd/bin/usb-init b/initrd/bin/usb-init new file mode 100755 index 0000000000..974e9552c8 --- /dev/null +++ b/initrd/bin/usb-init @@ -0,0 +1,65 @@ +#!/bin/sh +# Boot a Tails installation + +. /etc/functions +. /etc/config + +# Confirm we have a good TOTP unseal +if ! confirm_totp ; then + recovery 'Failed to unseal TOTP' +fi + +# Extend PCR4 as soon as possible +tpm extend -ix 4 -ic usb + +if [ ! "$totp_confirm" = "y" ]; then + recovery "Failed to confirm validity of TOTP" +fi + +# TODO: Do a scan of USB devices to detect the Tails USB +mount-usb "$CONFIG_USB_BOOT_DEV" + +# Check for ISO first +get_menu_option() { + echo "+++ Select your ISO boot option:" + n=0 + while read option + do + n=`expr $n + 1` + echo "$n. $option" + done < /tmp/iso_menu.txt + + read \ + -p "Choose the ISO boot option [1-$n, s for standard boot, a to abort]: " \ + option_index + + if [ "$option_index" = "a" ]; then + recovery "Aborting boot attempt" + fi + + if [ "$option_index" = "s" ]; then + option="" + return + fi + + option=`head -n $option_index /tmp/iso_menu.txt | tail -1` +} + +# create ISO menu options +ls -1r /media/*.iso 2>/dev/null > /tmp/iso_menu.txt +if [ `wc -l /tmp/iso_menu.txt | cut -d\ -f1` -gt 0 ]; then + option_confirm="" + while [ -z "$option" -a "$option_index" != "s" ] + do + get_menu_option + done + + if [ -n "$option" ]; then + exec usb-iso-init $option + fi +fi + +echo "!!! Could not find any ISO, trying bootable USB" +exec usb-select-boot /media + +recovery "Something failed..." diff --git a/initrd/bin/usb-iso-init b/initrd/bin/usb-iso-init new file mode 100755 index 0000000000..7e225f4891 --- /dev/null +++ b/initrd/bin/usb-iso-init @@ -0,0 +1,27 @@ +#!/bin/sh +# Boot from signed ISO + +. /etc/functions +. /etc/config + +ISO="$1" + +echo '+++ Verifying ISO' +# Verify the signature on the hashes +ISOSIG="$ISO.sig" +if ! [ -r "$ISOSIG" ]; then + ISOSIG="$ISO.asc" +fi + +gpgv "$ISOSIG" "$ISO" \ + || recovery 'ISO signature failed' + +echo '+++ Mounting ISO and booting' +mount -t iso9660 -o loop $ISO /boot + +ISO_FILE=${ISO:7} +DEV=$CONFIG_USB_BOOT_DEV +DEV_UUID=`blkid $DEV | tail -1 | tr " " "\n" | grep UUID | cut -d\" -f2` +ADD="fromiso=/dev/disk/by-uuid/$DEV_UUID/$ISO_FILE" + +/bin/usb-select-boot /boot "$ADD" diff --git a/initrd/bin/usb-parse-boot b/initrd/bin/usb-parse-boot new file mode 100755 index 0000000000..f7449dbbec --- /dev/null +++ b/initrd/bin/usb-parse-boot @@ -0,0 +1,171 @@ +#!/bin/sh +file=$1 +basefile=$(basename $file) + +reset_entry() { + name="" + kexectype="elf" + kernel="" + initrd="" + modules="" + append="" +} + +echo_entry() { + if [ "$kexectype" = "elf" ]; then + if [ -z "$kernel" -o -z "$initrd" ]; then return; fi + + entry="$name|$kexectype|kernel $kernel|initrd $initrd" + if [ -n "$append" ]; then entry="$entry|append $append"; fi + echo "$entry" + fi + if [ "$kexectype" = "multiboot" ]; then + if [ -z "$kernel" ]; then return; fi + + echo "$name|$kexectype|kernel $kernel$modules" + fi +} + +search_entry() { + case $line in + menuentry* | MENUENTRY* ) + state="grub" + reset_entry + name=`echo $line | tr "'" "\"" | cut -d\" -f 2` + ;; + + label* | LABEL* ) + state="syslinux" + reset_entry + esac +} + +grub_entry() { + if [ "$line" = "}" ]; then + echo_entry + state="search" + return + fi + + # add info to menuentry + trimcmd=`echo $line | tr '\t ' ' ' | tr -s ' '` + cmd=`echo $trimcmd | cut -d\ -f1` + val=`echo $trimcmd | cut -d\ -f2-` + case $cmd in + multiboot*) + kexectype="multiboot" + kernel="$val" + ;; + module*) + modules="$modules|module $val" + ;; + linux*) + kernel=`echo $trimcmd | cut -d\ -f2` + append=`echo $trimcmd | cut -d\ -f3-` + ;; + initrd*) + initrd="$val" + ;; + esac +} + +syslinux_end() { + # finish menuentry + + # attempt to parse out of append if missing initrd + if [ -z "$initrd" ]; then + newappend="" + for param in $append; do + case $param in + initrd=*) + initrd=`echo $param | cut -d\= -f2` + ;; + *) newappend="$newappend $param" ;; + esac + done + append="$newappend" + fi + + echo_entry + state="search" +} + +syslinux_multiboot_append() { + splitval=`echo "${val// --- /|}" | tr '|' '\n'` + while read line + do + if [ -z "$kernel" ]; then + kernel="$line" + else + modules="$modules|module $line" + fi + done << EOF +$splitval +EOF +} + +syslinux_entry() { + label_line=${line:0:5} + if [ -z "$line" ]; then + syslinux_end + fi + if [ "$label_line" = "label" -o "$label_line" = "LABEL" ]; then + search_entry + return + fi + + # add info to menuentry + trimcmd=`echo $line | tr '\t ' ' ' | tr -s ' '` + cmd=`echo $trimcmd | cut -d\ -f1` + val=`echo $trimcmd | cut -d\ -f2-` + case $trimcmd in + menu* | MENU* ) + cmd2=`echo $trimcmd | cut -d \ -f2` + if [ "$cmd2" = "label" -o "$cmd2" = "LABEL" ]; then + name=`echo ${trimcmd:11} | tr -d '^'` + fi + ;; + linux* | LINUX* | kernel* | KERNEL* ) + case $val in + *mboot.c32) kexectype="multiboot" ;; + *.c32) + # skip this entry + state="search" + ;; + *) + kernel="$val" + esac + ;; + initrd* | INITRD* ) + initrd="$val" + ;; + append* | APPEND* ) + if [ "$kexectype" = "multiboot" ]; then + syslinux_multiboot_append + else + append="$val" + fi + ;; + esac +} + +state="search" +while read line +do + case $state in + search) + search_entry + ;; + grub) + grub_entry + ;; + syslinux) + syslinux_entry + ;; + esac +done < $file + +# handle EOF case +if [ "$state" = "syslinux" ]; then + syslinux_end +fi diff --git a/initrd/bin/usb-select-boot b/initrd/bin/usb-select-boot new file mode 100755 index 0000000000..9f44673cff --- /dev/null +++ b/initrd/bin/usb-select-boot @@ -0,0 +1,59 @@ +#!/bin/sh +. /etc/functions + +bootdir=$1 +add=$2 + +get_menu_option() { + echo "+++ Select your boot option:" + n=0 + while read option + do + parse_option + n=`expr $n + 1` + echo "$n. $name [$kernel]" + done < /tmp/usb_menu.txt + + read \ + -p "Choose the boot option [1-$n, a to abort]: " \ + option_index + + if [ "$option_index" = "a" ]; then + recovery "Aborting boot attempt" + fi + + option=`head -n $option_index /tmp/usb_menu.txt | tail -1` + parse_option +} + +confirm_menu_option() { + echo "+++ Please confirm the boot details for $name:" + echo $option + + read \ + -n 1 \ + -p "Confirm selection by pressing 'y': " \ + option_confirm + echo +} + +parse_option() { + name=`echo $option | cut -d\| -f1` + kernel=`echo $option | cut -d\| -f3` +} + +echo "+++ Scanning USB media boot options" +for i in `find $bootdir -name "*.cfg"`; do usb-parse-boot $i >> /tmp/usb_options.txt; done +if [ ! -r /tmp/usb_options.txt ]; then + recovery "Failed to parse any boot options" +fi +sort /tmp/usb_options.txt | uniq > /tmp/usb_menu.txt + +option_confirm="" +while [ "$option_confirm" != "y" ] +do + get_menu_option + confirm_menu_option +done + +usb-boot -b $bootdir -e "$option" -a "intel_iommu=on $add" -r "quiet" diff --git a/initrd/etc/functions b/initrd/etc/functions index c8f9844b5f..73715ba554 100755 --- a/initrd/etc/functions +++ b/initrd/etc/functions @@ -28,3 +28,38 @@ recovery() { pcrs() { head -7 /sys/class/tpm/tpm0/pcrs } + +confirm_totp() +{ + last_half=X + + while true; do + + # update the TOTP code every thirty seconds + date=`date "+%Y-%m-%d %H:%M:%S"` + seconds=`date "+%s"` + half=`expr \( $seconds % 60 \) / 30` + if [ "$half" != "$last_half" ]; then + last_half=$half; + TOTP=`unseal-totp` \ + || recovery "TOTP code generation failed" + fi + + echo -n "$date $TOTP: " + + # read the first character, non-blocking + read \ + -t 1 \ + -n 1 \ + -s \ + -p "Confirm TOTP with a 'y': " \ + totp_confirm \ + && break + + # nothing typed, redraw the line + echo -ne '\r' + done + + # clean up with a newline + echo +} diff --git a/initrd/init b/initrd/init index 9875bba652..20bbbc3155 100755 --- a/initrd/init +++ b/initrd/init @@ -39,7 +39,7 @@ fi # Give the user a second to enter a recovery shell read \ -t "1" \ - -p "Press 'r' for recovery shell: " \ + -p "Press 'r' for recovery shell or 'u' for usb: " \ -n 1 \ boot_option echo @@ -52,6 +52,14 @@ if [ "$boot_option" = "r" ]; then exec /bin/ash fi +if [ "$boot_option" = "u" ]; then + echo '***** USB boot' + exec /bin/usb-init + # just in case... + tpm extend -ix 4 -ic recovery + exec /bin/ash +fi + echo '***** Normal boot' exec "$CONFIG_BOOTSCRIPT" From 1f8eaa696e792694f8fb866b6b3a0cb86a46a9e4 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sat, 29 Apr 2017 21:50:10 -0400 Subject: [PATCH 02/10] minor tweaks to config parsing --- initrd/bin/usb-parse-boot | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/initrd/bin/usb-parse-boot b/initrd/bin/usb-parse-boot index f7449dbbec..b380e08e22 100755 --- a/initrd/bin/usb-parse-boot +++ b/initrd/bin/usb-parse-boot @@ -13,7 +13,7 @@ reset_entry() { echo_entry() { if [ "$kexectype" = "elf" ]; then - if [ -z "$kernel" -o -z "$initrd" ]; then return; fi + if [ -z "$kernel" ]; then return; fi entry="$name|$kexectype|kernel $kernel|initrd $initrd" if [ -n "$append" ]; then entry="$entry|append $append"; fi @@ -57,6 +57,9 @@ grub_entry() { kernel="$val" ;; module*) + case $val in + --nounzip*) val=`echo $val | cut -d\ -f2-` ;; + esac modules="$modules|module $val" ;; linux*) @@ -105,14 +108,17 @@ EOF } syslinux_entry() { - label_line=${line:0:5} - if [ -z "$line" ]; then - syslinux_end - fi - if [ "$label_line" = "label" -o "$label_line" = "LABEL" ]; then - search_entry - return - fi + case $line in + "") + syslinux_end + return + ;; + label* | LABEL* ) + syslinux_end + search_entry + return + ;; + esac # add info to menuentry trimcmd=`echo $line | tr '\t ' ' ' | tr -s ' '` From 76a20288a33085d041f8396068528e413506e61a Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sun, 2 Jul 2017 14:27:02 -0400 Subject: [PATCH 03/10] Tweaks to allow qubes install w/o custom script usb-boot automatically uses internal xen binary / command line when multiboot is detected. also tweaked to evaluate/remove variable refs in kexec arguments --- initrd/bin/usb-boot | 18 +++++++++++++----- initrd/bin/usb-parse-boot | 8 +++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/initrd/bin/usb-boot b/initrd/bin/usb-boot index 2b4b646bc4..5b1fa06d71 100755 --- a/initrd/bin/usb-boot +++ b/initrd/bin/usb-boot @@ -33,6 +33,7 @@ adjust_cmd_line() { fi } +first_module=y while read line do key=`echo $line | cut -d\ -f1` @@ -40,16 +41,23 @@ do restval=`echo $line | cut -d\ -f3-` if [ "$key" = "kernel" ]; then fix_file_path - kexeccmd="$kexeccmd -l $filepath" if [ "$kexectype" = "multiboot" ]; then - cmdline="$restval" - adjust_cmd_line - kexeccmd="$kexeccmd --command-line \"$cmdline\"" + # always overload xen and with custom arguments + # TODO: control this replacement via flag + kexeccmd="$kexeccmd -l /bin/xen.gz" + kexeccmd="$kexeccmd --command-line \"no-real-mode reboot=no\"" + else + kexeccmd="$kexeccmd -l $filepath" fi fi if [ "$key" = "module" ]; then fix_file_path - kexeccmd="$kexeccmd --module \"$filepath $restval\"" + cmdline="$restval" + if [ -n $first_module ]; then + adjust_cmd_line + unset first_module + fi + kexeccmd="$kexeccmd --module \"$filepath $cmdline\"" fi if [ "$key" = "initrd" ]; then fix_file_path diff --git a/initrd/bin/usb-parse-boot b/initrd/bin/usb-parse-boot index b380e08e22..bb52ab29b1 100755 --- a/initrd/bin/usb-parse-boot +++ b/initrd/bin/usb-parse-boot @@ -15,14 +15,16 @@ echo_entry() { if [ "$kexectype" = "elf" ]; then if [ -z "$kernel" ]; then return; fi - entry="$name|$kexectype|kernel $kernel|initrd $initrd" + entry="$name|$kexectype|kernel $kernel" + if [ -n "$initrd" ]; then entry="$entry|initrd $initrd"; fi if [ -n "$append" ]; then entry="$entry|append $append"; fi - echo "$entry" + + echo $(eval "echo \"$entry\"") fi if [ "$kexectype" = "multiboot" ]; then if [ -z "$kernel" ]; then return; fi - echo "$name|$kexectype|kernel $kernel$modules" + echo $(eval "echo \"$name|$kexectype|kernel $kernel$modules\"") fi } From 3614044fffbf702c49e79e48e12dcd9988a8cbc3 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sun, 2 Jul 2017 23:01:04 -0400 Subject: [PATCH 04/10] Added a generic boot config and persistent params Refactored boot parsing code and applied that in local-init to scan /boot for grub options and allow the user to unsafely boot anything. This goes a long way to addressing #196. Optionally the user can customize those boot parameters or enforce arbitrary hashes on the boot device by creating and signing config files in /boot/ or /media/ or /media/kexec_iso/ISO_FILENAME/. --- config/x230-generic.config | 24 ++++++ initrd/bin/{usb-boot => kexec-boot} | 4 + initrd/bin/kexec-check-config | 22 +++++ initrd/bin/kexec-iso-init | 42 ++++++++++ .../bin/{usb-parse-boot => kexec-parse-boot} | 0 initrd/bin/kexec-select-boot | 82 +++++++++++++++++++ initrd/bin/kexec-sign-config | 45 ++++++++++ initrd/bin/local-init | 29 +++++++ initrd/bin/usb-init | 16 ++-- initrd/bin/usb-iso-init | 27 ------ initrd/bin/usb-select-boot | 59 ------------- 11 files changed, 259 insertions(+), 91 deletions(-) create mode 100644 config/x230-generic.config rename initrd/bin/{usb-boot => kexec-boot} (94%) create mode 100755 initrd/bin/kexec-check-config create mode 100755 initrd/bin/kexec-iso-init rename initrd/bin/{usb-parse-boot => kexec-parse-boot} (100%) create mode 100755 initrd/bin/kexec-select-boot create mode 100755 initrd/bin/kexec-sign-config create mode 100755 initrd/bin/local-init delete mode 100755 initrd/bin/usb-iso-init delete mode 100755 initrd/bin/usb-select-boot diff --git a/config/x230-generic.config b/config/x230-generic.config new file mode 100644 index 0000000000..272c97e299 --- /dev/null +++ b/config/x230-generic.config @@ -0,0 +1,24 @@ +# Configuration for a x230 running non-Qubes +BOARD=x230 + +CONFIG_CRYPTSETUP=y +CONFIG_FLASHROM=y +CONFIG_GPG=y +CONFIG_KEXEC=y +CONFIG_UTIL_LINUX=y +CONFIG_LVM2=y +CONFIG_MBEDTLS=y +CONFIG_PCIUTILS=y +CONFIG_POPT=y +CONFIG_QRENCODE=y +CONFIG_TPMTOTP=y +CONFIG_XEN=y +CONFIG_DROPBEAR=y + +CONFIG_LINUX_USB=y +CONFIG_LINUX_E1000E=y + +CONFIG_BOOTSCRIPT=/bin/local-init + +CONFIG_BOOT_DEV="/dev/sda1" +CONFIG_USB_BOOT_DEV="/dev/sdb1" diff --git a/initrd/bin/usb-boot b/initrd/bin/kexec-boot similarity index 94% rename from initrd/bin/usb-boot rename to initrd/bin/kexec-boot index 5b1fa06d71..eb9d095d7c 100755 --- a/initrd/bin/usb-boot +++ b/initrd/bin/kexec-boot @@ -14,6 +14,10 @@ kexectype=`echo $entry | cut -d\| -f2` kexecparams=`echo $entry | cut -d\| -f3- | tr '|' '\n'` kexeccmd="kexec" +# TODO: make this configurable +cmdadd="intel_iommu=on $cmdadd" +cmdremove="quiet $cmdremove" + fix_file_path() { filepath=`find $bootdir -path "*$firstval" | tail -1` if ! [ -r $filepath ]; then diff --git a/initrd/bin/kexec-check-config b/initrd/bin/kexec-check-config new file mode 100755 index 0000000000..386aebe169 --- /dev/null +++ b/initrd/bin/kexec-check-config @@ -0,0 +1,22 @@ +#!/bin/sh +# Check for valid kexec params and copy to tmp +. /etc/functions + +MEDIA="$1" +if [ ! -d /tmp/kexec ]; then + mkdir /tmp/kexec \ + || recovery 'failed to make kexec tmp dir' +fi + +if [ ! -r $MEDIA/kexec.sig ]; then + exit 0 +fi + +if [ -z `find $MEDIA/kexec*.txt` ]; then + exit 0 +fi + +if sha256sum `find $MEDIA/kexec*.txt` | gpg --verify $MEDIA/kexec.sig - ; then + echo "+++ Found verified kexec boot params" + cp $MEDIA/kexec*.txt /tmp/kexec +fi diff --git a/initrd/bin/kexec-iso-init b/initrd/bin/kexec-iso-init new file mode 100755 index 0000000000..ab8ffa175a --- /dev/null +++ b/initrd/bin/kexec-iso-init @@ -0,0 +1,42 @@ +#!/bin/sh +# Boot from signed ISO + +. /etc/functions +. /etc/config + +MOUNTED_ISO_PATH="$1" +ISO_PATH="$2" +DEV="$3" + +echo '+++ Verifying ISO' +# Verify the signature on the hashes +ISOSIG="$MOUNTED_ISO_PATH.sig" +if ! [ -r "$ISOSIG" ]; then + ISOSIG="$MOUNTED_ISO_PATH.asc" +fi + +gpgv "$ISOSIG" "$MOUNTED_ISO_PATH" \ + || recovery 'ISO signature failed' + +echo '+++ Mounting ISO and booting' +mount -t iso9660 -o loop $MOUNTED_ISO_PATH /boot \ + || recovery '$MOUNTED_ISO_PATH: Unable to mount /boot' + +DEV_UUID=`blkid $DEV | tail -1 | tr " " "\n" | grep UUID | cut -d\" -f2` +ADD="fromiso=/dev/disk/by-uuid/$DEV_UUID/$ISO_PATH" +REMOVE="" + +ADD_FILE=/tmp/kexec/kexec_iso_add.txt +if [ -r $ADD_FILE ]; then + NEW_ADD=`cat $ADD_FILE` + ADD=$(eval "echo \"$NEW_ADD\"") + echo "+++ Overriding standard ISO kernel add arguments: $ADD" +fi +REMOVE_FILE=/tmp/kexec/kexec_iso_remove.txt +if [ -r $REMOVE_FILE ]; then + NEW_REMOVE=`cat $REMOVE_FILE` + REMOVE=$(eval "echo \"$NEW_REMOVE\"") + echo "+++ Overriding standard ISO kernel remove arguments: $REMOVE"x +fi + +kexec-select-boot /boot "$ADD" "$REMOVE" diff --git a/initrd/bin/usb-parse-boot b/initrd/bin/kexec-parse-boot similarity index 100% rename from initrd/bin/usb-parse-boot rename to initrd/bin/kexec-parse-boot diff --git a/initrd/bin/kexec-select-boot b/initrd/bin/kexec-select-boot new file mode 100755 index 0000000000..3848dc712c --- /dev/null +++ b/initrd/bin/kexec-select-boot @@ -0,0 +1,82 @@ +#!/bin/sh +. /etc/functions + +bootdir=$1 +add=$2 +remove=$3 + +MENU_NAME="kexec_menu.txt" +HASH_NAME="kexec_hashes.txt" +TMP_MENU_FILE=/tmp/kexec/$MENU_NAME +TMP_HASH_FILE=/tmp/kexec/$HASH_NAME + +get_menu_option() { + echo "+++ Select your boot option:" + n=0 + while read option + do + parse_option + n=`expr $n + 1` + echo "$n. $name [$kernel]" + done < $TMP_MENU_FILE + + read \ + -p "Choose the boot option [1-$n, a to abort]: " \ + option_index + + if [ "$option_index" = "a" ]; then + recovery "Aborting boot attempt" + fi + + option=`head -n $option_index $TMP_MENU_FILE | tail -1` + parse_option +} + +confirm_menu_option() { + echo "+++ Please confirm the boot details for $name:" + echo $option + + read \ + -n 1 \ + -p "Confirm selection by pressing 'y': " \ + option_confirm + echo +} + +parse_option() { + name=`echo $option | cut -d\| -f1` + kernel=`echo $option | cut -d\| -f3` +} + +# optionally enforce file hashes +if [ -r $TMP_HASH_FILE ]; then + echo "+++ Checking verified boot hash file " + # Check the hashes of all the files + if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then + echo "+++ Verified boot hashes " + else + recovery "$TMP_HASH_FILE: boot hash mismatch" + fi +fi + +# otherwise scan the boot directory and generate options +if [ ! -r $TMP_MENU_FILE ]; then + echo "+++ Scanning for unsigned boot options" + option_file="/tmp/kexec_options.txt" + for i in `find $bootdir -name "*.cfg"`; do + kexec-parse-boot $i >> $option_file + done + if [ ! -r $option_file ]; then + recovery "Failed to parse any boot options" + fi + sort $option_file | uniq > $TMP_MENU_FILE +fi + +option_confirm="" +while [ "$option_confirm" != "y" ] +do + get_menu_option + confirm_menu_option +done + +kexec-boot -b $bootdir -e "$option" -a "$add" -r "$remove" diff --git a/initrd/bin/kexec-sign-config b/initrd/bin/kexec-sign-config new file mode 100755 index 0000000000..3a7b6e6101 --- /dev/null +++ b/initrd/bin/kexec-sign-config @@ -0,0 +1,45 @@ +#!/bin/sh +# Sign a valid directory of kexec params +. /etc/functions + +MEDIA="$1" + +if [ -z "$MEDIA" ]; then + die "Usage: $0 /boot " +fi + +# setup the USB so we can reach the GPG card +if ! lsmod | grep -q ehci_hcd; then + insmod /lib/modules/ehci-hcd.ko \ + || die "ehci_hcd: module load failed" +fi +if ! lsmod | grep -q ehci_pci; then + insmod /lib/modules/ehci-pci.ko \ + || die "ehci_pci: module load failed" +fi +if ! lsmod | grep -q xhci_hcd; then + insmod /lib/modules/xhci-hcd.ko \ + || die "ehci_hcd: module load failed" +fi +if ! lsmod | grep -q xhci_pci; then + insmod /lib/modules/xhci-pci.ko \ + || die "ehci_pci: module load failed" + sleep 2 +fi + +gpg --card-status \ +|| die "gpg card read failed" + +for tries in 1 2 3; do + if sha256sum `find $MEDIA/kexec*.txt` | gpg \ + --digest-algo SHA256 \ + --detach-sign \ + -a \ + > $MEDIA/kexec.sig \ + ; then + exit 0 + fi +done + +warn "$MEDIA: Unable to sign boot hashes" +exit 1 diff --git a/initrd/bin/local-init b/initrd/bin/local-init new file mode 100755 index 0000000000..559bda60f6 --- /dev/null +++ b/initrd/bin/local-init @@ -0,0 +1,29 @@ +#!/bin/sh +# Boot from a local disk installation + +. /etc/functions +. /etc/config + +# Confirm we have a good TOTP unseal +if ! confirm_totp ; then + recovery 'Failed to unseal TOTP' +fi + +# Extend PCR4 as soon as possible +tpm extend -ix 4 -ic local + +if [ ! "$totp_confirm" = "y" ]; then + recovery "Failed to confirm validity of TOTP" +fi + +# Mount local disk +if ! grep -q /boot /proc/mounts ; then + mount -o ro "$CONFIG_BOOT_DEV" /boot \ + || recovery '$CONFIG_BOOT_DEV: Unable to mount /boot' +fi + +# Attempt to pull verified config from device +kexec-check-config /boot +exec kexec-select-boot /boot + +recovery "Something failed..." diff --git a/initrd/bin/usb-init b/initrd/bin/usb-init index 974e9552c8..09c46aa416 100755 --- a/initrd/bin/usb-init +++ b/initrd/bin/usb-init @@ -1,5 +1,5 @@ #!/bin/sh -# Boot a Tails installation +# Boot a USB installation . /etc/functions . /etc/config @@ -16,8 +16,9 @@ if [ ! "$totp_confirm" = "y" ]; then recovery "Failed to confirm validity of TOTP" fi -# TODO: Do a scan of USB devices to detect the Tails USB -mount-usb "$CONFIG_USB_BOOT_DEV" +# Mount the USB boot device +mount-usb "$CONFIG_USB_BOOT_DEV" \ + || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' # Check for ISO first get_menu_option() { @@ -55,11 +56,16 @@ if [ `wc -l /tmp/iso_menu.txt | cut -d\ -f1` -gt 0 ]; then done if [ -n "$option" ]; then - exec usb-iso-init $option + MOUNTED_ISO=$option + ISO=${option:7} # remove /media/ to get device relative path + kexec-check-config /media/kexec_iso/$ISO + exec kexec-iso-init $MOUNTED_ISO $ISO $CONFIG_USB_BOOT_DEV fi fi echo "!!! Could not find any ISO, trying bootable USB" -exec usb-select-boot /media +# Attempt to pull verified config from device +kexec-check-config /media +exec kexec-select-boot /media recovery "Something failed..." diff --git a/initrd/bin/usb-iso-init b/initrd/bin/usb-iso-init deleted file mode 100755 index 7e225f4891..0000000000 --- a/initrd/bin/usb-iso-init +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh -# Boot from signed ISO - -. /etc/functions -. /etc/config - -ISO="$1" - -echo '+++ Verifying ISO' -# Verify the signature on the hashes -ISOSIG="$ISO.sig" -if ! [ -r "$ISOSIG" ]; then - ISOSIG="$ISO.asc" -fi - -gpgv "$ISOSIG" "$ISO" \ - || recovery 'ISO signature failed' - -echo '+++ Mounting ISO and booting' -mount -t iso9660 -o loop $ISO /boot - -ISO_FILE=${ISO:7} -DEV=$CONFIG_USB_BOOT_DEV -DEV_UUID=`blkid $DEV | tail -1 | tr " " "\n" | grep UUID | cut -d\" -f2` -ADD="fromiso=/dev/disk/by-uuid/$DEV_UUID/$ISO_FILE" - -/bin/usb-select-boot /boot "$ADD" diff --git a/initrd/bin/usb-select-boot b/initrd/bin/usb-select-boot deleted file mode 100755 index 9f44673cff..0000000000 --- a/initrd/bin/usb-select-boot +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/sh -. /etc/functions - -bootdir=$1 -add=$2 - -get_menu_option() { - echo "+++ Select your boot option:" - n=0 - while read option - do - parse_option - n=`expr $n + 1` - echo "$n. $name [$kernel]" - done < /tmp/usb_menu.txt - - read \ - -p "Choose the boot option [1-$n, a to abort]: " \ - option_index - - if [ "$option_index" = "a" ]; then - recovery "Aborting boot attempt" - fi - - option=`head -n $option_index /tmp/usb_menu.txt | tail -1` - parse_option -} - -confirm_menu_option() { - echo "+++ Please confirm the boot details for $name:" - echo $option - - read \ - -n 1 \ - -p "Confirm selection by pressing 'y': " \ - option_confirm - echo -} - -parse_option() { - name=`echo $option | cut -d\| -f1` - kernel=`echo $option | cut -d\| -f3` -} - -echo "+++ Scanning USB media boot options" -for i in `find $bootdir -name "*.cfg"`; do usb-parse-boot $i >> /tmp/usb_options.txt; done -if [ ! -r /tmp/usb_options.txt ]; then - recovery "Failed to parse any boot options" -fi -sort /tmp/usb_options.txt | uniq > /tmp/usb_menu.txt - -option_confirm="" -while [ "$option_confirm" != "y" ] -do - get_menu_option - confirm_menu_option -done - -usb-boot -b $bootdir -e "$option" -a "intel_iommu=on $add" -r "quiet" From ce4b91cad98f11f696bd320a4d6990cf831f1e3a Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Mon, 3 Jul 2017 13:07:03 -0400 Subject: [PATCH 05/10] Minor tweaks to signing params and boot options Also split out usb-scan to allow manual initiation of scan from the recovery shell --- initrd/bin/kexec-check-config | 5 ++- initrd/bin/kexec-select-boot | 39 ++++++++++++++-------- initrd/bin/local-init | 4 +-- initrd/bin/usb-init | 53 +----------------------------- initrd/bin/usb-scan | 61 +++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 69 deletions(-) create mode 100755 initrd/bin/usb-scan diff --git a/initrd/bin/kexec-check-config b/initrd/bin/kexec-check-config index 386aebe169..f614583747 100755 --- a/initrd/bin/kexec-check-config +++ b/initrd/bin/kexec-check-config @@ -6,13 +6,16 @@ MEDIA="$1" if [ ! -d /tmp/kexec ]; then mkdir /tmp/kexec \ || recovery 'failed to make kexec tmp dir' +else + rm -rf /tmp/kexec/* \ + || recovert 'failed to empty kexec tmp dir' fi if [ ! -r $MEDIA/kexec.sig ]; then exit 0 fi -if [ -z `find $MEDIA/kexec*.txt` ]; then +if [ `find $MEDIA/kexec*.txt | wc -l` -eq 0 ]; then exit 0 fi diff --git a/initrd/bin/kexec-select-boot b/initrd/bin/kexec-select-boot index 3848dc712c..391c5fedb0 100755 --- a/initrd/bin/kexec-select-boot +++ b/initrd/bin/kexec-select-boot @@ -10,23 +10,34 @@ HASH_NAME="kexec_hashes.txt" TMP_MENU_FILE=/tmp/kexec/$MENU_NAME TMP_HASH_FILE=/tmp/kexec/$HASH_NAME +first_menu="y" get_menu_option() { - echo "+++ Select your boot option:" - n=0 - while read option - do - parse_option - n=`expr $n + 1` - echo "$n. $name [$kernel]" - done < $TMP_MENU_FILE + num_options=`cat $TMP_MENU_FILE | wc -l` + if [ $num_options -eq 0 ]; then + recovery "No boot options" + fi - read \ - -p "Choose the boot option [1-$n, a to abort]: " \ - option_index + if [ $num_options -eq 1 -a $first_menu = "y" ]; then + option_index=1 + else + echo "+++ Select your boot option:" + n=0 + while read option + do + parse_option + n=`expr $n + 1` + echo "$n. $name [$kernel]" + done < $TMP_MENU_FILE + + read \ + -p "Choose the boot option [1-$n, a to abort]: " \ + option_index - if [ "$option_index" = "a" ]; then - recovery "Aborting boot attempt" + if [ "$option_index" = "a" ]; then + recovery "Aborting boot attempt" + fi fi + first_menu="n" option=`head -n $option_index $TMP_MENU_FILE | tail -1` parse_option @@ -59,7 +70,7 @@ if [ -r $TMP_HASH_FILE ]; then fi fi -# otherwise scan the boot directory and generate options +# if no saved options, scan the boot directory and generate if [ ! -r $TMP_MENU_FILE ]; then echo "+++ Scanning for unsigned boot options" option_file="/tmp/kexec_options.txt" diff --git a/initrd/bin/local-init b/initrd/bin/local-init index 559bda60f6..c410ecec4d 100755 --- a/initrd/bin/local-init +++ b/initrd/bin/local-init @@ -23,7 +23,7 @@ if ! grep -q /boot /proc/mounts ; then fi # Attempt to pull verified config from device -kexec-check-config /boot -exec kexec-select-boot /boot +kexec-check-config /boot/ +kexec-select-boot /boot/ recovery "Something failed..." diff --git a/initrd/bin/usb-init b/initrd/bin/usb-init index 09c46aa416..36f219e438 100755 --- a/initrd/bin/usb-init +++ b/initrd/bin/usb-init @@ -16,56 +16,5 @@ if [ ! "$totp_confirm" = "y" ]; then recovery "Failed to confirm validity of TOTP" fi -# Mount the USB boot device -mount-usb "$CONFIG_USB_BOOT_DEV" \ - || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' - -# Check for ISO first -get_menu_option() { - echo "+++ Select your ISO boot option:" - n=0 - while read option - do - n=`expr $n + 1` - echo "$n. $option" - done < /tmp/iso_menu.txt - - read \ - -p "Choose the ISO boot option [1-$n, s for standard boot, a to abort]: " \ - option_index - - if [ "$option_index" = "a" ]; then - recovery "Aborting boot attempt" - fi - - if [ "$option_index" = "s" ]; then - option="" - return - fi - - option=`head -n $option_index /tmp/iso_menu.txt | tail -1` -} - -# create ISO menu options -ls -1r /media/*.iso 2>/dev/null > /tmp/iso_menu.txt -if [ `wc -l /tmp/iso_menu.txt | cut -d\ -f1` -gt 0 ]; then - option_confirm="" - while [ -z "$option" -a "$option_index" != "s" ] - do - get_menu_option - done - - if [ -n "$option" ]; then - MOUNTED_ISO=$option - ISO=${option:7} # remove /media/ to get device relative path - kexec-check-config /media/kexec_iso/$ISO - exec kexec-iso-init $MOUNTED_ISO $ISO $CONFIG_USB_BOOT_DEV - fi -fi - -echo "!!! Could not find any ISO, trying bootable USB" -# Attempt to pull verified config from device -kexec-check-config /media -exec kexec-select-boot /media - +usb-scan recovery "Something failed..." diff --git a/initrd/bin/usb-scan b/initrd/bin/usb-scan new file mode 100755 index 0000000000..5d3a2a03c3 --- /dev/null +++ b/initrd/bin/usb-scan @@ -0,0 +1,61 @@ +#!/bin/sh +# Scan for USB installation options + +. /etc/functions +. /etc/config + +# Mount the USB boot device +mount-usb "$CONFIG_USB_BOOT_DEV" \ + || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' + +# Check for ISO first +get_menu_option() { + echo "+++ Select your ISO boot option:" + n=0 + while read option + do + n=`expr $n + 1` + echo "$n. $option" + done < /tmp/iso_menu.txt + + read \ + -p "Choose the ISO boot option [1-$n, s for standard boot, a to abort]: " \ + option_index + + if [ "$option_index" = "a" ]; then + recovery "Aborting boot attempt" + fi + + if [ "$option_index" = "s" ]; then + option="" + return + fi + + option=`head -n $option_index /tmp/iso_menu.txt | tail -1` +} + +# create ISO menu options +ls -1r /media/*.iso 2>/dev/null > /tmp/iso_menu.txt +if [ `cat /tmp/iso_menu.txt | wc -l` -gt 0 ]; then + option_confirm="" + while [ -z "$option" -a "$option_index" != "s" ] + do + get_menu_option + done + + if [ -n "$option" ]; then + MOUNTED_ISO=$option + ISO=${option:7} # remove /media/ to get device relative path + kexec-check-config /media/kexec_iso/$ISO/ + kexec-iso-init $MOUNTED_ISO $ISO $CONFIG_USB_BOOT_DEV + + recovery "Something failed..." + fi +fi + +echo "!!! Could not find any ISO, trying bootable USB" +# Attempt to pull verified config from device +kexec-check-config /media/ +kexec-select-boot /media/ + +recovery "Something failed..." From 8004b5df2a4dd626a01f6c8b7b67abe222ebb134 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Tue, 4 Jul 2017 19:49:14 -0400 Subject: [PATCH 06/10] Added the ability to persist a default boot option Similar to qubes-update, it will save then verify the hashes of the kexec files. Once TOTP is verified, a normal boot will verify that the file hashes and all the kexec params match and if successful, boot directly to OS. Also added a config option to require hash verification for non-recovery boots, failing to recovery not met. --- config/x230-generic.config | 1 + initrd/bin/kexec-boot | 19 ++-- initrd/bin/kexec-iso-init | 5 +- initrd/bin/kexec-parse-boot | 16 ++-- initrd/bin/kexec-save-default | 48 ++++++++++ initrd/bin/kexec-select-boot | 161 +++++++++++++++++++++++++++------- initrd/bin/kexec-sign-config | 22 +---- initrd/bin/local-init | 3 +- initrd/bin/qubes-update | 22 +---- initrd/bin/usb-scan | 16 ++-- initrd/etc/functions | 25 ++++++ 11 files changed, 244 insertions(+), 94 deletions(-) create mode 100755 initrd/bin/kexec-save-default diff --git a/config/x230-generic.config b/config/x230-generic.config index 272c97e299..28ec1f1944 100644 --- a/config/x230-generic.config +++ b/config/x230-generic.config @@ -20,5 +20,6 @@ CONFIG_LINUX_E1000E=y CONFIG_BOOTSCRIPT=/bin/local-init +CONFIG_BOOT_REQ_HASH=n CONFIG_BOOT_DEV="/dev/sda1" CONFIG_USB_BOOT_DEV="/dev/sdb1" diff --git a/initrd/bin/kexec-boot b/initrd/bin/kexec-boot index eb9d095d7c..b9b67def13 100755 --- a/initrd/bin/kexec-boot +++ b/initrd/bin/kexec-boot @@ -1,12 +1,14 @@ #!/bin/sh . /etc/functions -while getopts "b:e:r:a:" arg; do +dryrun="n" +while getopts "b:e:r:a:d" arg; do case $arg in b) bootdir="$OPTARG" ;; e) entry="$OPTARG" ;; r) cmdremove="$OPTARG" ;; a) cmdadd="$OPTARG" ;; + d) dryrun="y" ;; esac done @@ -19,6 +21,8 @@ cmdadd="intel_iommu=on $cmdadd" cmdremove="quiet $cmdremove" fix_file_path() { + if [ "$dryrun" = "y" ]; then echo ".$firstval"; fi + filepath=`find $bootdir -path "*$firstval" | tail -1` if ! [ -r $filepath ]; then die "failed to find file $firstval" @@ -44,20 +48,23 @@ do firstval=`echo $line | cut -d\ -f2` restval=`echo $line | cut -d\ -f3-` if [ "$key" = "kernel" ]; then - fix_file_path - if [ "$kexectype" = "multiboot" ]; then + if [ "$kexectype" = "xen" ]; then # always overload xen and with custom arguments - # TODO: control this replacement via flag kexeccmd="$kexeccmd -l /bin/xen.gz" kexeccmd="$kexeccmd --command-line \"no-real-mode reboot=no\"" + elif [ "$kexectype" = "multiboot" ]; then + fix_file_path + kexeccmd="$kexeccmd -l $filepath" + kexeccmd="$kexeccmd --command-line \"$restval\"" else + fix_file_path kexeccmd="$kexeccmd -l $filepath" fi fi if [ "$key" = "module" ]; then fix_file_path cmdline="$restval" - if [ -n $first_module ]; then + if [ -n "$first_module" ]; then adjust_cmd_line unset first_module fi @@ -76,6 +83,8 @@ done << EOF $kexecparams EOF +if [ "$dryrun" = "y" ]; then exit 0; fi + echo "Loading the new kernel:" echo "$kexeccmd" eval "$kexeccmd" \ diff --git a/initrd/bin/kexec-iso-init b/initrd/bin/kexec-iso-init index ab8ffa175a..1736699b69 100755 --- a/initrd/bin/kexec-iso-init +++ b/initrd/bin/kexec-iso-init @@ -36,7 +36,8 @@ REMOVE_FILE=/tmp/kexec/kexec_iso_remove.txt if [ -r $REMOVE_FILE ]; then NEW_REMOVE=`cat $REMOVE_FILE` REMOVE=$(eval "echo \"$NEW_REMOVE\"") - echo "+++ Overriding standard ISO kernel remove arguments: $REMOVE"x + echo "+++ Overriding standard ISO kernel remove arguments: $REMOVE" fi -kexec-select-boot /boot "$ADD" "$REMOVE" +# Call kexec and indicate that hashes have been verified +kexec-select-boot -b /boot/ -d /media/ -p "/media/kexec_iso/$ISO_PATH/" -a "$ADD" -r "$REMOVE" -c "*.cfg" -u -h diff --git a/initrd/bin/kexec-parse-boot b/initrd/bin/kexec-parse-boot index bb52ab29b1..fe003ef9d6 100755 --- a/initrd/bin/kexec-parse-boot +++ b/initrd/bin/kexec-parse-boot @@ -1,6 +1,5 @@ #!/bin/sh -file=$1 -basefile=$(basename $file) +file="$1" reset_entry() { name="" @@ -21,7 +20,7 @@ echo_entry() { echo $(eval "echo \"$entry\"") fi - if [ "$kexectype" = "multiboot" ]; then + if [ "$kexectype" = "multiboot" -o "$kexectype" = "xen" ]; then if [ -z "$kernel" ]; then return; fi echo $(eval "echo \"$name|$kexectype|kernel $kernel$modules\"") @@ -39,6 +38,7 @@ search_entry() { label* | LABEL* ) state="syslinux" reset_entry + name=`echo $line | cut -c6- ` esac } @@ -55,7 +55,8 @@ grub_entry() { val=`echo $trimcmd | cut -d\ -f2-` case $cmd in multiboot*) - kexectype="multiboot" + # TODO: differentiate between Xen and other multiboot kernels + kexectype="xen" kernel="$val" ;; module*) @@ -130,12 +131,13 @@ syslinux_entry() { menu* | MENU* ) cmd2=`echo $trimcmd | cut -d \ -f2` if [ "$cmd2" = "label" -o "$cmd2" = "LABEL" ]; then - name=`echo ${trimcmd:11} | tr -d '^'` + name=`echo $trimcmd | cut -c11- | tr -d '^'` fi ;; linux* | LINUX* | kernel* | KERNEL* ) case $val in - *mboot.c32) kexectype="multiboot" ;; + # TODO: differentiate between Xen and other multiboot kernels + *mboot.c32) kexectype="xen" ;; *.c32) # skip this entry state="search" @@ -148,7 +150,7 @@ syslinux_entry() { initrd="$val" ;; append* | APPEND* ) - if [ "$kexectype" = "multiboot" ]; then + if [ "$kexectype" = "multiboot" -o "$kexectype" = "xen" ]; then syslinux_multiboot_append else append="$val" diff --git a/initrd/bin/kexec-save-default b/initrd/bin/kexec-save-default new file mode 100755 index 0000000000..0b0586c384 --- /dev/null +++ b/initrd/bin/kexec-save-default @@ -0,0 +1,48 @@ +#!/bin/sh +# Save these options to be the persistent default +. /etc/functions + +while getopts "b:d:p:e:i:" arg; do + case $arg in + b) bootdir="$OPTARG" ;; + d) paramsdev="$OPTARG" ;; + p) paramsdir="$OPTARG" ;; + e) entry="$OPTARG" ;; + i) index="$OPTARG" ;; + esac +done + +if [ -z "$bootdir" -o -z "$entry" -o -z "$index" ]; then + die "Usage: $0 -b /boot/ -e \"boot params|...\" -i 1 " +fi + +if [ -z "$paramsdev" ]; then + paramsdev="$bootdir" +fi + +if [ -z "$paramsdir" ]; then + paramsdir="$bootdir" +fi + +ENTRY_FILE="$paramsdir/kexec_default.$index.txt" +HASH_FILE="$paramsdir/kexec_default_hashes.txt" + +# try to switch to rw mode +mount -o rw,remount $paramsdev + +if [ ! -d $paramsdir ]; then + mkdir -p $paramsdir +fi +rm "$paramsdir/kexec_default.*.txt" 2>/dev/null +echo "$entry" > $ENTRY_FILE +cd $bootdir && kexec-boot -e "$entry" -d | xargs sha256sum > $HASH_FILE +if [ ! -r $ENTRY_FILE -o ! -r $HASH_FILE ]; then + die "Failed to write default config" +fi + +if ! kexec-sign-config $paramsdir; then + die "Failed to sign default config" +fi + +# switch back to ro mode +mount -o ro,remount $paramsdev diff --git a/initrd/bin/kexec-select-boot b/initrd/bin/kexec-select-boot index 391c5fedb0..6dfa2da85c 100755 --- a/initrd/bin/kexec-select-boot +++ b/initrd/bin/kexec-select-boot @@ -1,14 +1,36 @@ #!/bin/sh +. /etc/config . /etc/functions -bootdir=$1 -add=$2 -remove=$3 +add="" +remove="" +config="*.cfg" +unique="n" +hashed="n" +while getopts "b:d:p:a:r:c:uh" arg; do + case $arg in + b) bootdir="$OPTARG" ;; + d) paramsdev="$OPTARG" ;; + p) paramsdir="$OPTARG" ;; + a) add="$OPTARG" ;; + r) remove="$OPTARG" ;; + c) config="$OPTARG" ;; + u) unique="y" ;; + h) hashed="y" ;; + esac +done + +if [ -z "$bootdir" ]; then + die "Usage: $0 -b /boot/" +fi -MENU_NAME="kexec_menu.txt" -HASH_NAME="kexec_hashes.txt" -TMP_MENU_FILE=/tmp/kexec/$MENU_NAME -TMP_HASH_FILE=/tmp/kexec/$HASH_NAME +if [ -z "$paramsdev" ]; then + paramsdev="$bootdir" +fi + +if [ -z "$paramsdir" ]; then + paramsdir="$bootdir" +fi first_menu="y" get_menu_option() { @@ -49,7 +71,7 @@ confirm_menu_option() { read \ -n 1 \ - -p "Confirm selection by pressing 'y': " \ + -p "Confirm selection by pressing 'y', make default with 'd': " \ option_confirm echo } @@ -59,35 +81,112 @@ parse_option() { kernel=`echo $option | cut -d\| -f3` } -# optionally enforce file hashes -if [ -r $TMP_HASH_FILE ]; then - echo "+++ Checking verified boot hash file " - # Check the hashes of all the files - if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then - echo "+++ Verified boot hashes " - else - recovery "$TMP_HASH_FILE: boot hash mismatch" - fi -fi - -# if no saved options, scan the boot directory and generate -if [ ! -r $TMP_MENU_FILE ]; then +scan_options() { echo "+++ Scanning for unsigned boot options" option_file="/tmp/kexec_options.txt" - for i in `find $bootdir -name "*.cfg"`; do + if [ -r $option_file ]; then rm $option_file; fi + for i in `find $bootdir -name "$config"`; do kexec-parse-boot $i >> $option_file done if [ ! -r $option_file ]; then recovery "Failed to parse any boot options" fi - sort $option_file | uniq > $TMP_MENU_FILE -fi + if [ "$unique" = 'y' ]; then + sort $option_file | uniq > $TMP_MENU_FILE + else + cp $option_file $TMP_MENU_FILE + fi +} -option_confirm="" -while [ "$option_confirm" != "y" ] -do - get_menu_option - confirm_menu_option -done +default_select() { + # Attempt boot with expected parameters + + # Check that entry matches that which is expected from menu + default_index=`basename "$TMP_DEFAULT_FILE" | cut -d. -f 2` + + # Check to see if entries have changed - useful for detecting grub update + expectedoption=`cat $TMP_DEFAULT_FILE` + option=`head -n $default_index $TMP_MENU_FILE | tail -1` + if [ "$option" != "$expectedoption" ]; then + recovery "Boot entry has changed: expected $expectedoption, found $option" + fi + parse_option + + # Enforce that default option hashes are valid + echo "+++ Checking verified default boot hash file " + # Check the hashes of all the files + if cd $bootdir && sha256sum -c "$TMP_DEFAULT_HASH_FILE" ; then + echo "+++ Verified default boot hashes " + else + recovery "$TMP_DEFAULT_HASH_FILE: default boot hash mismatch" + fi -kexec-boot -b $bootdir -e "$option" -a "$add" -r "$remove" + echo "+++ Executing default boot for $name:" + kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" + recovery "Something failed" +} + +user_select() { + # No default expected boot parameters, ask user + + # Optionally enforce device file hashes + if [ -r $TMP_HASH_FILE ]; then + echo "+++ Checking verified boot hash file " + # Check the hashes of all the files + if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then + echo "+++ Verified boot hashes " + hashed='y' + else + recovery "$TMP_HASH_FILE: boot hash mismatch" + fi + fi + + option_confirm="" + while [ "$option_confirm" != "y" -a "$option_confirm" != "d" ] + do + get_menu_option + confirm_menu_option + + if [ "$option_confirm" = 'd' ]; then + if ! kexec-save-default -b "$bootdir" -d "$paramsdev" -p "$paramsdir" -e "$option" -i "$option_index"; then + echo "!!!!!! Failed to save defaults" + else + echo "+++ Saved defaults to device" + sleep 2 + fi + fi + done + + if [ "$option_confirm" = "d" ]; then + # reload settings to reflect new default + continue + fi + + if [ "$CONFIG_BOOT_REQ_HASH" = "y" -a "$hashed" = "n" ]; then + recovery "!!!!!! Missing required boot hashes" + fi + + kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" + recovery "Something failed" +} + +while true; do + kexec-check-config $paramsdir + TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt" + TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt" + TMP_DEFAULT_FILE=`find /tmp/kexec/kexec_default.*.txt 2>/dev/null | head -1` + TMP_DEFAULT_HASH_FILE="/tmp/kexec/kexec_default_hashes.txt" + + # if no saved options, scan the boot directory and generate + if [ ! -r $TMP_MENU_FILE ]; then + scan_options + fi + + if [ -r "$TMP_DEFAULT_FILE" -a -r "$TMP_DEFAULT_HASH_FILE" ]; then + default_select + else + user_select + fi + + recovery "Something failed again" +done diff --git a/initrd/bin/kexec-sign-config b/initrd/bin/kexec-sign-config index 3a7b6e6101..d890a78a93 100755 --- a/initrd/bin/kexec-sign-config +++ b/initrd/bin/kexec-sign-config @@ -8,27 +8,7 @@ if [ -z "$MEDIA" ]; then die "Usage: $0 /boot " fi -# setup the USB so we can reach the GPG card -if ! lsmod | grep -q ehci_hcd; then - insmod /lib/modules/ehci-hcd.ko \ - || die "ehci_hcd: module load failed" -fi -if ! lsmod | grep -q ehci_pci; then - insmod /lib/modules/ehci-pci.ko \ - || die "ehci_pci: module load failed" -fi -if ! lsmod | grep -q xhci_hcd; then - insmod /lib/modules/xhci-hcd.ko \ - || die "ehci_hcd: module load failed" -fi -if ! lsmod | grep -q xhci_pci; then - insmod /lib/modules/xhci-pci.ko \ - || die "ehci_pci: module load failed" - sleep 2 -fi - -gpg --card-status \ -|| die "gpg card read failed" +confirm_gpg_card for tries in 1 2 3; do if sha256sum `find $MEDIA/kexec*.txt` | gpg \ diff --git a/initrd/bin/local-init b/initrd/bin/local-init index c410ecec4d..381e8ce69f 100755 --- a/initrd/bin/local-init +++ b/initrd/bin/local-init @@ -23,7 +23,6 @@ if ! grep -q /boot /proc/mounts ; then fi # Attempt to pull verified config from device -kexec-check-config /boot/ -kexec-select-boot /boot/ +kexec-select-boot -b /boot/ -c "grub.cfg" recovery "Something failed..." diff --git a/initrd/bin/qubes-update b/initrd/bin/qubes-update index 0b259a0cd3..a16c37c7b3 100755 --- a/initrd/bin/qubes-update +++ b/initrd/bin/qubes-update @@ -12,27 +12,7 @@ if [ -z "$XEN" -o -z "$KERNEL" -o -z "$INITRD" ]; then die "Usage: $0 /boot/xen... /boot/vmlinuz... /boot/initramfs..." fi -# setup the USB so we can reach the GPG card -if ! lsmod | grep -q ehci_hcd; then - insmod /lib/modules/ehci-hcd.ko \ - || die "ehci_hcd: module load failed" -fi -if ! lsmod | grep -q ehci_pci; then - insmod /lib/modules/ehci-pci.ko \ - || die "ehci_pci: module load failed" -fi -if ! lsmod | grep -q xhci_hcd; then - insmod /lib/modules/xhci-hcd.ko \ - || die "ehci_hcd: module load failed" -fi -if ! lsmod | grep -q xhci_pci; then - insmod /lib/modules/xhci-pci.ko \ - || die "ehci_pci: module load failed" - sleep 2 -fi - -gpg --card-status \ -|| die "gpg card read failed" +confirm_gpg_card # if the /boot.hashes file already exists, read the TPM counter ID # from it. diff --git a/initrd/bin/usb-scan b/initrd/bin/usb-scan index 5d3a2a03c3..2432d5dc68 100755 --- a/initrd/bin/usb-scan +++ b/initrd/bin/usb-scan @@ -4,9 +4,17 @@ . /etc/functions . /etc/config +# Unmount any previous boot device +if grep -q /boot /proc/mounts ; then + umount /boot \ + || recovery '$CONFIG_USB_BOOT_DEV: Unable to unmount /boot' +fi + # Mount the USB boot device -mount-usb "$CONFIG_USB_BOOT_DEV" \ - || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' +if ! grep -q /media /proc/mounts ; then + mount-usb "$CONFIG_USB_BOOT_DEV" \ + || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' +fi # Check for ISO first get_menu_option() { @@ -46,7 +54,6 @@ if [ `cat /tmp/iso_menu.txt | wc -l` -gt 0 ]; then if [ -n "$option" ]; then MOUNTED_ISO=$option ISO=${option:7} # remove /media/ to get device relative path - kexec-check-config /media/kexec_iso/$ISO/ kexec-iso-init $MOUNTED_ISO $ISO $CONFIG_USB_BOOT_DEV recovery "Something failed..." @@ -55,7 +62,6 @@ fi echo "!!! Could not find any ISO, trying bootable USB" # Attempt to pull verified config from device -kexec-check-config /media/ -kexec-select-boot /media/ +kexec-select-boot -b /media/ -c "*.cfg" -u recovery "Something failed..." diff --git a/initrd/etc/functions b/initrd/etc/functions index 73715ba554..72a7b3ad78 100755 --- a/initrd/etc/functions +++ b/initrd/etc/functions @@ -63,3 +63,28 @@ confirm_totp() # clean up with a newline echo } + +confirm_gpg_card() +{ + # setup the USB so we can reach the GPG card + if ! lsmod | grep -q ehci_hcd; then + insmod /lib/modules/ehci-hcd.ko \ + || die "ehci_hcd: module load failed" + fi + if ! lsmod | grep -q ehci_pci; then + insmod /lib/modules/ehci-pci.ko \ + || die "ehci_pci: module load failed" + fi + if ! lsmod | grep -q xhci_hcd; then + insmod /lib/modules/xhci-hcd.ko \ + || die "ehci_hcd: module load failed" + fi + if ! lsmod | grep -q xhci_pci; then + insmod /lib/modules/xhci-pci.ko \ + || die "ehci_pci: module load failed" + sleep 2 + fi + + gpg --card-status \ + || die "gpg card read failed" +} From d67360a24b215a65974253da6e8f18acd46a42bf Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sat, 8 Jul 2017 16:59:37 -0400 Subject: [PATCH 07/10] Added rollback protection to generic boot Changed the checking of required hashes or required rollback state to be right before boot, allowing the user to sign/set defaults in interactive mode. Also cleaned up usages of recovery and fixed iso parameter regression. --- config/x230-generic.config | 1 + initrd/bin/kexec-check-config | 25 ------ initrd/bin/kexec-iso-init | 12 ++- initrd/bin/kexec-save-default | 24 ++++-- initrd/bin/kexec-select-boot | 152 ++++++++++++++++++++++++++-------- initrd/bin/kexec-sign-config | 47 +++++++++-- initrd/bin/local-init | 4 +- initrd/bin/qubes-init | 2 +- initrd/bin/qubes-update | 25 +----- initrd/bin/usb-init | 2 +- initrd/bin/usb-scan | 10 +-- initrd/etc/functions | 64 ++++++++++++++ 12 files changed, 258 insertions(+), 110 deletions(-) delete mode 100755 initrd/bin/kexec-check-config diff --git a/config/x230-generic.config b/config/x230-generic.config index 28ec1f1944..b2797bb9e4 100644 --- a/config/x230-generic.config +++ b/config/x230-generic.config @@ -21,5 +21,6 @@ CONFIG_LINUX_E1000E=y CONFIG_BOOTSCRIPT=/bin/local-init CONFIG_BOOT_REQ_HASH=n +CONFIG_BOOT_REQ_ROLLBACK=n CONFIG_BOOT_DEV="/dev/sda1" CONFIG_USB_BOOT_DEV="/dev/sdb1" diff --git a/initrd/bin/kexec-check-config b/initrd/bin/kexec-check-config deleted file mode 100755 index f614583747..0000000000 --- a/initrd/bin/kexec-check-config +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh -# Check for valid kexec params and copy to tmp -. /etc/functions - -MEDIA="$1" -if [ ! -d /tmp/kexec ]; then - mkdir /tmp/kexec \ - || recovery 'failed to make kexec tmp dir' -else - rm -rf /tmp/kexec/* \ - || recovert 'failed to empty kexec tmp dir' -fi - -if [ ! -r $MEDIA/kexec.sig ]; then - exit 0 -fi - -if [ `find $MEDIA/kexec*.txt | wc -l` -eq 0 ]; then - exit 0 -fi - -if sha256sum `find $MEDIA/kexec*.txt` | gpg --verify $MEDIA/kexec.sig - ; then - echo "+++ Found verified kexec boot params" - cp $MEDIA/kexec*.txt /tmp/kexec -fi diff --git a/initrd/bin/kexec-iso-init b/initrd/bin/kexec-iso-init index 1736699b69..a1f5aa9d1c 100755 --- a/initrd/bin/kexec-iso-init +++ b/initrd/bin/kexec-iso-init @@ -16,16 +16,19 @@ if ! [ -r "$ISOSIG" ]; then fi gpgv "$ISOSIG" "$MOUNTED_ISO_PATH" \ - || recovery 'ISO signature failed' + || die 'ISO signature failed' echo '+++ Mounting ISO and booting' mount -t iso9660 -o loop $MOUNTED_ISO_PATH /boot \ - || recovery '$MOUNTED_ISO_PATH: Unable to mount /boot' + || die '$MOUNTED_ISO_PATH: Unable to mount /boot' DEV_UUID=`blkid $DEV | tail -1 | tr " " "\n" | grep UUID | cut -d\" -f2` ADD="fromiso=/dev/disk/by-uuid/$DEV_UUID/$ISO_PATH" REMOVE="" +paramsdir="/media/kexec_iso/$ISO_PATH/" +check_config $paramsdir + ADD_FILE=/tmp/kexec/kexec_iso_add.txt if [ -r $ADD_FILE ]; then NEW_ADD=`cat $ADD_FILE` @@ -40,4 +43,7 @@ if [ -r $REMOVE_FILE ]; then fi # Call kexec and indicate that hashes have been verified -kexec-select-boot -b /boot/ -d /media/ -p "/media/kexec_iso/$ISO_PATH/" -a "$ADD" -r "$REMOVE" -c "*.cfg" -u -h +kexec-select-boot -b /boot/ -d /media/ -p "$paramsdir" \ + -a "$ADD" -r "$REMOVE" -c "*.cfg" -u -i + +die "Something failed in selecting boot" diff --git a/initrd/bin/kexec-save-default b/initrd/bin/kexec-save-default index 0b0586c384..0f2d2e4d0a 100755 --- a/initrd/bin/kexec-save-default +++ b/initrd/bin/kexec-save-default @@ -7,13 +7,12 @@ while getopts "b:d:p:e:i:" arg; do b) bootdir="$OPTARG" ;; d) paramsdev="$OPTARG" ;; p) paramsdir="$OPTARG" ;; - e) entry="$OPTARG" ;; i) index="$OPTARG" ;; esac done -if [ -z "$bootdir" -o -z "$entry" -o -z "$index" ]; then - die "Usage: $0 -b /boot/ -e \"boot params|...\" -i 1 " +if [ -z "$bootdir" -o -z "$index" ]; then + die "Usage: $0 -b /boot/ -i menu_option " fi if [ -z "$paramsdev" ]; then @@ -24,14 +23,25 @@ if [ -z "$paramsdir" ]; then paramsdir="$bootdir" fi +TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt" ENTRY_FILE="$paramsdir/kexec_default.$index.txt" HASH_FILE="$paramsdir/kexec_default_hashes.txt" +if [ ! -r "$TMP_MENU_FILE" ]; then + die "No menu options available, please run kexec-select-boot" +fi + +entry=`head -n $index $TMP_MENU_FILE | tail -1` +if [ -z "$entry" ]; then + die "Invalid menu index $index" +fi + # try to switch to rw mode mount -o rw,remount $paramsdev if [ ! -d $paramsdir ]; then - mkdir -p $paramsdir + mkdir -p $paramsdir \ + || die "Failed to create params directory" fi rm "$paramsdir/kexec_default.*.txt" 2>/dev/null echo "$entry" > $ENTRY_FILE @@ -40,9 +50,9 @@ if [ ! -r $ENTRY_FILE -o ! -r $HASH_FILE ]; then die "Failed to write default config" fi -if ! kexec-sign-config $paramsdir; then - die "Failed to sign default config" -fi +# sign and auto-roll config counter +kexec-sign-config -p $paramsdir -u \ +|| die "Failed to sign default config" # switch back to ro mode mount -o ro,remount $paramsdev diff --git a/initrd/bin/kexec-select-boot b/initrd/bin/kexec-select-boot index 6dfa2da85c..12f014718c 100755 --- a/initrd/bin/kexec-select-boot +++ b/initrd/bin/kexec-select-boot @@ -1,4 +1,6 @@ #!/bin/sh +# Generic configurable boot script via kexec +set -o pipefail . /etc/config . /etc/functions @@ -6,8 +8,10 @@ add="" remove="" config="*.cfg" unique="n" -hashed="n" -while getopts "b:d:p:a:r:c:uh" arg; do +valid_hash="n" +valid_global_hash="n" +valid_rollback="n" +while getopts "b:d:p:a:r:c:ui" arg; do case $arg in b) bootdir="$OPTARG" ;; d) paramsdev="$OPTARG" ;; @@ -16,7 +20,7 @@ while getopts "b:d:p:a:r:c:uh" arg; do r) remove="$OPTARG" ;; c) config="$OPTARG" ;; u) unique="y" ;; - h) hashed="y" ;; + i) valid_hash="y"; valid_rollback="y" ;; esac done @@ -32,11 +36,40 @@ if [ -z "$paramsdir" ]; then paramsdir="$bootdir" fi +verify_global_hashes() +{ + echo "+++ Checking verified boot hash file " + # Check the hashes of all the files + if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then + echo "+++ Verified boot hashes " + valid_hash='y' + valid_global_hash='y' + else + die "$TMP_HASH_FILE: boot hash mismatch" + fi +} + +verify_rollback_counter() +{ + TPM_COUNTER=`grep counter $TMP_ROLLBACK_FILE | cut -d- -f2` + if [ -z "$TPM_COUNTER" ]; then + die "$TMP_ROLLBACK_FILE: TPM counter not found?" + fi + + read_tpm_counter $TPM_COUNTER \ + || die "Failed to read TPM counter" + + sha256sum -c $TMP_ROLLBACK_FILE \ + || die "Invalid TPM counter state" + + valid_rollback="y" +} + first_menu="y" get_menu_option() { num_options=`cat $TMP_MENU_FILE | wc -l` if [ $num_options -eq 0 ]; then - recovery "No boot options" + die "No boot options" fi if [ $num_options -eq 1 -a $first_menu = "y" ]; then @@ -56,7 +89,7 @@ get_menu_option() { option_index if [ "$option_index" = "a" ]; then - recovery "Aborting boot attempt" + die "Aborting boot attempt" fi fi first_menu="n" @@ -89,7 +122,7 @@ scan_options() { kexec-parse-boot $i >> $option_file done if [ ! -r $option_file ]; then - recovery "Failed to parse any boot options" + die "Failed to parse any boot options" fi if [ "$unique" = 'y' ]; then sort $option_file | uniq > $TMP_MENU_FILE @@ -98,6 +131,32 @@ scan_options() { fi } +save_default_option() { + read \ + -n 1 \ + -p "Saving a default will modify the disk. Proceed? (y/n): " \ + default_confirm + echo + + if [ "$default_confirm" = "y" ]; then + if kexec-save-default \ + -b "$bootdir" \ + -d "$paramsdev" \ + -p "$paramsdir" \ + -i "$option_index" \ + ; then + echo "+++ Saved defaults to device" + sleep 2 + default_failed="n" + return + else + echo "Failed to save defaults" + fi + fi + + option_confirm="n" +} + default_select() { # Attempt boot with expected parameters @@ -108,7 +167,9 @@ default_select() { expectedoption=`cat $TMP_DEFAULT_FILE` option=`head -n $default_index $TMP_MENU_FILE | tail -1` if [ "$option" != "$expectedoption" ]; then - recovery "Boot entry has changed: expected $expectedoption, found $option" + warn "!!! Boot entry has changed - please set a new default" + sleep 5 + return fi parse_option @@ -117,30 +178,19 @@ default_select() { # Check the hashes of all the files if cd $bootdir && sha256sum -c "$TMP_DEFAULT_HASH_FILE" ; then echo "+++ Verified default boot hashes " + valid_hash='y' else - recovery "$TMP_DEFAULT_HASH_FILE: default boot hash mismatch" + die "$TMP_DEFAULT_HASH_FILE: default boot hash mismatch" fi echo "+++ Executing default boot for $name:" - kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" - recovery "Something failed" + do_boot + warn "Failed to boot default option" } user_select() { # No default expected boot parameters, ask user - # Optionally enforce device file hashes - if [ -r $TMP_HASH_FILE ]; then - echo "+++ Checking verified boot hash file " - # Check the hashes of all the files - if cd $bootdir && sha256sum -c "$TMP_HASH_FILE" ; then - echo "+++ Verified boot hashes " - hashed='y' - else - recovery "$TMP_HASH_FILE: boot hash mismatch" - fi - fi - option_confirm="" while [ "$option_confirm" != "y" -a "$option_confirm" != "d" ] do @@ -148,12 +198,7 @@ user_select() { confirm_menu_option if [ "$option_confirm" = 'd' ]; then - if ! kexec-save-default -b "$bootdir" -d "$paramsdev" -p "$paramsdir" -e "$option" -i "$option_index"; then - echo "!!!!!! Failed to save defaults" - else - echo "+++ Saved defaults to device" - sleep 2 - fi + save_default_option fi done @@ -162,31 +207,66 @@ user_select() { continue fi - if [ "$CONFIG_BOOT_REQ_HASH" = "y" -a "$hashed" = "n" ]; then - recovery "!!!!!! Missing required boot hashes" + do_boot +} + +do_boot() +{ + if [ "$CONFIG_BOOT_REQ_ROLLBACK" = "y" -a "$valid_rollback" = "n" ]; then + warn "!!! Missing required rollback counter state" + return + fi + + if [ "$CONFIG_BOOT_REQ_HASH" = "y" -a "$valid_hash" = "n" ]; then + warn "!!! Missing required boot hashes" + return fi kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" - recovery "Something failed" + die "!!! Failed to boot w/ options: $option" } while true; do - kexec-check-config $paramsdir + check_config $paramsdir + TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt" TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt" TMP_DEFAULT_FILE=`find /tmp/kexec/kexec_default.*.txt 2>/dev/null | head -1` TMP_DEFAULT_HASH_FILE="/tmp/kexec/kexec_default_hashes.txt" + TMP_ROLLBACK_FILE="/tmp/kexec/kexec_rollback.txt" # if no saved options, scan the boot directory and generate - if [ ! -r $TMP_MENU_FILE ]; then + if [ ! -r "$TMP_MENU_FILE" ]; then scan_options fi - if [ -r "$TMP_DEFAULT_FILE" -a -r "$TMP_DEFAULT_HASH_FILE" ]; then + # Optionally enforce device file hashes + if [ -r "$TMP_HASH_FILE" ]; then + valid_global_hash="n" + + verify_global_hashes + + if [ "$valid_global_hash" = "n" ]; then + die "Failed to verify global hashes" + fi + fi + + if [ -r "$TMP_ROLLBACK_FILE" ]; then + # in the case of iso boot with a rollback file, do not assume valid + valid_rollback="n" + + verify_rollback_counter + fi + + if [ "$default_failed" != "y" \ + -a -r "$TMP_DEFAULT_FILE" \ + -a -r "$TMP_DEFAULT_HASH_FILE" ] \ + ; then default_select + default_failed="y" else user_select fi - - recovery "Something failed again" done + +die "!!! Shouldn't get here"" diff --git a/initrd/bin/kexec-sign-config b/initrd/bin/kexec-sign-config index d890a78a93..3b88595b75 100755 --- a/initrd/bin/kexec-sign-config +++ b/initrd/bin/kexec-sign-config @@ -2,24 +2,57 @@ # Sign a valid directory of kexec params . /etc/functions -MEDIA="$1" +rollback="n" +update_counter="n" +while getopts "p:c:u" arg; do + case $arg in + p) paramsdir="$OPTARG" ;; + c) counter="$OPTARG"; rollback="y" ;; + u) update_counter="y"; rollback="y" ;; + esac +done -if [ -z "$MEDIA" ]; then - die "Usage: $0 /boot " +if [ -z "$paramsdir" ]; then + die "Usage: $0 -p /boot/ [ -u | -c counter ]" fi confirm_gpg_card +if [ "$rollback" = "y" ]; then + rollback_file="$paramsdir/kexec_rollback.txt" + + if [ -n "$counter" ]; then + # use existing counter + read_tpm_counter $counter \ + || die "$paramsdir: Unable to read tpm counter '$counter'" + else + # increment counter + check_tpm_counter $rollback_file \ + || die "$paramsdir: Unable to find/create tpm counter" + counter="$TPM_COUNTER" + + increment_tpm_counter $counter \ + || die "$paramsdir: Unable to increment tpm counter" + fi + + sha256sum /tmp/counter-$counter > $rollback_file \ + || die "$paramsdir: Unable to create rollback file" +fi + +param_files=`find $paramsdir/kexec*.txt` +if [ -z "$param_files" ]; then + die "$paramsdir: No kexec parameter files to sign" +fi + for tries in 1 2 3; do - if sha256sum `find $MEDIA/kexec*.txt` | gpg \ + if sha256sum $param_files | gpg \ --digest-algo SHA256 \ --detach-sign \ -a \ - > $MEDIA/kexec.sig \ + > $paramsdir/kexec.sig \ ; then exit 0 fi done -warn "$MEDIA: Unable to sign boot hashes" -exit 1 +die "$paramsdir: Unable to sign kexec hashes" diff --git a/initrd/bin/local-init b/initrd/bin/local-init index 381e8ce69f..11da46c883 100755 --- a/initrd/bin/local-init +++ b/initrd/bin/local-init @@ -19,10 +19,10 @@ fi # Mount local disk if ! grep -q /boot /proc/mounts ; then mount -o ro "$CONFIG_BOOT_DEV" /boot \ - || recovery '$CONFIG_BOOT_DEV: Unable to mount /boot' + || recovery "$CONFIG_BOOT_DEV: Unable to mount /boot" fi # Attempt to pull verified config from device kexec-select-boot -b /boot/ -c "grub.cfg" -recovery "Something failed..." +recovery "Something failed during boot" diff --git a/initrd/bin/qubes-init b/initrd/bin/qubes-init index cf95d16bb1..e850b008d6 100755 --- a/initrd/bin/qubes-init +++ b/initrd/bin/qubes-init @@ -36,7 +36,7 @@ if [ -z "$TPM_COUNTER" ]; then recovery "$BOOT_HASHES: TPM counter not found?" fi -tpm counter_read -ix "$TPM_COUNTER" | tee "/tmp/counter-$TPM_COUNTER" +read_tpm_counter $TPM_COUNTER # Check the hashes of all the files sha256sum -c "$BOOT_HASHES" \ diff --git a/initrd/bin/qubes-update b/initrd/bin/qubes-update index a16c37c7b3..4a2258fdd9 100755 --- a/initrd/bin/qubes-update +++ b/initrd/bin/qubes-update @@ -14,33 +14,12 @@ fi confirm_gpg_card -# if the /boot.hashes file already exists, read the TPM counter ID -# from it. -if [ -r "$BOOT_HASHES" ]; then - TPM_COUNTER=`grep counter- "$BOOT_HASHES" | cut -d- -f2` -else - warn "$BOOT_HASHES does not exist; creating new TPM counter" - read -s -p "TPM Owner password: " tpm_password - echo - tpm counter_create \ - -pwdo "$tpm_password" \ - -pwdc '' \ - -la 3135106223 \ - | tee /tmp/counter \ - || die "Unable to create TPM counter" - TPM_COUNTER=`cut -d: -f1 < /tmp/counter` -fi - -if [ -z "$TPM_COUNTER" ]; then - die "$BOOT_HASHES: TPM Counter not found?" -fi +check_tpm_counter $BOOT_HASHES mount -o rw,remount /boot \ || die "Could not remount /boot" -tpm counter_increment -ix "$TPM_COUNTER" -pwdc '' \ - | tee /tmp/counter-$TPM_COUNTER \ -|| die "Counter increment failed" +increment_tpm_counter $TPM_COUNTER sha256sum \ "$XEN" \ diff --git a/initrd/bin/usb-init b/initrd/bin/usb-init index 36f219e438..21ed20af0e 100755 --- a/initrd/bin/usb-init +++ b/initrd/bin/usb-init @@ -17,4 +17,4 @@ if [ ! "$totp_confirm" = "y" ]; then fi usb-scan -recovery "Something failed..." +recovery "Something failed during USB boot" diff --git a/initrd/bin/usb-scan b/initrd/bin/usb-scan index 2432d5dc68..3e19feda2b 100755 --- a/initrd/bin/usb-scan +++ b/initrd/bin/usb-scan @@ -7,13 +7,13 @@ # Unmount any previous boot device if grep -q /boot /proc/mounts ; then umount /boot \ - || recovery '$CONFIG_USB_BOOT_DEV: Unable to unmount /boot' + || die '$CONFIG_USB_BOOT_DEV: Unable to unmount /boot' fi # Mount the USB boot device if ! grep -q /media /proc/mounts ; then mount-usb "$CONFIG_USB_BOOT_DEV" \ - || recovery '$CONFIG_USB_BOOT_DEV: Unable to mount /media' + || die '$CONFIG_USB_BOOT_DEV: Unable to mount /media' fi # Check for ISO first @@ -31,7 +31,7 @@ get_menu_option() { option_index if [ "$option_index" = "a" ]; then - recovery "Aborting boot attempt" + die "Aborting boot attempt" fi if [ "$option_index" = "s" ]; then @@ -56,7 +56,7 @@ if [ `cat /tmp/iso_menu.txt | wc -l` -gt 0 ]; then ISO=${option:7} # remove /media/ to get device relative path kexec-iso-init $MOUNTED_ISO $ISO $CONFIG_USB_BOOT_DEV - recovery "Something failed..." + die "Something failed in iso init" fi fi @@ -64,4 +64,4 @@ echo "!!! Could not find any ISO, trying bootable USB" # Attempt to pull verified config from device kexec-select-boot -b /media/ -c "*.cfg" -u -recovery "Something failed..." +die "Something failed in selecting boot" diff --git a/initrd/etc/functions b/initrd/etc/functions index 72a7b3ad78..3eda16d59b 100755 --- a/initrd/etc/functions +++ b/initrd/etc/functions @@ -88,3 +88,67 @@ confirm_gpg_card() gpg --card-status \ || die "gpg card read failed" } + + +check_tpm_counter() +{ + # if the /boot.hashes file already exists, read the TPM counter ID + # from it. + if [ -r "$1" ]; then + TPM_COUNTER=`grep counter- "$1" | cut -d- -f2` + else + warn "$BOOT_HASHES does not exist; creating new TPM counter" + read -s -p "TPM Owner password: " tpm_password + echo + tpm counter_create \ + -pwdo "$tpm_password" \ + -pwdc '' \ + -la 3135106223 \ + | tee /tmp/counter \ + || die "Unable to create TPM counter" + TPM_COUNTER=`cut -d: -f1 < /tmp/counter` + fi + + if [ -z "$TPM_COUNTER" ]; then + die "$1: TPM Counter not found?" + fi +} + +read_tpm_counter() +{ + tpm counter_read -ix "$1" | tee "/tmp/counter-$1" \ + || die "Counter read failed" +} + +increment_tpm_counter() +{ + tpm counter_increment -ix "$1" -pwdc '' \ + | tee /tmp/counter-$1 \ + || die "Counter increment failed" +} + +check_config() { + if [ ! -d /tmp/kexec ]; then + mkdir /tmp/kexec \ + || die 'Failed to make kexec tmp dir' + else + rm -rf /tmp/kexec/* \ + || die 'Failed to empty kexec tmp dir' + fi + + if [ ! -r $1/kexec.sig ]; then + return + fi + + if [ `find $1/kexec*.txt | wc -l` -eq 0 ]; then + return + fi + + if ! sha256sum `find $1/kexec*.txt` | gpgv $1/kexec.sig - ; then + die 'Invalid signature on kexec boot params' + fi + + echo "+++ Found verified kexec boot params" + cp $1/kexec*.txt /tmp/kexec \ + || die "Failed to copy kexec boot params to tmp" +} From 22a52ec4b82411198c1032caa6a722395b557d62 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Wed, 12 Jul 2017 00:17:45 -0400 Subject: [PATCH 08/10] Added TPM secret management to generic boot Also cleaned up error handling and boot parsing edge cases --- config/x230-generic.config | 2 +- initrd/bin/{local-init => generic-init} | 3 - initrd/bin/kexec-boot | 51 +++++++-- initrd/bin/kexec-insert-key | 61 ++++++++++ initrd/bin/kexec-iso-init | 2 +- initrd/bin/kexec-parse-boot | 37 ++++-- initrd/bin/kexec-save-default | 7 +- initrd/bin/kexec-save-key | 63 ++++++++++ initrd/bin/kexec-seal-key | 145 ++++++++++++++++++++++++ initrd/bin/kexec-select-boot | 34 +++++- initrd/bin/kexec-sign-config | 1 + initrd/bin/kexec-unseal-key | 45 ++++++++ initrd/bin/usb-scan | 4 +- 13 files changed, 424 insertions(+), 31 deletions(-) rename initrd/bin/{local-init => generic-init} (89%) create mode 100755 initrd/bin/kexec-insert-key create mode 100755 initrd/bin/kexec-save-key create mode 100755 initrd/bin/kexec-seal-key create mode 100755 initrd/bin/kexec-unseal-key diff --git a/config/x230-generic.config b/config/x230-generic.config index b2797bb9e4..966fdd29ff 100644 --- a/config/x230-generic.config +++ b/config/x230-generic.config @@ -18,7 +18,7 @@ CONFIG_DROPBEAR=y CONFIG_LINUX_USB=y CONFIG_LINUX_E1000E=y -CONFIG_BOOTSCRIPT=/bin/local-init +CONFIG_BOOTSCRIPT=/bin/generic-init CONFIG_BOOT_REQ_HASH=n CONFIG_BOOT_REQ_ROLLBACK=n diff --git a/initrd/bin/local-init b/initrd/bin/generic-init similarity index 89% rename from initrd/bin/local-init rename to initrd/bin/generic-init index 11da46c883..7b993068f0 100755 --- a/initrd/bin/local-init +++ b/initrd/bin/generic-init @@ -9,9 +9,6 @@ if ! confirm_totp ; then recovery 'Failed to unseal TOTP' fi -# Extend PCR4 as soon as possible -tpm extend -ix 4 -ic local - if [ ! "$totp_confirm" = "y" ]; then recovery "Failed to confirm validity of TOTP" fi diff --git a/initrd/bin/kexec-boot b/initrd/bin/kexec-boot index b9b67def13..77459bf222 100755 --- a/initrd/bin/kexec-boot +++ b/initrd/bin/kexec-boot @@ -1,17 +1,29 @@ #!/bin/sh +# Launches kexec from saved configuration entries +set -e -o pipefail . /etc/functions dryrun="n" -while getopts "b:e:r:a:d" arg; do +printfiles="n" +printinitrd="n" +while getopts "b:e:r:a:o:fi" arg; do case $arg in b) bootdir="$OPTARG" ;; e) entry="$OPTARG" ;; r) cmdremove="$OPTARG" ;; a) cmdadd="$OPTARG" ;; - d) dryrun="y" ;; + o) override_initrd="$OPTARG" ;; + f) dryrun="y"; printfiles="y" ;; + i) dryrun="y"; printinitrd="y" ;; esac done +if [ -z "$bootdir" -o -z "$entry" ]; then + die "Usage: $0 -b /boot/ -e 'kexec params|...|...'" +fi + +bootdir=${bootdir%%"/"} + kexectype=`echo $entry | cut -d\| -f2` kexecparams=`echo $entry | cut -d\| -f3- | tr '|' '\n'` kexeccmd="kexec" @@ -21,11 +33,15 @@ cmdadd="intel_iommu=on $cmdadd" cmdremove="quiet $cmdremove" fix_file_path() { - if [ "$dryrun" = "y" ]; then echo ".$firstval"; fi + if [ "$printfiles" = "y" ]; then + # output file relative to local boot directory + echo ".$firstval" + fi + + filepath="$bootdir$firstval" - filepath=`find $bootdir -path "*$firstval" | tail -1` if ! [ -r $filepath ]; then - die "failed to find file $firstval" + die "Failed to find file $firstval" fi } @@ -41,7 +57,7 @@ adjust_cmd_line() { fi } -first_module=y +module_number="1" while read line do key=`echo $line | cut -d\ -f1` @@ -64,14 +80,31 @@ do if [ "$key" = "module" ]; then fix_file_path cmdline="$restval" - if [ -n "$first_module" ]; then - adjust_cmd_line - unset first_module + if [ "$kexectype" = "xen" ]; then + if [ "$module_number" -eq 1 ]; then + adjust_cmd_line + elif [ "$module_number" -eq 2 ]; then + if [ "$printinitrd" = "y" ]; then + # output the current path to initrd + echo $filepath + fi + if [ -n "$override_initrd" ]; then + filepath="$override_initrd" + fi + fi fi + module_number=`expr $module_number + 1` kexeccmd="$kexeccmd --module \"$filepath $cmdline\"" fi if [ "$key" = "initrd" ]; then fix_file_path + if [ "$printinitrd" = "y" ]; then + # output the current path to initrd + echo $filepath + fi + if [ -n "$override_initrd" ]; then + filepath="$override_initrd" + fi kexeccmd="$kexeccmd --initrd=$filepath" fi if [ "$key" = "append" ]; then diff --git a/initrd/bin/kexec-insert-key b/initrd/bin/kexec-insert-key new file mode 100755 index 0000000000..ef403e3a45 --- /dev/null +++ b/initrd/bin/kexec-insert-key @@ -0,0 +1,61 @@ +#!/bin/sh +# Unseal a disk key from TPM and add to a new initramfs +set -e -o pipefail +. /etc/functions + +TMP_KEY_DEVICES="/tmp/kexec/kexec_key_devices.txt" +TMP_KEY_LVM="/tmp/kexec/kexec_key_lvm.txt" + +INITRD="$1" + +if [ -z "$INITRD" ]; then + die "Usage: $0 /boot/initramfs... " +fi + +if [ ! -r "$TMP_KEY_DEVICES" ]; then + die "No devices defined for disk encryption" +fi + +if [ -r "$TMP_KEY_LVM" ]; then + # Activate the LVM volume group + VOLUME_GROUP=`cat $TMP_KEY_LVM` + if [ -z "$TMP_KEY_LVM" ]; then + die "No LVM volume group defined for activation" + fi + lvm vgchange -a y $VOLUME_GROUP \ + || die "$VOLUME_GROUP: unable to activate volume group" +fi + +# Measure the LUKS headers before we unseal the disk key +cat "$TMP_KEY_DEVICES" | cut -d\ -f1 | xargs /bin/qubes-measure-luks \ + || die "LUKS measure failed" + +# Unpack the initrd and fixup the /etc/crypttab +# this is a hack to split it into two parts since +# we know that the first 0x3400 bytes are the microcode +INITRD_DIR=/tmp/secret/initrd +SECRET_CPIO=/tmp/secret/initrd.cpio +mkdir -p "$INITRD_DIR/etc" + +# Attempt to unseal the disk key from the TPM +# should we give this some number of tries? +if ! kexec-unseal-key "$INITRD_DIR/secret.key" ; then + die 'Unseal disk key failed' +fi + +# Override PCR 4 so that user can't read the key +tpm extend -ix 4 -ic generic \ + || die 'Unable to scramble PCR' + +echo '+++ Building initrd' +# pad the initramfs (dracut doesn't pad the last gz blob) +# without this the kernel init/initramfs.c fails to read +# the subsequent uncompressed/compressed cpio +dd if="$INITRD" of="$SECRET_CPIO" bs=512 conv=sync \ +|| die "Failed to copy initrd to /tmp" + +# overwrite /etc/crypttab to mirror the behavior for in seal-key +for uuid in `cat "$TMP_KEY_DEVICES" | cut -d\ -f2`; do + echo "luks-$uuid UUID=$uuid /secret.key" >> "$INITRD_DIR/etc/crypttab" +done +( cd "$INITRD_DIR" ; find . -type f | cpio -H newc -o ) >> "$SECRET_CPIO" diff --git a/initrd/bin/kexec-iso-init b/initrd/bin/kexec-iso-init index a1f5aa9d1c..95eb972f3d 100755 --- a/initrd/bin/kexec-iso-init +++ b/initrd/bin/kexec-iso-init @@ -1,6 +1,6 @@ #!/bin/sh # Boot from signed ISO - +set -e -o pipefail . /etc/functions . /etc/config diff --git a/initrd/bin/kexec-parse-boot b/initrd/bin/kexec-parse-boot index fe003ef9d6..4236c392fa 100755 --- a/initrd/bin/kexec-parse-boot +++ b/initrd/bin/kexec-parse-boot @@ -1,5 +1,8 @@ #!/bin/sh -file="$1" +set -e -o pipefail +bootdir="$1" +file="$2" +filedir=`dirname $file` reset_entry() { name="" @@ -10,20 +13,38 @@ reset_entry() { append="" } +bootdir=${bootdir%%"/"} +bootlen=${#bootdir} +appenddir=${filedir:$bootlen} + +fix_path() { + path="$@" + if [ "${path:0:1}" != "/" ]; then + path="$appenddir/$path" + fi +} + echo_entry() { if [ "$kexectype" = "elf" ]; then if [ -z "$kernel" ]; then return; fi - entry="$name|$kexectype|kernel $kernel" - if [ -n "$initrd" ]; then entry="$entry|initrd $initrd"; fi - if [ -n "$append" ]; then entry="$entry|append $append"; fi + fix_path $kernel + entry="$name|$kexectype|kernel $path" + if [ -n "$initrd" ]; then + fix_path $initrd + entry="$entry|initrd $path" + fi + if [ -n "$append" ]; then + entry="$entry|append $append" + fi echo $(eval "echo \"$entry\"") fi if [ "$kexectype" = "multiboot" -o "$kexectype" = "xen" ]; then if [ -z "$kernel" ]; then return; fi - echo $(eval "echo \"$name|$kexectype|kernel $kernel$modules\"") + fix_path $kernel + echo $(eval "echo \"$name|$kexectype|kernel $path$modules\"") fi } @@ -63,7 +84,8 @@ grub_entry() { case $val in --nounzip*) val=`echo $val | cut -d\ -f2-` ;; esac - modules="$modules|module $val" + fix_path $val + modules="$modules|module $path" ;; linux*) kernel=`echo $trimcmd | cut -d\ -f2` @@ -103,7 +125,8 @@ syslinux_multiboot_append() { if [ -z "$kernel" ]; then kernel="$line" else - modules="$modules|module $line" + fix_path $line + modules="$modules|module $path" fi done << EOF $splitval diff --git a/initrd/bin/kexec-save-default b/initrd/bin/kexec-save-default index 0f2d2e4d0a..b9aaa3f533 100755 --- a/initrd/bin/kexec-save-default +++ b/initrd/bin/kexec-save-default @@ -1,5 +1,6 @@ #!/bin/sh # Save these options to be the persistent default +set -e -o pipefail . /etc/functions while getopts "b:d:p:e:i:" arg; do @@ -43,9 +44,11 @@ if [ ! -d $paramsdir ]; then mkdir -p $paramsdir \ || die "Failed to create params directory" fi -rm "$paramsdir/kexec_default.*.txt" 2>/dev/null +rm "$paramsdir/kexec_default.*.txt" 2>/dev/null || true echo "$entry" > $ENTRY_FILE -cd $bootdir && kexec-boot -e "$entry" -d | xargs sha256sum > $HASH_FILE +cd $bootdir && kexec-boot -b "$bootdir" -e "$entry" -f | \ + xargs sha256sum > $HASH_FILE \ +|| die "Failed to create hashes of boot files" if [ ! -r $ENTRY_FILE -o ! -r $HASH_FILE ]; then die "Failed to write default config" fi diff --git a/initrd/bin/kexec-save-key b/initrd/bin/kexec-save-key new file mode 100755 index 0000000000..e7db449caa --- /dev/null +++ b/initrd/bin/kexec-save-key @@ -0,0 +1,63 @@ +#!/bin/sh +# Generate a TPM key used to unlock LUKS disks +set -e -o pipefail +. /etc/functions + +lvm_volume_group="" +while getopts "p:d:l:" arg; do + case $arg in + p) paramsdir="$OPTARG" ;; + d) paramsdev="$OPTARG" ;; + l) lvm_volume_group="$OPTARG" ;; + esac +done +shift `expr $OPTIND - 1` +key_devices="$@" + +if [ -z "$paramsdir" ]; then + die "Usage: $0 -p /boot/ [-l qubes_dom0] [/dev/sda2 /dev/sda5 ...] " +fi + +if [ -z "$paramsdev" ]; then + paramsdev="$paramsdir" +fi + +if [ -n "$lvm_volume_group" ]; then + lvm vgchange -a y $lvm_volume_group \ + || die "Failed to activate the LVM group" + + for dev in /dev/$lvm_volume_group/*; do + key_devices="$key_devices $dev" + done +fi + +if [ -z "$key_devices" ]; then + die "No devices specified for TPM key insertion" +fi + +# try to switch to rw mode +mount -o rw,remount $paramsdev + +rm -f $paramsdir/kexec_key_lvm.txt || true +if [ -n "$lvm_volume_group" ]; then + echo "$lvm_volume_group" > $paramsdir/kexec_key_lvm.txt \ + || die "Failed to write lvm group to key config " +fi + +rm -f $paramsdir/kexec_key_devices.txt || true +for dev in "$key_devices"; do + uuid=`cryptsetup luksUUID "$dev" 2>/dev/null` \ + || die "Failed to get UUID for device $dev" + echo "$dev $uuid" >> $paramsdir/kexec_key_devices.txt \ + || die "Failed to add $dev:$uuid to key devices config" +done + +kexec-seal-key $paramsdir \ +|| die "Failed to save and generate key in TPM" + +# sign and auto-roll config counter +kexec-sign-config -p $paramsdir -u \ +|| die "Failed to sign updated config" + +# switch back to ro mode +mount -o ro,remount $paramsdev diff --git a/initrd/bin/kexec-seal-key b/initrd/bin/kexec-seal-key new file mode 100755 index 0000000000..ec4ccb693c --- /dev/null +++ b/initrd/bin/kexec-seal-key @@ -0,0 +1,145 @@ +#!/bin/sh +# This will generate a disk encryption key and seal / ecncrypt +# with the current PCRs and then store it in the TPM NVRAM. +# It will then need to be bundled into initrd that is booted. +set -e -o pipefail + +TPM_INDEX=3 +TPM_SIZE=312 +KEY_FILE="/tmp/secret/secret.key" +TPM_SEALED="/tmp/secret/secret.sealed" +RECOVERY_KEY="/tmp/secret/recovery.key" + +. /etc/functions +. /etc/config + +paramsdir=$1 +if [ -z "$paramsdir" ]; then + die "Usage $0 /boot/" +fi + +KEY_DEVICES="$paramsdir/kexec_key_devices.txt" +KEY_LVM="$paramsdir/kexec_key_lvm.txt" + +if [ ! -r "$KEY_DEVICES" ]; then + die "No devices defined for disk encryption" +fi + +if [ -r "$KEY_LVM" ]; then + # Activate the LVM volume group + VOLUME_GROUP=`cat $KEY_LVM` + if [ -z "$VOLUME_GROUP" ]; then + die "No LVM volume group defined for activation" + fi + lvm vgchange -a y $VOLUME_GROUP \ + || die "$VOLUME_GROUP: unable to activate volume group" +fi + +# Key slot 0 is the manual recovery pass phrase +# that they user entered when they installed Qubes, +# key slot 1 is the one that we've generated. +read -s -p "Enter disk recovery key: " disk_password +echo -n "$disk_password" > "$RECOVERY_KEY" +echo + +read -s -p "New disk unlock password for booting: " key_password +echo +read -s -p "Repeat unlock code: " key_password2 +echo + +if [ "$key_password" != "$key_password2" ]; then + die "Key passwords do not match" +fi + +# Generate key file +dd \ + if=/dev/urandom \ + of="$KEY_FILE" \ + bs=1 \ + count=128 \ + 2>/dev/null \ +|| die "Unable to generate 128 random bytes" + +# Remove all the old keys from slot 1 +for dev in `cat "$KEY_DEVICES" | cut -d\ -f1`; do + echo "++++++ $dev: Removing old key slot" + cryptsetup luksKillSlot \ + --key-file "$RECOVERY_KEY" \ + $dev 1 \ + || warn "$dev: ignoring problem" + + echo "++++++ $dev: Adding key" + cryptsetup luksAddKey \ + --key-file "$RECOVERY_KEY" \ + --key-slot 1 \ + $dev "$KEY_FILE" \ + || die "$dev: Unable to add key" +done + +# Now that we have setup the new keys, measure the PCRs +# We don't care what ends up in PCR 6; we just want +# to get the /tmp/luksDump.txt file. We use PCR16 +# since it should still be zero +cat "$KEY_DEVICES" | cut -d\ -f1 | xargs /bin/qubes-measure-luks \ + || die "Unable to measure the LUKS headers" +luks_pcr=`tpm calcfuturepcr -ix 16 -if /tmp/luksDump.txt` + +# Note that PCR 4 needs to be set with the "normal-boot" +# path value, which we do not have right now since we are +# in a recovery shell. +# used to be -ix 4 f8fa3b6e32e7c6fe04c366e74636e505b28f3b0d \ +# now just all zeros in a normal boot +# PCR 5 must be all zero since no kernel modules should have +# been loaded during a normal boot, but might have been +# loaded in the recovery shell. +# Otherwise use the current values of the PCRs, which will be read +# from the TPM as part of the sealing ("X"). +tpm sealfile2 \ + -if "$KEY_FILE" \ + -of "$TPM_SEALED" \ + -pwdd "$key_password" \ + -hk 40000000 \ + -ix 0 X \ + -ix 1 X \ + -ix 2 X \ + -ix 3 X \ + -ix 4 0000000000000000000000000000000000000000 \ + -ix 5 0000000000000000000000000000000000000000 \ + -ix 6 $luks_pcr \ +|| die "Unable to seal secret" + +rm -f "$KEY_FILE" \ +|| die "Failed to delete key file" + +# try it without the owner password first +if ! tpm nv_writevalue \ + -in $TPM_INDEX \ + -if "$TPM_SEALED" \ +; then + # to create an nvram space we need the TPM owner password + # and the TPM physical presence must be asserted. + # + # The permissions are 0 since there is nothing special + # about the sealed file + tpm physicalpresence -s \ + || warn "Warning: Unable to assert physical presence" + + read -s -p "TPM Owner password: " tpm_password + echo + + tpm nv_definespace \ + -in $TPM_INDEX \ + -sz $TPM_SIZE \ + -pwdo "$tpm_password" \ + -per 0 \ + || warn "Warning: Unable to define NVRAM space; trying anyway" + + + tpm nv_writevalue \ + -in $TPM_INDEX \ + -if "$TPM_SEALED" \ + || die "Unable to write sealed secret to NVRAM" +fi + +rm "$TPM_SEALED" \ +|| warn "Failed to delete the sealed secret - continuing" diff --git a/initrd/bin/kexec-select-boot b/initrd/bin/kexec-select-boot index 12f014718c..40d2f33891 100755 --- a/initrd/bin/kexec-select-boot +++ b/initrd/bin/kexec-select-boot @@ -1,6 +1,6 @@ #!/bin/sh # Generic configurable boot script via kexec -set -o pipefail +set -e -o pipefail . /etc/config . /etc/functions @@ -119,7 +119,7 @@ scan_options() { option_file="/tmp/kexec_options.txt" if [ -r $option_file ]; then rm $option_file; fi for i in `find $bootdir -name "$config"`; do - kexec-parse-boot $i >> $option_file + kexec-parse-boot "$bootdir" "$i" >> $option_file done if [ ! -r $option_file ]; then die "Failed to parse any boot options" @@ -222,18 +222,40 @@ do_boot() return fi - kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" - die "!!! Failed to boot w/ options: $option" + if [ -r "$TMP_KEY_DEVICES" ]; then + INITRD=`kexec-boot -b "$bootdir" -e "$option" -i` \ + || die "!!! Failed to extract the initrd from boot option" + if [ -z "$INITRD" ]; then + die "!!! No initrd file found in boot option" + fi + + kexec-insert-key $INITRD \ + || die "!!! Failed to insert disk key into a new initrd" + + kexec-boot -b "$bootdir" -e "$option" \ + -a "$add" -r "$remove" -o "/tmp/secret/initrd.cpio" \ + || die "!!! Failed to boot w/ options: $option" + else + kexec-boot -b "$bootdir" -e "$option" -a "$add" -r "$remove" \ + || die "!!! Failed to boot w/ options: $option" + fi } while true; do check_config $paramsdir - + TMP_DEFAULT_FILE=`find /tmp/kexec/kexec_default.*.txt 2>/dev/null | head -1` || true TMP_MENU_FILE="/tmp/kexec/kexec_menu.txt" TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt" - TMP_DEFAULT_FILE=`find /tmp/kexec/kexec_default.*.txt 2>/dev/null | head -1` TMP_DEFAULT_HASH_FILE="/tmp/kexec/kexec_default_hashes.txt" TMP_ROLLBACK_FILE="/tmp/kexec/kexec_rollback.txt" + TMP_KEY_DEVICES="/tmp/kexec/kexec_key_devices.txt" + TMP_KEY_LVM="/tmp/kexec/kexec_key_lvm.txt" + + if [ ! -r "$TMP_KEY_DEVICES" ]; then + # Extend PCR4 as soon as possible + tpm extend -ix 4 -ic generic \ + || die "Failed to extend PCR 4" + fi # if no saved options, scan the boot directory and generate if [ ! -r "$TMP_MENU_FILE" ]; then diff --git a/initrd/bin/kexec-sign-config b/initrd/bin/kexec-sign-config index 3b88595b75..e4be119445 100755 --- a/initrd/bin/kexec-sign-config +++ b/initrd/bin/kexec-sign-config @@ -1,5 +1,6 @@ #!/bin/sh # Sign a valid directory of kexec params +set -e -o pipefail . /etc/functions rollback="n" diff --git a/initrd/bin/kexec-unseal-key b/initrd/bin/kexec-unseal-key new file mode 100755 index 0000000000..ce22889705 --- /dev/null +++ b/initrd/bin/kexec-unseal-key @@ -0,0 +1,45 @@ +#!/bin/sh +# This will unseal and unecncrypt the drive encryption key from the TPM +# The TOTP secret will be shown to the user on each encryption attempt. +# It will then need to be bundled into initrd that is booted with Qubes. +set -e -o pipefail + +TPM_INDEX=3 +TPM_SIZE=312 + +. /etc/functions +mkdir -p /tmp/secret + +sealed_file="/tmp/secret/sealed.key" +key_file="$1" + +if [ -z "$key_file" ]; then + key_file="/tmp/secret/secret.key" +fi + +tpm nv_readvalue \ + -in "$TPM_INDEX" \ + -sz "$TPM_SIZE" \ + -of "$sealed_file" \ +|| die "Unable to read key from TPM NVRAM" + +for tries in 1 2 3; do + read -s -p "Enter unlock password: " tpm_password + echo + + if tpm unsealfile \ + -if "$sealed_file" \ + -of "$key_file" \ + -pwdd "$tpm_password" \ + -hk 40000000 \ + ; then + # should be okay if this fails + rm -f /tmp/secret/sealed || true + exit 0 + fi + + pcrs + warn "Unable to unseal disk encryption key" +done + +die "Retry count exceeded..." diff --git a/initrd/bin/usb-scan b/initrd/bin/usb-scan index 3e19feda2b..0089caa28f 100755 --- a/initrd/bin/usb-scan +++ b/initrd/bin/usb-scan @@ -1,6 +1,6 @@ #!/bin/sh # Scan for USB installation options - +set -e -o pipefail . /etc/functions . /etc/config @@ -43,7 +43,7 @@ get_menu_option() { } # create ISO menu options -ls -1r /media/*.iso 2>/dev/null > /tmp/iso_menu.txt +ls -1r /media/*.iso 2>/dev/null > /tmp/iso_menu.txt || true if [ `cat /tmp/iso_menu.txt | wc -l` -gt 0 ]; then option_confirm="" while [ -z "$option" -a "$option_index" != "s" ] From 2a9ca6fdba3e5fd41153c59caf1b652d369623d8 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Wed, 12 Jul 2017 00:43:08 -0400 Subject: [PATCH 09/10] Fixed regression on kexec-save-key --- initrd/bin/kexec-save-key | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initrd/bin/kexec-save-key b/initrd/bin/kexec-save-key index e7db449caa..733a0d7678 100755 --- a/initrd/bin/kexec-save-key +++ b/initrd/bin/kexec-save-key @@ -45,7 +45,7 @@ if [ -n "$lvm_volume_group" ]; then fi rm -f $paramsdir/kexec_key_devices.txt || true -for dev in "$key_devices"; do +for dev in $key_devices; do uuid=`cryptsetup luksUUID "$dev" 2>/dev/null` \ || die "Failed to get UUID for device $dev" echo "$dev $uuid" >> $paramsdir/kexec_key_devices.txt \ From 11aca354e9b4c7e50c95f7b7209eeddbfb535e02 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Thu, 13 Jul 2017 00:33:49 -0400 Subject: [PATCH 10/10] Fixed edge case in kernel argument injection Debian 9 installer doesn't have kernel arguments so the iommu fix wasn't being applied properly. --- initrd/bin/kexec-boot | 10 ++++++++++ initrd/bin/kexec-parse-boot | 10 +++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/initrd/bin/kexec-boot b/initrd/bin/kexec-boot index 77459bf222..93565cb191 100755 --- a/initrd/bin/kexec-boot +++ b/initrd/bin/kexec-boot @@ -45,6 +45,7 @@ fix_file_path() { fi } +adjusted_cmd_line="n" adjust_cmd_line() { if [ -n "$cmdremove" ]; then for i in $cmdremove; do @@ -55,6 +56,7 @@ adjust_cmd_line() { if [ -n "$cmdadd" ]; then cmdline="$cmdline $cmdadd" fi + adjusted_cmd_line="y" } module_number="1" @@ -116,6 +118,14 @@ done << EOF $kexecparams EOF +if [ "$adjusted_cmd_line" = "n" ]; then + if [ "$kexectype" = "elf" ]; then + kexeccmd="$kexeccmd --append=\"$cmdadd\"" + else + die "Failed to add required kernel commands: $cmdadd" + fi +fi + if [ "$dryrun" = "y" ]; then exit 0; fi echo "Loading the new kernel:" diff --git a/initrd/bin/kexec-parse-boot b/initrd/bin/kexec-parse-boot index 4236c392fa..82b629946c 100755 --- a/initrd/bin/kexec-parse-boot +++ b/initrd/bin/kexec-parse-boot @@ -2,7 +2,10 @@ set -e -o pipefail bootdir="$1" file="$2" -filedir=`dirname $file` + +if [ -z "$bootdir" -o -z "$file" ]; then + die "Usage: $0 /boot/ /boot/grub/grub.cfg" +fi reset_entry() { name="" @@ -13,6 +16,7 @@ reset_entry() { append="" } +filedir=`dirname $file` bootdir=${bootdir%%"/"} bootlen=${#bootdir} appenddir=${filedir:$bootlen} @@ -111,7 +115,7 @@ syslinux_end() { *) newappend="$newappend $param" ;; esac done - append="$newappend" + append="${newappend##' '}" fi echo_entry @@ -196,7 +200,7 @@ do syslinux_entry ;; esac -done < $file +done < "$file" # handle EOF case if [ "$state" = "syslinux" ]; then