Skip to content

eBPF-based security platform

image: read the config image through its verified root alone

Daniel Borkmannd02af782c57e
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 208e2524..f4a063f0 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -292,6 +292,12 @@ was resolved from the kernel's BPF keyring. The kernel normally records no
serial for this singleton; a nonzero serial must match the frozen record.
A certificate in another caller keyring cannot authorize a load.
+Image content by descriptor. Everything a command takes from the
+runtime or config image, the bootstrap settings, the configuration,
+the certificate, the key set, the registries, the tree configuration
+and the node labels, is opened from the verified root descriptor with
+``openat2`` under ``RESOLVE_BENEATH``, never through a path string;
+``/proc/self/fd`` names in particular are not used for them.
bpffs and keyring guards. The identity map contains the bpffs and run-device
numbers, runtime root hash, BPF keyring kind and serial, enrolled key
serials, the NV writer's public key and key serials, and the name of the
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index 36b5d2c3..636f1ede 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -196,7 +196,13 @@ T3 Tampering with shirudo's files and process
a download, is opened as output, never a labelled inode, never
created under a labelled directory and never through a symbolic
link, and a registry download goes into the descriptor shirudo
- opened rather than a name reopened later.
+ opened rather than a name reopened later. What the verified
+ runtime and config images hold, settings, keys, registries and
+ labels, is read from the descriptor of the verified root, never by
+ a path: ``/proc/<pid>/fd`` is a directory root can mount over
+ before the exec that grants the mark, and a command whose
+ configuration came through such a path took root's settings for the
+ image's on the test target.
Ptrace and perf allocation checks deny against tasks already marked
when those hooks run. Concurrent perf attachment and exec remain a
separate, untested `race investigation <https://github.com/shirudo-dev/shirudo/issues/11>`__.
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 6cc668cb..7f62f979 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -281,6 +281,62 @@ static int source_path(const char *name, char *out, size_t out_sz)
return 0;
}
+/*
+ * What comes from the config image is authenticated content, and the
+ * host path it is named by in messages is not where it was read from;
+ * only a host file has an owner and mode to check.
+ */
+static int stream_untrusted(FILE *fp, const char *path)
+{
+ if (shirudo_image_config_active())
+ return 0;
+ return check_trusted(fileno(fp), path, false);
+}
+
+/* A source's file, from the config image's root when one is active. */
+static FILE *source_open(const char *path)
+{
+ int fd;
+
+ if (!shirudo_image_config_active())
+ return fopen(path, "r");
+ fd = shirudo_image_open_relative(path + 1, O_RDONLY);
+ if (fd < 0) {
+ errno = -fd;
+ return NULL;
+ }
+ return fdopen(fd, "r");
+}
+
+static DIR *registries_opendir(void)
+{
+ int fd;
+
+ if (!shirudo_image_config_active())
+ return opendir(SHIRUDO_BPM_REGISTRIES_DIR);
+ fd = shirudo_image_open(SHIRUDO_IMAGE_REGISTRIES,
+ O_RDONLY | O_DIRECTORY);
+ if (fd < 0) {
+ errno = -fd;
+ return NULL;
+ }
+ return fdopendir(fd);
+}
+
+static FILE *tree_conf_open(void)
+{
+ int fd;
+
+ if (!shirudo_image_config_active())
+ return fopen(SHIRUDO_BPM_TREE_ETC_PATH, "r");
+ fd = shirudo_image_open(SHIRUDO_IMAGE_TREE_CONF, O_RDONLY);
+ if (fd < 0) {
+ errno = -fd;
+ return NULL;
+ }
+ return fdopen(fd, "r");
+}
+
static int load_source(const char *name, struct bpm_source *s)
{
char path[PATH_MAX], line[PATH_MAX];
@@ -291,10 +347,10 @@ static int load_source(const char *name, struct bpm_source *s)
rc = source_path(name, path, sizeof(path));
if (rc)
return rc;
- fp = fopen(path, "r");
+ fp = source_open(path);
if (!fp)
return -errno;
- if (check_trusted(fileno(fp), path, false)) {
+ if (stream_untrusted(fp, path)) {
fclose(fp);
return -EPERM;
}
@@ -311,21 +367,33 @@ static int load_source(const char *name, struct bpm_source *s)
fclose(fp);
if (!s->endpoint[0] || !s->key_file[0])
return -EINVAL;
+ /* With a config image the key file is the image's, named from its root. */
if (shirudo_image_config_active()) {
- const char *relative = s->key_file;
- char resolved[BPM_MAX_KEY];
-
- if (!strncmp(relative, "/etc/shirudo/bpm/keys/", 22))
- relative++;
- rc = shirudo_image_path_relative(resolved, sizeof(resolved),
- relative);
- if (rc)
- return rc;
- bpm_copy_string(s->key_file, sizeof(s->key_file), resolved);
+ if (!strncmp(s->key_file, "/etc/shirudo/bpm/keys/", 22))
+ memmove(s->key_file, s->key_file + 1,
+ strlen(s->key_file));
+ if (s->key_file[0] == '/' || strstr(s->key_file, ".."))
+ return -EINVAL;
}
return 0;
}
+static int verify_with_source_key(const struct bpm_source *s,
+ const __u8 *data, size_t len,
+ const __u8 *sig, size_t sig_len)
+{
+ int fd, rc;
+
+ if (!shirudo_image_config_active())
+ return bpm_verify_cms(data, len, sig, sig_len, s->key_file);
+ fd = shirudo_image_open_relative(s->key_file, O_RDONLY);
+ if (fd < 0)
+ return fd;
+ rc = bpm_verify_cms_fd(data, len, sig, sig_len, fd);
+ close(fd);
+ return rc;
+}
+
static int save_source(const struct bpm_source *s)
{
char path[PATH_MAX], data[PATH_MAX * 2];
@@ -1011,7 +1079,8 @@ static int repo_update_one(const char *name)
rc = -EINVAL;
if (rc)
goto out;
- if (bpm_verify_cms(index.data, index.len, sig.data, sig.len, s.key_file)) {
+ if (verify_with_source_key(&s, index.data, index.len, sig.data,
+ sig.len)) {
rc = -EKEYREJECTED;
goto out;
}
@@ -1416,7 +1485,7 @@ static int cmd_bpm_config(int argc, const char **argv)
if (argc < 3)
return 2;
if (!strcmp(argv[2], "list")) {
- DIR *d = opendir(SHIRUDO_BPM_REGISTRIES_DIR);
+ DIR *d = registries_opendir();
struct dirent *e;
if (!d)
@@ -1501,10 +1570,10 @@ static int load_tree_config(struct bpm_tree_config *cfg)
FILE *fp;
*cfg = (struct bpm_tree_config){};
- fp = fopen(SHIRUDO_BPM_TREE_ETC_PATH, "r");
+ fp = tree_conf_open();
if (!fp)
return -errno;
- if (check_trusted(fileno(fp), SHIRUDO_BPM_TREE_ETC_PATH, false)) {
+ if (stream_untrusted(fp, SHIRUDO_BPM_TREE_ETC_PATH)) {
fclose(fp);
return -EPERM;
}
@@ -2590,7 +2659,7 @@ int cmd_bpm(int argc, const char **argv)
if (lock < 0)
return bpm_finish(-1, lock, "lock");
if (argc == 3) {
- DIR *d = opendir(SHIRUDO_BPM_REGISTRIES_DIR);
+ DIR *d = registries_opendir();
struct dirent *entry;
if (!d && errno != ENOENT)
@@ -2631,7 +2700,7 @@ int cmd_bpm(int argc, const char **argv)
* avoid requiring writable state directories) when it is
* not configured.
*/
- if (access(SHIRUDO_BPM_TREE_ETC_PATH, F_OK) != 0 && errno == ENOENT)
+ if (!tree_configured())
return 0;
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK)
return bpm_fail("initialize curl", -EIO);
diff --git a/builtin-init.c b/builtin-init.c
index 61fa7e60..5cd79b20 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -76,42 +76,6 @@ static void print_preflight(const struct shirudo_preflight_result *r)
r->n - r->n_failed, r->n, r->n_failed);
}
-static int read_der(const char *path, __u8 **out, size_t *out_sz)
-{
- struct stat st;
- __u8 *buf;
- ssize_t n;
- int fd;
-
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) < 0) {
- int rc = -errno;
-
- close(fd);
- return rc;
- }
- if (st.st_size <= 0 || st.st_size > 1u * 1024u * 1024u) {
- close(fd);
- return -EINVAL;
- }
- buf = xmalloc((size_t)st.st_size);
- if (!buf) {
- close(fd);
- return -ENOMEM;
- }
- n = read(fd, buf, (size_t)st.st_size);
- close(fd);
- if (n != st.st_size) {
- xfree(buf, (size_t)st.st_size);
- return -EIO;
- }
- *out = buf;
- *out_sz = (size_t)st.st_size;
- return 0;
-}
-
static void keyring_count_asymmetric(key_serial_t serial, const char *type,
const char *description, void *ud)
{
@@ -143,15 +107,14 @@ static int keyring_holds_set(key_serial_t ring,
size_t i, held = 0;
for (i = 0; i < set->n; i++) {
+ const __u8 *der = NULL;
size_t der_sz = 0;
- __u8 *der = NULL;
int rc;
- rc = read_der(set->certs[i].path, &der, &der_sz);
+ rc = keys_cert_der(&set->certs[i], &der, &der_sz);
if (rc)
return rc;
rc = keyring_holds_cert(ring, der, der_sz);
- xfree(der, der_sz);
if (rc < 0)
return rc;
held += rc;
@@ -175,18 +138,17 @@ static int init_bpf_keyring(key_serial_t ring,
size_t i;
for (i = 0; i < set->n; i++) {
+ const __u8 *der = NULL;
size_t der_sz = 0;
- __u8 *der = NULL;
key_serial_t key;
- rc = read_der(set->certs[i].path, &der, &der_sz);
+ rc = keys_cert_der(&set->certs[i], &der, &der_sz);
if (rc) {
pr_err("read %s: %s", set->certs[i].path,
strerror(-rc));
return rc;
}
key = keyring_bpf_add_cert(der, der_sz);
- xfree(der, der_sz);
if (key >= 0) {
pr_info("bpf keyring serial=%d: %s enrolled (key "
"serial=%d)", (int)ring, set->certs[i].path,
diff --git a/builtin-keys.c b/builtin-keys.c
index 827c20be..e54a0141 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -508,9 +508,9 @@ static int check_voucher_live(const struct shirudo_trusted_set *set,
const __u8 voucher[32])
{
struct shirudo_identity id;
+ const __u8 *der = NULL;
key_serial_t ring;
size_t der_sz = 0;
- __u8 *der = NULL;
int rc = 0;
size_t i;
@@ -526,11 +526,10 @@ static int check_voucher_live(const struct shirudo_trusted_set *set,
}
if (i == set->n)
return -ENOKEY;
- rc = keys_read_der(set->certs[i].path, &der, &der_sz);
+ rc = keys_cert_der(&set->certs[i], &der, &der_sz);
if (rc)
return rc;
rc = keyring_holds_cert(ring, der, der_sz);
- xfree(der, der_sz);
if (rc < 0)
return rc;
return rc ? 0 : -ENOKEY;
diff --git a/testing/helper/test-image-cli.c b/testing/helper/test-image-cli.c
index 43d90ada..24d6ba79 100644
--- a/testing/helper/test-image-cli.c
+++ b/testing/helper/test-image-cli.c
@@ -60,10 +60,14 @@ int main(int argc, const char **argv)
rc = load_source(argv[3], &source);
if (!rc) {
char content[64];
- FILE *file = fopen(source.key_file, "r");
+ int kfd = shirudo_image_open_relative(source.key_file,
+ O_RDONLY);
+ FILE *file = kfd < 0 ? NULL : fdopen(kfd, "r");
if (!file) {
- rc = -errno;
+ rc = kfd < 0 ? kfd : -errno;
+ if (kfd >= 0)
+ close(kfd);
} else {
if (!fgets(content, sizeof(content), file))
rc = -EIO;
diff --git a/testing/helper/test-image-config.c b/testing/helper/test-image-config.c
index 7fe5baf7..b6bc5b7f 100644
--- a/testing/helper/test-image-config.c
+++ b/testing/helper/test-image-config.c
@@ -34,7 +34,6 @@ static int read_bootstrap(int argc, char **argv)
int main(int argc, char **argv)
{
struct shirudo_config bootstrap, effective;
- char resolved[4096];
int fd, rc;
if (argc < 3 || argc > 4)
@@ -62,17 +61,22 @@ int main(int argc, char **argv)
(int)effective.tpm_owner_auth_len,
effective.tpm_owner_auth, effective.tpm_tcti,
effective.eventlog_max_bytes);
- printf("registries=%s\n",
- shirudo_image_path(SHIRUDO_IMAGE_REGISTRIES));
+ rc = shirudo_image_open(SHIRUDO_IMAGE_REGISTRIES,
+ O_RDONLY | O_DIRECTORY);
+ printf("registries=%s\n", rc >= 0 ? "open" : "missing");
+ if (rc >= 0)
+ close(rc);
+ rc = 0;
}
} else if (!strcmp(argv[1], "relative") && argc == 4) {
rc = shirudo_image_config_set(&bootstrap, fd);
if (!rc)
- rc = shirudo_image_path_relative(resolved,
- sizeof(resolved),
- argv[3]);
- if (!rc)
- printf("resolved=%s\n", resolved);
+ rc = shirudo_image_open_relative(argv[3], O_RDONLY);
+ if (rc >= 0) {
+ printf("resolved=open\n");
+ close(rc);
+ rc = 0;
+ }
} else {
rc = -EINVAL;
}
diff --git a/testing/helper/test-keys.c b/testing/helper/test-keys.c
index afa34192..55f3a8df 100644
--- a/testing/helper/test-keys.c
+++ b/testing/helper/test-keys.c
@@ -8,9 +8,11 @@
*/
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <shirudo/misc.h>
@@ -61,10 +63,14 @@ int main(int argc, char **argv)
}
if (!strcmp(argv[1], "image")) {
char primary[PATH_MAX];
+ int root;
snprintf(primary, sizeof(primary), "%s/cert.der", argv[2]);
- rc = shirudo_keys_load_image(primary, argv[2], NULL, true,
- &set);
+ root = open(argv[2], O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (root < 0)
+ return 1;
+ rc = shirudo_keys_load_image_at(primary, root, true, &set);
+ close(root);
} else {
rc = keys_load(argv[2], NULL,
argc < 4 || strcmp(argv[3], "primary"),
diff --git a/testing/helper/test-nv-writer-cleanup.c b/testing/helper/test-nv-writer-cleanup.c
index 97f0a340..9c061c35 100644
--- a/testing/helper/test-nv-writer-cleanup.c
+++ b/testing/helper/test-nv-writer-cleanup.c
@@ -81,7 +81,7 @@ static int enroll(const char *path)
size_t len = 0;
int rc;
- rc = read_der(path, &der, &len);
+ rc = keys_read_der(path, &der, &len);
if (rc)
return rc;
rc = keyring_bpf_add_cert(der, len);
diff --git a/testing/t0047-image-config.sh b/testing/t0047-image-config.sh
index fbc1bcf4..c358ef08 100755
--- a/testing/t0047-image-config.sh
+++ b/testing/t0047-image-config.sh
@@ -75,8 +75,9 @@ test_expect_success 'special files in config are refused without opening or bloc
'
test_expect_success 'relative lookup refuses absolute paths and parent traversal' '
+ touch "$WORK/good/etc/shirudo/bpm/upatch-tree.conf" &&
"$H" relative "$WORK/good" etc/shirudo/bpm/upatch-tree.conf >"$WORK/relative" &&
- grep -q "^resolved=/proc/self/fd/" "$WORK/relative" &&
+ grep -qx "resolved=open" "$WORK/relative" &&
for path in /etc/shirudo/shirudo.conf ../outside.conf etc/../../outside.conf; do
! "$H" relative "$WORK/good" "$path" || exit 1
done
diff --git a/util/bpm_package.c b/util/bpm_package.c
index e3e6dace..c0d6c1db 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -194,6 +194,18 @@ int bpm_verify_cms(const __u8 *payload, size_t payload_len,
return rc;
}
+int bpm_verify_cms_fd(const __u8 *payload, size_t payload_len,
+ const __u8 *sig, size_t sig_len, int cert_fd)
+{
+ struct shirudo_cert cert = {};
+ int rc = -EINVAL;
+
+ if (!cert_read_fd(cert_fd, &cert))
+ rc = verify_detached(payload, payload_len, sig, sig_len, &cert);
+ cert_free(&cert);
+ return rc;
+}
+
int bpm_tar_octal(const char *p, size_t n, __u64 *out)
{
size_t i = 0;
diff --git a/util/bpm_package.h b/util/bpm_package.h
index eb92f650..757a0d5a 100644
--- a/util/bpm_package.h
+++ b/util/bpm_package.h
@@ -41,6 +41,8 @@ int bpm_semver_compare(const char *a, const char *b);
int bpm_verify_cms(const __u8 *payload, size_t payload_len,
const __u8 *sig, size_t sig_len,
const char *cert_path);
+int bpm_verify_cms_fd(const __u8 *payload, size_t payload_len,
+ const __u8 *sig, size_t sig_len, int cert_fd);
int bpm_tar_octal(const char *p, size_t n, __u64 *out);
bool bpm_archive_rel_ok(const char *name);
diff --git a/util/bpm_tree.h b/util/bpm_tree.h
index a4427652..6a87062f 100644
--- a/util/bpm_tree.h
+++ b/util/bpm_tree.h
@@ -114,4 +114,7 @@ int tree_event_replay(__u8 out[32]);
void tree_emit_status(struct shirudo_json_emit *e);
+/* Whether a tree is configured, in the config image when one is active. */
+bool tree_configured(void);
+
#endif /* SHIRUDO_BPM_TREE_H */
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index a58041e9..b2f3295b 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -70,6 +70,22 @@ static int selector_values_match(const struct shirudo_tree_selector_kv *v,
return 0;
}
+bool tree_configured(void)
+{
+ int fd;
+
+ if (!shirudo_image_config_active())
+ return access(SHIRUDO_BPM_TREE_ETC_PATH, R_OK) == 0;
+ fd = shirudo_image_open(SHIRUDO_IMAGE_TREE_CONF, O_RDONLY);
+ if (fd < 0)
+ return false;
+ close(fd);
+ return true;
+}
+
+static int read_config_stream(FILE *f, struct shirudo_tree_kv *out, int *n,
+ int max);
+
static int read_config_file(const char *path, struct shirudo_tree_kv *out,
int *n, int max, bool gz)
{
@@ -119,34 +135,59 @@ static int read_config_file(const char *path, struct shirudo_tree_kv *out,
if (!f)
return -errno;
- while (fgets(line, sizeof(line), f)) {
- char *start = line, *end, *eq;
-
- while (*start && isspace((__u8)*start))
- start++;
- if (*start == '#' || *start == '\0')
- continue;
- end = start + strlen(start);
- while (end > start && isspace((__u8)end[-1]))
- *--end = '\0';
- eq = strchr(start, '=');
- if (!eq)
- continue;
- *eq++ = '\0';
- end = eq + strlen(eq);
- while (end > eq && isspace((__u8)end[-1]))
- *--end = '\0';
- while (*eq && isspace((__u8)*eq))
- eq++;
- rc = kv_add(out, n, max, start, eq);
- if (rc)
- break;
- }
- fclose(f);
+ return read_config_stream(f, out, n, max);
+ }
+}
+
+/* The stream is consumed and closed. */
+static int read_config_stream(FILE *f, struct shirudo_tree_kv *out, int *n,
+ int max)
+{
+ char line[512];
+ int rc = 0;
+
+ *n = 0;
+ while (fgets(line, sizeof(line), f)) {
+ char *start = line, *end, *eq;
+
+ while (*start && isspace((__u8)*start))
+ start++;
+ if (*start == '#' || *start == '\0')
+ continue;
+ end = start + strlen(start);
+ while (end > start && isspace((__u8)end[-1]))
+ *--end = '\0';
+ eq = strchr(start, '=');
+ if (!eq)
+ continue;
+ *eq++ = '\0';
+ end = eq + strlen(eq);
+ while (end > eq && isspace((__u8)end[-1]))
+ *--end = '\0';
+ while (*eq && isspace((__u8)*eq))
+ eq++;
+ rc = kv_add(out, n, max, start, eq);
+ if (rc)
+ break;
}
+ fclose(f);
return rc;
}
+static int read_config_fd(int fd, struct shirudo_tree_kv *out, int *n,
+ int max)
+{
+ FILE *f = fdopen(fd, "r");
+
+ if (!f) {
+ int rc = -errno;
+
+ close(fd);
+ return rc;
+ }
+ return read_config_stream(f, out, n, max);
+}
+
/*
* The kernel config can contain thousands of entries. Index only the
* selector keys, then scan the complete config once and retain matching
@@ -391,11 +432,20 @@ int tree_selector_matches(const struct shirudo_tree_selector *s)
}
}
if (s->n_labels) {
- const char *nodes =
- shirudo_image_path(SHIRUDO_IMAGE_NODE_LABELS);
+ if (shirudo_image_config_active()) {
+ int fd = shirudo_image_open(SHIRUDO_IMAGE_NODE_LABELS,
+ O_RDONLY);
- rc = read_config_file(nodes ?: "/etc/shirudo/node.labels",
- labels, &nl, ARRAY_SIZE(labels), false);
+ if (fd == -ENOENT)
+ return 0;
+ if (fd < 0)
+ return fd;
+ rc = read_config_fd(fd, labels, &nl, ARRAY_SIZE(labels));
+ } else {
+ rc = read_config_file("/etc/shirudo/node.labels",
+ labels, &nl, ARRAY_SIZE(labels),
+ false);
+ }
if (rc == -ENOENT)
return 0;
if (rc)
@@ -618,7 +668,7 @@ void tree_emit_status(struct shirudo_json_emit *e)
struct shirudo_tree_state s = {};
__u8 live[32], replay[32];
char hex[65];
- bool configured = access(SHIRUDO_BPM_TREE_ETC_PATH, R_OK) == 0;
+ bool configured = tree_configured();
json_obj_open_key(e, "upatch_tree");
json_kv_bool(e, "configured", configured);
diff --git a/util/config.c b/util/config.c
index ba5d46f1..23a8c424 100644
--- a/util/config.c
+++ b/util/config.c
@@ -7,11 +7,13 @@
*/
#include <errno.h>
+#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <shirudo/misc.h>
#include <shirudo/string.h>
@@ -200,147 +202,144 @@ static void set_owner_auth(struct shirudo_config *cfg, const char *val)
cfg->tpm_owner_auth_len = l;
}
-int config_load_path(struct shirudo_config *cfg, const char *path)
+/* The stream is consumed and closed; the file is named for the log. */
+static int config_parse(struct shirudo_config *cfg, FILE *f,
+ const char *path)
{
char line[SHIRUDO_MAP_EXPORT_PATH_MAX + 128];
int rc = 0;
- FILE *f;
- if (!cfg || !path)
- return -EINVAL;
- config_defaults(cfg);
-
- f = fopen(path, "r");
- if (!f) {
- if (errno != ENOENT)
- rc = -errno; /* still apply env override below */
- } else if (check_trusted(fileno(f), path, false)) {
+ if (check_trusted(fileno(f), path, false)) {
fclose(f);
return -EPERM;
- } else {
- while (fgets(line, sizeof(line), f)) {
- char *p, *eq, *key, *val;
- int c;
-
- /* Never take a line the buffer cut short. */
- if (!strchr(line, '\n') && !feof(f)) {
- pr_warn("config: line longer than %zu bytes "
- "(ignored)", sizeof(line) - 1);
- while ((c = fgetc(f)) != EOF && c != '\n')
- continue;
+ }
+ while (fgets(line, sizeof(line), f)) {
+ char *p, *eq, *key, *val;
+ int c;
+
+ /* Never take a line the buffer cut short. */
+ if (!strchr(line, '\n') && !feof(f)) {
+ pr_warn("config: line longer than %zu bytes "
+ "(ignored)", sizeof(line) - 1);
+ while ((c = fgetc(f)) != EOF && c != '\n')
continue;
+ continue;
+ }
+ p = trim(line);
+ if (*p == '\0' || *p == '#')
+ continue;
+ eq = strchr(p, '=');
+ if (!eq)
+ continue;
+ *eq = '\0';
+ key = trim(p);
+ val = trim(eq + 1);
+ if (strcmp(key, "eventlog.max_bytes") == 0) {
+ __u64 v;
+
+ if (parse_u64(val, strlen(val), UINT64_MAX, &v))
+ pr_warn("config: eventlog.max_bytes: "
+ "bad value '%s' (ignored)", val);
+ else
+ cfg->eventlog_max_bytes = v;
+ } else if (strcmp(key, "tpm.owner_auth") == 0) {
+ set_owner_auth(cfg, val);
+ } else if (strcmp(key, "tpm.tcti") == 0) {
+ if (strlen(val) >= sizeof(cfg->tpm_tcti))
+ pr_warn("config: tpm.tcti too long "
+ "(ignored)");
+ else
+ strscpy(cfg->tpm_tcti, val,
+ sizeof(cfg->tpm_tcti));
+ } else if (strcmp(key, "tls.classical_hosts") == 0) {
+ if (!set_string(cfg->tls_classical_hosts,
+ sizeof(cfg->tls_classical_hosts), val) ||
+ !tls_classical_hosts_valid(val)) {
+ cfg->tls_classical_hosts_valid = false;
+ pr_warn("config: tls.classical_hosts: invalid exact "
+ "DNS host list");
}
- p = trim(line);
- if (*p == '\0' || *p == '#')
- continue;
- eq = strchr(p, '=');
- if (!eq)
- continue;
- *eq = '\0';
- key = trim(p);
- val = trim(eq + 1);
- if (strcmp(key, "eventlog.max_bytes") == 0) {
- __u64 v;
-
- if (parse_u64(val, strlen(val), UINT64_MAX, &v))
- pr_warn("config: eventlog.max_bytes: "
- "bad value '%s' (ignored)", val);
- else
- cfg->eventlog_max_bytes = v;
- } else if (strcmp(key, "tpm.owner_auth") == 0) {
- set_owner_auth(cfg, val);
- } else if (strcmp(key, "tpm.tcti") == 0) {
- if (strlen(val) >= sizeof(cfg->tpm_tcti))
- pr_warn("config: tpm.tcti too long "
- "(ignored)");
- else
- strscpy(cfg->tpm_tcti, val,
- sizeof(cfg->tpm_tcti));
- } else if (strcmp(key, "tls.classical_hosts") == 0) {
- if (!set_string(cfg->tls_classical_hosts,
- sizeof(cfg->tls_classical_hosts), val) ||
- !tls_classical_hosts_valid(val)) {
- cfg->tls_classical_hosts_valid = false;
- pr_warn("config: tls.classical_hosts: invalid exact "
- "DNS host list");
- }
- } else if (strcmp(key, "map_export.json_path") == 0) {
- if (!set_string(cfg->map_export_json_path,
- sizeof(cfg->map_export_json_path), val))
- map_export_bad(cfg, key, val, "value too long");
- else if (val[0] && val[0] != '/')
- map_export_bad(cfg, key, val,
- "path must be absolute");
- } else if (strcmp(key, "map_export.otel_endpoint") == 0) {
- if (!set_string(cfg->map_export_otel_endpoint,
- sizeof(cfg->map_export_otel_endpoint), val))
- map_export_bad(cfg, key, val, "value too long");
- } else if (strcmp(key, "map_export.tenant_id") == 0) {
- if (val[0] && !tenant_id_valid(val))
- map_export_bad(cfg, key, val,
- "expected 1..128 characters from "
- "A-Z, a-z, 0-9, '.', '_', '-'");
- else
- (void)set_string(cfg->map_export_tenant_id,
- sizeof(cfg->map_export_tenant_id),
- val);
- } else if (strcmp(key, "map_export.otel_insecure") == 0) {
- if (!parse_bool(val,
- &cfg->map_export_otel_insecure))
- map_export_bad(cfg, key, val,
- "expected true or false");
- } else if (strcmp(key, "map_export.otel_retries") == 0) {
- __u64 v;
-
- if (parse_u64(val, strlen(val), 10, &v))
- map_export_bad(cfg, key, val,
- "expected 0..10");
- else
- cfg->map_export_otel_retries =
- (__u32)v;
- } else if (strcmp(key, "map_export.s3_uri") == 0) {
- if (!set_string(cfg->map_export_s3_uri,
- sizeof(cfg->map_export_s3_uri), val))
- map_export_bad(cfg, key, val, "value too long");
- } else if (strcmp(key, "map_export.log_record_max_bytes") == 0 ||
- strcmp(key, "map_export.otlp_max_bytes") == 0) {
- __u64 v;
-
- if (parse_u64(val, strlen(val), SHIRUDO_JSON_MAX_BYTES,
- &v) || v == 0)
- map_export_bad(cfg, key, val, "expected 1..8388608 bytes");
- else if (strcmp(key, "map_export.log_record_max_bytes") == 0)
- cfg->map_export_log_record_max_bytes = (__u32)v;
- else
- cfg->map_export_otlp_max_bytes = (__u32)v;
- } else if (!strncmp(key, "config.", 7) ||
- !strncmp(key, "runtime.", 8)) {
- continue;
+ } else if (strcmp(key, "map_export.json_path") == 0) {
+ if (!set_string(cfg->map_export_json_path,
+ sizeof(cfg->map_export_json_path), val))
+ map_export_bad(cfg, key, val, "value too long");
+ else if (val[0] && val[0] != '/')
+ map_export_bad(cfg, key, val,
+ "path must be absolute");
+ } else if (strcmp(key, "map_export.otel_endpoint") == 0) {
+ if (!set_string(cfg->map_export_otel_endpoint,
+ sizeof(cfg->map_export_otel_endpoint), val))
+ map_export_bad(cfg, key, val, "value too long");
+ } else if (strcmp(key, "map_export.tenant_id") == 0) {
+ if (val[0] && !tenant_id_valid(val))
+ map_export_bad(cfg, key, val,
+ "expected 1..128 characters from "
+ "A-Z, a-z, 0-9, '.', '_', '-'");
+ else
+ (void)set_string(cfg->map_export_tenant_id,
+ sizeof(cfg->map_export_tenant_id),
+ val);
+ } else if (strcmp(key, "map_export.otel_insecure") == 0) {
+ if (!parse_bool(val,
+ &cfg->map_export_otel_insecure))
+ map_export_bad(cfg, key, val,
+ "expected true or false");
+ } else if (strcmp(key, "map_export.otel_retries") == 0) {
+ __u64 v;
+
+ if (parse_u64(val, strlen(val), 10, &v))
+ map_export_bad(cfg, key, val,
+ "expected 0..10");
+ else
+ cfg->map_export_otel_retries =
+ (__u32)v;
+ } else if (strcmp(key, "map_export.s3_uri") == 0) {
+ if (!set_string(cfg->map_export_s3_uri,
+ sizeof(cfg->map_export_s3_uri), val))
+ map_export_bad(cfg, key, val, "value too long");
+ } else if (strcmp(key, "map_export.log_record_max_bytes") == 0 ||
+ strcmp(key, "map_export.otlp_max_bytes") == 0) {
+ __u64 v;
+
+ if (parse_u64(val, strlen(val), SHIRUDO_JSON_MAX_BYTES,
+ &v) || v == 0)
+ map_export_bad(cfg, key, val, "expected 1..8388608 bytes");
+ else if (strcmp(key, "map_export.log_record_max_bytes") == 0)
+ cfg->map_export_log_record_max_bytes = (__u32)v;
+ else
+ cfg->map_export_otlp_max_bytes = (__u32)v;
+ } else if (!strncmp(key, "config.", 7) ||
+ !strncmp(key, "runtime.", 8)) {
+ continue;
+ } else {
+ if (strncmp(key, "map_export.", 11) == 0) {
+ map_export_bad(cfg, key, val, "unknown key");
+ } else if (strncmp(key, "tls.", 4) == 0) {
+ cfg->tls_classical_hosts_valid = false;
+ pr_warn("config: %s: unknown TLS setting", key);
} else {
- if (strncmp(key, "map_export.", 11) == 0)
- map_export_bad(cfg, key, val, "unknown key");
- else if (strncmp(key, "tls.", 4) == 0) {
- cfg->tls_classical_hosts_valid = false;
- pr_warn("config: %s: unknown TLS setting", key);
- } else {
- pr_warn("config: unknown key '%s' (ignored)",
- key);
- }
+ pr_warn("config: unknown key '%s' (ignored)",
+ key);
}
}
- if (ferror(f) && !rc)
- rc = -EIO;
- /* The TPM owner's authorization is for no other user's eyes. */
- if (cfg->tpm_owner_auth_len &&
- check_trusted(fileno(f), path, true)) {
- explicit_bzero(cfg->tpm_owner_auth,
- sizeof(cfg->tpm_owner_auth));
- cfg->tpm_owner_auth_len = 0;
- rc = -EPERM;
- }
- if (fclose(f) && !rc)
- rc = -errno;
}
+ if (ferror(f) && !rc)
+ rc = -EIO;
+ /* The TPM owner's authorization is for no other user's eyes. */
+ if (cfg->tpm_owner_auth_len &&
+ check_trusted(fileno(f), path, true)) {
+ explicit_bzero(cfg->tpm_owner_auth,
+ sizeof(cfg->tpm_owner_auth));
+ cfg->tpm_owner_auth_len = 0;
+ rc = -EPERM;
+ }
+ if (fclose(f) && !rc)
+ rc = -errno;
+ return rc;
+}
+
+static int config_finish(struct shirudo_config *cfg, int rc)
+{
if (cfg->map_export_s3_uri[0] && !cfg->map_export_otel_endpoint[0])
map_export_bad(cfg, "map_export.s3_uri", "",
"requires map_export.otel_endpoint for its reference event");
@@ -350,6 +349,50 @@ int config_load_path(struct shirudo_config *cfg, const char *path)
return rc;
}
+int config_load_path(struct shirudo_config *cfg, const char *path)
+{
+ int rc = 0;
+ FILE *f;
+
+ if (!cfg || !path)
+ return -EINVAL;
+ config_defaults(cfg);
+ f = fopen(path, "r");
+ if (!f) {
+ if (errno != ENOENT)
+ rc = -errno; /* still apply env override below */
+ } else {
+ rc = config_parse(cfg, f, path);
+ }
+ return config_finish(cfg, rc);
+}
+
+/* A copy of the descriptor is read from its start; the caller keeps its own. */
+int config_load_fd(struct shirudo_config *cfg, int fd, const char *label)
+{
+ int copy, rc;
+ FILE *f;
+
+ if (!cfg || fd < 0)
+ return -EINVAL;
+ config_defaults(cfg);
+ copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+ if (copy < 0)
+ return -errno;
+ if (lseek(copy, 0, SEEK_SET) < 0) {
+ rc = -errno;
+ close(copy);
+ return rc;
+ }
+ f = fdopen(copy, "r");
+ if (!f) {
+ rc = -errno;
+ close(copy);
+ return rc;
+ }
+ return config_finish(cfg, config_parse(cfg, f, label));
+}
+
int config_load(struct shirudo_config *cfg)
{
const char *env;
diff --git a/util/config.h b/util/config.h
index f5638651..074103e1 100644
--- a/util/config.h
+++ b/util/config.h
@@ -57,5 +57,7 @@ int config_load(struct shirudo_config *cfg);
/* Testable path-specific loader; unlike config_load(), no env. */
int config_load_path(struct shirudo_config *cfg, const char *path);
+/* The same from an open descriptor, read from its start. */
+int config_load_fd(struct shirudo_config *cfg, int fd, const char *label);
#endif /* SHIRUDO_CONFIG_H */
diff --git a/util/crypto.h b/util/crypto.h
index 7f25fc5c..948f0960 100644
--- a/util/crypto.h
+++ b/util/crypto.h
@@ -44,6 +44,7 @@ struct shirudo_cert {
};
int cert_read_file(const char *path, struct shirudo_cert *cert);
+int cert_read_fd(int fd, struct shirudo_cert *cert);
void cert_free(struct shirudo_cert *cert);
int cert_pubkey_digest(const struct shirudo_cert *cert,
__u8 out[SHIRUDO_CRYPTO_SHA256_LEN]);
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index cb9accc0..5f3bcbb5 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -31,6 +31,7 @@
#include "crypto.h"
#include "io.h"
+#include "keys.h"
#include "log.h"
#include "malloc.h"
@@ -201,6 +202,22 @@ int cert_read_file(const char *path, struct shirudo_cert *cert)
return cert->impl ? 0 : -EINVAL;
}
+int cert_read_fd(int fd, struct shirudo_cert *cert)
+{
+ size_t len = 0;
+ __u8 *der;
+ int rc;
+
+ if (fd < 0 || !cert)
+ return -EINVAL;
+ rc = read_fd(fd, "certificate", SHIRUDO_KEYS_DER_MAX, &der, &len);
+ if (rc)
+ return rc;
+ rc = cert_read_der(der, len, cert);
+ xfree(der, len + 1);
+ return rc;
+}
+
void cert_free(struct shirudo_cert *cert)
{
if (!cert)
diff --git a/util/image_bootstrap.c b/util/image_bootstrap.c
index a8fcdeb4..bb63f19c 100644
--- a/util/image_bootstrap.c
+++ b/util/image_bootstrap.c
@@ -5,9 +5,11 @@
#include <shirudo/string.h>
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "config_measure.h"
#include "hex.h"
@@ -35,17 +37,13 @@ static char *trim(char *text)
return text;
}
-int shirudo_image_bootstrap_read(const char *path,
- struct shirudo_image_bootstrap *boot,
- bool initial)
+/* The stream is consumed and closed. */
+static int bootstrap_parse(FILE *file, struct shirudo_image_bootstrap *boot,
+ bool initial)
{
char line[PATH_MAX + 128];
- FILE *file;
int rc = 0;
- file = fopen(path, "re");
- if (!file)
- return -errno;
while (fgets(line, sizeof(line), file)) {
char *key, *value;
@@ -114,3 +112,39 @@ int shirudo_image_bootstrap_read(const char *path,
fclose(file);
return rc;
}
+
+int shirudo_image_bootstrap_read(const char *path,
+ struct shirudo_image_bootstrap *boot,
+ bool initial)
+{
+ FILE *file = fopen(path, "re");
+
+ if (!file)
+ return -errno;
+ return bootstrap_parse(file, boot, initial);
+}
+
+/* A copy of the descriptor is read from its start; the caller keeps its own. */
+int shirudo_image_bootstrap_read_fd(int fd,
+ struct shirudo_image_bootstrap *boot,
+ bool initial)
+{
+ int copy, rc;
+ FILE *file;
+
+ copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+ if (copy < 0)
+ return -errno;
+ if (lseek(copy, 0, SEEK_SET) < 0) {
+ rc = -errno;
+ close(copy);
+ return rc;
+ }
+ file = fdopen(copy, "r");
+ if (!file) {
+ rc = -errno;
+ close(copy);
+ return rc;
+ }
+ return bootstrap_parse(file, boot, initial);
+}
diff --git a/util/image_bootstrap.h b/util/image_bootstrap.h
index 13c6aed6..a9973bd1 100644
--- a/util/image_bootstrap.h
+++ b/util/image_bootstrap.h
@@ -25,5 +25,8 @@ bool shirudo_image_path_valid(const char *path);
int shirudo_image_bootstrap_read(const char *path,
struct shirudo_image_bootstrap *boot,
bool initial);
+int shirudo_image_bootstrap_read_fd(int fd,
+ struct shirudo_image_bootstrap *boot,
+ bool initial);
#endif /* SHIRUDO_IMAGE_BOOTSTRAP_H */
diff --git a/util/image_config.c b/util/image_config.c
index fcf73c2a..41461cd1 100644
--- a/util/image_config.c
+++ b/util/image_config.c
@@ -16,6 +16,7 @@
#include "config.h"
#include "image_config.h"
+#include "io.h"
#include "malloc.h"
static const char *const relative[SHIRUDO_IMAGE_PATH_COUNT] = {
@@ -28,7 +29,6 @@ static const char *const relative[SHIRUDO_IMAGE_PATH_COUNT] = {
[SHIRUDO_IMAGE_REVOKED_KEYS] = "keys/revoked",
};
-static char paths[SHIRUDO_IMAGE_PATH_COUNT][PATH_MAX];
static struct shirudo_config effective;
static bool active;
static int config_fd = -1;
@@ -51,19 +51,36 @@ int shirudo_image_config_root_fd(void)
return config_fd;
}
-const char *shirudo_image_path(enum shirudo_image_path which)
+/*
+ * What the config image holds is opened from its verified root and
+ * never through a path: a path, /proc/<pid>/fd included, is root's to
+ * mount over before the exec.
+ */
+int shirudo_image_open(enum shirudo_image_path which, int flags)
{
if (which >= SHIRUDO_IMAGE_PATH_COUNT || config_fd < 0)
- return NULL;
- return paths[which];
+ return -ENOENT;
+ return open_beneath(config_fd, relative[which], flags);
}
-int shirudo_image_path_relative(char *out, __u64 cap, const char *path)
+int shirudo_image_open_relative(const char *path, int flags)
{
if (config_fd < 0 || !path || path[0] == '/' || strstr(path, ".."))
return -EINVAL;
- return strsprintf(out, cap, "/proc/self/fd/%d/%s", config_fd, path) < 0 ?
- -ENAMETOOLONG : 0;
+ return open_beneath(config_fd, path, flags);
+}
+
+/* The image's shirudo.conf, read through its root. */
+static int config_load_root(struct shirudo_config *cfg, int root_fd)
+{
+ int fd, rc;
+
+ fd = open_beneath(root_fd, "etc/shirudo/shirudo.conf", O_RDONLY);
+ if (fd < 0)
+ return fd;
+ rc = config_load_fd(cfg, fd, "config image shirudo.conf");
+ close(fd);
+ return rc;
}
static int validate_tree(int root_fd, dev_t device, __u32 depth,
@@ -193,7 +210,6 @@ int shirudo_image_config_validate(int root_fd)
{
struct shirudo_config candidate;
__u32 entries = 0;
- char path[64];
struct stat st;
int rc;
@@ -204,12 +220,8 @@ int shirudo_image_config_validate(int root_fd)
rc = validate_tree(root_fd, st.st_dev, 0, &entries);
if (!rc)
rc = validate_settings(root_fd);
- if (!rc && strsprintf(path, sizeof(path),
- "/proc/self/fd/%d/etc/shirudo/shirudo.conf",
- root_fd) < 0)
- rc = -ENAMETOOLONG;
if (!rc) {
- rc = config_load_path(&candidate, path);
+ rc = config_load_root(&candidate, root_fd);
if (!rc && (!candidate.map_export_valid ||
!candidate.tls_classical_hosts_valid))
rc = -EINVAL;
@@ -222,9 +234,7 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
int root_fd)
{
struct shirudo_config candidate;
- char path[PATH_MAX];
int fd = -1, rc;
- __u32 i;
candidate = *bootstrap;
if (root_fd >= 0) {
@@ -234,12 +244,7 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
fd = fcntl(root_fd, F_DUPFD_CLOEXEC, 3);
if (fd < 0)
return -errno;
- if (strsprintf(path, sizeof(path),
- "/proc/self/fd/%d/etc/shirudo/shirudo.conf", fd) < 0) {
- close(fd);
- return -ENAMETOOLONG;
- }
- rc = config_load_path(&candidate, path);
+ rc = config_load_root(&candidate, fd);
if (rc || !candidate.map_export_valid ||
!candidate.tls_classical_hosts_valid) {
close(fd);
@@ -261,13 +266,6 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
config_fd = fd;
effective = candidate;
active = true;
- for (i = 0; i < SHIRUDO_IMAGE_PATH_COUNT; i++) {
- if (strsprintf(paths[i], sizeof(paths[i]), "/proc/self/fd/%d/%s",
- config_fd, relative[i]) < 0) {
- shirudo_image_config_clear();
- return -ENAMETOOLONG;
- }
- }
return 0;
}
@@ -278,5 +276,4 @@ void shirudo_image_config_clear(void)
config_fd = -1;
active = false;
explicit_bzero(&effective, sizeof(effective));
- memset(paths, 0, sizeof(paths));
}
diff --git a/util/image_config.h b/util/image_config.h
index 85ac3047..0fa6aa0f 100644
--- a/util/image_config.h
+++ b/util/image_config.h
@@ -27,8 +27,9 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
int root_fd);
int shirudo_image_config_validate(int root_fd);
void shirudo_image_config_clear(void);
-const char *shirudo_image_path(enum shirudo_image_path which);
-int shirudo_image_path_relative(char *out, __u64 cap, const char *path);
+/* An entry of the config image, opened from its verified root; -errno. */
+int shirudo_image_open(enum shirudo_image_path which, int flags);
+int shirudo_image_open_relative(const char *path, int flags);
int shirudo_image_config_root_fd(void);
#endif /* SHIRUDO_IMAGE_CONFIG_H */
diff --git a/util/image_keys.c b/util/image_keys.c
index c2ae1c4a..aeed121b 100644
--- a/util/image_keys.c
+++ b/util/image_keys.c
@@ -49,7 +49,7 @@ static int revocations(int root_fd, bool apply, bool boot)
while ((entry = readdir(dir)) != NULL) {
__u8 *der = NULL;
__u8 digest[32], fingerprint[32], expected[32];
- char canonical[69], path[64], name[65];
+ char canonical[69], name[65];
size_t size = 0;
int fd;
@@ -82,10 +82,8 @@ static int revocations(int root_fd, bool apply, bool boot)
st.st_size <= 0 ||
st.st_size > SHIRUDO_KEYS_DER_MAX))
rc = -EINVAL;
- if (!rc && strsprintf(path, sizeof(path), "/proc/self/fd/%d", fd) < 0)
- rc = -ENAMETOOLONG;
if (!rc)
- rc = keys_read_der(path, &der, &size);
+ rc = keys_read_der_fd(fd, &der, &size);
close(fd);
if (!rc)
rc = keys_digest_der(der, size, digest,
@@ -108,7 +106,6 @@ static int revocations(int root_fd, bool apply, bool boot)
int shirudo_keys_validate_image_set(const char *primary, int root_fd,
struct shirudo_trusted_set *set)
{
- char keys[64], revoked[80];
int rc;
rc = shirudo_image_config_validate(root_fd);
@@ -116,10 +113,7 @@ int shirudo_keys_validate_image_set(const char *primary, int root_fd,
rc = revocations(root_fd, false, false);
if (rc)
return rc;
- if (strsprintf(keys, sizeof(keys), "/proc/self/fd/%d/keys", root_fd) < 0 ||
- strsprintf(revoked, sizeof(revoked), "%s/revoked", keys) < 0)
- return -ENAMETOOLONG;
- rc = shirudo_keys_load_image(primary, keys, revoked, true, set);
+ rc = shirudo_keys_load_image_root(primary, root_fd, true, set);
if (!rc && (!set->n || set->n_unvouched))
rc = -EKEYREJECTED;
return rc;
@@ -135,14 +129,13 @@ int shirudo_keys_check_image_set(const struct shirudo_trusted_set *set)
if (keyring < 0)
return keyring;
for (i = 0; i < set->n; i++) {
- __u8 *der = NULL;
+ const __u8 *der = NULL;
size_t size = 0;
- rc = keys_read_der(set->certs[i].path, &der, &size);
+ rc = keys_cert_der(&set->certs[i], &der, &size);
if (rc)
return rc;
rc = keyring_holds_cert(keyring, der, size);
- xfree(der, size);
if (rc != 1)
return rc < 0 ? rc : -ENOKEY;
}
diff --git a/util/image_runtime.c b/util/image_runtime.c
index d7f2413a..5f1f8ff9 100644
--- a/util/image_runtime.c
+++ b/util/image_runtime.c
@@ -256,7 +256,6 @@ static int open_runtime_file(const char *path, dev_t device)
static int load_runtime_config(bool trusted_boot)
{
struct stat root;
- char path[64];
int fd, rc;
if (!trusted_boot) {
@@ -275,12 +274,10 @@ static int load_runtime_config(bool trusted_boot)
fd = open_runtime_file(SHIRUDO_RUNTIME_CONF_PATH, root.st_dev);
if (fd < 0)
return fd;
- if (strsprintf(path, sizeof(path), "/proc/self/fd/%d", fd) < 0)
- rc = -ENAMETOOLONG;
- else
- rc = shirudo_image_bootstrap_read(path, &image.bootstrap, false);
+ rc = shirudo_image_bootstrap_read_fd(fd, &image.bootstrap, false);
if (!rc)
- rc = config_load_path(&image.bootstrap_config, path);
+ rc = config_load_fd(&image.bootstrap_config, fd,
+ SHIRUDO_RUNTIME_CONF_PATH);
close(fd);
if (rc)
return rc;
@@ -306,11 +303,7 @@ static int load_runtime_config(bool trusted_boot)
if (rc)
return rc;
- /* Keep the verified config certificate open through key enrollment. */
- if (strsprintf(image.bootstrap.certificate,
- sizeof(image.bootstrap.certificate), "/proc/self/fd/%d",
- image.certificate_fd) < 0)
- return -ENAMETOOLONG;
+ /* The verified config certificate stays open through key enrollment. */
return 0;
}
@@ -716,7 +709,8 @@ static int enroll_config_key(void)
size_t size = 0;
int rc;
- rc = keys_read_der(image.bootstrap.certificate, &der, &size);
+ rc = image.certificate_fd < 0 ? -EBADF :
+ keys_read_der_fd(image.certificate_fd, &der, &size);
if (rc)
return boot_keys_error("read config verification certificate", rc);
ring = shirudo_keyring_dm_verity_serial();
diff --git a/util/io.c b/util/io.c
index 07a14d6c..8ba3c7dc 100644
--- a/util/io.c
+++ b/util/io.c
@@ -8,8 +8,11 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/syscall.h>
#include <unistd.h>
+#include <linux/openat2.h>
+
#include <shirudo/misc.h>
#include "io.h"
@@ -40,59 +43,93 @@ int write_complete(int fd, const void *buf, size_t len)
return 0;
}
-int read_file(const char *path, size_t max, void *out, size_t *len)
+/*
+ * A path under a verified root resolves from its descriptor alone: no
+ * symbolic link, no magic link and no step out of it, so nothing root
+ * mounts or links elsewhere, /proc/<pid>/fd before an exec say, is
+ * followed.
+ */
+int open_beneath(int dirfd, const char *rel, int flags)
+{
+ struct open_how how = {
+ .flags = (__u64)flags | O_CLOEXEC,
+ .resolve = RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS |
+ RESOLVE_NO_MAGICLINKS,
+ };
+ long fd;
+
+ if (dirfd < 0 || !rel || !*rel || *rel == '/')
+ return -EINVAL;
+ fd = syscall(SYS_openat2, dirfd, rel, &how, sizeof(how));
+ return fd < 0 ? -errno : (int)fd;
+}
+
+/* The whole file behind the descriptor, from its start, xmalloc'd. */
+int read_fd(int fd, const char *label, size_t max, void *out, size_t *len)
{
struct stat st;
char *buf, *p;
size_t left;
- int fd, rc;
+ off_t off;
+ int rc;
- if (under(path, SHIRUDO_VAR_LIB)) {
- fd = seal_open_read(path);
- if (fd < 0)
- return fd;
- } else {
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- }
- if (fstat(fd, &st) != 0) {
- rc = -errno;
- goto out;
- }
- if (!S_ISREG(st.st_mode)) {
- rc = -EINVAL;
- goto out;
- }
- rc = check_trusted(fd, path, false);
+ if (fstat(fd, &st) != 0)
+ return -errno;
+ if (!S_ISREG(st.st_mode))
+ return -EINVAL;
+ rc = check_trusted(fd, label, false);
if (rc)
- goto out;
- if ((uintmax_t)st.st_size > max) {
- rc = -EFBIG;
- goto out;
- }
+ return rc;
+ if ((uintmax_t)st.st_size > max)
+ return -EFBIG;
buf = xmalloc((size_t)st.st_size + 1);
- if (!buf) {
- rc = -ENOMEM;
- goto out;
- }
- for (p = buf, left = (size_t)st.st_size; left; ) {
- ssize_t n = read(fd, p, left);
+ if (!buf)
+ return -ENOMEM;
+ for (p = buf, left = (size_t)st.st_size, off = 0; left; ) {
+ ssize_t n = pread(fd, p, left, off);
if (n < 0 && errno == EINTR)
continue;
if (n <= 0) {
- rc = n < 0 ? -errno : -EIO;
xfree(buf, (size_t)st.st_size + 1);
- goto out;
+ return n < 0 ? -errno : -EIO;
}
p += n;
+ off += n;
left -= (size_t)n;
}
*(char **)out = buf;
*len = (size_t)st.st_size;
- rc = 0;
-out:
+ return 0;
+}
+
+int read_file(const char *path, size_t max, void *out, size_t *len)
+{
+ int fd, rc;
+
+ if (under(path, SHIRUDO_VAR_LIB)) {
+ fd = seal_open_read(path);
+ if (fd < 0)
+ return fd;
+ } else {
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ }
+ rc = read_fd(fd, path, max, out, len);
+ close(fd);
+ return rc;
+}
+
+int read_file_at(int dirfd, const char *rel, size_t max, void *out,
+ size_t *len)
+{
+ int fd, rc;
+
+ fd = open_beneath(dirfd, rel, O_RDONLY);
+ if (fd < 0)
+ return fd;
+ rc = read_fd(fd, rel, max, out, len);
close(fd);
return rc;
}
diff --git a/util/io.h b/util/io.h
index a646ac9f..16ccc56e 100644
--- a/util/io.h
+++ b/util/io.h
@@ -15,6 +15,10 @@ int write_complete(int fd, const void *buf, size_t len);
* char pointer.
*/
int read_file(const char *path, size_t max, void *out, size_t *len);
+int read_fd(int fd, const char *label, size_t max, void *out, size_t *len);
+int read_file_at(int dirfd, const char *rel, size_t max, void *out,
+ size_t *len);
+int open_beneath(int dirfd, const char *rel, int flags);
/*
* Whether the file open on fd at path may steer what shirudo trusts.
diff --git a/util/keys.c b/util/keys.c
index cdf62317..704e22db 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -21,6 +21,7 @@
#include "hex.h"
#include "io.h"
#include "keys.h"
+#include "macros.h"
#include "malloc.h"
#include "paths.h"
#include "seal.h"
@@ -30,13 +31,40 @@
struct keys_candidate {
char path[PATH_MAX];
+ char name[256]; /* in dirfd, when that is not -1 */
__u8 *der;
size_t der_sz;
__u8 sha256[32];
__u8 fingerprint[32];
+ int dirfd;
bool done;
};
+int keys_read_der_fd(int fd, __u8 **out, size_t *out_sz)
+{
+ int rc = read_fd(fd, "certificate", SHIRUDO_KEYS_DER_MAX, out, out_sz);
+
+ if (!rc && !*out_sz) {
+ xfree(*out, 1);
+ *out = NULL;
+ return -EINVAL;
+ }
+ return rc;
+}
+
+int keys_read_der_at(int dirfd, const char *name, __u8 **out,
+ size_t *out_sz)
+{
+ int fd, rc;
+
+ fd = open_beneath(dirfd, name, O_RDONLY);
+ if (fd < 0)
+ return fd;
+ rc = keys_read_der_fd(fd, out, out_sz);
+ close(fd);
+ return rc;
+}
+
int keys_read_der(const char *path, __u8 **out,
size_t *out_sz)
{
@@ -96,6 +124,87 @@ int keys_digest_der(const __u8 *der, size_t der_sz,
return rc;
}
+/*
+ * The DER of every certificate a set took, by its digest. Whoever needs
+ * the bytes later, to enrol, to look in the keyring or to check a vouch,
+ * takes them from here: a path reopened then may name other bytes, and
+ * a member read from a config image has no path at all.
+ */
+struct der_entry {
+ __u8 sha256[32];
+ __u8 *der;
+ size_t len;
+};
+
+static struct der_entry der_store[8 * SHIRUDO_KEYS_MAX];
+static size_t der_stored;
+
+static int der_keep(const __u8 sha256[32], const __u8 *der, size_t len)
+{
+ struct der_entry *e;
+ size_t i;
+
+ for (i = 0; i < der_stored; i++) {
+ if (!memcmp(der_store[i].sha256, sha256, 32))
+ return 0;
+ }
+ if (der_stored >= ARRAY_SIZE(der_store))
+ return -E2BIG;
+ e = &der_store[der_stored];
+ e->der = xmalloc(len);
+ if (!e->der)
+ return -ENOMEM;
+ memcpy(e->der, der, len);
+ memcpy(e->sha256, sha256, 32);
+ e->len = len;
+ der_stored++;
+ return 0;
+}
+
+int keys_cert_der(const struct shirudo_trusted_cert *c, const __u8 **der,
+ size_t *len)
+{
+ size_t i;
+
+ for (i = 0; i < der_stored; i++) {
+ if (!memcmp(der_store[i].sha256, c->sha256, 32)) {
+ *der = der_store[i].der;
+ *len = der_store[i].len;
+ return 0;
+ }
+ }
+ return -ENOKEY;
+}
+
+int keys_cert_read(const struct shirudo_trusted_cert *c,
+ struct shirudo_cert *cert)
+{
+ const __u8 *der;
+ size_t len;
+ int rc;
+
+ rc = keys_cert_der(c, &der, &len);
+ return rc ? rc : cert_read_der(der, len, cert);
+}
+
+/* The certificate at path, its digests, and its bytes kept for the set. */
+static int keys_take_file(const char *path, __u8 sha256[32],
+ __u8 fingerprint[32])
+{
+ size_t der_sz = 0;
+ __u8 *der = NULL;
+ int rc;
+
+ rc = keys_read_der(path, &der, &der_sz);
+ if (rc)
+ return rc;
+ rc = keys_digest_der(der, der_sz, sha256, fingerprint);
+ if (!rc)
+ rc = der_keep(sha256, der, der_sz);
+ xfree(der, der_sz);
+ return rc;
+}
+
int keys_digest_file(const char *path, __u8 sha256[32],
__u8 fingerprint[32])
{
@@ -134,6 +243,17 @@ bool keys_is_revoked(const char *revoked_dir,
return access(path, F_OK) == 0;
}
+static bool keys_is_revoked_at(int revoked_fd, const __u8 fingerprint[32])
+{
+ char name[64 + sizeof(".der")];
+
+ if (revoked_fd < 0)
+ return false;
+ hex_encode(fingerprint, 32, name);
+ memcpy(name + 64, ".der", sizeof(".der"));
+ return faccessat(revoked_fd, name, F_OK, 0) == 0;
+}
+
int keys_vouch_payload(const __u8 *der, size_t der_sz,
__u8 **out, size_t *out_sz)
{
@@ -166,7 +286,7 @@ int keys_verify_vouch(const struct shirudo_trusted_set *set,
for (i = 0; i < set->n && rc; i++) {
struct shirudo_cert cert = {};
- if (cert_read_file(set->certs[i].path, &cert))
+ if (keys_cert_read(&set->certs[i], &cert))
continue;
if (verify_detached(payload, payload_sz, sig, sig_sz,
&cert) == 0) {
@@ -199,12 +319,18 @@ static bool set_holds(const struct shirudo_trusted_set *set,
static int set_add(struct shirudo_trusted_set *set, const char *path,
const __u8 sha256[32], const __u8 fingerprint[32],
- const __u8 voucher[32])
+ const __u8 voucher[32], const __u8 *der, size_t der_sz)
{
struct shirudo_trusted_cert *c;
+ int rc;
if (set->n >= SHIRUDO_KEYS_MAX)
return -E2BIG;
+ if (der) {
+ rc = der_keep(sha256, der, der_sz);
+ if (rc)
+ return rc;
+ }
c = &set->certs[set->n];
memset(c, 0, sizeof(*c));
if (strscpy(c->path, path, sizeof(c->path)) < 0)
@@ -240,6 +366,22 @@ static size_t count_der(const char *dir)
return (size_t)n;
}
+static size_t count_der_at(int dirfd)
+{
+ struct dirent **names = NULL;
+ int n, i;
+
+ if (dirfd < 0)
+ return 0;
+ n = scandirat(dirfd, ".", &names, select_der, NULL);
+ if (n < 0)
+ return 0;
+ for (i = 0; i < n; i++)
+ libc_free(names[i]);
+ libc_free(names);
+ return (size_t)n;
+}
+
static int vouch_path_for(const char *der_path, char *out, size_t cap)
{
size_t n = strlen(der_path) - 4;
@@ -275,8 +417,11 @@ static int admit_vouched(struct shirudo_trusted_set *set,
c->done = true;
continue;
}
- if (vouch_path_for(c->path, vouch, sizeof(vouch)) ||
- keys_read_der(vouch, &sig, &sig_sz))
+ if (vouch_path_for(c->dirfd >= 0 ? c->name : c->path,
+ vouch, sizeof(vouch)) ||
+ (c->dirfd >= 0 ?
+ keys_read_der_at(c->dirfd, vouch, &sig, &sig_sz) :
+ keys_read_der(vouch, &sig, &sig_sz)))
continue;
rc = keys_verify_vouch(set, c->der, c->der_sz,
sig, sig_sz, voucher);
@@ -284,7 +429,7 @@ static int admit_vouched(struct shirudo_trusted_set *set,
if (rc)
continue;
rc = set_add(set, c->path, c->sha256, c->fingerprint,
- voucher);
+ voucher, c->der, c->der_sz);
if (rc)
return rc;
c->done = true;
@@ -333,6 +478,7 @@ static int scan_trusted(const char *keys_dir, const char *revoked_dir,
rc = -E2BIG;
break;
}
+ c->dirfd = -1;
if (strsprintf(c->path, sizeof(c->path), "%s/%s", dir,
names[i]->d_name) < 0) {
rc = -ENAMETOOLONG;
@@ -361,8 +507,61 @@ static int scan_trusted(const char *keys_dir, const char *revoked_dir,
return rc;
}
+/*
+ * The same scan from the descriptor of the keys directory, which the
+ * trusted directory's descriptor outlives into admit_vouched: the
+ * caller closes it after.
+ */
+static int scan_trusted_at(int keys_fd, const char *display,
+ struct keys_candidate *cand, size_t *n_cand,
+ int *trusted_fd)
+{
+ struct dirent **names = NULL;
+ int n, i, rc = 0;
+
+ *trusted_fd = open_beneath(keys_fd, "trusted", O_RDONLY | O_DIRECTORY);
+ if (*trusted_fd == -ENOENT)
+ return 0;
+ if (*trusted_fd < 0)
+ return *trusted_fd;
+ n = scandirat(*trusted_fd, ".", &names, select_der, alphasort);
+ if (n < 0)
+ return -errno;
+ for (i = 0; i < n && !rc; i++) {
+ struct keys_candidate *c = &cand[*n_cand];
+
+ if (*n_cand >= KEYS_SCAN_MAX) {
+ rc = -E2BIG;
+ break;
+ }
+ c->dirfd = *trusted_fd;
+ if (strscpy(c->name, names[i]->d_name, sizeof(c->name)) < 0 ||
+ strsprintf(c->path, sizeof(c->path), "%s/trusted/%s",
+ display, names[i]->d_name) < 0) {
+ rc = -ENAMETOOLONG;
+ break;
+ }
+ rc = keys_read_der_at(*trusted_fd, c->name, &c->der,
+ &c->der_sz);
+ if (rc)
+ break;
+ rc = keys_digest_der(c->der, c->der_sz, c->sha256,
+ c->fingerprint);
+ if (rc) {
+ xfree(c->der, c->der_sz);
+ c->der = NULL;
+ break;
+ }
+ (*n_cand)++;
+ }
+ for (i = 0; i < n; i++)
+ libc_free(names[i]);
+ libc_free(names);
+ return rc;
+}
+
static int load_set(const char *primary, const char *keys_dir,
- const char *revoked_dir, bool with_trusted, bool image,
+ const char *revoked_dir, bool with_trusted,
struct shirudo_trusted_set *set)
{
struct keys_candidate *cand = NULL;
@@ -378,74 +577,124 @@ static int load_set(const char *primary, const char *keys_dir,
return -ENAMETOOLONG;
revoked_dir = revoked;
}
- rc = keys_digest_file(primary, sha256, fingerprint);
+ rc = keys_take_file(primary, sha256, fingerprint);
if (rc)
return rc;
- if (image || !keys_is_revoked(revoked_dir, fingerprint)) {
- rc = set_add(set, primary, sha256, fingerprint, NULL);
+ if (!keys_is_revoked(revoked_dir, fingerprint)) {
+ rc = set_add(set, primary, sha256, fingerprint, NULL, NULL, 0);
if (rc)
return rc;
}
set->n_revoked = count_der(revoked_dir);
if (!with_trusted)
- goto filter;
+ goto sort;
cand = xcalloc(KEYS_SCAN_MAX, sizeof(*cand));
if (!cand)
return -ENOMEM;
- rc = scan_trusted(keys_dir, image ? NULL : revoked_dir, set, cand,
- &n_cand);
+ rc = scan_trusted(keys_dir, revoked_dir, set, cand, &n_cand);
if (!rc)
rc = admit_vouched(set, cand, n_cand);
candidates_free(cand, n_cand);
if (rc)
return rc;
-filter:
- if (image) {
- size_t i, kept = 0;
-
- for (i = 0; i < set->n; i++)
- if (!keys_is_revoked(revoked_dir,
- set->certs[i].fingerprint))
- set->certs[kept++] = set->certs[i];
- set->n = kept;
- }
+sort:
if (set->n > 1)
qsort(set->certs, set->n, sizeof(set->certs[0]), cert_cmp);
return 0;
}
+/*
+ * A config image's set: the primary and every vouched certificate under
+ * keys/trusted, less what keys/revoked names, all read through the
+ * image's descriptor. Historical vouches count before revocation, so a
+ * revoked certificate's vouch for another still admits it.
+ */
+static int load_set_at(const char *primary, int keys_fd, const char *display,
+ bool with_trusted, struct shirudo_trusted_set *set)
+{
+ struct keys_candidate *cand = NULL;
+ __u8 sha256[32], fingerprint[32];
+ int revoked_fd, trusted_fd = -1;
+ size_t n_cand = 0, i, kept = 0;
+ int rc;
+
+ memset(set, 0, sizeof(*set));
+ rc = keys_take_file(primary, sha256, fingerprint);
+ if (rc)
+ return rc;
+ rc = set_add(set, primary, sha256, fingerprint, NULL, NULL, 0);
+ if (rc)
+ return rc;
+ revoked_fd = open_beneath(keys_fd, "revoked", O_RDONLY | O_DIRECTORY);
+ set->n_revoked = count_der_at(revoked_fd);
+ if (with_trusted) {
+ cand = xcalloc(KEYS_SCAN_MAX, sizeof(*cand));
+ if (!cand) {
+ rc = -ENOMEM;
+ goto out;
+ }
+ rc = scan_trusted_at(keys_fd, display, cand, &n_cand,
+ &trusted_fd);
+ if (!rc)
+ rc = admit_vouched(set, cand, n_cand);
+ candidates_free(cand, n_cand);
+ if (rc)
+ goto out;
+ }
+ for (i = 0; i < set->n; i++)
+ if (!keys_is_revoked_at(revoked_fd, set->certs[i].fingerprint))
+ set->certs[kept++] = set->certs[i];
+ set->n = kept;
+ if (set->n > 1)
+ qsort(set->certs, set->n, sizeof(set->certs[0]), cert_cmp);
+out:
+ if (trusted_fd >= 0)
+ close(trusted_fd);
+ if (revoked_fd >= 0)
+ close(revoked_fd);
+ return rc;
+}
+
int shirudo_keys_load_split(const char *primary, const char *keys_dir,
const char *revoked_dir, bool with_trusted,
struct shirudo_trusted_set *set)
{
- return load_set(primary, keys_dir, revoked_dir, with_trusted, false,
- set);
+ return load_set(primary, keys_dir, revoked_dir, with_trusted, set);
}
-int shirudo_keys_load_image(const char *primary, const char *keys_dir,
- const char *revoked_dir, bool with_trusted,
- struct shirudo_trusted_set *set)
+int shirudo_keys_load_image_at(const char *primary, int keys_fd,
+ bool with_trusted,
+ struct shirudo_trusted_set *set)
{
- return load_set(primary, keys_dir, revoked_dir, with_trusted, true,
- set);
+ return load_set_at(primary, keys_fd, "keys", with_trusted, set);
+}
+
+int shirudo_keys_load_image_root(const char *primary, int root_fd,
+ bool with_trusted,
+ struct shirudo_trusted_set *set)
+{
+ int keys_fd, rc;
+
+ keys_fd = open_beneath(root_fd, "keys", O_RDONLY | O_DIRECTORY);
+ if (keys_fd < 0)
+ return keys_fd;
+ rc = shirudo_keys_load_image_at(primary, keys_fd, with_trusted, set);
+ close(keys_fd);
+ return rc;
}
int keys_load(const char *keys_dir, const char *revoked_dir,
bool with_trusted, struct shirudo_trusted_set *set)
{
- char keys[PATH_MAX], primary[PATH_MAX];
- int rc;
+ char primary[PATH_MAX];
if (shirudo_image_config_root_fd() >= 0 &&
(!strcmp(keys_dir, SHIRUDO_KEYS_DIR) ||
- !strcmp(keys_dir, SHIRUDO_RUNTIME_DIR "/keys"))) {
- rc = shirudo_image_path_relative(keys, sizeof(keys), "keys");
- if (rc)
- return rc;
- return shirudo_keys_load_image(SHIRUDO_RUNTIME_CERT_PATH, keys,
- SHIRUDO_KEYS_REVOKED_DIR, with_trusted, set);
- }
+ !strcmp(keys_dir, SHIRUDO_RUNTIME_DIR "/keys")))
+ return shirudo_keys_load_image_root(SHIRUDO_RUNTIME_CERT_PATH,
+ shirudo_image_config_root_fd(),
+ with_trusted, set);
if (strsprintf(primary, sizeof(primary), "%s/%s", keys_dir, SHIRUDO_CERT_FILE) < 0)
return -ENAMETOOLONG;
return shirudo_keys_load_split(primary, keys_dir, revoked_dir,
diff --git a/util/keys.h b/util/keys.h
index 46a7cc20..97623572 100644
--- a/util/keys.h
+++ b/util/keys.h
@@ -59,14 +59,36 @@ int shirudo_keys_load_split(const char *primary, const char *keys_dir,
const char *revoked_dir, bool with_trusted,
struct shirudo_trusted_set *set);
-/* Validate historical vouches before excluding revoked BPF signing keys. */
-int shirudo_keys_load_image(const char *primary, const char *keys_dir,
- const char *revoked_dir, bool with_trusted,
- struct shirudo_trusted_set *set);
+/*
+ * The set of a config image, read through its root descriptor; historical
+ * vouches are validated before revoked signing keys are excluded.
+ */
+int shirudo_keys_load_image_at(const char *primary, int keys_fd,
+ bool with_trusted,
+ struct shirudo_trusted_set *set);
+/* The same from the image root, whose keys directory is opened beneath it. */
+int shirudo_keys_load_image_root(const char *primary, int root_fd,
+ bool with_trusted,
+ struct shirudo_trusted_set *set);
+
+struct shirudo_cert;
+
+/*
+ * A member's DER as the loader read it, owned by the store, or its
+ * parsed certificate; -ENOKEY for a member no loader of this process
+ * admitted.
+ */
+int keys_cert_der(const struct shirudo_trusted_cert *c, const __u8 **der,
+ size_t *len);
+int keys_cert_read(const struct shirudo_trusted_cert *c,
+ struct shirudo_cert *cert);
/* A whole file of at most SHIRUDO_KEYS_DER_MAX bytes, xmalloc'd. */
int keys_read_der(const char *path, __u8 **out,
size_t *out_sz);
+int keys_read_der_fd(int fd, __u8 **out, size_t *out_sz);
+int keys_read_der_at(int dirfd, const char *name, __u8 **out,
+ size_t *out_sz);
int keys_digest_der(const __u8 *der, size_t der_sz,
__u8 sha256[32], __u8 fingerprint[32]);
diff --git a/util/paths.h b/util/paths.h
index d37511ea..b9fd50ba 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -24,12 +24,8 @@
#define SHIRUDO_CERT_FILE "cert.der"
#define SHIRUDO_CERT_PATH SHIRUDO_KEYS_DIR "/" SHIRUDO_CERT_FILE
/* Further certificates enrolled beside cert.der, and the revoked ones. */
-#define SHIRUDO_KEYS_TRUSTED_DIR \
- (shirudo_image_path(SHIRUDO_IMAGE_TRUSTED_KEYS) ?: \
- SHIRUDO_KEYS_DIR "/trusted")
-#define SHIRUDO_KEYS_REVOKED_DIR \
- (shirudo_image_path(SHIRUDO_IMAGE_REVOKED_KEYS) ?: \
- SHIRUDO_KEYS_DIR "/revoked")
+#define SHIRUDO_KEYS_TRUSTED_DIR SHIRUDO_KEYS_DIR "/trusted"
+#define SHIRUDO_KEYS_REVOKED_DIR SHIRUDO_KEYS_DIR "/revoked"
/* The initrd's own copy of the keys, measured with the initrd. */
#define SHIRUDO_INITRD_KEYS_DIR "/etc/shirudo/keys"
#define SHIRUDO_INITRD_CERT_PATH SHIRUDO_INITRD_KEYS_DIR "/" SHIRUDO_CERT_FILE
@@ -47,22 +43,16 @@
#define SHIRUDO_BPFFS_DIR SHIRUDO_RUN_DIR "/bpffs"
/* The init record written from the initrd, until the root adopts it. */
#define SHIRUDO_EVENTLOG_PENDING_PATH SHIRUDO_RUN_DIR "/eventlog.pending"
-#define SHIRUDO_BPM_ETC_DIR \
- (shirudo_image_path(SHIRUDO_IMAGE_ETC) ?: "/etc/shirudo/bpm")
-#define SHIRUDO_BPM_REGISTRIES_DIR \
- (shirudo_image_path(SHIRUDO_IMAGE_REGISTRIES) ?: \
- "/etc/shirudo/bpm/registries.d")
-#define SHIRUDO_BPM_KEYS_DIR \
- (shirudo_image_path(SHIRUDO_IMAGE_REPOSITORY_KEYS) ?: \
- "/etc/shirudo/bpm/keys")
+/* On a target with a config image these are the image's, opened from its root. */
+#define SHIRUDO_BPM_ETC_DIR "/etc/shirudo/bpm"
+#define SHIRUDO_BPM_REGISTRIES_DIR "/etc/shirudo/bpm/registries.d"
+#define SHIRUDO_BPM_KEYS_DIR "/etc/shirudo/bpm/keys"
#define SHIRUDO_BPM_VAR_DIR SHIRUDO_VAR_LIB "/bpm"
#define SHIRUDO_BPM_REPOS_DIR SHIRUDO_BPM_VAR_DIR "/repos"
#define SHIRUDO_BPM_PACKAGES_DIR SHIRUDO_BPM_VAR_DIR "/packages"
#define SHIRUDO_BPM_STAGING_DIR SHIRUDO_BPM_VAR_DIR "/staging"
#define SHIRUDO_BPM_STATE_PATH SHIRUDO_BPM_VAR_DIR "/state.json"
-#define SHIRUDO_BPM_TREE_ETC_PATH \
- (shirudo_image_path(SHIRUDO_IMAGE_TREE_CONF) ?: \
- "/etc/shirudo/bpm/upatch-tree.conf")
+#define SHIRUDO_BPM_TREE_ETC_PATH "/etc/shirudo/bpm/upatch-tree.conf"
#define SHIRUDO_BPM_TREE_VAR_DIR SHIRUDO_BPM_VAR_DIR "/upatch-tree"
#define SHIRUDO_BPM_TREE_STATE_PATH SHIRUDO_BPM_TREE_VAR_DIR "/state.json"
#define SHIRUDO_BPM_TREE_EVENTLOG_PATH SHIRUDO_BPM_TREE_VAR_DIR "/tree-eventlog"
diff --git a/util/preflight.c b/util/preflight.c
index 7e3678e7..06c33675 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -188,42 +188,6 @@ static void check_cert(struct shirudo_preflight_result *r)
shirudo_cert_path());
}
-static int read_cert(const char *path, __u8 **out, size_t *out_sz)
-{
- struct stat st;
- __u8 *buf;
- ssize_t n;
- int fd;
-
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) < 0) {
- int rc = -errno;
-
- close(fd);
- return rc;
- }
- if (st.st_size <= 0 || st.st_size > PREFLIGHT_CERT_MAX) {
- close(fd);
- return -EINVAL;
- }
- buf = xmalloc((size_t)st.st_size);
- if (!buf) {
- close(fd);
- return -ENOMEM;
- }
- n = read(fd, buf, (size_t)st.st_size);
- close(fd);
- if (n != st.st_size) {
- xfree(buf, (size_t)st.st_size);
- return -EIO;
- }
- *out = buf;
- *out_sz = (size_t)st.st_size;
- return 0;
-}
-
/*
* shirudo's trust anchor is ML-DSA-87 only: a classical certificate on
* disk is refused before any kernel state is touched, so a fleet cannot
@@ -258,7 +222,7 @@ static void check_cert_algorithm(struct shirudo_preflight_result *r)
char name[64];
int rc;
- if (cert_read_file(set.certs[i].path, &cert)) {
+ if (keys_cert_read(&set.certs[i], &cert)) {
fail(r, c, "%s: cannot parse", set.certs[i].path);
return;
}
@@ -303,12 +267,12 @@ static void check_cert_kernel_algo(struct shirudo_preflight_result *r)
return;
}
for (i = 0; i < set.n; i++) {
+ const __u8 *der = NULL;
size_t der_sz = 0;
- __u8 *der = NULL;
long key;
int rc;
- rc = read_cert(set.certs[i].path, &der, &der_sz);
+ rc = keys_cert_der(&set.certs[i], &der, &der_sz);
if (rc) {
fail(r, c, "%s: %s", set.certs[i].path, strerror(-rc));
return;
@@ -316,7 +280,6 @@ static void check_cert_kernel_algo(struct shirudo_preflight_result *r)
key = syscall(__NR_add_key, "asymmetric", "", der, der_sz,
(long)KEY_SPEC_THREAD_KEYRING);
rc = key < 0 ? errno : 0;
- xfree(der, der_sz);
syscall(__NR_keyctl, KEYCTL_CLEAR,
(long)KEY_SPEC_THREAD_KEYRING, 0L, 0L, 0L);
if (!rc)