Skip to content

Commit fffeade

Browse files
esposemLaszloGombos
authored andcommitted
feat(dracut): add --sbat option to add sbat policy to UKI
Take existing .sbat section from the uefi stub and merge it with vmlinux .sbat (if it exists) and user-provided .sbat parameters using the new --sbat option. For some reasons, --update-section in objcopy does not resize the .sbat section, so remove the section from the stub and add it to the UKI as new one, to avoid having incomplete SBAT strings. Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
1 parent af3076a commit fffeade

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

dracut.sh

+45-1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ Creates initial ramdisk images for preloading modules
272272
Use [FILE] as a splash image when creating an UEFI
273273
executable. Requires bitmap (.bmp) image format.
274274
--kernel-image [FILE] Location of the kernel image.
275+
--sbat [PARAMETERS] The SBAT parameters to be added to .sbat.
276+
The string "sbat,1,SBAT Version,sbat,1,
277+
https://github.com/rhboot/shim/blob/main/SBAT.md" is
278+
already added by default.
275279
--regenerate-all Regenerate all initramfs images at the default location
276280
for the kernel versions found on the system.
277281
-p, --parallel Use parallel processing if possible (currently only
@@ -463,6 +467,7 @@ rearrange_params() {
463467
--long uefi-stub: \
464468
--long uefi-splash-image: \
465469
--long kernel-image: \
470+
--long sbat: \
466471
--long no-hostonly-i18n \
467472
--long hostonly-i18n \
468473
--long hostonly-nics: \
@@ -840,6 +845,11 @@ while :; do
840845
PARMS_TO_STORE+=" '$2'"
841846
shift
842847
;;
848+
--sbat)
849+
sbat_l="$2"
850+
PARMS_TO_STORE+=" '$2'"
851+
shift
852+
;;
843853
--no-machineid)
844854
machine_id_l="no"
845855
;;
@@ -1079,6 +1089,7 @@ drivers_dir="${drivers_dir%"${drivers_dir##*[!/]}"}"
10791089
[[ $uefi_stub_l ]] && uefi_stub="$uefi_stub_l"
10801090
[[ $uefi_splash_image_l ]] && uefi_splash_image="$uefi_splash_image_l"
10811091
[[ $kernel_image_l ]] && kernel_image="$kernel_image_l"
1092+
[[ $sbat_l ]] && sbat="$sbat_l"
10821093
[[ $machine_id_l ]] && machine_id="$machine_id_l"
10831094

10841095
if ! [[ $outfile ]]; then
@@ -2452,6 +2463,24 @@ fi
24522463
24532464
umask 077
24542465
2466+
SBAT_DEFAULT="sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md"
2467+
sbat_out=$uefi_outdir/uki.sbat
2468+
2469+
clean_sbat_string() {
2470+
local inp=$1
2471+
local temp=$uefi_outdir/temp.sbat
2472+
sed "/${SBAT_DEFAULT//\//\\/}/d" "$inp" > "$temp"
2473+
[[ -s $temp ]] && cat "$temp" >> "$sbat_out"
2474+
rm "$temp"
2475+
}
2476+
2477+
get_sbat_string() {
2478+
local inp=$1
2479+
local out=$uefi_outdir/$2
2480+
objcopy -O binary --only-section=.sbat "$inp" "$out"
2481+
clean_sbat_string "$out"
2482+
}
2483+
24552484
if [[ $uefi == yes ]]; then
24562485
if [[ $kernel_cmdline ]]; then
24572486
echo -n "$kernel_cmdline" > "$uefi_outdir/cmdline.txt"
@@ -2506,6 +2535,16 @@ if [[ $uefi == yes ]]; then
25062535
unset uefi_splash_image
25072536
fi
25082537
2538+
echo "$SBAT_DEFAULT" > "$sbat_out"
2539+
if [[ -n $sbat ]]; then
2540+
echo "$sbat" | sed "/${SBAT_DEFAULT//\//\\/}/d" >> "$sbat_out"
2541+
fi
2542+
get_sbat_string "$kernel_image" kernel.sbat
2543+
get_sbat_string "$uefi_stub" stub.sbat
2544+
2545+
uefi_sbat_offs="${offs}"
2546+
offs=$((offs + $(stat -Lc%s "$sbat_out")))
2547+
offs=$((offs + "$align" - offs % "$align"))
25092548
uefi_linux_offs="${offs}"
25102549
offs=$((offs + $(stat -Lc%s "$kernel_image")))
25112550
offs=$((offs + "$align" - offs % "$align"))
@@ -2517,14 +2556,19 @@ if [[ $uefi == yes ]]; then
25172556
exit 1
25182557
fi
25192558
2559+
tmp_uefi_stub=$uefi_outdir/elf.stub
2560+
cp "$uefi_stub" "$tmp_uefi_stub"
2561+
objcopy --remove-section .sbat "$tmp_uefi_stub" &> /dev/null
2562+
25202563
if objcopy \
25212564
${uefi_osrelease:+--add-section .osrel="$uefi_osrelease" --change-section-vma .osrel=$(printf 0x%x "$uefi_osrelease_offs")} \
25222565
${uefi_cmdline:+--add-section .cmdline="$uefi_cmdline" --change-section-vma .cmdline=$(printf 0x%x "$uefi_cmdline_offs")} \
25232566
${uefi_splash_image:+--add-section .splash="$uefi_splash_image" --change-section-vma .splash=$(printf 0x%x "$uefi_splash_offs")} \
2567+
--add-section .sbat="$sbat_out" --change-section-vma .sbat="$(printf 0x%x "$uefi_sbat_offs")" \
25242568
--add-section .linux="$kernel_image" --change-section-vma .linux="$(printf 0x%x "$uefi_linux_offs")" \
25252569
--add-section .initrd="${DRACUT_TMPDIR}/initramfs.img" --change-section-vma .initrd="$(printf 0x%x "$uefi_initrd_offs")" \
25262570
--image-base="$(printf 0x%x "$base_image")" \
2527-
"$uefi_stub" "${uefi_outdir}/linux.efi"; then
2571+
"$tmp_uefi_stub" "${uefi_outdir}/linux.efi"; then
25282572
if [[ -n ${uefi_secureboot_key} && -n ${uefi_secureboot_cert} ]]; then
25292573
if sbsign \
25302574
${uefi_secureboot_engine:+--engine "$uefi_secureboot_engine"} \

man/dracut.8.asc

+5
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ and no /etc/cmdline/*.conf will be generated into the initramfs.
600600
default is _/lib/modules/<KERNEL-VERSION>/vmlinuz_ or
601601
_/boot/vmlinuz-<KERNEL-VERSION>_.
602602
603+
**--sbat <parameters>**::
604+
Specifies the SBAT parameters, which to include in the UEFI executable. By default
605+
the default SBAT string added is "sbat,1,SBAT Version,sbat,1,
606+
https://github.com/rhboot/shim/blob/main/SBAT.md".
607+
603608
**--enhanced-cpio**::
604609
Attempt to use the dracut-cpio binary, which optimizes archive creation for
605610
copy-on-write filesystems by using the copy_file_range(2) syscall via Rust's

man/dracut.conf.5.asc

+5
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ Logging levels:
302302
default is _/lib/modules/<KERNEL-VERSION>/vmlinuz_ or
303303
_/boot/vmlinuz-<KERNEL-VERSION>_.
304304

305+
*sbat=*"__parameters__"::
306+
Specifies the SBAT parameters, which to include in the UEFI executable. By default
307+
the default SBAT string added is "sbat,1,SBAT Version,sbat,1,
308+
https://github.com/rhboot/shim/blob/main/SBAT.md".
309+
305310
*enhanced_cpio=*"__{yes|no}__"::
306311
Attempt to use the dracut-cpio binary, which optimizes archive creation for
307312
copy-on-write filesystems (default=no).

shell-completion/bash/dracut

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ _dracut() {
4646
--kernel-cmdline --sshkey --persistent-policy --install-optional
4747
--loginstall --uefi-stub --kernel-image --squash-compressor
4848
--sysroot --hostonly-mode --hostonly-nics --include --logfile
49-
--uefi-splash-image
49+
--uefi-splash-image --sbat
5050
'
5151
)
5252

0 commit comments

Comments
 (0)