Skip to content

eBPF-based security platform

genkey: keep the old pair when --force cannot replace it

Daniel Borkmann35349736a755
diff --git a/builtin-genkey.c b/builtin-genkey.c
index 565f3d73..07effb85 100644
--- a/builtin-genkey.c
+++ b/builtin-genkey.c
@@ -313,6 +313,50 @@ out:
return rc;
}
+/*
+ * The two renames cannot be one, so the old key stays linked aside until
+ * the new certificate is in place: a rename that fails between them puts
+ * the old key back rather than leave the new key beside a certificate it
+ * does not match.
+ */
+static int replace_pair(const char *pkey_new, const char *pkey_path,
+ const char *cert_new, const char *cert_path,
+ const char *pkey_old)
+{
+ bool had_old = true;
+ int err;
+
+ if (link(pkey_path, pkey_old) != 0) {
+ if (errno != ENOENT) {
+ err = errno;
+ pr_err("genkey: cannot keep the old key aside as %s: %s",
+ pkey_old, strerror(err));
+ return -err;
+ }
+ had_old = false;
+ }
+ if (rename(pkey_new, pkey_path) != 0) {
+ err = errno;
+ pr_err("genkey: cannot move %s to %s: %s", pkey_new, pkey_path,
+ strerror(err));
+ if (had_old)
+ unlink(pkey_old);
+ return -err;
+ }
+ if (rename(cert_new, cert_path) != 0) {
+ err = errno;
+ pr_err("genkey: cannot move %s to %s: %s", cert_new, cert_path,
+ strerror(err));
+ if (had_old ? rename(pkey_old, pkey_path) : unlink(pkey_path))
+ pr_err("genkey: %s and %s are no longer a pair (%s); remove both and run genkey again",
+ pkey_path, cert_path, strerror(errno));
+ return -err;
+ }
+ if (had_old)
+ unlink(pkey_old);
+ return 0;
+}
+
static const struct shirudo_help_item genkey_options[] = {
{ "--force, -f", "overwrite an existing key/cert (refused otherwise)" },
{ "--keys-dir <dir>", "write signing_key.pem + cert.der under <dir> "
@@ -419,25 +463,30 @@ int cmd_genkey(int argc, const char **argv)
if (force) {
char pkey_new[PATH_MAX], cert_new[PATH_MAX];
+ char pkey_old[PATH_MAX];
/* The old pair goes only once the new one is on disk. */
if (strsprintf(pkey_new, sizeof(pkey_new), "%s.new",
pkey_path) < 0 ||
strsprintf(cert_new, sizeof(cert_new), "%s.new",
- cert_path) < 0) {
+ cert_path) < 0 ||
+ strsprintf(pkey_old, sizeof(pkey_old), "%s.old",
+ pkey_path) < 0) {
pr_err("genkey: no room for a temporary beside %s",
pkey_path);
goto out;
}
unlink(pkey_new);
unlink(cert_new);
+ unlink(pkey_old);
if (write_private_pem(pkey, pkey_new) != 0)
goto out;
- if (write_cert_der(cert, cert_new) != 0 ||
- rename(pkey_new, pkey_path) != 0 ||
- rename(cert_new, cert_path) != 0) {
- pr_err("genkey: cannot put the new pair in place: %s",
- strerror(errno));
+ if (write_cert_der(cert, cert_new) != 0) {
+ unlink(pkey_new);
+ goto out;
+ }
+ if (replace_pair(pkey_new, pkey_path, cert_new, cert_path,
+ pkey_old) != 0) {
unlink(pkey_new);
unlink(cert_new);
goto out;
diff --git a/testing/t0027-genkey.sh b/testing/t0027-genkey.sh
index b09bfdef..70fce4dc 100755
--- a/testing/t0027-genkey.sh
+++ b/testing/t0027-genkey.sh
@@ -7,6 +7,7 @@
# subject/authority key identifiers, and there is no algorithm option
# at all. The ML-DSA cases need an OpenSSL (>= 3.5) that knows the
# algorithm; on older toolchains genkey must refuse with an upgrade hint.
+# A --force that cannot put the new pair in place leaves the old one.
cd "$(dirname "$0")"
. ./test-lib.sh
@@ -72,6 +73,21 @@ if [ "$have_mldsa" = 1 ]; then
! "$SHIRUDO" genkey --keys-dir "$tmp/keys" >/dev/null 2>&1 &&
"$SHIRUDO" genkey --keys-dir "$tmp/keys" --force >/dev/null 2>&1
'
+
+ test_expect_success 'a --force that cannot finish keeps the old pair' '
+ cp "$tmp/keys/signing_key.pem" "$tmp/old.pem" &&
+ mv "$tmp/keys/cert.der" "$tmp/old.der" &&
+ mkdir "$tmp/keys/cert.der" &&
+ ! "$SHIRUDO" genkey --keys-dir "$tmp/keys" --force >"$tmp/force.out" 2>&1 &&
+ cmp "$tmp/old.pem" "$tmp/keys/signing_key.pem" &&
+ ! test -e "$tmp/keys/signing_key.pem.old" &&
+ ! test -e "$tmp/keys/signing_key.pem.new" &&
+ ! test -e "$tmp/keys/cert.der.new" &&
+ rmdir "$tmp/keys/cert.der" &&
+ mv "$tmp/old.der" "$tmp/keys/cert.der" &&
+ "$SHIRUDO" keys --keys-dir "$tmp/keys" >"$tmp/keys.out" 2>&1 &&
+ grep -q "signing key matches cert" "$tmp/keys.out"
+ '
else
test_expect_success 'genkey refuses with an upgrade hint on an OpenSSL without ML-DSA' '
! "$SHIRUDO" genkey --keys-dir "$tmp/keys" >"$tmp/genkey.out" 2>&1 &&