Skip to content

eBPF-based security platform

io: check the directories a trusted file was reached through

Daniel Borkmann0f2bfd5bebcc
diff --git a/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.md
index 3c5d6a37..bec0ac0f 100644
--- a/.agents/skills/security-audit/SKILL.md
+++ b/.agents/skills/security-audit/SKILL.md
@@ -128,7 +128,11 @@ those shapes are absent, not the class.
loaded or applied: reopened by path, read twice, a file root can
rewrite after the check, a name resolved through a mount root built,
a symbolic link in a directory of the path, which `O_NOFOLLOW` does
- not refuse.
+ not refuse. A check on a descriptor followed by a lookup of the same
+ path by name (`realpath()`, `stat()`, `access()`) checks what the
+ name leads to then, not what the descriptor was reached through: the
+ walk goes one name at a time through descriptors, as
+ `check_trusted()` and `seal_open_dir()` do.
- **Signed, not bound.** A signature covers bytes, not where they are
used: content signed for another name, version, architecture,
repository or generation, an index and a package from two
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c161cccc..4e483a73 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -1068,7 +1068,7 @@ Coverage by area
``t6004``, ``t6104``, ``t6112``, ``t6129``, ``t6140``
* - Signing keys and the trusted set
- ``t2001``, ``t2002``, ``t2003``, ``t2004``, ``t2005``,
- ``t5003``
+ ``t5003``. Unit: ``io``
* - TPM register
- ``t4001``, ``t4003``, ``t4101``, ``t4102``, ``t4103``,
``t6125``, ``t6128``, ``t6143``, ``t6144``. Unit: ``chain``
diff --git a/Documentation/operate/run/deliver-packages.rst b/Documentation/operate/run/deliver-packages.rst
index 699c1e08..d217368e 100644
--- a/Documentation/operate/run/deliver-packages.rst
+++ b/Documentation/operate/run/deliver-packages.rst
@@ -109,6 +109,12 @@ package and the upatches do not depend on it, but the transport does.
:doc:`/security/threat-model` describes what root on the target can do
with it.
+If you keep the auth file or the CA certificate under ``/etc/shirudo``,
+make the file and each directory its path leads through root's and not
+writable by group or others, and each symbolic link on the path root's.
+A link of root's to the distribution's CA certificates passes;
+:ref:`trusted-files` gives the rules.
+
Before you begin
----------------
diff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rst
index a5b6fabe..fbcf50da 100644
--- a/Documentation/operate/troubleshoot/troubleshooting.rst
+++ b/Documentation/operate/troubleshoot/troubleshooting.rst
@@ -213,6 +213,78 @@ Action
Raise the limit in the shell (``ulimit -l unlimited`` as root) or
set ``LimitMEMLOCK=infinity`` in the unit. The shipped units set it.
+Files refused
+-------------
+
+shirudo reads a settings, repository, tree or certificate file under
+``/etc/shirudo`` or ``/var/lib/shirudo``, and the registry auth file or
+CA certificate the environment names there, only when no user but root
+or the one running the command could change or replace it, see
+:ref:`trusted-files`. A refusal names the path the command was given,
+then what fails the check, and the command fails.
+
+``<path>: link <link>, owner <uid>: refusing a file another user could replace``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: console
+
+ target# REGISTRY_AUTH_FILE=/etc/shirudo/bpm/auth.json shirudo bpm tree sync
+ /etc/shirudo/bpm/auth.json: link /etc/shirudo/bpm/auth.json, owner 65534: refusing a file another user could replace
+ bpm tree sync: Operation not permitted
+
+Meaning
+ A symbolic link on the path belongs to a user other than root and
+ the one running the command, who could point it at another file.
+ The same message names a directory on the path, the one that holds a
+ link included, as ``directory <dir>, owner <uid>, mode <mode>``,
+ when another user owns it or it is writable by group or others and
+ not root's and sticky, and the file itself as ``owner <uid>, mode
+ <mode>``.
+
+Causes
+ - The link was made as another user, or copied with its owner by
+ ``tar``, ``rsync -a`` or ``cp -a`` run as root.
+ - A link leads through a directory that another user can write. A CA
+ certificate linked to a local one in ``/etc/ssl/certs``, which
+ ``update-ca-certificates`` links on into
+ ``/usr/local/share/ca-certificates``, is refused where a directory
+ on the way is writable by its group, as Debian's ``root:staff``
+ mode ``2775`` layout of ``/usr/local`` leaves it. The message names
+ that directory.
+
+Action
+ - Give the link to root, ``chown -h root:root <link>``, or make it
+ again as root.
+ - Make each directory on the path root's and not writable by group
+ or others, or keep the file in a directory that is, such as
+ ``/etc/shirudo`` itself.
+
+``<path>: names another file than the one opened; refusing it``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+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.
+
+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.
+
+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.
+
upatch load and unload failures
-------------------------------
diff --git a/Documentation/reference/configuration.rst b/Documentation/reference/configuration.rst
index 2b52d2a1..74a99778 100644
--- a/Documentation/reference/configuration.rst
+++ b/Documentation/reference/configuration.rst
@@ -330,16 +330,33 @@ image is refused when:
- ``eventlog.max_bytes`` is not a decimal integer (``EINVAL``);
- the map export settings are invalid (``EINVAL``).
+.. _trusted-files:
+
Ownership and mode
~~~~~~~~~~~~~~~~~~
-A settings, repository or tree file read from a path under
-``/etc/shirudo`` or ``/var/lib/shirudo`` is refused with ``EPERM`` unless:
+A settings, repository, tree or certificate file, and the registry auth
+file and CA certificate that ``REGISTRY_AUTH_FILE`` and
+``BPM_OCI_CA_FILE`` name, read from a path under ``/etc/shirudo`` or
+``/var/lib/shirudo`` is refused with ``EPERM`` unless:
- it is a regular file owned by root or by the effective user;
- it is not writable by group or others;
-- every directory above it is owned by root or the effective user and is
- not writable by group or others, or is root's and sticky.
+- every directory the path leads through is owned by root or the
+ effective user and is not writable by group or others, or is root's
+ and sticky;
+- every symbolic link on the path is owned by root or the effective
+ user.
+
+The path is resolved one name at a time, so the directory that holds a
+symbolic link is checked as well as the directories of the link's
+target. A link may lead out of ``/etc/shirudo``, to the distribution's
+CA certificates for example, when every link and directory on the way
+passes. 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`
+gives the messages.
A file that sets ``tpm.owner_auth`` must also not be readable by group or
others, mode ``0600`` or stricter. This applies to the initrd bootstrap
diff --git a/Documentation/reference/environment.rst b/Documentation/reference/environment.rst
index c920fdca..30bf93a8 100644
--- a/Documentation/reference/environment.rst
+++ b/Documentation/reference/environment.rst
@@ -123,7 +123,9 @@ Honored on a target.
must be https or http``.
* - ``BPM_OCI_CA_FILE``
- PEM file whose first certificate is added to the trusted CAs of
- registry connections.
+ registry connections. Under ``/etc/shirudo`` or
+ ``/var/lib/shirudo``, the file and each directory and symbolic
+ link on its path must pass :ref:`trusted-files`.
* - ``BPM_OCI_USERNAME``, ``BPM_OCI_PASSWORD``
- Registry credentials. Used only when both are set, ahead of any
auth file.
@@ -137,7 +139,10 @@ Honored on a target.
- Directory holding ``config.json``, read when
``REGISTRY_AUTH_FILE`` is unset. Without either,
``$HOME/.docker/config.json`` is read. An auth file that holds a
- NUL byte is refused, wherever it is read from.
+ NUL byte is refused, wherever it is read from. Under
+ ``/etc/shirudo`` or ``/var/lib/shirudo``, an auth file and each
+ directory and symbolic link on its path must pass
+ :ref:`trusted-files`.
Telemetry export
----------------
diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rst
index 69206b40..eec70aa9 100644
--- a/Documentation/reference/files.rst
+++ b/Documentation/reference/files.rst
@@ -497,8 +497,10 @@ Host configuration
shirudo refuses a configuration file under ``/etc/shirudo`` or
``/var/lib/shirudo`` that is not a regular file, is owned by another
user than root or the caller, or is writable by group or others, and
-the same for each directory above it unless that directory is root's
-and sticky. A ``shirudo.conf`` that sets
+the same for each directory its path leads through unless that
+directory is root's and sticky. A symbolic link on the path must be
+root's or the caller's, and the directory that holds it is checked like
+the directories of its target. A ``shirudo.conf`` that sets
``tpm.owner_auth`` must also not be readable by group or others. The
``EnvironmentFile`` paths are read by systemd, not by shirudo.
diff --git a/testing/unit/io.c b/testing/unit/io.c
index 40e81c2d..88281db6 100644
--- a/testing/unit/io.c
+++ b/testing/unit/io.c
@@ -3,14 +3,25 @@
#include <errno.h>
#include <fcntl.h>
+#include <ftw.h>
+#include <limits.h>
#include <stdlib.h>
#include <sys/file.h>
+#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <shirudo/string.h>
-#include "io.h"
+#include "../../util/paths.h"
+
+static char etc_dir[4096];
+
+#undef ETC_DIR
+#define ETC_DIR etc_dir
+
+#include "../../util/io.c"
+
#include "unit.h"
static int log_with(const char *text, size_t len)
@@ -129,6 +140,163 @@ static void bpffs_mounted_at_wants_the_root_of_a_bpffs(void)
EXPECT_EQ(bpffs_mounted_at("/dev/null"), -ENOTDIR);
}
+static int etc_fd = -1;
+
+static int remove_entry(const char *path, const struct stat *st, int type,
+ struct FTW *ftw)
+{
+ return remove(path);
+}
+
+static void remove_etc(void)
+{
+ close(etc_fd);
+ ASSERT_EQ(nftw(etc_dir, remove_entry, 16, FTW_DEPTH | FTW_PHYS), 0);
+}
+
+static void make_etc_in(const char *dir)
+{
+ ASSERT_TRUE(strsprintf(etc_dir, sizeof(etc_dir),
+ "%s/shirudo-unit-etc-XXXXXX", dir) > 0);
+ ASSERT_TRUE(mkdtemp(etc_dir));
+ etc_fd = open(etc_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ ASSERT_TRUE(etc_fd >= 0);
+}
+
+static void make_dir(const char *name, mode_t mode)
+{
+ ASSERT_EQ(mkdirat(etc_fd, name, 0700), 0);
+ ASSERT_EQ(fchmodat(etc_fd, name, mode, 0), 0);
+}
+
+static void make_file(const char *name, mode_t mode)
+{
+ int fd = openat(etc_fd, name, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
+ 0600);
+
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(fchmod(fd, mode), 0);
+ close(fd);
+}
+
+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. */
+static int trusted(const char *opened, const char *asked, bool secret)
+{
+ int fd = openat(etc_fd, opened, O_RDONLY | O_CLOEXEC), ret;
+ char path[PATH_MAX];
+
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/%s", etc_dir, asked) > 0);
+ ret = check_trusted(fd, path, secret);
+ close(fd);
+ return ret;
+}
+
+/* The check walks up to /, so the tree goes where every directory passes it. */
+static void make_etc(void)
+{
+ const char *dir = getenv("TMPDIR");
+
+ make_etc_in(dir && dir[0] ? dir : "/tmp");
+ make_file("probe", 0644);
+ if (!trusted("probe", "probe", false))
+ return;
+ remove_etc();
+ make_etc_in("/tmp");
+}
+
+static void check_trusted_takes_a_file_nobody_else_can_change(void)
+{
+ make_etc();
+ make_dir("keys", 0755);
+ make_file("keys/cert.der", 0644);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", false), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys//./cert.der", false), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/../keys/cert.der", false), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", true), -EPERM);
+ ASSERT_EQ(fchmodat(etc_fd, "keys/cert.der", 0600, 0), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", true), 0);
+ ASSERT_EQ(fchmodat(etc_fd, "keys/cert.der", 0664, 0), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", false), -EPERM);
+ remove_etc();
+}
+
+static void check_trusted_refuses_a_directory_others_can_write(void)
+{
+ make_etc();
+ make_dir("keys", 0775);
+ make_file("keys/cert.der", 0644);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", false), -EPERM);
+ /* A sticky directory guards its entries only when it is root's. */
+ ASSERT_EQ(fchmodat(etc_fd, "keys", 01777, 0), 0);
+ EXPECT_EQ(trusted("keys/cert.der", "keys/cert.der", false),
+ geteuid() ? -EPERM : 0);
+ remove_etc();
+}
+
+static void check_trusted_follows_a_link_nobody_else_made(void)
+{
+ char target[PATH_MAX];
+
+ make_etc();
+ make_dir("keys", 0700);
+ make_file("keys/0a.der", 0644);
+ ASSERT_TRUE(strsprintf(target, sizeof(target), "%s/keys/0a.der",
+ etc_dir) > 0);
+ make_link(target, "keys/cert.der");
+ EXPECT_EQ(trusted("keys/0a.der", "keys/cert.der", false), 0);
+ make_link("../keys/0a.der", "keys/up.der");
+ EXPECT_EQ(trusted("keys/0a.der", "keys/up.der", false), 0);
+ make_link("keys", "current");
+ EXPECT_EQ(trusted("keys/0a.der", "current/cert.der", false), 0);
+ make_link("loop.der", "keys/loop.der");
+ EXPECT_EQ(trusted("keys/0a.der", "keys/loop.der", false), -ELOOP);
+ make_link("0a.der/more", "keys/past.der");
+ EXPECT_EQ(trusted("keys/0a.der", "keys/past.der", false), -ENOTDIR);
+ remove_etc();
+}
+
+static void check_trusted_refuses_a_link_others_can_replace(void)
+{
+ make_etc();
+ make_dir("keys", 0700);
+ make_file("keys/0a.der", 0644);
+ make_dir("open", 0777);
+ make_link("../keys/0a.der", "open/cert.der");
+ EXPECT_EQ(trusted("keys/0a.der", "open/cert.der", false), -EPERM);
+ make_file("open/0b.der", 0644);
+ make_link("../open/0b.der", "keys/cert.der");
+ EXPECT_EQ(trusted("open/0b.der", "keys/cert.der", false), -EPERM);
+ /* In a sticky directory of root's a link is its owner's to replace. */
+ if (!geteuid()) {
+ ASSERT_EQ(fchmodat(etc_fd, "open", 01777, 0), 0);
+ EXPECT_EQ(trusted("keys/0a.der", "open/cert.der", false), 0);
+ ASSERT_EQ(fchownat(etc_fd, "open/cert.der", 65534, 65534,
+ AT_SYMLINK_NOFOLLOW), 0);
+ EXPECT_EQ(trusted("keys/0a.der", "open/cert.der", false),
+ -EPERM);
+ }
+ remove_etc();
+}
+
+/* What a name swapped after the open leaves: one file open, another named. */
+static void check_trusted_wants_the_file_the_path_names(void)
+{
+ make_etc();
+ make_dir("keys", 0700);
+ make_file("keys/cert.der", 0644);
+ make_dir("open", 0777);
+ make_file("open/cert.der", 0644);
+ EXPECT_EQ(trusted("open/cert.der", "keys/cert.der", false), -ESTALE);
+ EXPECT_EQ(trusted("keys/cert.der", "keys", false), -EISDIR);
+ remove_etc();
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(lines_len_ends_at_the_last_newline),
UNIT_CASE(append_line_adds_one_record),
@@ -137,6 +305,11 @@ static const struct unit_case cases[] = {
UNIT_CASE(lock_exclusive_keeps_others_out),
UNIT_CASE(kernel_dev_is_major_over_20_minor_bits),
UNIT_CASE(bpffs_mounted_at_wants_the_root_of_a_bpffs),
+ UNIT_CASE(check_trusted_takes_a_file_nobody_else_can_change),
+ UNIT_CASE(check_trusted_refuses_a_directory_others_can_write),
+ 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),
{}
};
diff --git a/util/io.c b/util/io.c
index 460561b2..8cd65361 100644
--- a/util/io.c
+++ b/util/io.c
@@ -19,6 +19,7 @@
#include <linux/openat2.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "io.h"
#include "log.h"
@@ -234,10 +235,171 @@ static bool trusted_owner(uid_t uid)
return uid == 0 || uid == geteuid();
}
+/* As many links as the kernel follows on the way to one file. */
+#define TRUSTED_LINKS_MAX 40
+
+struct trusted_walk {
+ const char *path; /* as the caller named it, for messages */
+ char rest[PATH_MAX]; /* the names still to look up */
+ int dirfd; /* the directory the next name is looked up in */
+ int links;
+};
+
+/* What fd is open on, named for a message. */
+static const char *opened_path(int fd, char name[PATH_MAX])
+{
+ char link[32];
+ ssize_t n;
+
+ if (strsprintf(link, sizeof(link), "/proc/self/fd/%d", fd) < 0)
+ return "?";
+ n = readlink(link, name, PATH_MAX - 1);
+ if (n < 0)
+ return "?";
+ name[n] = '\0';
+ return name;
+}
+
+static int trusted_dir(const struct trusted_walk *walk)
+{
+ char name[PATH_MAX];
+ struct stat st;
+
+ if (fstat(walk->dirfd, &st))
+ return -errno;
+ if (!S_ISDIR(st.st_mode))
+ return -ENOTDIR;
+ if (trusted_owner(st.st_uid) &&
+ (!(st.st_mode & 022) ||
+ ((st.st_mode & S_ISVTX) && st.st_uid == 0)))
+ return 0;
+ pr_err("%s: directory %s, owner %u, mode %04o: refusing a file another user could replace",
+ walk->path, opened_path(walk->dirfd, name), (__u32)st.st_uid,
+ (__u32)(st.st_mode & 07777));
+ return -EPERM;
+}
+
+static int walk_from_root(struct trusted_walk *walk)
+{
+ if (walk->dirfd >= 0)
+ close(walk->dirfd);
+ walk->dirfd = open("/", O_PATH | O_DIRECTORY | O_CLOEXEC);
+ return walk->dirfd < 0 ? -errno : 0;
+}
+
+/*
+ * The target of the link open on fd goes ahead of the names left after
+ * it. In a sticky directory a link is its owner's to replace, so another
+ * user's link is refused wherever it is found.
+ */
+static int walk_link(struct trusted_walk *walk, int fd, uid_t owner,
+ const char *left)
+{
+ char target[PATH_MAX], name[PATH_MAX];
+ ssize_t n;
+
+ if (!trusted_owner(owner)) {
+ pr_err("%s: link %s, owner %u: refusing a file another user could replace",
+ walk->path, opened_path(fd, name), (__u32)owner);
+ return -EPERM;
+ }
+ if (++walk->links > TRUSTED_LINKS_MAX)
+ return -ELOOP;
+ n = readlinkat(fd, "", target, sizeof(target));
+ if (n < 0)
+ return -errno;
+ if ((size_t)n >= sizeof(target))
+ return -ENAMETOOLONG;
+ target[n] = '\0';
+ if (strsprintf(target + n, sizeof(target) - (size_t)n, "/%s", left) < 0 ||
+ strscpy(walk->rest, target, sizeof(walk->rest)) < 0)
+ return -ENAMETOOLONG;
+ return target[0] == '/' ? walk_from_root(walk) : 0;
+}
+
+/* The name that ends at end, looked up in the directory and not followed. */
+static int walk_open(const struct trusted_walk *walk, const char *name,
+ char *end)
+{
+ char cut = *end;
+ int fd, err;
+
+ *end = '\0';
+ fd = openat(walk->dirfd, name, O_PATH | O_NOFOLLOW | O_CLOEXEC);
+ err = errno;
+ *end = cut;
+ return fd < 0 ? -err : fd;
+}
+
+/*
+ * The path resolved one name at a time, each directory checked through
+ * the descriptor the next name is looked up in, and each link read
+ * 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.
+ */
+static char *skip_slashes(char *s)
+{
+ while (*s == '/')
+ s++;
+ return s;
+}
+
+static int trusted_walk(struct trusted_walk *walk, const struct stat *file)
+{
+ char *name = walk->rest, *end;
+ struct stat st;
+ int fd, ret;
+ bool link;
+
+ for (;;) {
+ ret = trusted_dir(walk);
+ if (ret)
+ return ret;
+ name = skip_slashes(name);
+ end = strchr(name, '/');
+ if (!end)
+ end = name + strlen(name);
+ if (end == name)
+ return -EISDIR;
+ fd = walk_open(walk, name, end);
+ if (fd < 0)
+ return fd;
+ if (fstat(fd, &st)) {
+ ret = -errno;
+ close(fd);
+ return ret;
+ }
+ if (S_ISDIR(st.st_mode)) {
+ close(walk->dirfd);
+ walk->dirfd = fd;
+ name = end;
+ continue;
+ }
+ link = S_ISLNK(st.st_mode);
+ ret = link ? walk_link(walk, fd, st.st_uid, end) : 0;
+ close(fd);
+ if (ret)
+ return ret;
+ if (!link)
+ break;
+ name = walk->rest;
+ }
+ if (*skip_slashes(end))
+ return -ENOTDIR;
+ if (st.st_dev == file->st_dev && st.st_ino == file->st_ino)
+ return 0;
+ pr_err("%s: names another file than the one opened; refusing it",
+ walk->path);
+ return -ESTALE;
+}
+
int check_trusted(int fd, const char *path, bool secret)
{
- char real[PATH_MAX], *slash;
+ struct trusted_walk walk = { .path = path, .dirfd = -1 };
struct stat st;
+ int ret;
if (!under(path, ETC_DIR) && !under(path, VAR_LIB))
return 0;
@@ -251,27 +413,14 @@ int check_trusted(int fd, const char *path, bool secret)
secret ? "read or change" : "change");
return -EPERM;
}
- if (!realpath(path, real))
- return -errno;
- do {
- slash = strrchr(real, '/');
- if (slash == real)
- slash[1] = '\0';
- else
- *slash = '\0';
- if (stat(real, &st))
- return -errno;
- if (!S_ISDIR(st.st_mode) || !trusted_owner(st.st_uid) ||
- ((st.st_mode & 022) &&
- !((st.st_mode & S_ISVTX) && st.st_uid == 0))) {
- pr_err("%s: directory %s, owner %u, mode %04o: "
- "refusing a file another user could replace",
- path, real, (__u32)st.st_uid,
- (__u32)(st.st_mode & 07777));
- return -EPERM;
- }
- } while (slash != real);
- return 0;
+ if (strscpy(walk.rest, path, sizeof(walk.rest)) < 0)
+ return -ENAMETOOLONG;
+ ret = walk_from_root(&walk);
+ if (!ret)
+ ret = trusted_walk(&walk, &st);
+ if (walk.dirfd >= 0)
+ close(walk.dirfd);
+ return ret;
}
__u32 kernel_dev(dev_t dev)
diff --git a/util/io.h b/util/io.h
index 28bd3c27..81f5772f 100644
--- a/util/io.h
+++ b/util/io.h
@@ -40,10 +40,12 @@ int read_text_line(FILE *f, char **line, size_t *cap, size_t max);
* Whether the file open on fd at path may steer what shirudo trusts.
* Under /etc/shirudo and /var/lib/shirudo, where an installed shirudo
* keeps such files, it must be a regular file of root's or of the user
- * running shirudo that nobody else can write, nor with secret read, in
- * directories up to / that nobody else can change; a sticky directory of
- * root's guards its files the same way. A path elsewhere, where a test
- * points shirudo, is the caller's to vouch for. 0, or -EPERM logged.
+ * running shirudo that nobody else can write, nor with secret read, and
+ * the path must lead to it through directories nobody else can change
+ * and links nobody else made; a sticky directory of root's guards its
+ * entries the same way. A path elsewhere, where a test points shirudo,
+ * 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);