Skip to content

Commit c7b87ed

Browse files
committed
Merge pull request #24 from petronio/support_external_module_signing
Add support for automatically signing external modules
2 parents d15ae23 + 8097e3c commit c7b87ed

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

buildkernel

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ shopt -s nullglob
3131
# ********************** variables *********************
3232
PROGNAME="$(basename "${0}")"
3333
CONFFILE="/etc/${PROGNAME}.conf"
34-
VERSION="1.0.33"
34+
VERSION="1.0.34"
3535
ETCPROFILE="/etc/profile"
3636
DEFAULTEFIBOOTFILE="bootx64.efi"
3737
EFIBOOTFILE="${DEFAULTEFIBOOTFILE}"
@@ -109,6 +109,7 @@ GPGBUILDDIR="/root/tmpgpgbuild"
109109
TMPGPGPATH="${GPGBUILDDIR}/usr/bin/gpg"
110110
declare -i USINGUSBKEYFOREFI=0
111111
declare -i BACKUPOLDKERNEL=1
112+
declare -i BUILT_EXTERNAL_MODULES=1
112113

113114
EFIPARTNAME="EFI boot partition"
114115
DEFAULTKEYMAP="us"
@@ -423,6 +424,29 @@ source_etc_conf_file() {
423424
if [[ -v INITSYSTEM ]]; then
424425
INITSYSTEM="${INITSYSTEM,,}"
425426
fi
427+
# perform checks on KERNEL_SIGNING_CERT and KERNEL_SIGNING_KEY
428+
if [[ -v KERNEL_SIGNING_CERT ]]; then
429+
if [[ ! -v KERNEL_SIGNING_KEY ]]; then
430+
die "Cannot proceed; KERNEL_SIGNING_CERT is configured, but KERNEL_SIGNING_KEY is not."
431+
fi
432+
if [[ "${KERNEL_SIGNING_CERT}" == "auto" || "${KERNEL_SIGNING_KEY}" == "auto" ]]; then
433+
if [[ "${KERNEL_SIGNING_CERT}" != "${KERNEL_SIGNING_KEY}" ]]; then
434+
die "Cannot proceed; in automatic external module signing mode, both KERNEL_SIGNING_CERT and KERNEL_SIGNING_KEY must be set to \"auto\""
435+
fi
436+
KERNEL_SIGNING_CERT="${LINUXDIR}/certs/signing_key.x509"
437+
KERNEL_SIGNING_KEY="${LINUXDIR}/certs/signing_key.pem"
438+
else
439+
if [[ ! -f "${KERNEL_SIGNING_CERT}" ]]; then
440+
die "Cannot proceed; KERNEL_SIGNING_CERT is not a valid path to a file."
441+
fi
442+
if [[ ! -f "${KERNEL_SIGNING_KEY}" ]]; then
443+
die "Cannot proceed; KERNEL_SIGNING_KEY is not a valid path to a file."
444+
fi
445+
fi
446+
fi
447+
if [[ -v KERNEL_SIGNING_KEY && ! -v KERNEL_SIGNING_CERT ]]; then
448+
die "Cannot proceed; KERNEL_SIGNING_KEY is configured, but KERNEL_SIGNING_CERT is not."
449+
fi
426450
}
427451
setup_final_variables() {
428452
# post-processing once buildkernel.conf loaded
@@ -1818,8 +1842,14 @@ rebuild_external_modules_if_necessary() {
18181842
else
18191843
warning "Failed to complete emerge @module-rebuild due to error"
18201844
warning "Continuing..."
1845+
BUILT_EXTERNAL_MODULES=0
18211846
fi
18221847
fi
1848+
if [[ ${BUILT_EXTERNAL_MODULES}==1 && -v KERNEL_SIGNING_CERT ]] ; then
1849+
for EXTERNAL_MODULE in `find /lib/modules/${NEWVERSION#"linux-"}/* -type f -name '*.ko' -not -path '*/kernel/*'`; do
1850+
"${LINUXDIR}/scripts/sign-file" sha512 "${KERNEL_SIGNING_KEY}" "${KERNEL_SIGNING_CERT}" "${EXTERNAL_MODULE}"
1851+
done
1852+
fi
18231853
fi
18241854
}
18251855
create_initramfs_using_genkernel() {

buildkernel.8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH BUILDKERNEL 8 "Version 1.0.33: October 2018"
1+
.TH BUILDKERNEL 8 "Version 1.0.34: April 2019"
22
.SH NAME
33
buildkernel \- build secure boot kernel, save to EFI system partition
44
.SH SYNOPSIS
@@ -61,7 +61,7 @@ cleans the kernel tree (if you specify \fB--clean\fR; you will be asked whether
6161
.IP \(bu 2
6262
builds the kernel, and its modules, with the specified configuration; in this first pass, an empty initramfs is used (since it must be incorporated in the kernel, to be protected by UEFI secure boot, but we don't have everything necessary to include in it, yet!);
6363
.IP \(bu 2
64-
builds any external modules (such as those required for VirtualBox), using \fBemerge @module-rebuild\fR, if you so specify (using the option \fB--rebuild-external-modules\fR);
64+
builds any external modules (such as those required for VirtualBox), using \fBemerge @module-rebuild\fR, if you so specify (using the option \fB--rebuild-external-modules\fR), and optionally signs them (if you have set up the variables \fBKERNEL_SIGNING_CERT\fR and \fBKERNEL_SIGNING_KEY\fR in \fI/etc/buildkernel.conf\fR);
6565
.IP \(bu 2
6666
creates a first cut of the initramfs using \fBgenkernel\fR(8) (see below for more details); this will contain \fBgenkernel\fR(8)'s \fBinit\fR(8) script, compiled modules, any necessary firmware (if you haven't deblobbed), and a minimal set of binaries; it does \fInot\fR at this point contain a static copy of \fBgpg\fR;
6767
.IP \(bu 2

buildkernel.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@
8383
# however, doing so should not be necessary.
8484
#CMDLINE_ROOTFSTYPE="ext4"
8585

86+
# if you sign your kernel modules, configure the signing certificate and key
87+
# paths to sign external modules as well once built. Setting the variables to
88+
# "auto" will use the kernel's automatically generated certificate and key if
89+
# you have configured it to generate them. By default the variable is unset and
90+
# modules will not be signed.
91+
#KERNEL_SIGNING_CERT="auto"
92+
#KERNEL_SIGNING_KEY="auto"
93+
8694
# if you need to conform the config file for some reason, uncomment this
8795
# hook function and fill it out to suit your requirements
8896
# NB you should only really need to do this to override a setting forced

buildkernel.conf.5

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH BUILDKERNEL 5 "Version 1.0.33: October 2018"
1+
.TH BUILDKERNEL 5 "Version 1.0.34: April 2019"
22
.SH NAME
33
buildkernel.conf \- a configuration file for \fBbuildkernel\fR(8)
44
.SH SYNOPSIS
@@ -194,6 +194,26 @@ automatically detect the filesystem type of \fBCMDLINE_REAL_ROOT\fR
194194
(falling back to \fBext4\fR, in case of error).
195195

196196
Most users will not need to override the default.
197+
.br
198+
.TP
199+
.BR KERNEL_SIGNING_CERT
200+
If you sign your kernel modules, set this to the path for the signing
201+
certificate so that your external modules are signed after being built.
202+
Setting to \fBauto\fR uses the kernel's automatically generated signing
203+
certificate if you have configured it to generate it.
204+
205+
By default this is not set and causes external modules to not be signed.
206+
Requires that the \fBKERNEL_SIGNING_KEY\fR variable is set.
207+
.br
208+
.TP
209+
.BR KERNEL_SIGNING_KEY
210+
If you sign your kernel modules, set this to the path for the signing key so
211+
that your external modules are signed after being built. Setting to \fBauto\fR
212+
uses the kernel's automatically generated signing key if you have configured it
213+
to generate it.
214+
215+
By default this is not set and causes external modules to not be signed.
216+
Requires that the \fBKERNEL_SIGNING_CERT\fR variable is set.
197217

198218
.RE
199219
.SH FUNCTIONS

0 commit comments

Comments
 (0)