eBPF-based security platform
io: reopen a trusted file renamed over during its check
Daniel Borkmanncea6d1ba3d54
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex 35cddbfa..97bbd1e8 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -400,7 +400,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`) |+| 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()` || 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/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.mdindex bec0ac0f..9857dae9 100644--- a/.agents/skills/security-audit/SKILL.md+++ b/.agents/skills/security-audit/SKILL.md@@ -132,7 +132,10 @@ those shapes are absent, not the class.path by name (`realpath()`, `stat()`, `access()`) checks what thename leads to then, not what the descriptor was reached through: thewalk goes one name at a time through descriptors, as- `check_trusted()` and `seal_open_dir()` do.+ `check_trusted()` and `seal_open_dir()` do. `open_trusted()` opens a+ name renamed over before its check again and checks each descriptor+ in full; a retry that kept anything from an earlier attempt would be+ this class.- **Signed, not bound.** A signature covers bytes, not where they areused: content signed for another name, version, architecture,repository or generation, an index and a package from twodiff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rstindex fbcf50da..29f3e3cc 100644--- a/Documentation/operate/troubleshoot/troubleshooting.rst+++ b/Documentation/operate/troubleshoot/troubleshooting.rst@@ -263,27 +263,23 @@ Action~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Meaning- The file was replaced while the command read it: between the open- and the check, a new file was renamed over its name, or a new link- over a link on its path, and the path now leads to another file. The- command uses nothing from the file it opened. It fails with ``Stale- file handle`` where it reads the file whole, as it reads the registry- auth file and its own state, and with ``Operation not permitted``- otherwise.+ The name kept changing while the command read it. Between each open+ and its check, a new file was renamed over the name, or a new link+ over a link on its path, so the path led to another file than the+ one opened. A file renamed over once, as a configuration manager, a+ package or shirudo's own state writes replace one, is opened again+ and checked in full, up to three more times; only a name that+ changes on every attempt ends here. The command uses nothing from the+ files it opened, and fails, most commands with ``Stale file handle``.Causes- - A configuration manager, a package or a switch to a new NixOS- generation renamed a new file over the old one while the command- or a timer service ran.- - One shirudo command wrote its state while another read it, as- ``bpm upgrade`` writes ``/var/lib/shirudo/bpm/state.json`` while- ``bpm list`` reads it.+ Something renames new files over the file, or over a link on its+ path, in a loop: a configuration manager that rewrites it on every+ run, or a script.Action- - Run the command again; it reads the new file. A timer service- fails this one run, and its next run reads the new file.- - If the message comes back on every run, find what rewrites the- file that often.+ Find what rewrites the file that often and stop it, then run the+ command again.upatch load and unload failures-------------------------------diff --git a/Documentation/reference/configuration.rst b/Documentation/reference/configuration.rstindex 74a99778..f6a26a41 100644--- a/Documentation/reference/configuration.rst+++ b/Documentation/reference/configuration.rst@@ -353,9 +353,9 @@ symbolic link is checked as well as the directories of the link'starget. A link may lead out of ``/etc/shirudo``, to the distribution'sCA certificates for example, when every link and directory on the waypasses. A file that the path no longer names when the check runs, one-renamed over in between, is refused as well, with ``ESTALE`` where the-command reads the file whole, as it reads the registry auth file. The-next run reads the new file. :doc:`/operate/troubleshoot/troubleshooting`+renamed over in between, is opened again and checked in full, up to+three more times. Only a name renamed over before every check is+refused, with ``ESTALE``. :doc:`/operate/troubleshoot/troubleshooting`gives the messages.A file that sets ``tpm.owner_auth`` must also not be readable by group ordiff --git a/builtin-bpm.c b/builtin-bpm.cindex aafabf27..32a9e49c 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -308,25 +308,13 @@ static int source_path(const char *name, char *out, size_t out_sz)}/*- * 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.+ * A stream on fd, or NULL with errno set. 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, which open_trusted() does.*/-static int stream_untrusted(FILE *fp, const char *path)+static FILE *source_stream(int fd){- if (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 (!image_config_active())- return fopen(path, "re");- fd = image_open_relative(path + 1, O_RDONLY);if (fd < 0) {errno = -fd;return NULL;@@ -334,6 +322,14 @@ static FILE *source_open(const char *path)return fdopen(fd, "r");}+/* A source's file, from the config image's root when one is active. */+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));+}+static DIR *registries_opendir(void){int fd;@@ -370,16 +366,9 @@ static int source_index_repository_valid(const char *endpoint,static FILE *tree_conf_open(void){- int fd;-- if (!image_config_active())- return fopen(BPM_TREE_ETC_PATH, "re");- fd = image_open(IMAGE_TREE_CONF, O_RDONLY);- if (fd < 0) {- errno = -fd;- return NULL;- }- return fdopen(fd, "r");+ return source_stream(image_config_active() ?+ image_open(IMAGE_TREE_CONF, O_RDONLY) :+ open_trusted(BPM_TREE_ETC_PATH, O_RDONLY, false));}static int load_source(const char *name, struct bpm_source *s)@@ -396,10 +385,6 @@ static int load_source(const char *name, struct bpm_source *s)fp = source_open(path);if (!fp)return -errno;- if (stream_untrusted(fp, path)) {- fclose(fp);- return -EPERM;- }if (strscpy(s->name, name, sizeof(s->name)) < 0)ret = -ENAMETOOLONG;while (!ret && (got = read_text_line(fp, &line, &capacity,@@ -1744,10 +1729,6 @@ static int load_tree_config(struct bpm_tree_config *cfg)fp = tree_conf_open();if (!fp)return -errno;- if (stream_untrusted(fp, BPM_TREE_ETC_PATH)) {- fclose(fp);- return -EPERM;- }while (!ret && (got = read_text_line(fp, &line, &capacity,PATH_MAX - 1)) != 0) {if (got < 0 ||diff --git a/builtin-keys.c b/builtin-keys.cindex 7c88358b..58a76adf 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -50,16 +50,12 @@static X509 *read_cert_der(const char *path){- int fd = open(path, O_RDONLY | O_CLOEXEC);+ int fd = open_trusted(path, O_RDONLY, false);BIO *bio;X509 *c;if (fd < 0) {- pr_err("open %s: %s", path, strerror(errno));- return NULL;- }- if (check_trusted(fd, path, false)) {- close(fd);+ pr_err("open %s: %s", path, strerror(-fd));return NULL;}bio = BIO_new_fd(fd, BIO_CLOSE);diff --git a/testing/unit/io.c b/testing/unit/io.cindex 88281db6..ba9bbb42 100644--- a/testing/unit/io.c+++ b/testing/unit/io.c@@ -210,6 +210,22 @@ static void make_etc(void)make_etc_in("/tmp");}+/* As config managers write: the new file beside its name, renamed over it. */+static void replace_mode(const char *name, const char *text, mode_t mode)+{+ char tmp[PATH_MAX];+ int fd;++ ASSERT_TRUE(strsprintf(tmp, sizeof(tmp), "%s.tmp", name) > 0);+ fd = openat(etc_fd, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,+ 0600);+ ASSERT_TRUE(fd >= 0);+ ASSERT_EQ(fchmod(fd, mode), 0);+ ASSERT_EQ(write_complete(fd, text, strlen(text)), 0);+ close(fd);+ ASSERT_EQ(renameat(etc_fd, tmp, etc_fd, name), 0);+}+static void check_trusted_takes_a_file_nobody_else_can_change(void){make_etc();@@ -297,6 +313,83 @@ static void check_trusted_wants_the_file_the_path_names(void)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+ * and the check, on cue.+ */+static struct race {+ const char *name;+ mode_t mode;+ int renames;+ int opens;+} race;++static int open_then_replace(const char *path, int flags)+{+ int fd = open_path(path, flags);++ if (fd >= 0 && race.opens++ < race.renames)+ replace_mode(race.name, "b\n", race.mode);+ return fd;+}++static int open_racing(const char *name, mode_t mode, int renames)+{+ char path[PATH_MAX];++ 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);+}++static void open_trusted_reopens_a_file_renamed_over(void)+{+ char text[4] = {};+ int fd;++ make_etc();+ make_dir("keys", 0755);+ replace_mode("keys/cert.der", "a\n", 0644);+ fd = open_racing("keys/cert.der", 0644, 1);+ ASSERT_TRUE(fd >= 0);+ EXPECT_EQ(race.opens, 2);+ EXPECT_EQ(pread(fd, text, sizeof(text) - 1, 0), 2);+ EXPECT_STREQ(text, "b\n");+ close(fd);+ fd = open_racing("keys/cert.der", 0644, TRUSTED_REOPENS);+ ASSERT_TRUE(fd >= 0);+ EXPECT_EQ(race.opens, TRUSTED_REOPENS + 1);+ close(fd);+ remove_etc();+}++static void open_trusted_gives_up_on_a_name_that_keeps_changing(void)+{+ make_etc();+ make_dir("keys", 0755);+ replace_mode("keys/cert.der", "a\n", 0644);+ EXPECT_EQ(open_racing("keys/cert.der", 0644,+ TRUSTED_REOPENS + 1), -ESTALE);+ EXPECT_EQ(race.opens, TRUSTED_REOPENS + 1);+ remove_etc();+}++/* A reopen checks the new file in full, and only a rename is reopened. */+static void open_trusted_checks_each_file_it_opens(void)+{+ make_etc();+ make_dir("keys", 0755);+ replace_mode("keys/cert.der", "a\n", 0644);+ EXPECT_EQ(open_racing("keys/cert.der", 0664, 1), -EPERM);+ EXPECT_EQ(race.opens, 2);+ /* The file the rename left is group-writable. */+ EXPECT_EQ(open_racing("keys/cert.der", 0644, 0), -EPERM);+ EXPECT_EQ(race.opens, 1);+ remove_etc();+}+static const struct unit_case cases[] = {UNIT_CASE(lines_len_ends_at_the_last_newline),UNIT_CASE(append_line_adds_one_record),@@ -310,6 +403,9 @@ 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(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.cindex e5a2f274..e1287885 100644--- a/util/config.c+++ b/util/config.c@@ -134,7 +134,11 @@ static void set_owner_auth(struct config *cfg, const char *val)cfg->tpm_owner_auth_len = l;}-/* The stream is consumed and closed; the file is named for the log. */+/*+ * 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.+ */static int config_parse(struct config *cfg, FILE *f,const char *path){@@ -142,12 +146,8 @@ static int config_parse(struct config *cfg, FILE *f,char buffer[BUFSIZ];size_t capacity = 0;char *line = NULL;- int got, ret = 0;+ int got, check, ret = 0;- if (check_trusted(fileno(f), path, false)) {- fclose(f);- return -EPERM;- }/* stdio's own buffer would keep the owner's authorization after fclose(). */setvbuf(f, buffer, _IOFBF, sizeof(buffer));while ((got = read_text_line(f, &line, &capacity, max)) != 0) {@@ -265,12 +265,13 @@ 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. */- if (cfg->tpm_owner_auth_len &&- check_trusted(fileno(f), path, true)) {+ check = cfg->tpm_owner_auth_len ?+ check_trusted(fileno(f), path, true) : 0;+ if (check) {memzero_explicit(cfg->tpm_owner_auth,sizeof(cfg->tpm_owner_auth));cfg->tpm_owner_auth_len = 0;- ret = -EPERM;+ ret = check == -ESTALE ? check : -EPERM;}if (line) {/* The owner's authorization may have been read into it. */@@ -299,20 +300,35 @@ static int config_finish(struct config *cfg, int ret)return ret;}-int config_load_path(struct config *cfg, const char *path)+/* The settings at path, read once, or 0 with none there. */+static int config_load_once(struct config *cfg, const char *path){- int ret = 0;+ int fd, ret;FILE *f;- if (!cfg || !path)- return -EINVAL;config_defaults(cfg);- f = fopen(path, "re");+ fd = open_trusted(path, O_RDONLY, false);+ if (fd < 0)+ return fd == -ENOENT ? 0 : fd;+ f = fdopen(fd, "r");if (!f) {- if (errno != ENOENT)- ret = -errno; /* still apply env override below */- } else {- ret = config_parse(cfg, f, path);+ ret = -errno;+ close(fd);+ return ret;+ }+ return config_parse(cfg, f, 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);}@@ -334,6 +350,11 @@ int config_load_fd(struct config *cfg, int fd, const char *label)close(copy);return ret;}+ ret = check_trusted(copy, label, false);+ if (ret) {+ close(copy);+ return config_finish(cfg, -EPERM);+ }f = fdopen(copy, "r");if (!f) {ret = -errno;diff --git a/util/crypto_openssl.c b/util/crypto_openssl.cindex a115610b..e5fbd8fa 100644--- a/util/crypto_openssl.c+++ b/util/crypto_openssl.c@@ -39,19 +39,13 @@static int read_x509(const char *path, X509 **out){- int fd = open(path, O_RDONLY | O_CLOEXEC);+ int fd = open_trusted(path, O_RDONLY, false);X509 *cert;BIO *bio;- int ret;*out = NULL;if (fd < 0)- return -errno;- ret = check_trusted(fd, path, false);- if (ret) {- close(fd);- return ret;- }+ return fd;bio = BIO_new_fd(fd, BIO_CLOSE);if (!bio) {close(fd);diff --git a/util/io.c b/util/io.cindex 8cd65361..da84da4f 100644--- a/util/io.c+++ b/util/io.c@@ -28,6 +28,8 @@#include "seal.h"static bool under(const char *path, const char *dir);+static int open_checked(const char *path, int flags, bool secret,+ int (*opener)(const char *path, int flags));int write_complete(int fd, const void *buf, size_t len){@@ -143,22 +145,16 @@ int open_beneath(int dirfd, const char *rel, int flags)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)+/* The whole regular file behind the descriptor, from its start, xmalloc'd. */+static int read_whole(int fd, size_t max, void *out, size_t *len){struct stat st;char *buf, *p;size_t left;off_t off;- int ret;if (fstat(fd, &st) != 0)return -errno;- if (!S_ISREG(st.st_mode))- return -EINVAL;- ret = check_trusted(fd, label, false);- if (ret)- return ret;if ((uintmax_t)st.st_size > max)return -EFBIG;buf = xmalloc((size_t)st.st_size + 1);@@ -182,6 +178,19 @@ int read_fd(int fd, const char *label, size_t max, void *out, size_t *len)return 0;}+int read_fd(int fd, const char *label, size_t max, void *out, size_t *len)+{+ struct stat st;+ int ret;++ if (fstat(fd, &st) != 0)+ return -errno;+ if (!S_ISREG(st.st_mode))+ return -EINVAL;+ ret = check_trusted(fd, label, false);+ return ret ? ret : read_whole(fd, max, out, len);+}+/** One line of text, at most max bytes before its newline, in getline's* buffer, which the caller frees with libc_free: 1, 0 at the end, -E2BIG@@ -204,21 +213,40 @@ int read_text_line(FILE *f, char **line, size_t *cap, size_t max)return (size_t)n > max ? -E2BIG : 1;}-int read_file(const char *path, size_t max, void *out, size_t *len)+static int open_path(const char *path, int flags)+{+ int fd = open(path, flags | O_CLOEXEC);++ return fd < 0 ? -errno : fd;+}++/* A regular file for read_file(), as sealed state under /var/lib/shirudo. */+static int open_regular(const char *path, int flags){+ struct stat st;int fd, ret;- if (under(path, VAR_LIB)) {- fd = seal_open_read(path);- if (fd < 0)- return fd;- } else {- /* O_NONBLOCK: a FIFO is refused below, not waited on. */- fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);- if (fd < 0)- return -errno;+ /* O_NONBLOCK: a FIFO is refused below, not waited on. */+ fd = under(path, VAR_LIB) ? seal_open_read(path) :+ open_path(path, flags | O_NONBLOCK);+ if (fd < 0)+ return fd;+ ret = fstat(fd, &st) ? -errno : S_ISREG(st.st_mode) ? 0 : -EINVAL;+ if (ret) {+ close(fd);+ return ret;}- ret = read_fd(fd, path, max, out, len);+ return fd;+}++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);+ if (fd < 0)+ return fd;+ ret = read_whole(fd, max, out, len);close(fd);return ret;}@@ -337,7 +365,7 @@ static int walk_open(const struct trusted_walk *walk, const char *name,* through the one it was found by: a lookup of the whole path by name* can go another way than the open of the file did, through a name* another user swapped in between or a link another user made. 0 when- * it ends at the file *file describes.+ * it ends at the file *file describes, -ESTALE unlogged when at another.*/static char *skip_slashes(char *s){@@ -388,14 +416,18 @@ static int trusted_walk(struct trusted_walk *walk, const struct stat *file)}if (*skip_slashes(end))return -ENOTDIR;- if (st.st_dev == file->st_dev && st.st_ino == file->st_ino)- return 0;+ return st.st_dev == file->st_dev && st.st_ino == file->st_ino ?+ 0 : -ESTALE;+}++static void report_stale(const char *path)+{pr_err("%s: names another file than the one opened; refusing it",- walk->path);- return -ESTALE;+ path);}-int check_trusted(int fd, const char *path, bool secret)+/* check_trusted(), a file renamed over left for the caller to report. */+static int trusted_check(int fd, const char *path, bool secret){struct trusted_walk walk = { .path = path, .dirfd = -1 };struct stat st;@@ -423,6 +455,46 @@ int check_trusted(int fd, const char *path, bool secret)return ret;}+int check_trusted(int fd, const char *path, bool secret)+{+ int ret = trusted_check(fd, path, secret);++ if (ret == -ESTALE)+ report_stale(path);+ return ret;+}++/*+ * 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,+ int (*opener)(const char *path, int flags))+{+ int attempt, fd, ret;++ for (attempt = 0;; attempt++) {+ fd = opener(path, flags);+ if (fd < 0)+ return fd;+ ret = trusted_check(fd, path, secret);+ if (!ret)+ return fd;+ close(fd);+ if (!trusted_reopen(ret, attempt))+ break;+ }+ if (ret == -ESTALE)+ report_stale(path);+ return ret;+}++int open_trusted(const char *path, int flags, bool secret)+{+ return open_checked(path, flags, secret, open_path);+}+__u32 kernel_dev(dev_t dev){return (__u32)major(dev) << 20 | (minor(dev) & 0xfffffU);diff --git a/util/io.h b/util/io.hindex 81f5772f..98ede166 100644--- a/util/io.h+++ b/util/io.h@@ -4,6 +4,7 @@#ifndef SHIRUDO_IO_H#define SHIRUDO_IO_H+#include <errno.h>#include <stdbool.h>#include <stddef.h>#include <stdio.h>@@ -29,7 +30,8 @@ int append_line(int fd, const char *path, const char *line, size_t len);/** A regular file of at most max bytes, into *out xmalloc'd to *len + 1* bytes and NUL-terminated; out is the address of a char or unsigned- * char pointer.+ * char pointer. read_file() opens path as open_trusted() does, read_fd()+ * checks what is open on fd once, under the name label.*/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);@@ -49,6 +51,25 @@ int read_text_line(FILE *f, char **line, size_t *cap, size_t max);*/int check_trusted(int fd, const char *path, bool secret);+/*+ * 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.+ */+#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);+/* 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.cindex 4e829f3f..1f8f04a0 100644--- a/util/keys.c+++ b/util/keys.c@@ -74,9 +74,9 @@ int keys_read_der(const char *path, __u8 **out,ssize_t n;int fd;- fd = open(path, O_RDONLY | O_CLOEXEC);+ fd = open_trusted(path, O_RDONLY, false);if (fd < 0)- return -errno;+ return fd;if (fstat(fd, &st) < 0) {int ret = -errno;@@ -88,10 +88,6 @@ int keys_read_der(const char *path, __u8 **out,close(fd);return -EINVAL;}- if (check_trusted(fd, path, false)) {- close(fd);- return -EPERM;- }buf = xmalloc((size_t)st.st_size);if (!buf) {close(fd);diff --git a/util/tls_openssl.c b/util/tls_openssl.cindex b0603e57..3a6c6e17 100644--- a/util/tls_openssl.c+++ b/util/tls_openssl.c@@ -145,13 +145,9 @@ static CURLcode sslctx_callback(CURL *curl, void *sslctx, void *userptr)}if (!path || !path[0])return CURLE_OK;- fd = open(path, O_RDONLY | O_CLOEXEC);+ fd = open_trusted(path, O_RDONLY, false);if (fd < 0)return CURLE_SSL_CACERT_BADFILE;- if (check_trusted(fd, path, false)) {- close(fd);- return CURLE_SSL_CACERT_BADFILE;- }bio = BIO_new_fd(fd, BIO_CLOSE);if (!bio) {close(fd);