Skip to content

Conversation

@danzatt
Copy link
Contributor

@danzatt danzatt commented Nov 14, 2025

Summary

This PR fixes kernel module signing to work correctly with ephemeral keys for official builds while maintaining persistent keys for development builds. It also resolves portage sandbox violations that occurred when building kernel modules.

Problem

After switching to ephemeral keys (in /tmp) for official builds, two issues emerged:

  1. Overly strict validation: The get_sig_key() function was enforcing /tmp/* paths for ALL builds, but this check should only apply to official builds.

  2. Sandbox violations: For development builds using persistent keys in /home/sdk/.module-signing-keys, portage's sandbox was blocking write access during the setup_keys() function, causing build failures.

Solution

Commit 1: Fix ephemeral key directory paths baked into container images

  • Made the /tmp enforcement conditional on COREOS_OFFICIAL=1
  • Development builds can now use persistent directories outside /tmp
  • Added runtime detection of stale temp directories in sdk_entry.sh
  • Cleaned up ephemeral key variables from SDK container images during build

Commit 2: Add portage sandbox exception for development builds

  • Added addwrite "${MODULE_SIGNING_KEY_DIR}" in setup_keys() for unofficial builds
  • This allows portage to write to /home/sdk/.module-signing-keys during development
  • Official builds using /tmp don't need this exception (already writable)

Testing

  • ✅ Official builds now use ephemeral keys in /tmp successfully
  • ✅ Development builds use persistent keys in /home/sdk/.module-signing-keys without sandbox violations
  • ✅ Module signing works for both in-tree and out-of-tree kernel modules

Files Changed

  • sdk_container/src/third_party/coreos-overlay/eclass/coreos-kernel.eclass: Added conditional /tmp check and sandbox exception
  • sdk_lib/sdk_entry.sh: Added stale directory detection and conditional key directory creation
  • sdk_lib/Dockerfile.sdk-build: Clean up ephemeral variables during build
  • sdk_lib/Dockerfile.sdk-import: Clean up ephemeral variables during build
  • sdk_lib/Dockerfile.sdk-update: Clean up ephemeral variables during build

Signed-off-by: Daniel Zatovic daniel.zatovic@gmail.com

@danzatt danzatt requested a review from a team as a code owner November 14, 2025 11:35
# Check if MODULE_SIGNING_KEY_DIR exists in .bashrc and if the directory actually exists
if grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
# Extract the existing path
EXISTING_DIR=$(grep 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc | sed "s/.*MODULE_SIGNING_KEY_DIR='\(.*\)'/\1/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably could use cut like:

Suggested change
EXISTING_DIR=$(grep 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc | sed "s/.*MODULE_SIGNING_KEY_DIR='\(.*\)'/\1/")
EXISTING_DIR=$(grep 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc | cut -f2- -d=)

# Extract the existing path
EXISTING_DIR=$(grep 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc | sed "s/.*MODULE_SIGNING_KEY_DIR='\(.*\)'/\1/")
# If directory doesn't exist (stale from image build), remove the old entries and recreate
if [[ ! -d "$EXISTING_DIR" ]]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to quote variables inside double brackets in bash:

Suggested change
if [[ ! -d "$EXISTING_DIR" ]]; then
if [[ ! -d ${EXISTING_DIR} ]]; then

Comment on lines 62 to 64
sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a single invocation to sed, like:

(Also I'd add = to the keys to be sure we match the exact key, not like MODULE_SIGNING_KEY_DIR_HAHA_MY_OWN).

Suggested change
sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
sed -i -e '/export MODULE_SIGNING_KEY_DIR=/d' \
-e '/export MODULES_SIGN_KEY=/d' \
-e '/export MODULES_SIGN_CERT=/d' /home/sdk/.bashrc

Comment on lines 22 to 24
RUN sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a single sed invocation, and I'd add = to keys, see other comment below for details.

Comment on lines 59 to 61
RUN sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a single sed invocation, and I'd add = to keys, see other comment below for details.

Comment on lines 24 to 26
RUN sed -i '/export MODULE_SIGNING_KEY_DIR/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_KEY/d' /home/sdk/.bashrc && \
sed -i '/export MODULES_SIGN_CERT/d' /home/sdk/.bashrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a single sed invocation, and I'd add = to keys, see other comment below for details.

grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc || {
MODULE_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
# Check if MODULE_SIGNING_KEY_DIR exists in .bashrc and if the directory actually exists
if grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
if grep -q 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc; then

fi

# Create key directory if not already configured in .bashrc
if ! grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ! grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc; then
if ! grep -q 'export MODULE_SIGNING_KEY_DIR=' /home/sdk/.bashrc; then

MODULE_SIGNING_KEY_DIR=$(su sdk -c "mktemp -d")
else
MODULE_SIGNING_KEY_DIR="/home/sdk/.module-signing-keys"
su sdk -c "mkdir -p '$MODULE_SIGNING_KEY_DIR'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use quoting feature of bash (search for ${parameter@operator} in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion):

Suggested change
su sdk -c "mkdir -p '$MODULE_SIGNING_KEY_DIR'"
su sdk -c "mkdir -p ${MODULE_SIGNING_KEY_DIR@Q}"

@danzatt
Copy link
Contributor Author

danzatt commented Nov 17, 2025

Thanks. Applied all review suggestions from @krnowak (consolidated sed calls, added = to patterns, fixed indentation, quoted variables, improved variable extraction method).

Also I've discovered a critical bug: COREOS_OFFICIAL wasn't available during sdk_entry.sh execution, causing official builds to incorrectly use persistent keys instead of ephemeral /tmp keys. Fixed by sourcing /mnt/host/source/.sdkenv at the beginning of sdk_entry.sh.

Test:

# Official build - should use /tmp
COREOS_OFFICIAL=1 ./run_sdk_container -t --rm
# In container: grep MODULE_SIGNING_KEY_DIR ~/.bashrc
# echo $MODULE_SIGNING_KEY_DIR
# Expected: /tmp/tmp.XXXXXXXX

# Dev build - should use persistent dir
./run_sdk_container -t --rm
# Expected: /home/sdk/.module-signing-keys

@danzatt
Copy link
Contributor Author

danzatt commented Nov 17, 2025

Also, I am not quite sure how to properly test the COREOS_OFFICIAL. In official builds, it seems to be passed also in the sdk_container/.repo/manifests/version.txt Should I also include that?

@danzatt
Copy link
Contributor Author

danzatt commented Nov 19, 2025

CI is failing due to missing bincache artifacts (due to bincache migration). However I've rebased my #3162 onto this branch and the tests passed there yesterday, so I think we're good. If there are no objections, I'll merge.

@chewi
Copy link
Contributor

chewi commented Nov 19, 2025

Also, I am not quite sure how to properly test the COREOS_OFFICIAL. In official builds, it seems to be passed also in the sdk_container/.repo/manifests/version.txt Should I also include that?

I don't think it's passed in that file but determined by the format of FLATCAR_VERSION via the is_official function. Whenever I've needed to test a "pretend" official build, I've just temporarily adjusted the places where COREOS_OFFICIAL is checked.

@tormath1
Copy link
Contributor

CI is failing due to missing bincache artifacts (due to bincache migration). However I've rebased my #3162 onto this branch and the tests passed there yesterday, so I think we're good. If there are no objections, I'll merge.

FWIW, one can still access the old bincache artifacts while the instance is still up: http://145.40.90.147/

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>
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>
…erns in Dockerfile.sdk-update

Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com>
Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com>
Fix bug where COREOS_OFFICIAL wasn't available during sdk_entry.sh
execution, causing official builds to incorrectly use persistent
module signing keys instead of ephemeral /tmp keys.

Signed-off-by: Daniel Zatovic <daniel.zatovic@gmail.com>
@danzatt danzatt force-pushed the danzatt/ephemeral-keys-only-official branch from 71b3de3 to 5de9580 Compare November 19, 2025 13:26
@danzatt
Copy link
Contributor Author

danzatt commented Nov 20, 2025

Jenkins job http://jenkins.infra.kinvolk.io:8080/job/container/job/packages_all_arches/7003 just succeeded. I'll merge, thanks for the review.

@danzatt danzatt merged commit f05097d into flatcar:main Nov 20, 2025
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants