Skip to content

eBPF-based security platform

io: check who may read a secret on the file, not along its path again

Daniel Borkmann5471bc5a8695
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 10fb3703..744c2a3d 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -402,7 +402,7 @@ already, or should. The basics:
| Smaller or larger | `min()`, `max()` (`<shirudo/misc.h>`) |
| Memory | `xmalloc()`, `xcalloc()`, `xreallocarray()`, `xstrndup()`, `xasprintf()`, `xfree(p, size)` (`util/malloc.h`) |
| Secrets | `memzero_explicit()`, `memneq_explicit()` |
-| Files | `read_file()`, `read_fd()`, `write_complete()`, `append_line()`, `lines_len()`, `open_beneath()`, `lock_exclusive()` (`util/io.h`); `open_trusted()` to open by name a file under `/etc/shirudo` or `/var/lib/shirudo` that steers what shirudo trusts, never `open()` and `check_trusted()` |
+| Files | `read_file()`, `read_fd()`, `write_complete()`, `append_line()`, `lines_len()`, `open_beneath()`, `lock_exclusive()` (`util/io.h`); `open_trusted()` to open by name a file under `/etc/shirudo` or `/var/lib/shirudo` that steers what shirudo trusts, never `open()` and `check_trusted()`, and `check_secret()` once such a file turns out to hold a secret |
| Sealed state and paths root names | `seal_open()`, `seal_lock()`, `seal_append_line()`, `seal_write_file()`; `seal_output_create()` and `seal_output_commit()` for a report or export (`util/seal.h`) |
| Bytes on the wire | `get_le32()`, `put_be32()`, `le32_to_cpu()` and kin (`<shirudo/byteorder.h>`), `hex_encode()`, `hex_decode()`, `hex_decode_max()` (`util/hex.h`) |
| SHA-256 | `sha256_buffer()`, or `sha256_init()`, `sha256_update()` and `sha256_final()` over pieces (`util/crypto.h`); `sha256_digest_encode()` and `sha256_digest_decode()` for the `sha256:<hex>` form (`util/hex.h`), `bpm_sha256_digest()` to hash and write it at once (`util/bpm_oci.h`) |
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 2f849485..2f47e2df 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -327,7 +327,7 @@ static FILE *source_open(const char *path)
{
return source_stream(image_config_active() ?
image_open_relative(path + 1, O_RDONLY) :
- open_trusted(path, O_RDONLY, false));
+ open_trusted(path, O_RDONLY));
}
static DIR *registries_opendir(void)
@@ -368,7 +368,7 @@ static FILE *tree_conf_open(void)
{
return source_stream(image_config_active() ?
image_open(IMAGE_TREE_CONF, O_RDONLY) :
- open_trusted(BPM_TREE_ETC_PATH, O_RDONLY, false));
+ open_trusted(BPM_TREE_ETC_PATH, O_RDONLY));
}
static int load_source(const char *name, struct bpm_source *s)
diff --git a/builtin-keys.c b/builtin-keys.c
index e12f41db..e2c9b4ee 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -50,7 +50,7 @@
static X509 *read_cert_der(const char *path)
{
- int fd = open_trusted(path, O_RDONLY, false);
+ int fd = open_trusted(path, O_RDONLY);
BIO *bio;
X509 *c;
diff --git a/testing/unit/io.c b/testing/unit/io.c
index da7e0aee..5f452227 100644
--- a/testing/unit/io.c
+++ b/testing/unit/io.c
@@ -192,7 +192,10 @@ static void make_link(const char *target, const char *name)
ASSERT_EQ(symlinkat(target, etc_fd, name), 0);
}
-/* check_trusted() of the file opened as one name and asked about as another. */
+/*
+ * check_trusted() of the file opened as one name and asked about as
+ * another, and with secret check_secret() of a file that passed.
+ */
static int trusted(const char *opened, const char *asked, bool secret)
{
int fd = openat(etc_fd, opened, O_RDONLY | O_CLOEXEC), ret;
@@ -200,7 +203,9 @@ static int trusted(const char *opened, const char *asked, bool secret)
ASSERT_TRUE(fd >= 0);
ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/%s", etc_dir, asked) > 0);
- ret = check_trusted(fd, path, secret);
+ ret = check_trusted(fd, path);
+ if (!ret && secret)
+ ret = check_secret(fd, path);
close(fd);
return ret;
}
@@ -302,7 +307,7 @@ static int trusted_fd(int fd, const char *asked)
ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/%s", etc_dir,
asked) > 0);
- return check_trusted(fd, path, false);
+ return check_trusted(fd, path);
}
static void need_root(const char *reason)
@@ -401,6 +406,30 @@ static void check_trusted_wants_the_file_the_path_names(void)
remove_etc();
}
+/* A secret's readers are the file's own to say, where its name went or not. */
+static void check_secret_asks_the_file_alone(void)
+{
+ char path[PATH_MAX];
+ int fd;
+
+ make_etc();
+ make_dir("keys", 0700);
+ replace_mode("keys/auth.conf", "a\n", 0600);
+ ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/keys/auth.conf",
+ etc_dir) > 0);
+ fd = openat(etc_fd, "keys/auth.conf", O_RDONLY | O_CLOEXEC);
+ ASSERT_TRUE(fd >= 0);
+ EXPECT_EQ(check_trusted(fd, path), 0);
+ replace_mode("keys/auth.conf", "b\n", 0600);
+ EXPECT_EQ(check_trusted(fd, path), -ESTALE);
+ EXPECT_EQ(check_secret(fd, path), 0);
+ ASSERT_EQ(fchmod(fd, 0640), 0);
+ EXPECT_EQ(check_secret(fd, path), -EPERM);
+ EXPECT_EQ(check_secret(fd, "/tmp/elsewhere.conf"), 0);
+ close(fd);
+ remove_etc();
+}
+
/*
* open_checked() with an open that renames a new file over the one it
* opened, for its first race.renames opens: the race between the open
@@ -429,7 +458,7 @@ static int open_racing(const char *name, mode_t mode, int renames)
race = (struct race){ .name = name, .mode = mode, .renames = renames };
ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/%s", etc_dir,
name) > 0);
- return open_checked(path, O_RDONLY, false, open_then_replace);
+ return open_checked(path, O_RDONLY, open_then_replace);
}
static void open_trusted_reopens_a_file_renamed_over(void)
@@ -810,6 +839,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(check_trusted_follows_a_link_nobody_else_made),
UNIT_CASE(check_trusted_refuses_a_link_others_can_replace),
UNIT_CASE(check_trusted_wants_the_file_the_path_names),
+ UNIT_CASE(check_secret_asks_the_file_alone),
UNIT_CASE(open_trusted_reopens_a_file_renamed_over),
UNIT_CASE(open_trusted_gives_up_on_a_name_that_keeps_changing),
UNIT_CASE(open_trusted_checks_each_file_it_opens),
diff --git a/util/config.c b/util/config.c
index a14ef7ad..f8f66ee3 100644
--- a/util/config.c
+++ b/util/config.c
@@ -148,8 +148,7 @@ static void set_owner_auth(struct config *cfg, const char *val)
/*
* The stream, which passed check_trusted(), is consumed and closed; the
- * file is named for the log. -ESTALE when it was renamed over before the
- * check of a file holding the TPM owner's authorization.
+ * file is named for the log.
*/
static int config_parse(struct config *cfg, FILE *f,
const char *path)
@@ -158,7 +157,7 @@ static int config_parse(struct config *cfg, FILE *f,
char buffer[BUFSIZ];
size_t capacity = 0;
char *line = NULL;
- int got, check, ret = 0;
+ int got, ret = 0;
/* stdio's own buffer would keep the owner's authorization after fclose(). */
setvbuf(f, buffer, _IOFBF, sizeof(buffer));
@@ -273,13 +272,11 @@ static int config_parse(struct config *cfg, FILE *f,
if (ferror(f) && !ret)
ret = -EIO;
/* The TPM owner's authorization is for no other user's eyes. */
- check = cfg->tpm_owner_auth_len ?
- check_trusted(fileno(f), path, true) : 0;
- if (check) {
+ if (cfg->tpm_owner_auth_len && check_secret(fileno(f), path)) {
memzero_explicit(cfg->tpm_owner_auth,
sizeof(cfg->tpm_owner_auth));
cfg->tpm_owner_auth_len = 0;
- ret = check == -ESTALE ? check : -EPERM;
+ ret = -EPERM;
}
if (line) {
/* The owner's authorization may have been read into it. */
@@ -308,14 +305,13 @@ static int config_finish(struct config *cfg, int ret)
return ret;
}
-/* The settings at path, read once, or 0 with none there. */
-static int config_load_once(struct config *cfg, const char *path)
+/* The settings at path, or 0 with none there. */
+static int config_read(struct config *cfg, const char *path)
{
int fd, ret;
FILE *f;
- config_defaults(cfg);
- fd = open_trusted(path, O_RDONLY, false);
+ fd = open_trusted(path, O_RDONLY);
if (fd < 0)
return fd == -ENOENT ? 0 : fd;
f = fdopen(fd, "r");
@@ -329,16 +325,10 @@ static int config_load_once(struct config *cfg, const char *path)
int config_load_path(struct config *cfg, const char *path)
{
- int attempt, ret;
-
if (!cfg || !path)
return -EINVAL;
- for (attempt = 0;; attempt++) {
- ret = config_load_once(cfg, path);
- if (!trusted_reopen(ret, attempt))
- break;
- }
- return config_finish(cfg, ret);
+ config_defaults(cfg);
+ return config_finish(cfg, config_read(cfg, path));
}
/* A copy of the descriptor is read from its start; the caller keeps its own. */
@@ -358,7 +348,7 @@ int config_load_fd(struct config *cfg, int fd, const char *label)
close(copy);
return ret;
}
- ret = check_trusted(copy, label, false);
+ ret = check_trusted(copy, label);
if (ret) {
close(copy);
return config_finish(cfg, -EPERM);
diff --git a/util/crypto_openssl.c b/util/crypto_openssl.c
index e5fbd8fa..3b44faab 100644
--- a/util/crypto_openssl.c
+++ b/util/crypto_openssl.c
@@ -39,7 +39,7 @@
static int read_x509(const char *path, X509 **out)
{
- int fd = open_trusted(path, O_RDONLY, false);
+ int fd = open_trusted(path, O_RDONLY);
X509 *cert;
BIO *bio;
diff --git a/util/io.c b/util/io.c
index 4b6f31aa..e19639cb 100644
--- a/util/io.c
+++ b/util/io.c
@@ -28,7 +28,7 @@
#include "seal.h"
static bool under(const char *path, const char *dir);
-static int open_checked(const char *path, int flags, bool secret,
+static int open_checked(const char *path, int flags,
int (*opener)(const char *path, int flags));
int write_complete(int fd, const void *buf, size_t len)
@@ -187,7 +187,7 @@ int read_fd(int fd, const char *label, size_t max, void *out, size_t *len)
return -errno;
if (!S_ISREG(st.st_mode))
return -EINVAL;
- ret = check_trusted(fd, label, false);
+ ret = check_trusted(fd, label);
return ret ? ret : read_whole(fd, max, out, len);
}
@@ -243,7 +243,7 @@ int read_file(const char *path, size_t max, void *out, size_t *len)
{
int fd, ret;
- fd = open_checked(path, O_RDONLY, false, open_regular);
+ fd = open_checked(path, O_RDONLY, open_regular);
if (fd < 0)
return fd;
ret = read_whole(fd, max, out, len);
@@ -426,25 +426,40 @@ static void report_stale(const char *path)
path);
}
-/* check_trusted(), a file renamed over left for the caller to report. */
-static int trusted_check(int fd, const char *path, bool secret)
+static bool trusted_place(const char *path)
{
- struct trusted_walk walk = { .path = path, .dirfd = -1 };
- struct stat st;
- int ret;
+ return under(path, ETC_DIR) || under(path, VAR_LIB);
+}
- if (!under(path, ETC_DIR) && !under(path, VAR_LIB))
- return 0;
- if (fstat(fd, &st))
+/* The file open on fd by itself, whatever path led to it. */
+static int trusted_file(int fd, const char *path, bool secret,
+ struct stat *st)
+{
+ if (fstat(fd, st))
return -errno;
- if (!S_ISREG(st.st_mode) || !trusted_owner(st.st_uid) ||
- (st.st_mode & (secret ? 077 : 022))) {
+ if (!S_ISREG(st->st_mode) || !trusted_owner(st->st_uid) ||
+ (st->st_mode & (secret ? 077 : 022))) {
pr_err("%s: owner %u, mode %04o: refusing a file another user "
- "could %s", path, (__u32)st.st_uid,
- (__u32)(st.st_mode & 07777),
+ "could %s", path, (__u32)st->st_uid,
+ (__u32)(st->st_mode & 07777),
secret ? "read or change" : "change");
return -EPERM;
}
+ return 0;
+}
+
+/* check_trusted(), a file renamed over left for the caller to report. */
+static int trusted_check(int fd, const char *path)
+{
+ struct trusted_walk walk = { .path = path, .dirfd = -1 };
+ struct stat st;
+ int ret;
+
+ if (!trusted_place(path))
+ return 0;
+ ret = trusted_file(fd, path, false, &st);
+ if (ret)
+ return ret;
if (strscpy(walk.rest, path, sizeof(walk.rest)) < 0)
return -ENAMETOOLONG;
ret = walk_from_root(&walk);
@@ -455,21 +470,31 @@ static int trusted_check(int fd, const char *path, bool secret)
return ret;
}
-int check_trusted(int fd, const char *path, bool secret)
+int check_trusted(int fd, const char *path)
{
- int ret = trusted_check(fd, path, secret);
+ int ret = trusted_check(fd, path);
if (ret == -ESTALE)
report_stale(path);
return ret;
}
+int check_secret(int fd, const char *path)
+{
+ struct stat st;
+
+ return trusted_place(path) ? trusted_file(fd, path, true, &st) : 0;
+}
+
+/* Reopens of a name that an open_trusted() makes after its first open. */
+#define TRUSTED_REOPENS 3
+
/*
* Each attempt opens the name again and runs the whole check on its own
* descriptor, so that one passes only what a single check passes. A
* rename that an attempt absorbs is not reported; the last is.
*/
-static int open_checked(const char *path, int flags, bool secret,
+static int open_checked(const char *path, int flags,
int (*opener)(const char *path, int flags))
{
int attempt, fd, ret;
@@ -478,11 +503,11 @@ static int open_checked(const char *path, int flags, bool secret,
fd = opener(path, flags);
if (fd < 0)
return fd;
- ret = trusted_check(fd, path, secret);
+ ret = trusted_check(fd, path);
if (!ret)
return fd;
close(fd);
- if (!trusted_reopen(ret, attempt))
+ if (ret != -ESTALE || attempt == TRUSTED_REOPENS)
break;
}
if (ret == -ESTALE)
@@ -490,9 +515,9 @@ static int open_checked(const char *path, int flags, bool secret,
return ret;
}
-int open_trusted(const char *path, int flags, bool secret)
+int open_trusted(const char *path, int flags)
{
- return open_checked(path, flags, secret, open_path);
+ return open_checked(path, flags, open_path);
}
__u32 kernel_dev(dev_t dev)
diff --git a/util/io.h b/util/io.h
index 98ede166..b08e52d9 100644
--- a/util/io.h
+++ b/util/io.h
@@ -49,26 +49,25 @@ int read_text_line(FILE *f, char **line, size_t *cap, size_t max);
* is the caller's to vouch for. 0, -EPERM logged, or -ESTALE logged when
* the path names another file than the one open on fd.
*/
-int check_trusted(int fd, const char *path, bool secret);
+int check_trusted(int fd, const char *path);
+
+/*
+ * Whether the file open on fd, which passed check_trusted() at path, may
+ * hold a secret: nobody but its owner may read it either. Who reads a
+ * file is the file's own to say, so the path is not walked again. 0 or
+ * -EPERM logged.
+ */
+int check_secret(int fd, const char *path);
/*
* A trusted file a config manager, a package or shirudo itself renames
* over between its open and its check fails the check with -ESTALE.
- * trusted_reopen() is whether the attempt numbered attempt, from 0,
- * that ended in ret is followed by another, which opens the name again
- * and checks the new descriptor in full: up to TRUSTED_REOPENS more,
- * after which -ESTALE goes to the caller. open_trusted() opens path
- * with flags so: a descriptor that passed check_trusted(), or the
- * open's or the last check's negative errno.
+ * open_trusted() then opens the name again and checks the new
+ * descriptor in full, up to three more times, after which -ESTALE goes
+ * to the caller: a descriptor on path opened with flags that passed
+ * check_trusted(), or the open's or the last check's negative errno.
*/
-#define TRUSTED_REOPENS 3
-
-static inline bool trusted_reopen(int ret, int attempt)
-{
- return ret == -ESTALE && attempt < TRUSTED_REOPENS;
-}
-
-int open_trusted(const char *path, int flags, bool secret);
+int open_trusted(const char *path, int flags);
/* An exclusive flock() on fd, waited for through signals. 0 or -errno. */
int lock_exclusive(int fd);
diff --git a/util/keys.c b/util/keys.c
index 1f8f04a0..67b40eea 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -74,7 +74,7 @@ int keys_read_der(const char *path, __u8 **out,
ssize_t n;
int fd;
- fd = open_trusted(path, O_RDONLY, false);
+ fd = open_trusted(path, O_RDONLY);
if (fd < 0)
return fd;
if (fstat(fd, &st) < 0) {
diff --git a/util/tls_openssl.c b/util/tls_openssl.c
index 0335fad7..eee34437 100644
--- a/util/tls_openssl.c
+++ b/util/tls_openssl.c
@@ -144,7 +144,7 @@ static CURLcode sslctx_callback(CURL *curl, void *sslctx, void *userptr)
}
if (!path || !path[0])
return CURLE_OK;
- fd = open_trusted(path, O_RDONLY, false);
+ fd = open_trusted(path, O_RDONLY);
if (fd < 0)
return CURLE_SSL_CACERT_BADFILE;
bio = BIO_new_fd(fd, BIO_CLOSE);