Skip to content

Commit 6ed0788

Browse files
Domenico Panellapandom79
authored andcommitted
New feature: bootloader signing
1 parent e060405 commit 6ed0788

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

grub/grub_void.cfg.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ insmod font
1515

1616
if loadfont "(${voidlive})/boot/grub/fonts/unicode.pf2" ; then
1717
insmod gfxterm
18-
set gfxmode="auto"
18+
set gfxmode="1920x1440"
1919

2020
terminal_input console
2121
terminal_output gfxterm

mklive.sh.in

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ umask 022
3232
readonly REQUIRED_PKGS="base-files libgcc dash coreutils sed tar gawk syslinux grub-i386-efi grub-x86_64-efi squashfs-tools xorriso"
3333
readonly INITRAMFS_PKGS="binutils xz device-mapper dhclient dracut-network openresolv"
3434
readonly PROGNAME=$(basename "$0")
35+
toSign=0
3536

3637
info_msg() {
3738
printf "\033[1m$@\n\033[m"
@@ -82,6 +83,8 @@ directory if unset).
8283
-C "cmdline args" Add additional kernel command line arguments.
8384
-T "title" Modify the bootloader title.
8485
-v linux<version> Install a custom Linux version on ISO image (linux meta-package if unset).
86+
-d <key-file> Set a key file to sign bootloader.
87+
-t <cert-file> Set a certificate file to sign bootloader.
8588
-K Do not remove builddir.
8689
8790
The $PROGNAME script generates a live image of the Void Linux distribution.
@@ -190,6 +193,17 @@ generate_isolinux_boot() {
190193
"$ISOLINUX_DIR"/isolinux.cfg
191194
}
192195

196+
dosign() {
197+
print_step "Signing $2..."
198+
199+
if ! sbsign --key "$DBKEY" --cert "$DBCRT" --output "$1.signed" "$1"; then
200+
die "Failed to sign $2"
201+
fi
202+
if ! sbverify --cert "$DBCRT" "$1.signed"; then
203+
die "failed to verify the signature"
204+
fi
205+
}
206+
193207
generate_grub_efi_boot() {
194208
cp -f grub/grub.cfg "$GRUB_DIR"
195209
cp -f grub/grub_void.cfg.in "$GRUB_DIR"/grub_void.cfg
@@ -226,6 +240,12 @@ generate_grub_efi_boot() {
226240
fi
227241
mkdir -p "${GRUB_EFI_TMPDIR}"/EFI/BOOT
228242
cp -f "$VOIDHOSTDIR"/tmp/bootia32.efi "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTIA32.EFI
243+
244+
#Bootloader signing
245+
if [ $toSign -eq 1 ] && [ -f "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTX32.EFI ]; then
246+
dosign "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTX32.EFI BOOTX32.EFI
247+
fi
248+
229249
xbps-uchroot "$VOIDHOSTDIR" grub-mkstandalone -- \
230250
--directory="/usr/lib/grub/x86_64-efi" \
231251
--format="x86_64-efi" \
@@ -237,6 +257,12 @@ generate_grub_efi_boot() {
237257
die "Failed to generate EFI loader"
238258
fi
239259
cp -f "$VOIDHOSTDIR"/tmp/bootx64.efi "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTX64.EFI
260+
261+
#Bootloader signing
262+
if [ $toSign -eq 1 ] && [ -f "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTX64.EFI ]; then
263+
dosign "${GRUB_EFI_TMPDIR}"/EFI/BOOT/BOOTX64.EFI BOOTX64.EFI
264+
fi
265+
240266
umount "$GRUB_EFI_TMPDIR"
241267
losetup --detach "${LOOP_DEVICE}"
242268
rm -rf "$GRUB_EFI_TMPDIR"
@@ -282,7 +308,7 @@ generate_iso_image() {
282308
#
283309
# main()
284310
#
285-
while getopts "a:b:r:c:C:T:Kk:l:i:I:s:S:o:p:v:h" opt; do
311+
while getopts "a:b:r:c:C:T:Kk:l:i:I:s:S:o:p:v:d:t:h" opt; do
286312
case $opt in
287313
a) BASE_ARCH="$OPTARG";;
288314
b) BASE_SYSTEM_PKG="$OPTARG";;
@@ -300,6 +326,8 @@ while getopts "a:b:r:c:C:T:Kk:l:i:I:s:S:o:p:v:h" opt; do
300326
C) BOOT_CMDLINE="$OPTARG";;
301327
T) BOOT_TITLE="$OPTARG";;
302328
v) LINUX_VERSION="$OPTARG";;
329+
d) DBKEY="$OPTARG";;
330+
t) DBCRT="$OPTARG";;
303331
h) usage;;
304332
*) usage;;
305333
esac
@@ -330,6 +358,22 @@ if [ "$(id -u)" -ne 0 ]; then
330358
die "Must be run as root, exiting..."
331359
fi
332360

361+
#The -d and -t options are complementary. If one exists, the other must also exist.
362+
#If these options are set, I also check sbsign command.
363+
if ([ $DBKEY ] && [ ! $DBCRT ]) || ([ ! $DBKEY ] && [ $DBCRT ]); then
364+
die "Must be set a key and certificate via -d and -t option, exiting..."
365+
elif [ $DBKEY ] && [ $DBCRT ]; then
366+
if [ ! -f $DBKEY ]; then
367+
die "$DBKEY does not exist, exiting..."
368+
elif [ ! -f $DBCRT ]; then
369+
die "$DBCRT does not exist, exiting..."
370+
elif ! command -v sbsign > /dev/null; then
371+
die "sbsign command does not exist, exiting..."
372+
else
373+
toSign=1
374+
fi
375+
fi
376+
333377
if [ -n "$ROOTDIR" ]; then
334378
BUILDDIR=$(mktemp --tmpdir="$ROOTDIR" -d)
335379
else

0 commit comments

Comments
 (0)