11#! /bin/bash
2- # filepath: /home/user/heads/initrd/bin/kexec-sign-config
32# Sign a valid directory of kexec params
43set -e -o pipefail
54. /tmp/config
@@ -10,19 +9,19 @@ TRACE_FUNC
109rollback=" n"
1110update=" n"
1211while getopts " p:c:ur" arg; do
13- case $arg in
14- p) paramsdir=" $OPTARG " ;;
15- c)
16- counter=" $OPTARG "
17- rollback=" y"
18- ;;
19- u) update=" y" ;;
20- r) rollback=" y" ;;
21- esac
12+ case $arg in
13+ p) paramsdir=" $OPTARG " ;;
14+ c)
15+ counter=" $OPTARG "
16+ rollback=" y"
17+ ;;
18+ u) update=" y" ;;
19+ r) rollback=" y" ;;
20+ esac
2221done
2322
2423if [ -z " $paramsdir " ]; then
25- die " Usage: $0 -p /boot [ -u | -c counter ]"
24+ die " Usage: $0 -p /boot [ -u | -c counter ]"
2625fi
2726
2827paramsdir=" ${paramsdir%%/ } "
@@ -40,96 +39,102 @@ DEBUG "Signing kexec parameters in $paramsdir, rollback=$rollback, update=$updat
4039
4140# update hashes in /boot before signing
4241if [ " $update " = " y" ]; then
43- (
44- TRACE_FUNC
45- DEBUG " update=y: Updating kexec hashes in /boot"
46- cd /boot
47- find ./ -type f ! -path ' ./kexec*' -print0 | xargs -0 sha256sum > /boot/kexec_hashes.txt
48- if [ -e /boot/kexec_default_hashes.txt ]; then
49- DEBUG " /boot/kexec_default_hashes.txt exists, updating /boot/kexec_default_hashes.txt"
50- DEFAULT_FILES=$( cat /boot/kexec_default_hashes.txt | cut -f3 -d ' ' )
51- echo $DEFAULT_FILES | xargs sha256sum > /boot/kexec_default_hashes.txt
52- fi
53-
54- # also save the file & directory structure to detect added files
55- print_tree > /boot/kexec_tree.txt
56- TRACE_FUNC
57- )
58- [ $? -eq 0 ] || die " $paramsdir : Failed to update hashes."
59-
60- # Remove any package trigger log files
61- # We don't need them after the user decides to sign
62- rm -f /boot/kexec_package_trigger*
42+ (
43+ TRACE_FUNC
44+ DEBUG " update=y: Updating kexec hashes in /boot"
45+ cd /boot
46+ find ./ -type f ! -path ' ./kexec*' -print0 | xargs -0 sha256sum > /boot/kexec_hashes.txt
47+ if [ -e /boot/kexec_default_hashes.txt ]; then
48+ DEBUG " /boot/kexec_default_hashes.txt exists, updating /boot/kexec_default_hashes.txt"
49+ DEFAULT_FILES=$( cat /boot/kexec_default_hashes.txt | cut -f3 -d ' ' )
50+ echo $DEFAULT_FILES | xargs sha256sum > /boot/kexec_default_hashes.txt
51+ fi
52+
53+ # also save the file & directory structure to detect added files
54+ print_tree > /boot/kexec_tree.txt
55+ TRACE_FUNC
56+ )
57+ [ $? -eq 0 ] || die " $paramsdir : Failed to update hashes."
58+
59+ # Remove any package trigger log files
60+ # We don't need them after the user decides to sign
61+ rm -f /boot/kexec_package_trigger*
6362fi
6463
6564if [ " $rollback " = " y" ]; then
66-
67- # this script was called with -c $OPTARG (counter=$OPTARG) or -r (rollback=y)
68- DEBUG " rollback=y, counter=$counter , paramsdir=$paramsdir "
69- TRACE_FUNC
70-
71- rollback_file=" $paramsdir /kexec_rollback.txt"
72-
73- if [ -n " $counter " ]; then
74- DEBUG " rollback=y: counter=$counter , will read tpm counter next"
75- TRACE_FUNC
76-
77- # use existing tpm counter
78- DO_WITH_DEBUG read_tpm_counter $counter > /dev/null 2>&1 ||
79- die " $paramsdir : Unable to read tpm counter '$counter '"
80- else
81- DEBUG " rollback=y: counter is empty: checking for existing TPM counter"
82- TRACE_FUNC
83-
84- if [ ! -e $rollback_file ]; then
85- DEBUG " Rollback file $rollback_file does not exist. Creating new TPM counter."
86- DO_WITH_DEBUG check_tpm_counter $rollback_file ||
87- die " $paramsdir : Unable to find/create tpm counter"
88-
89- TRACE_FUNC
90- DEBUG " rollback=y: checked for existing counter under $rollback_file , found TPM_COUNTER=$TPM_COUNTER "
91- # we checked for existing counter and didn't die; increment it
92- DEBUG " rollback=y: Incrementing counter:$TPM_COUNTER ."
93-
94- DO_WITH_DEBUG increment_tpm_counter $counter > /dev/null 2>&1 ||
95- die " $paramsdir : Unable to increment tpm counter"
96- TRACE_FUNC
97- DEBUG " rollback=y: Incremented counter $counter "
98- else
99- die " $paramsdir : No rollback file existing. Please reset TPM through the Heads menu: Options -> TPM/TOTP/HOTP Options -> Reset the TPM"
100- fi
101- fi
102-
103- # Ensure the TPM counter file exists
104- DEBUG " Checking if TPM counter file '/tmp/counter-$counter exists."
105- if [ ! -e " /tmp/counter-$counter " ]; then
106- die " $paramsdir : TPM counter file '/tmp/counter-$counter ' not found after incrementing."
107- fi
108-
109- # Create the rollback file
110- sha256sum /tmp/counter-$counter > $rollback_file ||
111- die " $paramsdir : Unable to create rollback file"
65+ rollback_file=" $paramsdir /kexec_rollback.txt"
66+
67+ DEBUG " rollback=y, counter=$counter , paramsdir=$paramsdir , rollback_file=$rollback_file "
68+ TRACE_FUNC
69+
70+ if [ -n " $counter " ]; then
71+ DEBUG " rollback=y: provided counter=$counter , will read tpm counter next"
72+ TRACE_FUNC
73+
74+ # use existing tpm counter
75+ DO_WITH_DEBUG read_tpm_counter " $counter " > /dev/null 2>&1 ||
76+ die " $paramsdir : Unable to read tpm counter '$counter '"
77+ else
78+ DEBUG " rollback=y: counter was not provided: checking for existing TPM counter from TPM rollback_file=$rollback_file "
79+ TRACE_FUNC
80+
81+ if [ -e " $rollback_file " ]; then
82+ # Extract TPM_COUNTER from rollback file
83+ TPM_COUNTER=$( grep -o ' counter-[0-9a-f]*' " $rollback_file " | cut -d- -f2)
84+ DEBUG " rollback=y: Found TPM counter $TPM_COUNTER in rollback file $rollback_file "
85+ else
86+ DEBUG " Rollback file $rollback_file does not exist. Creating new TPM counter."
87+ DO_WITH_DEBUG check_tpm_counter $rollback_file ||
88+ die " $paramsdir : Unable to find/create tpm counter"
89+
90+ TRACE_FUNC
91+ TPM_COUNTER=$( cut -d: -f1 < /tmp/counter)
92+ DEBUG " rollback=y: Created new TPM counter $TPM_COUNTER "
93+ fi
94+ fi
95+
96+ TRACE_FUNC
97+
98+ # Increment the TPM counter
99+ DEBUG " rollback=y: Incrementing counter $TPM_COUNTER ."
100+ DO_WITH_DEBUG increment_tpm_counter $TPM_COUNTER > /dev/null 2>&1 ||
101+ die " $paramsdir : Unable to increment tpm counter"
102+
103+ # Ensure the incremented counter file exists
104+ incremented_counter_file=" /tmp/counter-$TPM_COUNTER "
105+ if [ ! -e " $incremented_counter_file " ]; then
106+ DEBUG " TPM counter file '$incremented_counter_file ' not found. Attempting to read it again."
107+ DO_WITH_DEBUG read_tpm_counter " $TPM_COUNTER " > /dev/null 2>&1 ||
108+ die " $paramsdir : TPM counter file '$incremented_counter_file ' not found after incrementing."
109+ fi
110+
111+ DEBUG " TPM counter file '$incremented_counter_file ' found."
112+
113+ # Create the rollback file
114+ sha256sum " $incremented_counter_file " > $rollback_file ||
115+ die " $paramsdir : Unable to create rollback file"
112116fi
113117
118+ TRACE_FUNC
114119param_files=$( find $paramsdir /kexec* .txt)
115120if [ -z " $param_files " ]; then
116- die " $paramsdir : No kexec parameter files to sign"
121+ die " $paramsdir : No kexec parameter files to sign"
117122fi
118123
119124for tries in 1 2 3; do
120- if DO_WITH_DEBUG sha256sum $param_files | gpg \
121- --detach-sign \
122- -a \
123- > $paramsdir /kexec.sig \
124- ; then
125- # successful - update the validated params
126- check_config $paramsdir
127-
128- # remount /boot as ro
129- mount -o remount,ro /boot
130-
131- exit 0
132- fi
125+ if DO_WITH_DEBUG sha256sum $param_files | gpg \
126+ --detach-sign \
127+ -a \
128+ > $paramsdir /kexec.sig \
129+ ; then
130+ # successful - update the validated params
131+ check_config $paramsdir
132+
133+ # remount /boot as ro
134+ mount -o remount,ro /boot
135+
136+ exit 0
137+ fi
133138done
134139
135140# remount /boot as ro
0 commit comments