Thread View: linux.gentoo.dev
6 messages
6 total messages
Started by David Seifert
Tue, 01 Dec 2026 16:00
[gentoo-dev] [PATCH 1/6] secureboot.eclass: support PKCS#11 keys with OpenSSL 4
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
68 lines
3612 bytes
3612 bytes
OpenSSL 4 dropped the ENGINE API. Use pkcs11-provider for the key check
and systemd-sbsign, and sbsign's --provider option from
sbsigntools-0.9.5_p1 on.
Signed-off-by: David Seifert <soap@gentoo.org>
---
eclass/secureboot.eclass | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/eclass/secureboot.eclass b/eclass/secureboot.eclass
index 15c86e58ffab0..a259ce362833f 100644
--- a/eclass/secureboot.eclass
+++ b/eclass/secureboot.eclass
@@ -65,7 +65,8 @@ BDEPEND="
# @DEFAULT_UNSET
# @DESCRIPTION:
# Used with USE=secureboot. Should be set to the path of the private
-# key in PEM format to use, or a PKCS#11 URI.
+# key in PEM format to use, or a PKCS#11 URI. With OpenSSL 4, PKCS#11
+# URIs require dev-libs/pkcs11-provider.
# If unspecified the following locations are tried in order:
# - /etc/portage/secureboot.pem
# - /var/lib/sbctl/keys/db/db.{key,pem} (from app-crypt/sbctl)
@@ -201,7 +202,12 @@ secureboot_pkg_setup() {
-noout -nocert
)
if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${SECUREBOOT_SIGN_KEY}" )
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ openssl_args+=( -provider default -provider pkcs11 -key "${SECUREBOOT_SIGN_KEY}" )
+ else
+ openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${SECUREBOOT_SIGN_KEY}" )
+ fi
else
openssl_args+=( -keyform PEM -key "${SECUREBOOT_SIGN_KEY}" )
fi
@@ -243,7 +249,13 @@ secureboot_sign_efi_file() {
"--cert=${SECUREBOOT_SIGN_CERT}"
)
if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then
- args+=( "--engine=pkcs11" )
+ # 0.9.5_p1 replaced --engine with --provider
+ if has_version -b ">=app-crypt/sbsigntools-0.9.5_p1"; then
+ # sbsign loads the default provider before this one
+ args+=( "--provider=pkcs11" )
+ else
+ args+=( "--engine=pkcs11" )
+ fi
fi
"${EPREFIX}/usr/bin/sbsign" "${input_file}" "${args[@]}"
@@ -254,7 +266,12 @@ secureboot_sign_efi_file() {
"--certificate=${SECUREBOOT_SIGN_CERT}"
)
if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then
- args+=( "--private-key-source=engine:pkcs11" )
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ args+=( "--private-key-source=provider:pkcs11" )
+ else
+ args+=( "--private-key-source=engine:pkcs11" )
+ fi
fi
"${EPREFIX}/usr/lib/systemd/systemd-sbsign" sign "${input_file}" "${args[@]}"
--
2.56.1
[gentoo-dev] [PATCH 4/6] secureboot.eclass, kernel-build.eclass: check key/cert without signing
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
148 lines
7071 bytes
7071 bytes
"openssl x509 -key" self-signs, costing a PIN entry or touch with a
hardware token, and does not catch a key not matching the certificate.
Compare public keys instead.
Signed-off-by: David Seifert <soap@gentoo.org>
---
eclass/kernel-build.eclass | 46 +++++---------------------------
eclass/secureboot.eclass | 54 ++++++++++++++++++++++++++------------
2 files changed, 43 insertions(+), 57 deletions(-)
diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass
index 2681be4781dd1..4400fcc9754b2 100644
--- a/eclass/kernel-build.eclass
+++ b/eclass/kernel-build.eclass
@@ -182,29 +182,10 @@ kernel-build_pkg_setup() {
secureboot_pkg_setup
if use modules-sign && [[ -n ${MODULES_SIGN_KEY} ]]; then
- # Sanity check: fail early if key/cert in DER format or does not exist
- local openssl_args=(
- -noout -nocert
- )
- if [[ -n ${MODULES_SIGN_CERT} ]]; then
- openssl_args+=( -inform PEM -in "${MODULES_SIGN_CERT}" )
- else
- # If no cert specified, we assume the pem key also contains the cert
- openssl_args+=( -inform PEM -in "${MODULES_SIGN_KEY}" )
- fi
- if [[ ${MODULES_SIGN_KEY} == pkcs11:* ]]; then
- # OpenSSL 4 removed the ENGINE API
- if has_version -b ">=dev-libs/openssl-4"; then
- openssl_args+=( -provider default -provider pkcs11 -key "${MODULES_SIGN_KEY}" )
- else
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MODULES_SIGN_KEY}" )
- fi
- else
- openssl_args+=( -keyform PEM -key "${MODULES_SIGN_KEY}" )
- fi
-
- openssl x509 "${openssl_args[@]}" ||
- die "Kernel module signing certificate or key not found or not PEM format."
+ # Sanity check: fail early if key/cert in DER format or does not exist.
+ # If no cert specified, we assume the pem key also contains the cert
+ _secureboot_check_keypair "${MODULES_SIGN_KEY}" "${MODULES_SIGN_CERT:-${MODULES_SIGN_KEY}}" ||
+ die "Kernel module signing certificate or key not found, not PEM format, or not matching."
if [[ ${MODULES_SIGN_KEY} != pkcs11:* ]]; then
if [[ -n ${MODULES_SIGN_CERT} && ${MODULES_SIGN_CERT} != ${MODULES_SIGN_KEY} ]]; then
@@ -234,23 +215,8 @@ kernel-build_pkg_setup() {
fi
# Sanity check: fail early if key/cert in DER format or does not exist
- local openssl_args=(
- -inform PEM -in "${MEASUREDBOOT_SIGN_CERT}"
- -noout -nocert
- )
- if [[ ${MEASUREDBOOT_SIGN_KEY} == pkcs11:* ]]; then
- # OpenSSL 4 removed the ENGINE API
- if has_version -b ">=dev-libs/openssl-4"; then
- openssl_args+=( -provider default -provider pkcs11 -key "${MEASUREDBOOT_SIGN_KEY}" )
- else
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MEASUREDBOOT_SIGN_KEY}" )
- fi
- else
- openssl_args+=( -keyform PEM -key "${MEASUREDBOOT_SIGN_KEY}" )
- fi
-
- openssl x509 "${openssl_args[@]}" ||
- die "Measured Boot signing certificate or key not found or not PEM format."
+ _secureboot_check_keypair "${MEASUREDBOOT_SIGN_KEY}" "${MEASUREDBOOT_SIGN_CERT}" ||
+ die "Measured Boot signing certificate or key not found, not PEM format, or not matching."
fi
fi
fi
diff --git a/eclass/secureboot.eclass b/eclass/secureboot.eclass
index a259ce362833f..91c3b4f99b4e5 100644
--- a/eclass/secureboot.eclass
+++ b/eclass/secureboot.eclass
@@ -91,6 +91,41 @@ _SECUREBOOT_ECLASS=1
inherit linux-info
+# @FUNCTION: _secureboot_check_keypair
+# @USAGE: <key> <cert>
+# @INTERNAL
+# @DESCRIPTION:
+# Check that <key>, a PEM file or PKCS#11 URI, and the PEM certificate
+# <cert> exist and belong together. Compares public keys instead of
+# signing anything, which would cost a PIN or touch on a hardware token.
+_secureboot_check_keypair() {
+ local key=${1} cert=${2} key_args=() key_pub cert_pub
+
+ if [[ ${key} == pkcs11:* ]]; then
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ # Load default first: without a property query, OpenSSL uses
+ # the first registered implementation, and pkcs11-provider
+ # also offers digests if the PKCS#11 module does, which would
+ # route hashing through the module.
+ key_args=( -provider default -provider pkcs11 )
+ else
+ key_args=( -engine pkcs11 -inform ENGINE )
+ fi
+ else
+ # Without it, pkey also accepts DER, which signing tools reject
+ key_args=( -inform PEM )
+ fi
+
+ # Re-encode with the default provider only: pkcs11-provider exports
+ # PKCS#1 instead of SubjectPublicKeyInfo, and libp11's ENGINE may
+ # print diagnostics to stdout, which the PEM reader skips.
+ key_pub=$(openssl pkey "${key_args[@]}" -in "${key}" -pubout |
+ openssl pkey -pubin -pubout) || return 1
+ cert_pub=$(openssl x509 -inform PEM -in "${cert}" -noout -pubkey) || return 1
+ [[ ${key_pub} == "${cert_pub}" ]]
+}
+
# @FUNCTION: secureboot_pkg_setup
# @DESCRIPTION:
# Checks if required user variables are set before starting the build
@@ -197,23 +232,8 @@ secureboot_pkg_setup() {
fi
# Sanity check: fail early if key/cert in DER format or does not exist
- local openssl_args=(
- -inform PEM -in "${SECUREBOOT_SIGN_CERT}"
- -noout -nocert
- )
- if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then
- # OpenSSL 4 removed the ENGINE API
- if has_version -b ">=dev-libs/openssl-4"; then
- openssl_args+=( -provider default -provider pkcs11 -key "${SECUREBOOT_SIGN_KEY}" )
- else
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${SECUREBOOT_SIGN_KEY}" )
- fi
- else
- openssl_args+=( -keyform PEM -key "${SECUREBOOT_SIGN_KEY}" )
- fi
-
- openssl x509 "${openssl_args[@]}" ||
- die "Secure Boot signing certificate or key not found or not PEM format."
+ _secureboot_check_keypair "${SECUREBOOT_SIGN_KEY}" "${SECUREBOOT_SIGN_CERT}" ||
+ die "Secure Boot signing certificate or key not found, not PEM format, or not matching."
fi
}
--
2.56.1
[gentoo-dev] [PATCH 6/6] kernel-build.eclass, linux-mod-r1.eclass: KBUILD_SIGN_PIN is PEM only
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
43 lines
2813 bytes
2813 bytes
With OpenSSL 3 and newer, sign-file ignores KBUILD_SIGN_PIN for PKCS#11 keys. Document passing the PIN in the URI instead. Signed-off-by: David Seifert <soap@gentoo.org> --- eclass/kernel-build.eclass | 5 +++++ eclass/linux-mod-r1.eclass | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass index 41777d0fc1c5a..5e23a074cd372 100644 --- a/eclass/kernel-build.eclass +++ b/eclass/kernel-build.eclass @@ -125,6 +125,11 @@ REQUIRED_USE="secureboot? ( modules-sign )" # eclass unsets the variable after use to mitigate the issue (notably # for shared binary packages), but use this with care. # +# With OpenSSL 3 and newer, sign-file loads PKCS#11 keys through the +# "pkcs11" provider (dev-libs/pkcs11-provider) and ignores +# KBUILD_SIGN_PIN for them. Pass the PIN in the URI (pin-value= or +# pin-source=) or in the provider configuration instead. +# # Default if unset: certs/signing_key.pem # @ECLASS_VARIABLE: MODULES_SIGN_CERT diff --git a/eclass/linux-mod-r1.eclass b/eclass/linux-mod-r1.eclass index 5049b90ac7b68..6e8158b40687f 100644 --- a/eclass/linux-mod-r1.eclass +++ b/eclass/linux-mod-r1.eclass @@ -236,6 +236,11 @@ fi # eclass unsets the variable after use to mitigate the issue (notably # for shared binary packages), but use this with care. # +# With OpenSSL 3 and newer, sign-file loads PKCS#11 keys through the +# "pkcs11" provider (dev-libs/pkcs11-provider) and ignores +# KBUILD_SIGN_PIN for them. Pass the PIN in the URI (pin-value= or +# pin-source=) or in the provider configuration instead. +# # Default if unset: kernel CONFIG_MODULE_SIG_KEY's value which itself # defaults to certs/signing_key.pem -- 2.56.1
[gentoo-dev] [PATCH 2/6] kernel-build.eclass: support PKCS#11 keys with OpenSSL 4
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
80 lines
4140 bytes
4140 bytes
OpenSSL 4 dropped the ENGINE API. Use pkcs11-provider for the key checks
and ukify. ukify can only pass an engine to sbsign, so die early when
that combination would fail.
Signed-off-by: David Seifert <soap@gentoo.org>
---
eclass/kernel-build.eclass | 37 ++++++++++++++++++++++++++++++++-----
1 file changed, 32 insertions(+), 5 deletions(-)
diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass
index a66481aeeb5cc..f8e5abbca4a92 100644
--- a/eclass/kernel-build.eclass
+++ b/eclass/kernel-build.eclass
@@ -178,7 +178,12 @@ kernel-build_pkg_setup() {
openssl_args+=( -inform PEM -in "${MODULES_SIGN_KEY}" )
fi
if [[ ${MODULES_SIGN_KEY} == pkcs11:* ]]; then
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MODULES_SIGN_KEY}" )
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ openssl_args+=( -provider default -provider pkcs11 -key "${MODULES_SIGN_KEY}" )
+ else
+ openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MODULES_SIGN_KEY}" )
+ fi
else
openssl_args+=( -keyform PEM -key "${MODULES_SIGN_KEY}" )
fi
@@ -196,6 +201,16 @@ kernel-build_pkg_setup() {
fi
if [[ ${KERNEL_IUSE_GENERIC_UKI} ]] && use generic-uki; then
+ # ukify can only pass an engine to sbsign, which
+ # sbsigntools-0.9.5_p1 replaced with providers
+ if use secureboot &&
+ [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* &&
+ ${SECUREBOOT_SIGN_TOOL} == sbsign ]] &&
+ has_version -b ">=app-crypt/sbsigntools-0.9.5_p1"
+ then
+ die "USE=generic-uki with a PKCS#11 SECUREBOOT_SIGN_KEY requires SECUREBOOT_SIGN_TOOL=systemd-sbsign"
+ fi
+
if [[ -n ${MEASUREDBOOT_SIGN_KEY} ]]; then
if [[ -z ${MEASUREDBOOT_SIGN_CERT} ]]; then
ewarn "A MEASUREDBOOT_SIGN_KEY was specified but no MEASUREDBOOT_SIGN_CERT"
@@ -209,7 +224,12 @@ kernel-build_pkg_setup() {
-noout -nocert
)
if [[ ${MEASUREDBOOT_SIGN_KEY} == pkcs11:* ]]; then
- openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MEASUREDBOOT_SIGN_KEY}" )
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ openssl_args+=( -provider default -provider pkcs11 -key "${MEASUREDBOOT_SIGN_KEY}" )
+ else
+ openssl_args+=( -engine pkcs11 -keyform ENGINE -key "${MEASUREDBOOT_SIGN_KEY}" )
+ fi
else
openssl_args+=( -keyform PEM -key "${MEASUREDBOOT_SIGN_KEY}" )
fi
@@ -682,9 +702,16 @@ kernel-build_src_install() {
if use secureboot; then
if [[ ${SECUREBOOT_SIGN_KEY} == pkcs11:* ]]; then
- ukify_args+=(
- --signing-engine="pkcs11"
- )
+ # OpenSSL 4 removed the ENGINE API
+ if has_version -b ">=dev-libs/openssl-4"; then
+ ukify_args+=(
+ --signing-provider="pkcs11"
+ )
+ else
+ ukify_args+=(
+ --signing-engine="pkcs11"
+ )
+ fi
fi
# systemd-sbsign does not support --sign-kernel.
# Disable unconditionally since we know the kernel
--
2.56.1
[gentoo-dev] [PATCH 5/6] {secureboot,kernel-build,linux-mod-r1}.eclass: add *_SIGN_GNUPGHOME
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
157 lines
7583 bytes
7583 bytes
Let a pre-started gpg-agent, e.g. used via app-crypt/scute, keep a token
unlocked across all signing processes of a build, so the PIN only has
to be entered once.
Signed-off-by: David Seifert <soap@gentoo.org>
---
eclass/kernel-build.eclass | 20 +++++++++++++++++
eclass/linux-mod-r1.eclass | 17 ++++++++++++++
eclass/secureboot.eclass | 46 ++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+)
diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass
index 4400fcc9754b2..41777d0fc1c5a 100644
--- a/eclass/kernel-build.eclass
+++ b/eclass/kernel-build.eclass
@@ -150,6 +150,18 @@ REQUIRED_USE="secureboot? ( modules-sign )"
# MOK. With USE=secureboot, also set SECUREBOOT_SIGN_KEY, as the
# discarded key cannot serve as the Secure Boot signing key fallback.
+# @ECLASS_VARIABLE: MODULES_SIGN_GNUPGHOME
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Used with USE=modules-sign and a PKCS#11 MODULES_SIGN_KEY provided by
+# a module that talks to gpg-agent, such as app-crypt/scute. Can be set
+# to the GnuPG home directory of an already running gpg-agent, see
+# SECUREBOOT_SIGN_GNUPGHOME in secureboot.eclass for details.
+#
+# With USE=secureboot, it must match SECUREBOOT_SIGN_GNUPGHOME, as the
+# modules and the EFI images are signed in the same phase.
+
# @ECLASS_VARIABLE: KERNEL_GENERIC_UKI_CMDLINE
# @USER_VARIABLE
# @DESCRIPTION:
@@ -182,6 +194,14 @@ kernel-build_pkg_setup() {
secureboot_pkg_setup
if use modules-sign && [[ -n ${MODULES_SIGN_KEY} ]]; then
+ # GNUPGHOME is a single environment variable
+ if use secureboot && [[ -n ${MODULES_SIGN_GNUPGHOME} &&
+ ${MODULES_SIGN_GNUPGHOME} != "${SECUREBOOT_SIGN_GNUPGHOME}" ]]
+ then
+ die "MODULES_SIGN_GNUPGHOME and SECUREBOOT_SIGN_GNUPGHOME must match with USE=secureboot"
+ fi
+ _secureboot_setup_gnupghome MODULES_SIGN_GNUPGHOME
+
# Sanity check: fail early if key/cert in DER format or does not exist.
# If no cert specified, we assume the pem key also contains the cert
_secureboot_check_keypair "${MODULES_SIGN_KEY}" "${MODULES_SIGN_CERT:-${MODULES_SIGN_KEY}}" ||
diff --git a/eclass/linux-mod-r1.eclass b/eclass/linux-mod-r1.eclass
index 247739b951989..5049b90ac7b68 100644
--- a/eclass/linux-mod-r1.eclass
+++ b/eclass/linux-mod-r1.eclass
@@ -249,6 +249,15 @@ fi
# relative to the kernel build directory being used.
: "${MODULES_SIGN_CERT:=certs/signing_key.x509}"
+# @ECLASS_VARIABLE: MODULES_SIGN_GNUPGHOME
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Used with USE=modules-sign and a PKCS#11 MODULES_SIGN_KEY provided by
+# a module that talks to gpg-agent, such as app-crypt/scute. Can be set
+# to the GnuPG home directory of an already running gpg-agent, see
+# SECUREBOOT_SIGN_GNUPGHOME in secureboot.eclass for details.
+
# @ECLASS_VARIABLE: MODULES_KERNEL_MAX
# @DEFAULT_UNSET
# @DESCRIPTION:
@@ -783,6 +792,14 @@ _modules_prepare_sign() {
_modules_sign_die "the private key '${MODULES_SIGN_KEY}' was not found"
[[ -f ${MODULES_SIGN_CERT} ]] ||
_modules_sign_die "the public key certificate '${MODULES_SIGN_CERT}' was not found"
+
+ if [[ -n ${MODULES_SIGN_GNUPGHOME} ]]; then
+ [[ -d ${MODULES_SIGN_GNUPGHOME} ]] ||
+ _modules_sign_die "MODULES_SIGN_GNUPGHOME '${MODULES_SIGN_GNUPGHOME}' is not a directory"
+ export GNUPGHOME=${MODULES_SIGN_GNUPGHOME}
+ # gpgsm takes lock files there; carried over into later phases
+ addwrite "${GNUPGHOME}"
+ fi
}
# @FUNCTION: _modules_prepare_toolchain
diff --git a/eclass/secureboot.eclass b/eclass/secureboot.eclass
index 91c3b4f99b4e5..5f0f6ac6ccf09 100644
--- a/eclass/secureboot.eclass
+++ b/eclass/secureboot.eclass
@@ -86,6 +86,34 @@ BDEPEND="
# Default if unset: sbsign
: "${SECUREBOOT_SIGN_TOOL:=sbsign}"
+# @ECLASS_VARIABLE: SECUREBOOT_SIGN_GNUPGHOME
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Used with USE=secureboot and a PKCS#11 SECUREBOOT_SIGN_KEY provided
+# by a module that talks to gpg-agent, such as app-crypt/scute. Can be
+# set to the GnuPG home directory of a gpg-agent that the user started
+# before the build. The agent keeps the token unlocked, so the PIN is
+# only entered once instead of once per signing process.
+#
+# The eclass exports it as GNUPGHOME and allows writes to it in the
+# sandbox. Signing in pkg_setup and src_install runs as root, so the
+# agent must be reachable for root, either by running it as root with
+# this home directory, or via an Assuan socket redirection file named
+# S.gpg-agent in this directory. kernel-build.eclass also signs the
+# in-tree modules in src_test, which runs as the portage user with
+# FEATURES=userpriv; the agent must then be reachable for that user
+# too, and the directory writable by it, as gpgsm takes lock files
+# there.
+#
+# While the token is unlocked, any process that can reach the agent can
+# sign without entering the PIN. scute also sends "SCD LEARN --force"
+# to the agent, which may rewrite the agent's card stubs, so a dedicated
+# agent is preferable to the user's own one.
+#
+# Default if unset: MODULES_SIGN_GNUPGHOME
+: "${SECUREBOOT_SIGN_GNUPGHOME:=${MODULES_SIGN_GNUPGHOME}}"
+
if [[ -z ${_SECUREBOOT_ECLASS} ]]; then
_SECUREBOOT_ECLASS=1
@@ -126,6 +154,22 @@ _secureboot_check_keypair() {
[[ ${key_pub} == "${cert_pub}" ]]
}
+# @FUNCTION: _secureboot_setup_gnupghome
+# @USAGE: <variable name>
+# @INTERNAL
+# @DESCRIPTION:
+# If the named variable is set, export its value as GNUPGHOME and allow
+# writes to it in the sandbox, see SECUREBOOT_SIGN_GNUPGHOME.
+_secureboot_setup_gnupghome() {
+ local var=${1}
+ [[ -n ${!var} ]] || return 0
+
+ [[ -d ${!var} ]] || die "${var}=${!var} is not a directory"
+ export GNUPGHOME=${!var}
+ # gpgsm takes lock files there; carried over into later phases
+ addwrite "${GNUPGHOME}"
+}
+
# @FUNCTION: secureboot_pkg_setup
# @DESCRIPTION:
# Checks if required user variables are set before starting the build
@@ -136,6 +180,8 @@ secureboot_pkg_setup() {
# If we are merging a binary then the files in this binary
# are already signed, no need to check the variables.
if [[ ${MERGE_TYPE} != binary ]]; then
+ _secureboot_setup_gnupghome SECUREBOOT_SIGN_GNUPGHOME
+
if [[ -z ${SECUREBOOT_SIGN_KEY} ]]; then
# No key specified, try some usual suspects
linux-info_pkg_setup
--
2.56.1
[gentoo-dev] [PATCH 3/6] kernel-build.eclass: add MODULES_SIGN_EPHEMERAL
Author: David Seifert
Date: Tue, 01 Dec 2026 16:00
Date: Tue, 01 Dec 2026 16:00
75 lines
4482 bytes
4482 bytes
Discard the key generated by the kernel build instead of installing it
into /usr/src, where root could use it to sign modules. In-tree modules
only need it during the build, out-of-tree modules can use a separate
MODULES_SIGN_KEY.
Signed-off-by: David Seifert <soap@gentoo.org>
---
eclass/kernel-build.eclass | 31 ++++++++++++++++++++++++++++---
1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass
index f8e5abbca4a92..2681be4781dd1 100644
--- a/eclass/kernel-build.eclass
+++ b/eclass/kernel-build.eclass
@@ -135,6 +135,21 @@ REQUIRED_USE="secureboot? ( modules-sign )"
# key in PEM format to use. Must be specified if MODULES_SIGN_KEY
# is set to a path of a file that only contains the private key.
+# @ECLASS_VARIABLE: MODULES_SIGN_EPHEMERAL
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Used with USE=modules-sign when MODULES_SIGN_KEY is unset. If set to
+# a non-empty value, the private key generated by the kernel build
+# system is discarded after the in-tree modules have been signed,
+# instead of being installed into the kernel source directory.
+#
+# Out-of-tree modules then need their own MODULES_SIGN_KEY (see
+# linux-mod-r1.eclass), whose certificate the kernel must trust, e.g.
+# via CONFIG_SYSTEM_TRUSTED_KEYS in /etc/kernel/config.d or a trusted
+# MOK. With USE=secureboot, also set SECUREBOOT_SIGN_KEY, as the
+# discarded key cannot serve as the Secure Boot signing key fallback.
+
# @ECLASS_VARIABLE: KERNEL_GENERIC_UKI_CMDLINE
# @USER_VARIABLE
# @DESCRIPTION:
@@ -524,7 +539,12 @@ kernel-build_src_install() {
# If a key was generated, exclude it from the binpkg
local generated_key=${ED}${kernel_dir}/certs/signing_key.pem
if [[ -r ${generated_key} ]]; then
- mv "${generated_key}" "${T}/signing_key.pem" || die
+ if [[ ${MODULES_SIGN_EPHEMERAL} ]]; then
+ rm "${generated_key}" || die
+ else
+ # kernel-install_pkg_preinst moves it back into place
+ mv "${generated_key}" "${T}/signing_key.pem" || die
+ fi
fi
# building modules fails with 'vmlinux has no symtab?' if stripped
@@ -775,7 +795,11 @@ kernel-build_pkg_postinst() {
kernel-install_pkg_postinst
savedconfig_pkg_postinst
- if use modules-sign && [[ -z ${MODULES_SIGN_KEY} ]]; then
+ if use modules-sign && [[ -z ${MODULES_SIGN_KEY} && ${MODULES_SIGN_EPHEMERAL} ]]; then
+ elog "MODULES_SIGN_EPHEMERAL is set, the signing key generated by the"
+ elog "kernel build system was discarded. Out-of-tree modules must be"
+ elog "signed with MODULES_SIGN_KEY, whose certificate the kernel trusts."
+ elif use modules-sign && [[ -z ${MODULES_SIGN_KEY} ]]; then
ewarn
ewarn "MODULES_SIGN_KEY was not set, this means the kernel build system"
ewarn "automatically generated the signing key. This key was installed"
@@ -787,7 +811,8 @@ kernel-build_pkg_postinst() {
ewarn "Use emerge @module-rebuild, or manually sign the modules as"
ewarn "described on the wiki [1]"
ewarn
- ewarn "Consider using the MODULES_SIGN_KEY variable to use an external key."
+ ewarn "Consider using the MODULES_SIGN_KEY variable to use an external key,"
+ ewarn "or MODULES_SIGN_EPHEMERAL to discard the generated key."
ewarn
ewarn "[1]: https://wiki.gentoo.org/wiki/Signed_kernel_module_support"
fi
--
2.56.1
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads