Skip to content

Commit 01abbb6

Browse files
committed
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: https://www.github.com/delphix/linux-pkg/pull/371
1 parent d4ac034 commit 01abbb6

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

default-package-config.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# be overriden.
2424
#
2525

26+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27+
source "$SCRIPT_DIR/packages/common.sh"
28+
2629
function fetch() {
2730
logmust fetch_repo_from_git
2831
}
@@ -160,6 +163,33 @@ function kernel_build() {
160163
#
161164
logmust fakeroot debian/rules printenv "${debian_rules_args[@]}"
162165

166+
#
167+
# Download SB keys and configure signing keys/certs before build
168+
#
169+
download_keys
170+
171+
FLAVOUR=$platform
172+
OBJ=debian/build/build-$FLAVOUR
173+
CERTS=$OBJ/certs
174+
175+
ensure the objdir + certs dir exist
176+
mkdir -p "$CERTS"
177+
178+
# provide the key the packaging expects INSIDE the objdir
179+
# (symlink or copy)
180+
logmust ln -sf "${SB_KEYS_DIR}/signing_key.pem" "$CERTS/signing_key.pem"
181+
logmust chmod 600 "$CERTS/signing_key.pem"
182+
183+
# create the DER .x509 that sign-file needs from .crt)
184+
logmust openssl x509 -in "${SB_KEYS_DIR}/db.crt" -outform DER -out "$CERTS/signing_key.x509"
185+
# sanity checks
186+
logmust test -s "$CERTS/signing_key.pem" || { echo "missing signing_key.pem"; exit 1; }
187+
logmust test -s "$CERTS/signing_key.x509" || { echo "missing signing_key.x509"; exit 1; }
188+
logmust openssl pkey -in "$CERTS/signing_key.pem" -noout >/dev/null || { echo "key unreadable"; exit 1; }
189+
190+
SBSIGN_KEY="${SBSIGN_KEY:-$SB_KEYS_DIR/db.key}"
191+
SBSIGN_CERT="${SBSIGN_CERT:-$SB_KEYS_DIR/db.crt}"
192+
163193
#
164194
# The default value of the tool argument for mk-build-deps
165195
# is the following:
@@ -203,6 +233,33 @@ function kernel_build() {
203233
# one of the .debs produced
204234
#
205235
logmust test -f "artifacts/linux-image-${kernel_version}_"*.deb
236+
237+
#
238+
# After the build, unpackage linux-image package and sign vmlinuz
239+
#
240+
linux_deb=$(find artifacts -type f -name "linux-image-${kernel_version}*.deb" | head -n1)
241+
temp_dir=$(mktemp -d -p "/var/tmp/")
242+
logmust fakeroot dpkg-deb -R $linux_deb "$temp_dir"
243+
244+
bz="$temp_dir/boot/vmlinuz-${kernel_version}"
245+
logmust sbsign --key $SBSIGN_KEY --cert $SBSIGN_CERT --output "$bz.signed" "$bz"
246+
logmust mv "$bz.signed" "$bz"
247+
logmust sbverify --list "$bz"
248+
249+
# Update md5sums
250+
# ( cd "$temp_dir" || exit
251+
# : > DEBIAN/md5sums
252+
# print paths relative to root of package
253+
# while IFS= read -r -d '' f; do
254+
# rel="${f#./}"
255+
# md5sum "$rel" >> DEBIAN/md5sums
256+
# done < <(find . -type f ! -path './DEBIAN/*' -print0)
257+
# )
258+
259+
# Repack the .deb"
260+
update_md5sums "$temp_dir"
261+
repack_deb $linux_deb $temp_dir
262+
delete_keys
206263
}
207264

208265
#

packages/common.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
S3_KEYS_URL="s3://secure-boot-keys-prod/temp"
2+
SB_KEYS_DIR="/var/tmp/sbkeys"
3+
SBSIGN_KEY="$SB_KEYS_DIR/db.key"
4+
SBSIGN_DER="$SB_KEYS_DIR/db.der"
5+
6+
function download_keys() {
7+
logmust mkdir -p $SB_KEYS_DIR
8+
logmust aws s3 cp --recursive s3://secure-boot-keys-prod/temp/db/ $SB_KEYS_DIR
9+
}
10+
11+
function delete_keys() {
12+
logmust rm -r $SB_KEYS_DIR
13+
}
14+
15+
#
16+
# Update DEBIAN/md5sum for package directory after
17+
# some files were updated, i.e. secure-boot signed.
18+
#
19+
function update_md5sums() {
20+
pkg_dir=$1
21+
echo_bold "Updating md5sums for $pkg_dir"
22+
23+
( cd "$pkg_dir" || exit
24+
: > DEBIAN/md5sums
25+
# print paths relative to root of package
26+
while IFS= read -r -d '' f; do
27+
rel="${f#./}"
28+
md5sum "$rel" >> DEBIAN/md5sums
29+
done < <(find . -type f ! -path './DEBIAN/*' ! -path './etc/depmod*' -print0)
30+
)
31+
}
32+
33+
function repack_deb() {
34+
deb_name=$1
35+
deb_dir=$2
36+
temp_deb=$(mktemp /tmp/deb.XXXXXX)
37+
38+
logmust fakeroot dpkg-deb -b "$deb_dir" "$temp_deb"
39+
logmust mv "$temp_deb" "$deb_name"
40+
}

packages/connstat/config.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#
1717
# shellcheck disable=SC2034
1818

19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20+
source "$SCRIPT_DIR/../common.sh"
21+
1922
DEFAULT_PACKAGE_GIT_URL="https://github.com/delphix/connstat.git"
2023
PACKAGE_DEPENDENCIES="@linux-kernel dwarves"
2124

@@ -50,4 +53,31 @@ function build() {
5053

5154
logmust cd "$WORKDIR/repo"
5255
logmust mv ./*deb "$WORKDIR/artifacts/"
56+
57+
# Sign the module
58+
sign_module
59+
}
60+
61+
#
62+
# Unpack connstat module package, sign, then repack
63+
#
64+
function sign_module() {
65+
echo_bold "Signing constat module"
66+
download_keys
67+
68+
for connstat_pkg in $(find "$WORKDIR/artifacts" -type f -name "connstat-module-*.deb" ! -name "*-dbg*"); do
69+
echo_bold "Processing $connstat_pkg"
70+
temp_dir=$(mktemp -d -p "/var/tmp/")
71+
logmust fakeroot dpkg-deb -R "$connstat_pkg" "$temp_dir"
72+
73+
connstat_mod=$(find "$temp_dir" -type f -name connstat.ko)
74+
logmust kmodsign sha256 "$SBSIGN_KEY" "$SBSIGN_DER" "$connstat_mod" "$connstat_mod.signed"
75+
logmust mv "$connstat_mod.signed" "$connstat_mod"
76+
logmust modinfo -F signer "$connstat_mod"
77+
78+
# Repack the .deb"
79+
update_md5sums "$temp_dir"
80+
repack_deb "$connstat_pkg" "$temp_dir"
81+
done
82+
delete_keys
5383
}

packages/zfs/config.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#
1717
# shellcheck disable=SC2034
1818

19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20+
source "$SCRIPT_DIR/../common.sh"
21+
1922
DEFAULT_PACKAGE_GIT_URL="https://github.com/delphix/zfs.git"
2023
PACKAGE_DEPENDENCIES="@linux-kernel delphix-rust delphix-go dwarves"
2124

@@ -174,6 +177,37 @@ function build() {
174177
done
175178
logmust cd "$WORKDIR"
176179
logmust mv "all-packages/"*.deb "artifacts/"
180+
181+
# Sign ZFS modules in all packages
182+
sign_zfs_modules
183+
}
184+
185+
#
186+
# Unpack zfs-modules packages, sign, then repack
187+
#
188+
function sign_zfs_modules() {
189+
echo_bold "Signing ZFS modules"
190+
download_keys
191+
for zfs_pkg in $(find "$WORKDIR/artifacts" -type f -name "zfs-modules-*.deb" ! -name "*-dbg*"); do
192+
echo_bold "Processing $zfs_pkg"
193+
temp_dir=$(mktemp -d -p "/var/tmp/")
194+
logmust fakeroot dpkg-deb -R $zfs_pkg "$temp_dir"
195+
196+
zfs=$(find $temp_dir -type f -name zfs.ko)
197+
spl=$(find $temp_dir -type f -name spl.ko)
198+
199+
logmust kmodsign sha256 $SBSIGN_KEY $SBSIGN_DER $zfs "$zfs.signed"
200+
logmust kmodsign sha256 $SBSIGN_KEY $SBSIGN_DER $spl "$spl.signed"
201+
logmust mv "$zfs.signed" "$zfs"
202+
logmust mv "$spl.signed" "$spl"
203+
logmust modinfo -F signer "$zfs"
204+
logmust modinfo -F signer "$spl"
205+
206+
# Repack the .deb"
207+
update_md5sums "$temp_dir"
208+
repack_deb "$zfs_pkg" "$temp_dir"
209+
done
210+
delete_keys
177211
}
178212

179213
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': 'y'}>
58

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

0 commit comments

Comments
 (0)