Debian Codegraph @

launcher/image/measure/measure.sh

raw
1 #!/bin/bash
2
3 # This script calculates measured boot hashes for a given OS image.
4 # It extracts boot components from partition 12, computes their hashes,
5 # and outputs the results to a JSON file.
6
7 set -euo pipefail
8
9 # --- Global Variables ---
10 # These are set by setup_temp_dir().
11 declare TMP_DIR_NAME
12 declare P12_FILE
13 declare VMLINUZ_A_FILE VMLINUZ_B_FILE
14 declare GRUB_CFG_FILE
15 declare BOOT_EFI_FILE GRUB_EFI_FILE
16 declare SHIM_SIG_FILE GRUB_SIG_FILE
17
18
19 # --- Core Functions ---
20
21 ##
22 # Cleans up the temporary directory on exit.
23 #
24 cleanup() {
25 if [[ -n "${TMP_DIR_NAME:-}" && -d "$TMP_DIR_NAME" ]]; then
26 echo "Cleaning up temporary directory '$TMP_DIR_NAME'..."
27 rm -rf "$TMP_DIR_NAME"
28 fi
29 }
30
31 ##
32 # Verifies that all required command-line utilities are installed.
33 #
34 check_dependencies() {
35 echo "Checking for required command-line utilities..."
36 local missing_cmds=0
37 for cmd in cgpt dd mcopy sha256sum sbattach openssl jq awk grep printf dirname mkdir; do
38 if ! command -v "$cmd" &>/dev/null; then
39 echo "Error: Required command '$cmd' is not installed." >&2
40 missing_cmds=1
41 fi
42 done
43 if [ "$missing_cmds" -eq 1 ]; then
44 echo "Please install the missing commands and try again." >&2
45 return 1
46 fi
47 }
48
49 ##
50 # Creates a temporary directory and defines global paths for extracted files.
51 #
52 setup_temp_dir() {
53 TMP_DIR_NAME="$(mktemp -d)"
54 echo "Created temporary directory: '$TMP_DIR_NAME'..."
55
56 # Define file paths within the temporary directory
57 P12_FILE="$TMP_DIR_NAME/p12"
58 VMLINUZ_A_FILE="$TMP_DIR_NAME/vmlinuz.A"
59 VMLINUZ_B_FILE="$TMP_DIR_NAME/vmlinuz.B"
60 GRUB_CFG_FILE="$TMP_DIR_NAME/grub.cfg"
61 BOOT_EFI_FILE="$TMP_DIR_NAME/boot.efi"
62 GRUB_EFI_FILE="$TMP_DIR_NAME/grub-lakitu.efi"
63 SHIM_SIG_FILE="$TMP_DIR_NAME/shim_out.sig"
64 GRUB_SIG_FILE="$TMP_DIR_NAME/grub_out.sig"
65 }
66
67 ##
68 # Extracts partition 12 from the OS image using cgpt and dd.
69 # @param $1: Path to the OS image.
70 #
71 extract_partition_12() {
72 local os_image_path="$1"
73 echo "Extracting partition 12 from '$os_image_path'..."
74
75 local skip_sectors size_sectors
76 skip_sectors=$(cgpt show -i 12 -b -n "$os_image_path")
77 size_sectors=$(cgpt show -i 12 -s -n "$os_image_path")
78
79 if ! [[ "$skip_sectors" =~ ^[0-9]+$ ]] || ! [[ "$size_sectors" =~ ^[0-9]+$ ]]; then
80 echo "Error: Failed to get valid numeric skip/size sectors for partition 12." >&2
81 cgpt show -i 12 "$os_image_path" >&2
82 return 1
83 fi
84
85 echo "Partition 12 details: skip=$skip_sectors sectors, size=$size_sectors sectors."
86 dd if="$os_image_path" of="$P12_FILE" skip="$skip_sectors" count="$size_sectors" bs=512
87 echo "Partition 12 copied to '$P12_FILE'."
88 }
89
90 ##
91 # Copies boot-related files from the extracted partition image using mcopy.
92 #
93 extract_boot_components() {
94 local arch="$1"
95 local shim_path=""
96
97 if [ "$arch" == "x86_64" ]; then
98 shim_path="::/efi/boot/bootx64.efi"
99 elif [ "$arch" == "aarch64" ]; then
100 shim_path="::/efi/boot/bootaa64.efi"
101 else
102 echo "Error: Unknown arch '$arch'."
103 return 1
104 fi
105
106 echo "Copying files from partition image '$P12_FILE'..."
107
108 declare -A files_to_copy=(
109 ["::/syslinux/vmlinuz.A"]="$VMLINUZ_A_FILE"
110 ["::/efi/boot/grub.cfg"]="$GRUB_CFG_FILE"
111 ["$shim_path"]="$BOOT_EFI_FILE"
112 ["::/efi/boot/grub-lakitu.efi"]="$GRUB_EFI_FILE"
113 )
114
115 # vmlinuz.B file exists on amd64 but not on arm64
116 if [ "$arch" == "x86_64" ]; then
117 files_to_copy["::/syslinux/vmlinuz.B"]="$VMLINUZ_B_FILE"
118 fi
119
120 for src in "${!files_to_copy[@]}"; do
121 local dest="${files_to_copy[$src]}"
122 echo "Copying '$src' to '$dest'..."
123 if ! mcopy -i "$P12_FILE" "$src" "$dest"; then
124 echo "Error: Failed to mcopy '$src' from '$P12_FILE'."
125 return 1
126 fi
127 done
128 echo "All boot components copied successfully."
129 }
130
131 # --- Hash Calculation Functions ---
132
133 ##
134 # Computes the SHA256 hash of a given file.
135 # @param $1: Path to the file to hash.
136 # @return The SHA256 hash string to stdout.
137 #
138 compute_file_hash() {
139 sha256sum "$1" | awk '{print $1}'
140 }
141
142 ##
143 # Computes the shell-interpreted kernel command line from grub.cfg.
144 # @param $1: Path to grub.cfg.
145 # @param $2: Image identifier ('A' or 'B').
146 # @return The shell-interpreted command line to stdout.
147 #
148 compute_cmdline() {
149 local grub_cfg="$1"
150 local image_id="$2"
151
152 local cmdline_string result=()
153 cmdline_string=$(grep "verified image $image_id" -A 1 "$grub_cfg" | tail -n 1)
154
155 # This logic correctly re-assembles the command line, handling quoted arguments.
156 eval set -- $cmdline_string
157 shift # Remove 'linux' command
158 for arg in "$@"; do
159 if [[ "$arg" = *[[:space:]]* ]]; then
160 result+=('"'"$arg"'"')
161 else
162 result+=("$arg")
163 fi
164 done
165
166 printf '%s' "$(echo "${result[@]}")"
167 }
168
169 ##
170 # Computes the SHA256 hash of a kernel command line from grub.cfg.
171 # @param $1: Path to grub.cfg.
172 # @param $2: Image identifier ('A' or 'B').
173 # @return The SHA256 hash of the command line to stdout.
174 #
175 compute_cmdline_hash() {
176 compute_cmdline $1 $2 | sha256sum | awk '{print $1}'
177 }
178
179 ##
180 # Computes the hash of a signed EFI binary by detaching its signature.
181 # @param $1: Path to the signed EFI file (e.g., bootx64.efi).
182 # @param $2: Path to store the detached signature.
183 # @return The hash string to stdout.
184 #
185 compute_efi_hash() {
186 local efi_file="$1"
187 local sig_file="$2"
188
189 if ! sbattach --detach "$sig_file" "$efi_file"; then
190 echo "Error: sbattach --detach failed for '$efi_file'." >&2
191 return 1
192 fi
193
194 local efi_hash
195 efi_hash=$(openssl pkcs7 -in "$sig_file" -inform der -print | grep "HEX DUMP" | awk -F'HEX DUMP]:' '{print $2}' | awk '{$1=$1;print}')
196
197 if [[ -z "$efi_hash" ]]; then
198 echo "Error: Extracted EFI hash is empty for '$efi_file'." >&2
199 return 1
200 fi
201 echo "$efi_hash"
202 }
203
204 ##
205 # Writes the collected hashes to a final JSON file.
206 # @param $1: Output JSON file path.
207 # @param $2: Channel name.
208 # @param $3-${10}: The six required hashes.
209 #
210 write_json_output() {
211 echo "Writing data to JSON file: '$output_json_file'..."
212 local output_dir
213 output_dir=$(dirname "$output_json_file")
214
215 if [[ ! -d "$output_dir" ]]; then
216 echo "Error: Output directory '$output_dir' does not exist." >&2
217 return 1
218 fi
219
220 jq -n \
221 --arg chan "$2" \
222 --arg shim "$3" \
223 --arg grub "$4" \
224 --arg vml_a "$5" \
225 --arg vml_b "$6" \
226 --arg cmd_a_h "$7" \
227 --arg cmd_b_h "$8" \
228 --arg cmd_a "$9" \
229 --arg cmd_b "${10}" \
230 '{
231 channel: $chan,
232 shim: $shim,
233 grub: $grub,
234 vmlinuz_a: $vml_a,
235 vmlinuz_b: $vml_b,
236 kernel_cmdline_a_hash: $cmd_a_h,
237 kernel_cmdline_b_hash: $cmd_b_h,
238 kernel_cmdline_a: $cmd_a,
239 kernel_cmdline_b: $cmd_b
240 }' > "$output_json_file"
241
242 if [[ $? -ne 0 || ! -s "$output_json_file" ]]; then
243 echo "Error: Failed to create or write to output JSON file '$output_json_file'." >&2
244 return 1
245 fi
246 }
247
248 # --- Main Logic ---
249
250 main() {
251 # 1. Parameter Handling & Initial Checks
252 if [[ "$#" -ne 4 ]]; then
253 echo "Usage: $0 <os_image_path> <output_json_file> <channel> <build_architecture>"
254 echo "Example: $0 /path/to/image.bin /path/to/output.json stable x86_64"
255 return 1
256 fi
257 local os_image_path="$1"
258 local output_json_file="$2"
259 local channel="$3"
260 local arch="$4"
261
262 if [[ ! -f "$os_image_path" ]]; then
263 echo "Error: OS image path '$os_image_path' not found."
264 return 1
265 fi
266
267 check_dependencies || return 1
268
269 # 2. Setup and Extraction
270 setup_temp_dir
271 extract_partition_12 "$os_image_path" || return 1
272 extract_boot_components "$arch" || return 1
273
274 # 3. Compute All Hashes
275 echo "Computing all required hashes..."
276 local vmlinuz_a_hash vmlinuz_b_hash="" kernel_cmdline_a_hash kernel_cmdline_b_hash shim_hash grub_hash
277
278 vmlinuz_a_hash=$(compute_file_hash "$VMLINUZ_A_FILE")
279 if [ "$arch" == "x86_64" ]; then
280 vmlinuz_b_hash=$(compute_file_hash "$VMLINUZ_B_FILE")
281 fi
282 kernel_cmdline_a=$(compute_cmdline "$GRUB_CFG_FILE" "A")
283 kernel_cmdline_b=$(compute_cmdline "$GRUB_CFG_FILE" "B")
284 kernel_cmdline_a_hash=$(compute_cmdline_hash "$GRUB_CFG_FILE" "A")
285 kernel_cmdline_b_hash=$(compute_cmdline_hash "$GRUB_CFG_FILE" "B")
286 shim_hash=$(compute_efi_hash "$BOOT_EFI_FILE" "$SHIM_SIG_FILE") || return 1
287 grub_hash=$(compute_efi_hash "$GRUB_EFI_FILE" "$GRUB_SIG_FILE") || return 1
288
289 echo "Kernel (vmlinuz.A) hash: $vmlinuz_a_hash"
290 echo "Kernel (vmlinuz.B) hash: $vmlinuz_b_hash"
291 echo "Kernel cmdline (image A): $kernel_cmdline_a"
292 echo "Kernel cmdline (image B): $kernel_cmdline_b"
293 echo "Kernel cmdline (image A) hash: $kernel_cmdline_a_hash"
294 echo "Kernel cmdline (image B) hash: $kernel_cmdline_b_hash"
295 echo "bootx64.efi/bootaa64.efi (shim) hash: $shim_hash"
296 echo "grub-lakitu.efi (grub) hash: $grub_hash"
297
298 # 4. Final Output with escaped kernel command line strings
299 write_json_output "$output_json_file" "$channel" "$shim_hash" "$grub_hash" \
300 "$vmlinuz_a_hash" "$vmlinuz_b_hash" "$kernel_cmdline_a_hash" "$kernel_cmdline_b_hash" \
301 "$kernel_cmdline_a" "$kernel_cmdline_b" || return 1
302
303 echo "Measured boot hashes successfully written to '$output_json_file'."
304 }
305
306 main "$@"