Skip to content

Commit f05097d

Browse files
authored
Fix kernel module signing with ephemeral keys for official builds (#3493)
* sdk: Fix ephemeral key directory paths baked into container images The SDK container build process was persisting temporary directory paths for module signing keys into /home/sdk/.bashrc. This caused all container instances to share the same ephemeral key location. Fixed by: - Runtime check in sdk_entry.sh to recreate stale temp directories - Build-time cleanup in Dockerfiles to remove the variables Each container instance now gets unique temporary directories. Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com> * sdk_entry: use persistent module signing keys for unofficial builds For official builds (COREOS_OFFICIAL=1), continue using ephemeral temporary directories for module signing keys. For unofficial/development builds, use a persistent directory at /mnt/host/source/.module-signing-keys to preserve keys across container restarts. Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com> --------- Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com>
1 parent 72a74fd commit f05097d

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

sdk_container/src/third_party/coreos-overlay/eclass/coreos-kernel.eclass

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ get_sig_key() {
143143
die "MODULE_SIG_KEY is using the default value"
144144
fi
145145

146-
if [[ ${sig_key} != /tmp/* ]]; then
147-
die "Refusing to to continue with modules key outside of /tmp, so that it stays in RAM only."
146+
# For official builds, enforce /tmp to keep keys in RAM only
147+
# For unofficial builds, allow persistent directory
148+
if [[ ${COREOS_OFFICIAL:-0} -eq 1 ]]; then
149+
if [[ ${sig_key} != /tmp/* ]]; then
150+
die "Refusing to continue with modules key outside of /tmp for official builds, so that it stays in RAM only."
151+
fi
148152
fi
149153
if [ "$sig_key" != "${MODULES_SIGN_KEY}" ]; then
150154
die "MODULES_SIGN_KEY variable is different than MODULE_SIG_KEY in kernel config."
151155
fi
152156

153-
echo $sig_key
157+
echo "$sig_key"
154158
}
155159

156160
validate_sig_key() {
@@ -165,8 +169,14 @@ setup_keys() {
165169

166170
echo "Preparing keys at $sig_key"
167171

168-
mkdir -p $MODULE_SIGNING_KEY_DIR
169-
pushd $MODULE_SIGNING_KEY_DIR
172+
if [[ ${COREOS_OFFICIAL:-0} -eq 0 ]]; then
173+
# Allow portage sandbox to write to the module signing key directory,
174+
# which is in home for unofficial builds
175+
addwrite "${MODULE_SIGNING_KEY_DIR}"
176+
fi
177+
178+
mkdir -p "$MODULE_SIGNING_KEY_DIR"
179+
pushd "$MODULE_SIGNING_KEY_DIR"
170180

171181
mkdir -p gen_certs || die
172182
# based on the default config the kernel auto-generates

sdk_lib/Dockerfile.sdk-build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ RUN /home/sdk/sdk_entry.sh ./build_packages --board="amd64-usr" --only_resolve_c
1717

1818
RUN rm /mnt/host/source/.env
1919
RUN rm -rf /home/sdk/toolchain-pkgs
20+
21+
# Clean up ephemeral key directory variables that were added during build
22+
RUN sed -i -e '/export MODULE_SIGNING_KEY_DIR=/d' \
23+
-e '/export MODULES_SIGN_KEY=/d' \
24+
-e '/export MODULES_SIGN_CERT=/d' /home/sdk/.bashrc

sdk_lib/Dockerfile.sdk-import

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ RUN chmod 755 /home/sdk/sdk_entry.sh
5555
# it's likely that scripts and SDK tarball are out of sync
5656
RUN /home/sdk/sdk_entry.sh ./update_chroot --toolchain_boards="amd64-usr arm64-usr"
5757

58+
# Clean up ephemeral key directory variables that were added during build
59+
RUN sed -i -e '/export MODULE_SIGNING_KEY_DIR=/d' \
60+
-e '/export MODULES_SIGN_KEY=/d' \
61+
-e '/export MODULES_SIGN_CERT=/d' /home/sdk/.bashrc
62+
5863
ENTRYPOINT ["/home/sdk/sdk_entry.sh"]

sdk_lib/Dockerfile.sdk-update

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ RUN /home/sdk/sdk_entry.sh ./setup_board --board="amd64-usr" --regen_configs
1919
# Restore original .bashrc to remove sandbox disablement
2020
RUN mv /home/sdk/.bashrc.bak /home/sdk/.bashrc
2121
RUN chown sdk:sdk /home/sdk/.bashrc
22+
23+
# Clean up ephemeral key directory variables that were added during build
24+
RUN sed -i -e '/export MODULE_SIGNING_KEY_DIR=/d' \
25+
-e '/export MODULES_SIGN_KEY=/d' \
26+
-e '/export MODULES_SIGN_CERT=/d' /home/sdk/.bashrc

sdk_lib/sdk_entry.sh

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
# Source SDK environment variables if available (includes COREOS_OFFICIAL, etc.)
4+
if [ -f /mnt/host/source/.sdkenv ]; then
5+
source /mnt/host/source/.sdkenv
6+
fi
7+
38
if [ -n "${SDK_USER_ID:-}" ] ; then
49
# If the "core" user from /usr/share/baselayout/passwd has the same ID, allow to take it instead
510
usermod --non-unique -u $SDK_USER_ID sdk
@@ -52,16 +57,36 @@ sed -i -r '/^masters =/s/\bcoreos(\s|$)/coreos-overlay\1/g' /usr/local/portage/c
5257
# SDK container is launched using the su command below, which does not preserve environment
5358
# moreover, if multiple shells are attached to the same container,
5459
# we want all of them to share the same value of the variable, therefore we need to save it in .bashrc
55-
grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc || {
56-
MODULE_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
57-
if [[ ! "$MODULE_SIGNING_KEY_DIR" || ! -d "$MODULE_SIGNING_KEY_DIR" ]]; then
58-
echo "Failed to create temporary directory for secure boot keys."
60+
# Check if MODULE_SIGNING_KEY_DIR exists in .bashrc and if the directory actually exists
61+
if grep -q 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc; then
62+
# Extract the existing path
63+
EXISTING_DIR=$(source /home/sdk/.bashrc 2>/dev/null; echo "$MODULE_SIGNING_KEY_DIR")
64+
# If directory doesn't exist (stale from image build), remove the old entries and recreate
65+
if [[ ! -d ${EXISTING_DIR} ]]; then
66+
echo "Deleting stale module signing directory."
67+
sed -i -e '/export MODULE_SIGNING_KEY_DIR=/d' \
68+
-e '/export MODULES_SIGN_KEY=/d' \
69+
-e '/export MODULES_SIGN_CERT=/d' /home/sdk/.bashrc
70+
fi
71+
fi
72+
73+
# Create key directory if not already configured in .bashrc
74+
if ! grep -q 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc; then
75+
# For official builds, use ephemeral keys. For unofficial builds, use persistent directory
76+
if [[ ${COREOS_OFFICIAL:-0} -eq 1 ]]; then
77+
MODULE_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
78+
else
79+
MODULE_SIGNING_KEY_DIR="/home/sdk/.module-signing-keys"
80+
su sdk -c "mkdir -p ${MODULE_SIGNING_KEY_DIR@Q}"
81+
fi
82+
if [[ ! ${MODULE_SIGNING_KEY_DIR} || ! -d ${MODULE_SIGNING_KEY_DIR} ]]; then
83+
echo "Failed to create directory for module signing keys."
5984
else
6085
echo "export MODULE_SIGNING_KEY_DIR='$MODULE_SIGNING_KEY_DIR'" >> /home/sdk/.bashrc
6186
echo "export MODULES_SIGN_KEY='${MODULE_SIGNING_KEY_DIR}/certs/modules.pem'" >> /home/sdk/.bashrc
6287
echo "export MODULES_SIGN_CERT='${MODULE_SIGNING_KEY_DIR}/certs/modules.pub.pem'" >> /home/sdk/.bashrc
6388
fi
64-
}
89+
fi
6590

6691
# This is ugly.
6792
# We need to sudo su - sdk -c so the SDK user gets a fresh login.

0 commit comments

Comments
 (0)