Skip to content

Commit 935efe2

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 4ee3c33 commit 935efe2

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

run_sdk_container

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ if [[ -z ${stat} ]] ; then
151151
--network host
152152
-e SDK_USER_ID="$(id -u)"
153153
-e SDK_GROUP_ID="$(id -g)"
154+
-e MODULE_SIGNING_KEY_DIR="/tmp/$(uuidgen)"
154155
--name="${name}"
155156
--hostname="${hostname}"
156157
--entrypoint /bin/bash

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

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

139+
get_sig_key() {
140+
local sig_key="$(getconfig MODULE_SIG_KEY)"
141+
142+
if [ "$sig_key" == "${sig_key#/}" ]
143+
then
144+
echo "build/$sig_key"
145+
else
146+
echo $sig_key
147+
fi
148+
}
149+
139150
# Generate the module signing key for this build.
140151
setup_keys() {
141152
local sig_hash sig_key
142153
sig_hash=$(getconfig MODULE_SIG_HASH)
143-
sig_key="build/$(getconfig MODULE_SIG_KEY)"
154+
sig_key="$(get_sig_key)"
155+
156+
echo "Preparing keys at $sig_key"
144157

145158
if [[ "${sig_key}" == "build/certs/signing_key.pem" ]]; then
146159
die "MODULE_SIG_KEY is using the default value"
147160
fi
148161

149-
mkdir -p certs "${sig_key%/*}" || die
162+
if [ "$sig_key" == "${sig_key#/tmp/}" ]
163+
then
164+
die "Refusing to generate the key outside of /tmp, so that it stays in RAM only."
165+
fi
166+
if [ "$sig_key" != "${MODULES_SIGN_KEY}" ]
167+
then
168+
die "MODULES_SIGN_KEY variable is different than MODULE_SIG_KEY in kernel config."
169+
fi
150170

171+
mkdir -p $MODULE_SIGNING_KEY_DIR
172+
pushd $MODULE_SIGNING_KEY_DIR
173+
174+
mkdir -p gen_certs || die
151175
# based on the default config the kernel auto-generates
152-
cat >certs/modules.cnf <<-EOF
176+
cat >gen_certs/modules.cnf <<-EOF
153177
[ req ]
154178
default_bits = 4096
155179
distinguished_name = req_distinguished_name
@@ -169,19 +193,25 @@ setup_keys() {
169193
EOF
170194
openssl req -new -nodes -utf8 -days 36500 -batch -x509 \
171195
"-${sig_hash}" -outform PEM \
172-
-config certs/modules.cnf \
173-
-out certs/modules.pub.pem \
174-
-keyout certs/modules.key.pem \
196+
-config gen_certs/modules.cnf \
197+
-out gen_certs/modules.pub.pem \
198+
-keyout gen_certs/modules.key.pem \
175199
|| die "Generating module signing key failed"
176-
cat certs/modules.pub.pem certs/modules.key.pem > "${sig_key}"
200+
201+
# copy the cert/key to desired location
202+
mkdir -p "${MODULES_SIGN_CERT%/*}" "${MODULES_SIGN_KEY%/*}" || die
203+
cat gen_certs/modules.pub.pem gen_certs/modules.key.pem > "$MODULES_SIGN_KEY" || die
204+
cp gen_certs/modules.pub.pem $MODULES_SIGN_CERT || die
205+
206+
shred -u gen_certs/* || die
207+
rmdir gen_certs || die
208+
209+
popd
177210
}
178211

179212
# Discard the module signing key but keep public certificate.
180213
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
214+
shred -u "${MODULES_SIGN_KEY}" || die
185215
}
186216

187217
# 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.76.ebuild

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,21 @@ 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+
6465
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
66+
67+
if [ "$sig_key" == "${sig_key#/tmp/}" ]
68+
then
69+
die "Refusing to use module key stored outside of /tmp."
70+
fi
71+
72+
# keeping the old logic here for now, unreacheble due to the previous condition
73+
if [ "$sig_key" == "${sig_key#/}" ]
74+
then
75+
# sig_key is a relative path
76+
mkdir -p "build/${sig_key%/*}" || die
77+
cp -v "${KV_OUT_DIR}/${sig_key}" "build/${sig_key}" || die
78+
fi
6779

6880
# Symlink to bootengine.cpio so we can stick with relative paths in .config
6981
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.76.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
find "build/" -type d -empty -delete || die
5857
rm "build/.config.old" || die

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
@@ -459,7 +459,7 @@ CONFIG_MMC_SDHCI_PCI=m
459459
CONFIG_MODULES=y
460460
CONFIG_MODULE_COMPRESS_XZ=y
461461
CONFIG_MODULE_SIG=y
462-
CONFIG_MODULE_SIG_KEY="certs/modules.pem"
462+
CONFIG_MODULE_SIG_KEY="${MODULE_SIGNING_KEY_DIR}/certs/modules.pem"
463463
CONFIG_MODULE_SIG_SHA256=y
464464
CONFIG_MODULE_UNLOAD=y
465465
CONFIG_MOUSE_PS2=m

sdk_lib/sdk_entry.sh

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

52+
# SDK container is launched in another shell, so we need to smuggle the variables inside
53+
grep -q 'export MODULE_SIGNING_KEY_DIR' /home/sdk/.bashrc || {
54+
echo "export MODULE_SIGNING_KEY_DIR='$MODULE_SIGNING_KEY_DIR'" >> /home/sdk/.bashrc
55+
echo "export MODULES_SIGN_KEY='${MODULE_SIGNING_KEY_DIR}/certs/modules.pem'" >> /home/sdk/.bashrc
56+
echo "export MODULES_SIGN_CERT='${MODULE_SIGNING_KEY_DIR}/certs/modules.pub.pem'" >> /home/sdk/.bashrc
57+
}
58+
5259
# This is ugly.
5360
# We need to sudo su - sdk -c so the SDK user gets a fresh login.
5461
# 'sdk' is member of multiple groups, and plain docker USER only

0 commit comments

Comments
 (0)