Skip to content

eBPF-based security platform

nv-writer: unlink writer rings on initialization failure

Leonard Cohnen94b3cd3581b1
diff --git a/Documentation/tpm.rst b/Documentation/tpm.rst
index 9d5f8708..e0e596df 100644
--- a/Documentation/tpm.rst
+++ b/Documentation/tpm.rst
@@ -93,6 +93,8 @@ the signer in its lasting keyring with owner view/search permissions,
then restricts and seals the ring. Bootstrap is trusted: production
runs in the measured initrd before untrusted workloads. The public key,
private-key ID and keyring ID are frozen in the identity map before attachment.
+Failed writer creation or enforcement setup removes the writer's links
+from the user and session keyrings.
There is no additional state key or mutable binding-attempt marker.
The keyring is already available to later invocations;
``lsm/key_permission`` must be attached and pinned before initialization
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 7da892d4..3f24e703 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -627,6 +627,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-exec-churn \
testing/upatch/kernel-obs/exec/exec-paths
TEST_HELPERS += testing/helper/test-nv-writer
+TEST_HELPERS += testing/helper/test-nv-writer-cleanup
TEST_HELPERS += testing/helper/test-tpm-preopened
testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o \
@@ -667,6 +668,22 @@ testing/helper/test-nv-writer: testing/helper/test-nv-writer.c $(REGISTER_OBJS)
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
+testing/fixtures/nv-writer-fault.bpf.o: testing/fixtures/nv-writer-fault.bpf.c \
+ testing/fixtures/nv-writer-fault.h $(VMLINUX_H) $(BPF_LIBBPF_HEADERS)
+ @printf ' CLANG $@\n'
+ $(Q)$(CLANG_BPF) -I$(dir $(VMLINUX_H)) -I$(LIBBPF_VENDOR) -c $< -o $@
+
+testing/helper/test-nv-writer-cleanup: testing/helper/test-nv-writer-cleanup.c \
+ testing/fixtures/nv-writer-fault.h builtin-init.c \
+ $(filter-out $(call obj,builtin-init.o),$(BUILTIN_OBJS)) \
+ $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A) \
+ testing/fixtures/nv-writer-fault.bpf.o
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< \
+ $(filter-out testing/fixtures/nv-writer-fault.bpf.o,$(filter %.o %.a,$^)) \
+ -Wl,--wrap=shirudo_image_runtime_root \
+ $(STATIC_LIBS) $(DYNAMIC_LIBS)
+
testing/helper/test-tpm-preopened: testing/helper/test-tpm-preopened.c \
$(REGISTER_OBJS)
@printf ' CC $@\n'
diff --git a/builtin-init.c b/builtin-init.c
index 8ab6804a..0e10678f 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -856,6 +856,8 @@ out:
bpf_link__destroy(link_setx);
bpf_link__destroy(link_unlink);
bpf_object__close(obj);
+ if (err)
+ nv_writer_destroy(id.nv_writer_ring);
return err;
}
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.sh
index 1f15aefc..2a6b0b3e 100755
--- a/contrib/build-test-vm-bundle.sh
+++ b/contrib/build-test-vm-bundle.sh
@@ -316,7 +316,10 @@ make -C "$REPO_ROOT" -j"$(nproc)" -f Makefile.shirudo \
PROFILE=target CONFIG_MK=config-target.mk verity-test-helpers
install -d "$TMP_DIR/test-helpers/testing/helper" \
+ "$TMP_DIR/test-helpers/testing/fixtures" \
"$TMP_DIR/test-helpers/testing/upatch/kernel-obs/exec"
+install -m 0644 "$REPO_ROOT/testing/fixtures/nv-writer-fault.bpf.o" \
+ "$TMP_DIR/test-helpers/testing/fixtures/"
while IFS= read -r helper; do
install -m 0755 "$REPO_ROOT/$helper" "$TMP_DIR/test-helpers/$helper"
done <<'EOF'
@@ -330,6 +333,7 @@ testing/helper/tpmsim-cmd
testing/helper/test-tpm-wire
testing/helper/test-nv-cphash
testing/helper/test-nv-writer
+testing/helper/test-nv-writer-cleanup
testing/helper/test-tpm-preopened
testing/helper/test-config
testing/helper/test-bpf-load
diff --git a/contrib/test-vm-guest.sh b/contrib/test-vm-guest.sh
index 699e4449..83cc4ef2 100755
--- a/contrib/test-vm-guest.sh
+++ b/contrib/test-vm-guest.sh
@@ -352,6 +352,10 @@ if [[ "${TEST_VM_SUITE:-all}" == register ]]; then
: >/mnt/shirudo-out/failed-suites
(
cd testing
+ SHIRUDO_TEST_REGISTER_VM=1 \
+ SHIRUDO_TEST_NV_CERT="$BUNDLE/fixtures/cert.der" \
+ SHIRUDO_TEST_FAILURES_FILE=/mnt/shirudo-out/failed-tests \
+ sh tpm/t1128-nv-writer-cleanup.sh
SHIRUDO_TEST_REGISTER_VM=1 \
SHIRUDO_TEST_FAILURES_FILE=/mnt/shirudo-out/failed-tests \
sh tpm/t0034-register.sh
diff --git a/testing/Makefile b/testing/Makefile
index a67c7290..7d4dbf92 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -126,6 +126,7 @@ TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \
$(ROOT)/testing/upatch/kernel-obs/exec/*.c $(ROOT)/completion/* $(ROOT)/systemd/*.in \
$(ROOT)/systemd/*.timer $(ROOT)/testing/fixtures/*.c $(ROOT)/testing/fixtures/*.lua \
$(ROOT)/testing/fixtures/*.conf $(ROOT)/testing/fixtures/*.sh \
+ $(ROOT)/testing/fixtures/*.h \
$(ROOT)/testing/fixtures/decoders/* \
$(ROOT)/testing/signing/*.sh \
$(ROOT)/upatches/kernel-obs/*.c $(ROOT)/upatches/kernel-obs/*.h $(ROOT)/upatches/kernel-obs/*.list \
diff --git a/testing/fixtures/nv-writer-fault.bpf.c b/testing/fixtures/nv-writer-fault.bpf.c
new file mode 100644
index 00000000..44d98f80
--- /dev/null
+++ b/testing/fixtures/nv-writer-fault.bpf.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+#include "nv-writer-fault.h"
+
+#define EIO 5
+#define EBADMSG 74
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, struct nv_writer_fault);
+} fault SEC(".maps");
+
+static __always_inline struct nv_writer_fault *current_fault(void)
+{
+ struct nv_writer_fault *state;
+ __u32 zero = 0;
+
+ state = bpf_map_lookup_elem(&fault, &zero);
+ if (!state || state->pid != bpf_get_current_pid_tgid() >> 32)
+ return NULL;
+ return state;
+}
+
+SEC("lsm/key_alloc")
+int BPF_PROG(fail_writer_import, struct key *key, const struct cred *cred,
+ unsigned long flags, int ret)
+{
+ char description[sizeof("shirudo.nv-writer") + 1] = {};
+ struct nv_writer_fault *state;
+
+ if (ret)
+ return ret;
+ state = current_fault();
+ if (!state)
+ return 0;
+ if (bpf_probe_read_kernel_str(description, sizeof(description),
+ BPF_CORE_READ(key, description)) !=
+ sizeof("shirudo.nv-writer") ||
+ __builtin_memcmp(description, "shirudo.nv-writer", sizeof("shirudo.nv-writer")))
+ return 0;
+ state->imports++;
+ if (!state->fail_import)
+ return 0;
+ state->failures++;
+ return -EBADMSG;
+}
+
+SEC("lsm/bpf")
+int BPF_PROG(fail_writer_map, int cmd, union bpf_attr *attr,
+ unsigned int size, bool kernel, int ret)
+{
+ struct nv_writer_fault *state;
+
+ if (ret)
+ return ret;
+ state = current_fault();
+ if (!state || !state->imports || kernel || !state->command ||
+ state->command != (__u32)cmd)
+ return 0;
+ state->failures++;
+ return -EIO;
+}
+
+char _license[] SEC("license") = "Dual BSD/GPL";
diff --git a/testing/fixtures/nv-writer-fault.h b/testing/fixtures/nv-writer-fault.h
new file mode 100644
index 00000000..e81648f8
--- /dev/null
+++ b/testing/fixtures/nv-writer-fault.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+#ifndef SHIRUDO_TEST_NV_WRITER_FAULT_H
+#define SHIRUDO_TEST_NV_WRITER_FAULT_H
+
+struct nv_writer_fault {
+ __u32 pid;
+ __u32 command;
+ __u32 fail_import;
+ __u32 imports;
+ __u32 failures;
+};
+
+#endif /* SHIRUDO_TEST_NV_WRITER_FAULT_H */
diff --git a/testing/helper/test-nv-writer-cleanup.c b/testing/helper/test-nv-writer-cleanup.c
new file mode 100644
index 00000000..97f0a340
--- /dev/null
+++ b/testing/helper/test-nv-writer-cleanup.c
@@ -0,0 +1,250 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <linux/keyctl.h>
+
+#include "../../builtin-init.c"
+#include "../fixtures/nv-writer-fault.h"
+
+const __u8 *__wrap_shirudo_image_runtime_root(void);
+
+/* The injected failure precedes runtime mounting and guard attachment. */
+const __u8 *__wrap_shirudo_image_runtime_root(void)
+{
+ static const __u8 root[32] = { 1 };
+
+ return root;
+}
+
+struct ring_snapshot {
+ key_serial_t keys[256];
+ long bytes;
+};
+
+static int serial_order(const void *a, const void *b)
+{
+ key_serial_t right = *(const key_serial_t *)b;
+ key_serial_t left = *(const key_serial_t *)a;
+
+ return (left > right) - (left < right);
+}
+
+static int snapshot(key_serial_t ring, struct ring_snapshot *out)
+{
+ out->bytes = syscall(__NR_keyctl, KEYCTL_READ, ring, out->keys,
+ sizeof(out->keys), 0);
+ if (out->bytes < 0)
+ return -errno;
+ if ((size_t)out->bytes > sizeof(out->keys) ||
+ out->bytes % sizeof(out->keys[0]))
+ return -EOVERFLOW;
+ qsort(out->keys, out->bytes / sizeof(out->keys[0]),
+ sizeof(out->keys[0]), serial_order);
+ return 0;
+}
+
+static int unchanged(key_serial_t ring, const struct ring_snapshot *before)
+{
+ struct ring_snapshot after = {};
+ int rc;
+
+ rc = snapshot(ring, &after);
+ if (rc)
+ return rc;
+ if (before->bytes != after.bytes ||
+ memcmp(before->keys, after.keys, before->bytes)) {
+ fprintf(stderr, "keyring %d changed: before %ld bytes, after %ld bytes\n",
+ ring, before->bytes, after.bytes);
+ return -EUCLEAN;
+ }
+ return 0;
+}
+
+static int contains(key_serial_t parent, key_serial_t ring)
+{
+ struct ring_snapshot state = {};
+ size_t i;
+ int rc;
+
+ rc = snapshot(parent, &state);
+ if (rc)
+ return rc;
+ for (i = 0; i < (size_t)state.bytes / sizeof(state.keys[0]); i++)
+ if (state.keys[i] == ring)
+ return 0;
+ return -ENOKEY;
+}
+
+static int enroll(const char *path)
+{
+ __u8 *der = NULL;
+ size_t len = 0;
+ int rc;
+
+ rc = read_der(path, &der, &len);
+ if (rc)
+ return rc;
+ rc = keyring_bpf_add_cert(der, len);
+ xfree(der, len);
+ return rc < 0 ? rc : keyring_bpf_seal();
+}
+
+static int exercise(const char *mode, const char *object)
+{
+ struct bpf_link *import_link = NULL, *map_link = NULL;
+ struct ring_snapshot user = {}, session = {};
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES] = {};
+ __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES];
+ key_serial_t ring = -1, writer = -1;
+ struct nv_writer_fault state = {};
+ struct bpf_object *obj = NULL;
+ struct bpf_program *program;
+ int rc, result, fd = -1;
+ __u8 digest[32] = {};
+ __u32 zero = 0;
+
+ rc = snapshot(KEY_SPEC_USER_KEYRING, &user);
+ if (!rc)
+ rc = snapshot(KEY_SPEC_SESSION_KEYRING, &session);
+ if (rc)
+ return rc;
+ if (object) {
+ state.pid = getpid();
+ state.fail_import = !strcmp(mode, "import");
+ if (!strcmp(mode, "update"))
+ state.command = BPF_MAP_UPDATE_ELEM;
+ else if (!strcmp(mode, "freeze"))
+ state.command = BPF_MAP_FREEZE;
+ obj = bpf_object__open_file(object, NULL);
+ rc = libbpf_get_error(obj);
+ if (rc)
+ return rc;
+ rc = bpf_object__load(obj);
+ if (rc)
+ goto out;
+ fd = bpf_object__find_map_fd_by_name(obj, "fault");
+ if (fd < 0 || bpf_map_update_elem(fd, &zero, &state, BPF_ANY)) {
+ rc = fd < 0 ? fd : -errno;
+ goto out;
+ }
+ program = bpf_object__find_program_by_name(obj, "fail_writer_import");
+ if (!program) {
+ rc = -ENOENT;
+ goto out;
+ }
+ import_link = bpf_program__attach_lsm(program);
+ rc = libbpf_get_error(import_link);
+ if (rc)
+ goto out;
+ program = bpf_object__find_program_by_name(obj, "fail_writer_map");
+ if (!program) {
+ rc = -ENOENT;
+ goto out;
+ }
+ map_link = bpf_program__attach_lsm(program);
+ rc = libbpf_get_error(map_link);
+ if (rc)
+ goto out;
+ }
+ if (state.command) {
+ result = init_lsm();
+ rc = result == -EIO ? 0 : -EPROTO;
+ if (rc)
+ fprintf(stderr, "init_lsm returned %d, expected -EIO\n", result);
+ } else {
+ result = nv_writer_create(&ring, &writer, public);
+ if (!strcmp(mode, "success")) {
+ rc = result;
+ if (!rc)
+ rc = contains(KEY_SPEC_USER_KEYRING, ring);
+ if (!rc)
+ rc = contains(KEY_SPEC_SESSION_KEYRING, ring);
+ if (!rc)
+ rc = nv_writer_sign_key(writer, digest, signature);
+ nv_writer_destroy(ring);
+ } else {
+ rc = result == -EBADMSG && !ring && !writer ? 0 : -EPROTO;
+ if (rc)
+ fprintf(stderr, "import returned %d, ring %d, writer %d\n",
+ result, ring, writer);
+ }
+ }
+ if (fd >= 0) {
+ if (bpf_map_lookup_elem(fd, &zero, &state)) {
+ rc = -errno;
+ goto out;
+ }
+ if (state.imports != 1 || state.failures != 1) {
+ fprintf(stderr, "fault did not fire exactly once: imports %u, failures %u\n",
+ state.imports, state.failures);
+ rc = -EPROTO;
+ }
+ }
+ result = unchanged(KEY_SPEC_USER_KEYRING, &user);
+ if (result)
+ rc = result;
+ result = unchanged(KEY_SPEC_SESSION_KEYRING, &session);
+ if (result)
+ rc = result;
+ if (state.command && (!access(SHIRUDO_IDENTITY_PIN, F_OK) ||
+ !access(SHIRUDO_LSM_LINK_PIN, F_OK)))
+ rc = -EUCLEAN;
+out:
+ bpf_link__destroy(map_link);
+ bpf_link__destroy(import_link);
+ bpf_object__close(obj);
+ return rc;
+}
+
+int main(int argc, char **argv)
+{
+ const char *vm = getenv("SHIRUDO_TEST_REGISTER_VM");
+ key_serial_t sentinel;
+ int rc, cleanup;
+ long ret;
+
+ log_init();
+ if (!vm || strcmp(vm, "1") || getuid() || argc < 2 ||
+ !access(SHIRUDO_LSM_LINK_PIN, F_OK)) {
+ fprintf(stderr, "requires an opted-in, uninitialized disposable VM\n");
+ return 2;
+ }
+ if (argc == 3 && !strcmp(argv[1], "enroll")) {
+ rc = enroll(argv[2]);
+ goto done;
+ }
+ if (!((argc == 2 && (!strcmp(argv[1], "no-pkcs8") ||
+ !strcmp(argv[1], "success"))) ||
+ (argc == 3 && (!strcmp(argv[1], "import") ||
+ !strcmp(argv[1], "update") ||
+ !strcmp(argv[1], "freeze"))))) {
+ fprintf(stderr, "usage: %s enroll CERT | success | no-pkcs8 | {import,update,freeze} BPF_OBJECT\n",
+ argv[0]);
+ return 2;
+ }
+ ret = syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL, 0, 0, 0);
+ if (ret < 0) {
+ rc = -errno;
+ goto done;
+ }
+ ret = syscall(__NR_add_key, "keyring", "shirudo.nv-test.keep", NULL, 0,
+ KEY_SPEC_SESSION_KEYRING);
+ if (ret < 0) {
+ rc = -errno;
+ goto done;
+ }
+ sentinel = (key_serial_t)ret;
+ ret = syscall(__NR_keyctl, KEYCTL_LINK, sentinel, KEY_SPEC_USER_KEYRING, 0, 0);
+ if (ret < 0) {
+ rc = -errno;
+ goto done;
+ }
+ rc = exercise(argv[1], argc == 3 ? argv[2] : NULL);
+ cleanup = keyring_unlink_key(KEY_SPEC_USER_KEYRING, sentinel);
+ if (cleanup)
+ rc = cleanup;
+done:
+ if (rc)
+ fprintf(stderr, "NV writer cleanup test: %s\n", strerror(-rc));
+ return rc ? 1 : 0;
+}
diff --git a/testing/tpm/t1128-nv-writer-cleanup.sh b/testing/tpm/t1128-nv-writer-cleanup.sh
new file mode 100644
index 00000000..a545d796
--- /dev/null
+++ b/testing/tpm/t1128-nv-writer-cleanup.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+cd "$(dirname "$0")/.."
+. ./test-lib.sh
+
+[ "${SHIRUDO_TEST_REGISTER_VM:-0}" = 1 ] ||
+ test_skip_all "requires an uninitialized disposable VM"
+[ "$(id -u)" = 0 ] && [ ! -e /run/shirudo/bpffs/identity ] &&
+ [ ! -e /run/shirudo/bpffs/lsm_link ] || {
+ echo "NV cleanup tests require root before shirudo init" >&2
+ exit 1
+}
+R=./helper/test-nv-writer-cleanup
+F=./fixtures/nv-writer-fault.bpf.o
+[ -x "$R" ] || { echo "missing $R" >&2; exit 1; }
+
+if [ "${SHIRUDO_TEST_NO_PKCS8:-0}" = 1 ]; then
+ config="/boot/config-$(uname -r)"
+ grep -qx '# CONFIG_PKCS8_PRIVATE_KEY_PARSER is not set' "$config" || {
+ echo "parser-disabled test requires the booted kernel config at $config" >&2
+ exit 1
+ }
+ test_expect_success 'PKCS#8 rejection leaves both keyrings unchanged on repeated attempts' '
+ for attempt in 1 2 3; do
+ "$R" no-pkcs8 || exit 1
+ done
+ '
+ test_done
+fi
+
+: "${SHIRUDO_TEST_NV_CERT:?path to a test DER certificate is required}"
+[ -s "$F" ] || { echo "missing $F" >&2; exit 1; }
+mkdir -p /run/shirudo/bpffs
+if mountpoint -q /run/shirudo/bpffs; then
+ echo "NV cleanup tests require an unused private bpffs" >&2
+ exit 1
+fi
+mount -t bpf -o nosuid,nodev,noexec bpf /run/shirudo/bpffs
+cleanup() {
+ status=$?
+ trap - EXIT
+ umount /run/shirudo/bpffs || status=1
+ exit "$status"
+}
+trap cleanup EXIT
+trap 'exit 1' HUP INT TERM
+"$R" enroll "$SHIRUDO_TEST_NV_CERT"
+
+test_expect_success 'successful creation retains both links and can sign until explicit cleanup' '
+ "$R" success
+'
+
+test_expect_success 'private-key import failure leaves both keyrings unchanged on repeated attempts' '
+ for attempt in 1 2 3; do
+ "$R" import "$F" || exit 1
+ done
+'
+
+test_expect_success 'identity-map update failure rolls back the successfully created writer' '
+ "$R" update "$F"
+'
+
+test_expect_success 'identity-map freeze failure rolls back the successfully created writer' '
+ "$R" freeze "$F"
+'
+
+test_done
diff --git a/util/nv_writer.c b/util/nv_writer.c
index 76f76569..366b6391 100644
--- a/util/nv_writer.c
+++ b/util/nv_writer.c
@@ -19,6 +19,7 @@
#include "crypto.h"
#include "identity.h"
#include "log.h"
+#include "macros.h"
#include "malloc.h"
#include "nv_writer.h"
@@ -41,9 +42,27 @@
#define NV_WRITER_SIGN_INFO "enc=pkcs1 hash=sha256"
+void nv_writer_destroy(key_serial_t ring)
+{
+ const key_serial_t parents[] = {
+ KEY_SPEC_USER_KEYRING, KEY_SPEC_SESSION_KEYRING,
+ };
+ size_t i;
+
+ if (ring <= 0)
+ return;
+ for (i = 0; i < ARRAY_SIZE(parents); i++) {
+ if (syscall(__NR_keyctl, KEYCTL_UNLINK, ring, parents[i], 0, 0) < 0 &&
+ errno != ENOENT)
+ pr_err("nv writer: unlink ring %d from %d: %s",
+ ring, parents[i], strerror(errno));
+ }
+}
+
int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
{
+ key_serial_t writer_ring = 0, writer_key = 0;
PKCS8_PRIV_KEY_INFO *info = NULL;
EVP_PKEY_CTX *ctx = NULL;
int len = 0, rc = -EIO;
@@ -55,6 +74,8 @@ int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
__u64 nonce;
long ret;
+ *ring = 0;
+ *writer = 0;
ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
if (!ctx || EVP_PKEY_keygen_init(ctx) <= 0 ||
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 3072) <= 0 ||
@@ -92,24 +113,27 @@ int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
KEY_SPEC_SESSION_KEYRING);
if (ret < 0)
goto key_error;
- *ring = (key_serial_t)ret;
- if (syscall(__NR_keyctl, KEYCTL_SETPERM, *ring, NV_RING_WRITE, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_LINK, *ring, KEY_SPEC_USER_KEYRING, 0, 0) < 0)
+ writer_ring = (key_serial_t)ret;
+ if (syscall(__NR_keyctl, KEYCTL_SETPERM, writer_ring, NV_RING_WRITE, 0, 0) < 0 ||
+ syscall(__NR_keyctl, KEYCTL_LINK, writer_ring, KEY_SPEC_USER_KEYRING, 0, 0) < 0)
goto key_error;
ret = syscall(__NR_add_key, "asymmetric", "shirudo.nv-writer",
- der, (size_t)len, *ring);
+ der, (size_t)len, writer_ring);
if (ret < 0)
goto key_error;
- *writer = (key_serial_t)ret;
- if (syscall(__NR_keyctl, KEYCTL_SETPERM, *writer,
+ writer_key = (key_serial_t)ret;
+ if (syscall(__NR_keyctl, KEYCTL_SETPERM, writer_key,
KEY_USR_VIEW | KEY_USR_SEARCH, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_RESTRICT_KEYRING, *ring, 0, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_SETPERM, *ring, NV_RING_READ, 0, 0) < 0)
+ syscall(__NR_keyctl, KEYCTL_RESTRICT_KEYRING, writer_ring, 0, 0, 0) < 0 ||
+ syscall(__NR_keyctl, KEYCTL_SETPERM, writer_ring, NV_RING_READ, 0, 0) < 0)
goto key_error;
+ *ring = writer_ring;
+ *writer = writer_key;
rc = 0;
goto out;
key_error:
rc = -errno;
+ nv_writer_destroy(writer_ring);
out:
xfree(der, len > 0 ? (size_t)len : 0);
PKCS8_PRIV_KEY_INFO_free(info);
diff --git a/util/nv_writer.h b/util/nv_writer.h
index ede2980e..bef46ba3 100644
--- a/util/nv_writer.h
+++ b/util/nv_writer.h
@@ -8,6 +8,7 @@
int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
+void nv_writer_destroy(key_serial_t ring);
int nv_writer_read(__u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
int nv_writer_sign(const __u8 digest[SHIRUDO_TPM_DIGEST_LEN],
__u8 signature[SHIRUDO_TPM_NV_RSA_BYTES]);