Skip to content

Commit 27638a2

Browse files
committed
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>
1 parent 72a74fd commit 27638a2

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ 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."

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 '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
23+
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
24+
sed -i '/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 '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
60+
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
61+
sed -i '/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 '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
25+
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
26+
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc

sdk_lib/sdk_entry.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,30 @@ sed -i -r '/^masters =/s/\bcoreos(\s|$)/coreos-overlay\1/g' /usr/local/portage/c
5252
# SDK container is launched using the su command below, which does not preserve environment
5353
# moreover, if multiple shells are attached to the same container,
5454
# 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 || {
55+
# Check if MODULE_SIGNING_KEY_DIR exists in .bashrc and if the directory actually exists
56+
if grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
57+
# Extract the existing path
58+
EXISTING_DIR=$(grep 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc | sed "s/.*MODULE_SIGNING_KEY_DIR='\(.*\)'/\1/")
59+
# If directory doesn't exist (stale from image build), remove the old entries and recreate
60+
if [[ ! -d "$EXISTING_DIR" ]]; then
61+
echo "Deleting stale module signing directory."
62+
sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc
63+
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc
64+
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
65+
fi
66+
fi
67+
68+
# Create key directory if not already configured in .bashrc
69+
if ! grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
5670
MODULE_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
5771
if [[ ! "$MODULE_SIGNING_KEY_DIR" || ! -d "$MODULE_SIGNING_KEY_DIR" ]]; then
58-
echo "Failed to create temporary directory for secure boot keys."
72+
echo "Failed to create directory for module signing keys."
5973
else
6074
echo "export MODULE_SIGNING_KEY_DIR='$MODULE_SIGNING_KEY_DIR'" >> /home/sdk/.bashrc
6175
echo "export MODULES_SIGN_KEY='${MODULE_SIGNING_KEY_DIR}/certs/modules.pem'" >> /home/sdk/.bashrc
6276
echo "export MODULES_SIGN_CERT='${MODULE_SIGNING_KEY_DIR}/certs/modules.pub.pem'" >> /home/sdk/.bashrc
6377
fi
64-
}
78+
fi
6579

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

0 commit comments

Comments
 (0)