Skip to content

Commit 54c1efe

Browse files
authored
CP-12693 Sign kernel modules and image during kernel build (no shim)
CP-12693 Sign kernel modules and image during kernel build (no shim) CP-12694 Sign ZFS modules after ZFS build (no shim) CP-12695 Sign connstat module after build (no shim) PR URL: #371
2 parents d4ac034 + 9a747b7 commit 54c1efe

File tree

5 files changed

+153
-3
lines changed

5 files changed

+153
-3
lines changed

default-package-config.sh

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright 2018, 2020 Delphix
3+
# Copyright 2018, 2025 Delphix
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -160,6 +160,44 @@ function kernel_build() {
160160
#
161161
logmust fakeroot debian/rules printenv "${debian_rules_args[@]}"
162162

163+
#
164+
# Configure signing keys/certs before build
165+
#
166+
# CONFIG_MODULE_SIG_KEY is set to /var/tmp/sbkeys/signing_key.pem in
167+
# resources/delphix_kernel_annotations
168+
#
169+
FLAVOUR=$platform
170+
OBJ=debian/build/build-$FLAVOUR
171+
CERTS=$OBJ/certs
172+
173+
# ensure the objdir + certs dir exist
174+
mkdir -p "$CERTS"
175+
download_keys
176+
177+
# provide the key the packaging expects INSIDE the objdir
178+
# (symlink or copy)
179+
logmust ln -sf "${SB_KEYS_DIR}/signing_key.pem" "$CERTS/signing_key.pem"
180+
logmust chmod 600 "$CERTS/signing_key.pem"
181+
182+
# create the DER .x509 that sign-file needs from .crt)
183+
logmust openssl x509 -in "${SB_KEYS_DIR}/db.crt" -outform DER -out "$CERTS/signing_key.x509"
184+
185+
# sanity checks
186+
logmust test -s "$CERTS/signing_key.pem" || {
187+
echo "missing signing_key.pem"
188+
exit 1
189+
}
190+
logmust test -s "$CERTS/signing_key.x509" || {
191+
echo "missing signing_key.x509"
192+
exit 1
193+
}
194+
logmust openssl pkey -in "$CERTS/signing_key.pem" -noout >/dev/null || {
195+
echo "key unreadable"
196+
exit 1
197+
}
198+
SBSIGN_KEY="${SBSIGN_KEY:-$SB_KEYS_DIR/db.key}"
199+
SBSIGN_CERT="${SBSIGN_CERT:-$SB_KEYS_DIR/db.crt}"
200+
163201
#
164202
# The default value of the tool argument for mk-build-deps
165203
# is the following:
@@ -203,6 +241,23 @@ function kernel_build() {
203241
# one of the .debs produced
204242
#
205243
logmust test -f "artifacts/linux-image-${kernel_version}_"*.deb
244+
245+
#
246+
# After the build, unpackage linux-image package and sign vmlinuz
247+
#
248+
linux_deb=$(find artifacts -type f -name "linux-image-${kernel_version}*.deb" | head -n1)
249+
temp_dir=$(mktemp -d -p "/var/tmp/")
250+
logmust fakeroot dpkg-deb -R $linux_deb "$temp_dir"
251+
252+
bz="$temp_dir/boot/vmlinuz-${kernel_version}"
253+
logmust sbsign --key $SBSIGN_KEY --cert $SBSIGN_CERT --output "$bz.signed" "$bz"
254+
logmust mv "$bz.signed" "$bz"
255+
logmust sbverify --list "$bz"
256+
257+
# Repack the .deb"
258+
update_md5sums "$temp_dir"
259+
repack_deb $linux_deb $temp_dir
260+
delete_keys
206261
}
207262

208263
#

lib/common.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,3 +1414,87 @@ function set_secret_build_args() {
14141414
_SECRET_BUILD_ARGS+=("-DSECRET_DB_AWS_REGION=$SECRET_DB_AWS_REGION")
14151415
fi
14161416
}
1417+
1418+
#
1419+
# Secure boot variables and functions
1420+
#
1421+
# S3 bucket containing keys and certs
1422+
# ./db subdirectory contains the db key and various certs:
1423+
# .der is for signing modules like ZFS and connstat
1424+
# .crt is for signing vmlinuz
1425+
# signing_key.pem is the format expected by kernel build for signing its modules
1426+
#
1427+
# ./pub contains the auth files, secure boot enrollment certs.
1428+
#
1429+
S3_KEYS_URL="s3://secure-boot-keys-prod/release"
1430+
#
1431+
# The kernel build expects the signing_key.pem in this directory, i.e.
1432+
# CONFIG_MODULE_SIG_KEY is set to /var/tmp/sbkeys/signing_key.pem in
1433+
# resources/delphix_kernel_annotations
1434+
#
1435+
SB_KEYS_DIR="/var/tmp/sbkeys"
1436+
SBSIGN_KEY="$SB_KEYS_DIR/db.key"
1437+
SBSIGN_DER="$SB_KEYS_DIR/db.der"
1438+
1439+
function download_keys() {
1440+
logmust mkdir -p $SB_KEYS_DIR
1441+
logmust aws s3 cp --recursive "$S3_KEYS_URL/db/" $SB_KEYS_DIR
1442+
}
1443+
1444+
function delete_keys() {
1445+
logmust rm -r $SB_KEYS_DIR
1446+
}
1447+
1448+
# Update DEBIAN/md5sum for package directory after
1449+
# some files were updated, i.e. secure-boot signed.
1450+
#
1451+
function update_md5sums() {
1452+
pkg_dir=$1
1453+
echo_bold "Updating md5sums for $pkg_dir"
1454+
1455+
(
1456+
cd "$pkg_dir" || exit
1457+
: >DEBIAN/md5sums
1458+
# print paths relative to root of package
1459+
while IFS= read -r -d '' f; do
1460+
rel="${f#./}"
1461+
md5sum "$rel" >>DEBIAN/md5sums
1462+
done < <(find . -type f ! -path './DEBIAN/*' ! -path './etc/depmod*' -print0)
1463+
)
1464+
}
1465+
1466+
function repack_deb() {
1467+
deb_name=$1
1468+
deb_dir=$2
1469+
temp_deb=$(mktemp /tmp/deb.XXXXXX)
1470+
1471+
logmust fakeroot dpkg-deb -b "$deb_dir" "$temp_deb"
1472+
logmust mv "$temp_deb" "$deb_name"
1473+
}
1474+
1475+
#
1476+
# Sign .ko files in the module list
1477+
#
1478+
function sign_modules() {
1479+
deb_pkgs="$1"
1480+
echo_bold "Signing $deb_pkgs"
1481+
download_keys
1482+
1483+
while IFS= read -r pkg; do
1484+
echo_bold "Processing $pkg"
1485+
temp_dir=$(mktemp -d -p "/var/tmp/")
1486+
logmust fakeroot dpkg-deb -R "$pkg" "$temp_dir"
1487+
1488+
# Find and sign all .ko files in package
1489+
find "$temp_dir" -type f -name "*.ko" -print0 |
1490+
while IFS= read -r -d '' kernel_mod; do
1491+
logmust kmodsign sha256 "$SBSIGN_KEY" "$SBSIGN_DER" "$kernel_mod" "$kernel_mod.signed"
1492+
logmust mv "$kernel_mod.signed" "$kernel_mod"
1493+
logmust modinfo -F signer "$kernel_mod"
1494+
done
1495+
# Repack the .deb"
1496+
update_md5sums "$temp_dir"
1497+
repack_deb "$pkg" "$temp_dir"
1498+
done <<<"$deb_pkgs"
1499+
delete_keys
1500+
}

packages/connstat/config.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright 2018, 2020 Delphix
3+
# Copyright 2018, 2025 Delphix
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -50,4 +50,8 @@ function build() {
5050

5151
logmust cd "$WORKDIR/repo"
5252
logmust mv ./*deb "$WORKDIR/artifacts/"
53+
54+
# Sign the generated modules
55+
connstat_pkgs=$(find "$WORKDIR/artifacts" -type f -name "connstat-module-*.deb" ! -name "*-dbg*")
56+
sign_modules "$connstat_pkgs"
5357
}

packages/zfs/config.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Copyright 2019, 2020 Delphix
3+
# Copyright 2019, 2025 Delphix
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -174,6 +174,10 @@ function build() {
174174
done
175175
logmust cd "$WORKDIR"
176176
logmust mv "all-packages/"*.deb "artifacts/"
177+
178+
# Sign ZFS modules in all packages
179+
zfs_pkgs=$(find "$WORKDIR/artifacts" -type f -name "zfs-modules-*.deb" ! -name "*-dbg*")
180+
sign_modules "$zfs_pkgs"
177181
}
178182

179183
function update_upstream() {

resources/delphix_kernel_annotations

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# FORMAT: 4
33
# ARCH: amd64
44
# FLAVOUR: amd64-aws amd64-azure amd64-generic amd64-gcp amd64-oracle
5+
#
6+
CONFIG_MODULE_SIG_KEY policy<{'amd64': '"/var/tmp/sbkeys/signing_key.pem"'}>
7+
CONFIG_MODULE_SIG_FORCE policy<{'amd64': 'y', 'arm64': 'n'}>
58

69
#
710
# Disable various "net" modules which we don't use.

0 commit comments

Comments
 (0)