Skip to content

Commit 729d83c

Browse files
committed
eclass/coreos-kernel,sys-kernel/coreos-modules:
Move module signing key to /tmp, so that it stays in RAM. Disable shredding signing key after coreos-modules finishes, but rather shred it after coreos-kernel finishes, so that out of tree modules (like ZFS from upstream portage) can also use the key before it is shreded.
1 parent a924cb8 commit 729d83c

File tree

6 files changed

+63
-23
lines changed

6 files changed

+63
-23
lines changed

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,41 @@ getconfig() {
136136
echo "${value}"
137137
}
138138

139+
get_sig_key() {
140+
local sig_key="$(getconfig MODULE_SIG_KEY)"
141+
142+
if [[ "${sig_key}" == "build/certs/signing_key.pem" ]]; then
143+
die "MODULE_SIG_KEY is using the default value"
144+
fi
145+
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."
148+
fi
149+
if [ "$sig_key" != "${MODULES_SIGN_KEY}" ]; then
150+
die "MODULES_SIGN_KEY variable is different than MODULE_SIG_KEY in kernel config."
151+
fi
152+
153+
echo $sig_key
154+
}
155+
156+
validate_sig_key() {
157+
get_sig_key > /dev/null
158+
}
159+
139160
# Generate the module signing key for this build.
140161
setup_keys() {
141162
local sig_hash sig_key
142163
sig_hash=$(getconfig MODULE_SIG_HASH)
143-
sig_key="build/$(getconfig MODULE_SIG_KEY)"
164+
sig_key="$(get_sig_key)"
144165

145-
if [[ "${sig_key}" == "build/certs/signing_key.pem" ]]; then
146-
die "MODULE_SIG_KEY is using the default value"
147-
fi
166+
echo "Preparing keys at $sig_key"
148167

149-
mkdir -p certs "${sig_key%/*}" || die
168+
mkdir -p $MODULE_SIGNING_KEY_DIR
169+
pushd $MODULE_SIGNING_KEY_DIR
150170

171+
mkdir -p gen_certs || die
151172
# based on the default config the kernel auto-generates
152-
cat >certs/modules.cnf <<-EOF
173+
cat >gen_certs/modules.cnf <<-EOF
153174
[ req ]
154175
default_bits = 4096
155176
distinguished_name = req_distinguished_name
@@ -169,19 +190,20 @@ setup_keys() {
169190
EOF
170191
openssl req -new -nodes -utf8 -days 36500 -batch -x509 \
171192
"-${sig_hash}" -outform PEM \
172-
-config certs/modules.cnf \
173-
-out certs/modules.pub.pem \
174-
-keyout certs/modules.key.pem \
193+
-config gen_certs/modules.cnf \
194+
-out gen_certs/modules.pub.pem \
195+
-keyout gen_certs/modules.key.pem \
175196
|| die "Generating module signing key failed"
176-
cat certs/modules.pub.pem certs/modules.key.pem > "${sig_key}"
177-
}
178197

179-
# Discard the module signing key but keep public certificate.
180-
shred_keys() {
181-
local sig_key
182-
sig_key="build/$(getconfig MODULE_SIG_KEY)"
183-
shred -u certs/modules.key.pem "${sig_key}" || die
184-
cp certs/modules.pub.pem "${sig_key}" || die
198+
# copy the cert/key to desired location
199+
mkdir -p "${MODULES_SIGN_CERT%/*}" "${MODULES_SIGN_KEY%/*}" || die
200+
cat gen_certs/modules.pub.pem gen_certs/modules.key.pem > "$MODULES_SIGN_KEY" || die
201+
cp gen_certs/modules.pub.pem $MODULES_SIGN_CERT || die
202+
203+
shred -u gen_certs/* || die
204+
rmdir gen_certs || die
205+
206+
popd
185207
}
186208

187209
# Populate /lib/modules/$(uname -r)/{build,source}

sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,10 @@ CGO_ENABLED=1
124124

125125
# Keep using old binary format for now.
126126
BINPKG_FORMAT=xpak
127+
128+
# move signing key and cert to /tmp so that the ephemeral key is not stored on a disk
129+
MODULES_SIGN_KEY="/tmp/certs/modules.pem"
130+
MODULES_SIGN_CERT="/tmp/certs/modules.pub.pem"
131+
132+
# enable signing kernel modules from portage
133+
USE="${USE} modules-sign"

sdk_container/src/third_party/coreos-overlay/sys-kernel/coreos-kernel/coreos-kernel-6.6.87.ebuild

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ src_prepare() {
5858
# Pull in the config and public module signing key
5959
KV_OUT_DIR="${ESYSROOT}/lib/modules/${COREOS_SOURCE_NAME#linux-}/build"
6060
cp -v "${KV_OUT_DIR}/.config" build/ || die
61-
local sig_key="$(getconfig MODULE_SIG_KEY)"
62-
mkdir -p "build/${sig_key%/*}" || die
63-
cp -v "${KV_OUT_DIR}/${sig_key}" "build/${sig_key}" || die
61+
validate_sig_key
6462

6563
config_update 'CONFIG_INITRAMFS_SOURCE="bootengine.cpio"'
6664

sdk_container/src/third_party/coreos-overlay/sys-kernel/coreos-modules/coreos-modules-6.6.87.ebuild

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ src_prepare() {
1616
local archconfig="$(find_archconfig)"
1717
local commonconfig="$(find_commonconfig)"
1818
elog "Building using config ${archconfig} and ${commonconfig}"
19-
cat "${archconfig}" "${commonconfig}" >> build/.config || die
19+
cat "${archconfig}" "${commonconfig}" | envsubst '$MODULE_SIGNING_KEY_DIR' >> build/.config || die
2020
fi
2121
cpio -ov </dev/null >build/bootengine.cpio
2222

@@ -52,7 +52,6 @@ src_install() {
5252
rm "${D}/usr/lib/debug/usr/lib/modules/${KV_FULL}/build" || die
5353

5454
# Clean up the build tree
55-
shred_keys
5655
kmake clean
5756

5857
# TODO: ensure that fixdep and kbuild tools shipped inside the image

sdk_container/src/third_party/coreos-overlay/sys-kernel/coreos-modules/files/commonconfig-6.6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ CONFIG_MMC_SDHCI_PCI=m
498498
CONFIG_MODULES=y
499499
CONFIG_MODULE_COMPRESS_XZ=y
500500
CONFIG_MODULE_SIG=y
501-
CONFIG_MODULE_SIG_KEY="certs/modules.pem"
501+
CONFIG_MODULE_SIG_KEY="${MODULE_SIGNING_KEY_DIR}/certs/modules.pem"
502502
CONFIG_MODULE_SIG_SHA256=y
503503
CONFIG_MODULE_UNLOAD=y
504504
CONFIG_MOUSE_PS2=m

sdk_lib/sdk_entry.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ sed -i -r '/^masters =/s/\bcoreos(\s|$)/coreos-overlay\1/g' /usr/local/portage/c
4949
fi
5050
)
5151

52+
# SDK container is launched using the su command below, which does not preserve environment
53+
# moreover, if multiple shells are attached to the same container,
54+
# 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."
59+
else
60+
echo "export MODULE_SIGNING_KEY_DIR='$MODULE_SIGNING_KEY_DIR'" >> /home/sdk/.bashrc
61+
echo "export MODULES_SIGN_KEY='${MODULE_SIGNING_KEY_DIR}/certs/modules.pem'" >> /home/sdk/.bashrc
62+
echo "export MODULES_SIGN_CERT='${MODULE_SIGNING_KEY_DIR}/certs/modules.pub.pem'" >> /home/sdk/.bashrc
63+
fi
64+
}
65+
5266
# This is ugly.
5367
# We need to sudo su - sdk -c so the SDK user gets a fresh login.
5468
# 'sdk' is member of multiple groups, and plain docker USER only

0 commit comments

Comments
 (0)