Skip to content

Commit 98fa473

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 98fa473

File tree

6 files changed

+209
-6
lines changed

6 files changed

+209
-6
lines changed

.github/scripts/verify-query-packages.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash -ex
22
# shellcheck disable=SC2012
3+
# shellcheck disable=SC2010
34

45
set -o pipefail
56

@@ -31,11 +32,11 @@ test ${#fields[@]} -eq 1
3132
test "${fields[0]}" == 'https://github.com/delphix/zfs.git'
3233

3334
# Expect that "list all" outputs all directory names under packages/
34-
diff <(ls -1 packages | sort) <(./query-packages.sh list all 2>&1 | sort)
35+
diff <(ls -1 packages | grep -v common.sh | sort) <(./query-packages.sh list all 2>&1 | sort)
3536

3637
# Expect that outputing dependencies & git-url for all packages works and that the output
3738
# length corresponds to the number of packages.
38-
test "$(ls -1 packages | wc -l)" -eq \
39+
test "$(ls -1 packages | grep -c -v common.sh)" -eq \
3940
"$(./query-packages.sh list -o name,dependencies,can-update,git-url all 2>&1 | wc -l)"
4041

4142
# Check that all package lists under package-lists\ can be loaded and that each
@@ -76,6 +77,6 @@ test "$(TARGET_KERNEL_FLAVORS="generic aws" ./query-packages.sh single -o depend
7677
# Check that executing query-packages works from another directory.
7778
# This redoes the "list all" test from above
7879
cd packages
79-
diff <(ls -1 | sort) <(../query-packages.sh list all 2>&1 | sort)
80+
diff <(ls -1 | grep -v common.sh | sort) <(../query-packages.sh list all 2>&1 | sort)
8081

8182
echo "All tests passed"

default-package-config.sh

Lines changed: 59 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.
@@ -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,44 @@ function kernel_build() {
160163
#
161164
logmust fakeroot debian/rules printenv "${debian_rules_args[@]}"
162165

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

208266
#

packages/common.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# Copyright 2025 Delphix
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# shellcheck shell=bash
17+
# shellcheck disable=SC2034
18+
# common helpers...
19+
20+
#
21+
# S3 bucket containing keys and certs
22+
# ./db subdirectory contains the db key and various certs:
23+
# .der is for signing modules like ZFS and connstat
24+
# .crt is for signing vmlinuz
25+
# signing_key.pem is the format expected by kernel build for signing its modules
26+
#
27+
# ./pub contains the auth files, secure boot enrollment certs.
28+
#
29+
S3_KEYS_URL="s3://secure-boot-keys-prod/temp"
30+
#
31+
# The kernel build expects the signing_key.pem in this directory, i.e.
32+
# CONFIG_MODULE_SIG_KEY is set to /var/tmp/sbkeys/signing_key.pem in
33+
# resources/delphix_kernel_annotations
34+
#
35+
SB_KEYS_DIR="/var/tmp/sbkeys"
36+
SBSIGN_KEY="$SB_KEYS_DIR/db.key"
37+
SBSIGN_DER="$SB_KEYS_DIR/db.der"
38+
39+
function download_keys() {
40+
logmust mkdir -p $SB_KEYS_DIR
41+
logmust aws s3 cp --recursive "$S3_KEYS_URL/db/" $SB_KEYS_DIR
42+
}
43+
44+
function delete_keys() {
45+
logmust rm -r $SB_KEYS_DIR
46+
}
47+
48+
#
49+
# Update DEBIAN/md5sum for package directory after
50+
# some files were updated, i.e. secure-boot signed.
51+
#
52+
function update_md5sums() {
53+
pkg_dir=$1
54+
echo_bold "Updating md5sums for $pkg_dir"
55+
56+
(
57+
cd "$pkg_dir" || exit
58+
: >DEBIAN/md5sums
59+
# print paths relative to root of package
60+
while IFS= read -r -d '' f; do
61+
rel="${f#./}"
62+
md5sum "$rel" >>DEBIAN/md5sums
63+
done < <(find . -type f ! -path './DEBIAN/*' ! -path './etc/depmod*' -print0)
64+
)
65+
}
66+
67+
function repack_deb() {
68+
deb_name=$1
69+
deb_dir=$2
70+
temp_deb=$(mktemp /tmp/deb.XXXXXX)
71+
72+
logmust fakeroot dpkg-deb -b "$deb_dir" "$temp_deb"
73+
logmust mv "$temp_deb" "$deb_name"
74+
}

packages/connstat/config.sh

Lines changed: 32 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.
@@ -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,32 @@ 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 connstat module"
66+
download_keys
67+
68+
find "$WORKDIR/artifacts" -type f -name "connstat-module-*.deb" ! -name "*-dbg*" -print0 |
69+
while IFS= read -r -d '' connstat_pkg; do
70+
echo_bold "Processing $connstat_pkg"
71+
temp_dir=$(mktemp -d -p "/var/tmp/")
72+
logmust fakeroot dpkg-deb -R "$connstat_pkg" "$temp_dir"
73+
74+
connstat_mod=$(find "$temp_dir" -type f -name connstat.ko)
75+
logmust kmodsign sha256 "$SBSIGN_KEY" "$SBSIGN_DER" "$connstat_mod" "$connstat_mod.signed"
76+
logmust mv "$connstat_mod.signed" "$connstat_mod"
77+
logmust modinfo -F signer "$connstat_mod"
78+
79+
# Repack the .deb"
80+
update_md5sums "$temp_dir"
81+
repack_deb "$connstat_pkg" "$temp_dir"
82+
done
83+
delete_keys
5384
}

packages/zfs/config.sh

Lines changed: 37 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.
@@ -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,39 @@ 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+
192+
find "$WORKDIR/artifacts" -type f -name "zfs-modules-*.deb" ! -name "*-dbg*" -print0 |
193+
while IFS= read -r -d '' zfs_pkg; do
194+
echo_bold "Processing $zfs_pkg"
195+
temp_dir=$(mktemp -d -p "/var/tmp/")
196+
logmust fakeroot dpkg-deb -R "$zfs_pkg" "$temp_dir"
197+
198+
zfs=$(find "$temp_dir" -type f -name zfs.ko)
199+
spl=$(find "$temp_dir" -type f -name spl.ko)
200+
201+
logmust kmodsign sha256 "$SBSIGN_KEY" "$SBSIGN_DER" "$zfs" "$zfs.signed"
202+
logmust kmodsign sha256 "$SBSIGN_KEY" "$SBSIGN_DER" "$spl" "$spl.signed"
203+
logmust mv "$zfs.signed" "$zfs"
204+
logmust mv "$spl.signed" "$spl"
205+
logmust modinfo -F signer "$zfs"
206+
logmust modinfo -F signer "$spl"
207+
208+
# Repack the .deb"
209+
update_md5sums "$temp_dir"
210+
repack_deb "$zfs_pkg" "$temp_dir"
211+
done
212+
delete_keys
177213
}
178214

179215
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)