Skip to content

Commit f5f7fb2

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 c1649c9 commit f5f7fb2

File tree

6 files changed

+65
-23
lines changed

6 files changed

+65
-23
lines changed

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,43 @@ 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/* ]]
147+
then
148+
die "Refusing to to continue with modules key outside of /tmp, so that it stays in RAM only."
149+
fi
150+
if [ "$sig_key" != "${MODULES_SIGN_KEY}" ]
151+
then
152+
die "MODULES_SIGN_KEY variable is different than MODULE_SIG_KEY in kernel config."
153+
fi
154+
155+
echo $sig_key
156+
}
157+
158+
validate_sig_key() {
159+
get_sig_key > /dev/null
160+
}
161+
139162
# Generate the module signing key for this build.
140163
setup_keys() {
141164
local sig_hash sig_key
142165
sig_hash=$(getconfig MODULE_SIG_HASH)
143-
sig_key="build/$(getconfig MODULE_SIG_KEY)"
166+
sig_key="$(get_sig_key)"
144167

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

149-
mkdir -p certs "${sig_key%/*}" || die
170+
mkdir -p $MODULE_SIGNING_KEY_DIR
171+
pushd $MODULE_SIGNING_KEY_DIR
150172

173+
mkdir -p gen_certs || die
151174
# based on the default config the kernel auto-generates
152-
cat >certs/modules.cnf <<-EOF
175+
cat >gen_certs/modules.cnf <<-EOF
153176
[ req ]
154177
default_bits = 4096
155178
distinguished_name = req_distinguished_name
@@ -169,19 +192,20 @@ setup_keys() {
169192
EOF
170193
openssl req -new -nodes -utf8 -days 36500 -batch -x509 \
171194
"-${sig_hash}" -outform PEM \
172-
-config certs/modules.cnf \
173-
-out certs/modules.pub.pem \
174-
-keyout certs/modules.key.pem \
195+
-config gen_certs/modules.cnf \
196+
-out gen_certs/modules.pub.pem \
197+
-keyout gen_certs/modules.key.pem \
175198
|| die "Generating module signing key failed"
176-
cat certs/modules.pub.pem certs/modules.key.pem > "${sig_key}"
177-
}
178199

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
200+
# copy the cert/key to desired location
201+
mkdir -p "${MODULES_SIGN_CERT%/*}" "${MODULES_SIGN_KEY%/*}" || die
202+
cat gen_certs/modules.pub.pem gen_certs/modules.key.pem > "$MODULES_SIGN_KEY" || die
203+
cp gen_certs/modules.pub.pem $MODULES_SIGN_CERT || die
204+
205+
shred -u gen_certs/* || die
206+
rmdir gen_certs || die
207+
208+
popd
185209
}
186210

187211
# 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.84.ebuild

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ src_prepare() {
6161
# Pull in the config and public module signing key
6262
KV_OUT_DIR="${SYSROOT%/}/lib/modules/${COREOS_SOURCE_NAME#linux-}/build"
6363
cp -v "${KV_OUT_DIR}/.config" build/ || die
64-
local sig_key="$(getconfig MODULE_SIG_KEY)"
65-
mkdir -p "build/${sig_key%/*}" || die
66-
cp -v "${KV_OUT_DIR}/${sig_key}" "build/${sig_key}" || die
64+
validate_sig_key
6765

6866
# Symlink to bootengine.cpio so we can stick with relative paths in .config
6967
ln -sv "${SYSROOT%/}"/usr/share/bootengine/bootengine.cpio build/ || die

sdk_container/src/third_party/coreos-overlay/sys-kernel/coreos-modules/coreos-modules-6.6.84.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
@@ -462,7 +462,7 @@ CONFIG_MMC_SDHCI_PCI=m
462462
CONFIG_MODULES=y
463463
CONFIG_MODULE_COMPRESS_XZ=y
464464
CONFIG_MODULE_SIG=y
465-
CONFIG_MODULE_SIG_KEY="certs/modules.pem"
465+
CONFIG_MODULE_SIG_KEY="${MODULE_SIGNING_KEY_DIR}/certs/modules.pem"
466466
CONFIG_MODULE_SIG_SHA256=y
467467
CONFIG_MODULE_UNLOAD=y
468468
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)