Skip to content

eBPF-based security platform

seal: make files and directories below an open directory

KP Singh263d7945efc2
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 4e8adb42..76f1d951 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -1041,7 +1041,7 @@ OCI registry. The macOS workflow checks the Lima template with
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 43 of the modules in
+The unit suites in :src:`testing/unit/` cover 44 of the modules in
:src:`util/`: ``attest``, ``bpm_cleanup``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,
@@ -1050,7 +1050,7 @@ The unit suites in :src:`testing/unit/` cover 43 of the modules in
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,
``options``,
-``paths``, ``report``, ``string``, ``sysinfo``, ``timefmt``, ``upatch``,
+``paths``, ``report``, ``seal``, ``string``, ``sysinfo``, ``timefmt``, ``upatch``,
``upatch_decode``, ``upatch_pins``, ``upatch_resolve``, ``upatch_verify``
and ``xdp``, and
``overflow`` the arithmetic of
diff --git a/testing/unit/io.c b/testing/unit/io.c
index 763d2e81..69b91554 100644
--- a/testing/unit/io.c
+++ b/testing/unit/io.c
@@ -628,6 +628,29 @@ static void open_beneath_same_mount_refuses_a_bind(void)
remove_etc();
}
+static void create_beneath_same_mount_makes_one_new_file(void)
+{
+ struct stat st;
+ int fd;
+
+ make_etc_in("/tmp");
+ fd = create_beneath_same_mount(etc_fd, "new", O_WRONLY, 0600);
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(fstat(fd, &st), 0);
+ EXPECT_EQ(st.st_mode & 07777, 0600);
+ close(fd);
+ EXPECT_EQ(create_beneath_same_mount(etc_fd, "new", O_WRONLY, 0600),
+ -EEXIST);
+ EXPECT_EQ(create_beneath_same_mount(etc_fd, "../new", O_WRONLY, 0600),
+ -EXDEV);
+ /* A link under the name is in the way, and its target is not made. */
+ ASSERT_EQ(symlinkat("missing", etc_fd, "link"), 0);
+ EXPECT_EQ(create_beneath_same_mount(etc_fd, "link", O_WRONLY, 0600),
+ -EEXIST);
+ EXPECT_EQ(faccessat(etc_fd, "missing", F_OK, 0), -1);
+ remove_etc();
+}
+
/* The same, the directory linked to on a file system of its own. */
static void check_trusted_follows_its_prefix_onto_another_mount(void)
{
@@ -865,6 +888,7 @@ static void read_file_takes_a_file_renamed_over_or_refuses_it(void)
static const struct unit_case cases[] = {
UNIT_CASE(open_beneath_same_mount_stops_at_proc),
UNIT_CASE(open_beneath_same_mount_refuses_a_bind),
+ UNIT_CASE(create_beneath_same_mount_makes_one_new_file),
UNIT_CASE(lines_len_ends_at_the_last_newline),
UNIT_CASE(append_line_adds_one_record),
UNIT_CASE(append_line_drops_what_did_not_finish),
diff --git a/testing/unit/seal.c b/testing/unit/seal.c
new file mode 100644
index 00000000..6b1d2433
--- /dev/null
+++ b/testing/unit/seal.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <ftw.h>
+#include <limits.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <shirudo/string.h>
+
+#include "seal.h"
+#include "unit.h"
+
+/*
+ * A directory that carries no label, as a state root does before the
+ * first init: the helpers then check everything but the label.
+ */
+static char dir_path[PATH_MAX];
+static int dir = -1;
+
+static void new_dir(void)
+{
+ strscpy_literal(dir_path, "/tmp/shirudo-unit-seal-XXXXXX");
+ ASSERT_TRUE(mkdtemp(dir_path));
+ dir = open(dir_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ ASSERT_TRUE(dir >= 0);
+}
+
+static int remove_entry(const char *path, const struct stat *st, int type,
+ struct FTW *ftw)
+{
+ return remove(path);
+}
+
+static void remove_dir(void)
+{
+ close(dir);
+ ASSERT_EQ(nftw(dir_path, remove_entry, 16, FTW_DEPTH | FTW_PHYS), 0);
+}
+
+static off_t size_of(const char *name)
+{
+ struct stat st;
+
+ ASSERT_EQ(fstatat(dir, name, &st, 0), 0);
+ return st.st_size;
+}
+
+static void mkdir_child_makes_or_takes_one_directory(void)
+{
+ int fd;
+
+ new_dir();
+ fd = seal_mkdir_child(dir, "made", 0700, true);
+ ASSERT_TRUE(fd >= 0);
+ close(fd);
+ EXPECT_EQ(seal_mkdir_child(dir, "made", 0700, true), -EEXIST);
+ fd = seal_mkdir_child(dir, "made", 0700, false);
+ EXPECT_TRUE(fd >= 0);
+ close(fd);
+
+ EXPECT_EQ(seal_mkdir_child(dir, "made/below", 0700, false), -EINVAL);
+ EXPECT_EQ(seal_mkdir_child(dir, "..", 0700, false), -EINVAL);
+ EXPECT_EQ(seal_mkdir_child(dir, "", 0700, false), -EINVAL);
+
+ fd = openat(dir, "file", O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0600);
+ ASSERT_TRUE(fd >= 0);
+ close(fd);
+ EXPECT_EQ(seal_mkdir_child(dir, "file", 0700, false), -ENOTDIR);
+ ASSERT_EQ(symlinkat("made", dir, "link"), 0);
+ EXPECT_EQ(seal_mkdir_child(dir, "link", 0700, false), -ELOOP);
+ remove_dir();
+}
+
+static void create_child_makes_or_takes_one_regular_file(void)
+{
+ int fd;
+
+ new_dir();
+ EXPECT_EQ(seal_create_child(dir, "file", O_RDWR, 0600), -ENOENT);
+ fd = seal_create_child(dir, "file", O_WRONLY | O_CREAT | O_EXCL, 0600);
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(write(fd, "abc", 3), 3);
+ close(fd);
+ EXPECT_EQ(seal_create_child(dir, "file", O_WRONLY | O_CREAT | O_EXCL,
+ 0600), -EEXIST);
+ EXPECT_EQ(size_of("file"), 3);
+ fd = seal_create_child(dir, "file", O_RDWR | O_CREAT, 0600);
+ EXPECT_TRUE(fd >= 0);
+ close(fd);
+ EXPECT_EQ(size_of("file"), 3);
+ fd = seal_create_child(dir, "file", O_RDWR | O_CREAT | O_TRUNC, 0600);
+ EXPECT_TRUE(fd >= 0);
+ close(fd);
+ EXPECT_EQ(size_of("file"), 0);
+
+ EXPECT_EQ(seal_create_child(dir, "a/b", O_RDWR | O_CREAT, 0600), -EINVAL);
+ EXPECT_EQ(seal_create_child(dir, ".", O_RDONLY, 0600), -EINVAL);
+ remove_dir();
+}
+
+/* What is under the name must be a regular file, and is not truncated first. */
+static void create_child_takes_nothing_but_a_regular_file(void)
+{
+ int fd;
+
+ new_dir();
+ ASSERT_EQ(mkdirat(dir, "sub", 0700), 0);
+ EXPECT_EQ(seal_create_child(dir, "sub", O_RDONLY, 0600), -EINVAL);
+ EXPECT_EQ(seal_create_child(dir, "sub", O_RDWR | O_CREAT, 0600), -EISDIR);
+ ASSERT_EQ(mkfifoat(dir, "fifo", 0600), 0);
+ EXPECT_EQ(seal_create_child(dir, "fifo", O_RDONLY | O_CREAT | O_TRUNC,
+ 0600), -EINVAL);
+
+ fd = openat(dir, "target", O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0600);
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(write(fd, "abc", 3), 3);
+ close(fd);
+ ASSERT_EQ(symlinkat("target", dir, "link"), 0);
+ EXPECT_EQ(seal_create_child(dir, "link", O_RDWR | O_CREAT | O_TRUNC,
+ 0600), -ELOOP);
+ EXPECT_EQ(size_of("target"), 3);
+ ASSERT_EQ(symlinkat("missing", dir, "dangling"), 0);
+ EXPECT_EQ(seal_create_child(dir, "dangling", O_RDWR | O_CREAT, 0600),
+ -ELOOP);
+ EXPECT_EQ(faccessat(dir, "missing", F_OK, 0), -1);
+ remove_dir();
+}
+
+static void a_child_on_another_mount_is_refused(void)
+{
+ char source[PATH_MAX], target[PATH_MAX];
+
+ if (geteuid())
+ unit_skip("needs root for a private bind mount");
+ if (unshare(CLONE_NEWNS))
+ unit_skip("cannot make a mount namespace");
+ ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
+ new_dir();
+ ASSERT_EQ(mkdirat(dir, "source", 0700), 0);
+ ASSERT_EQ(mkdirat(dir, "alias", 0700), 0);
+ ASSERT_TRUE(strsprintf(source, sizeof(source), "%s/source", dir_path) > 0);
+ ASSERT_TRUE(strsprintf(target, sizeof(target), "%s/alias", dir_path) > 0);
+ ASSERT_EQ(mount(source, target, NULL, MS_BIND, NULL), 0);
+ EXPECT_EQ(seal_mkdir_child(dir, "alias", 0700, false), -EXDEV);
+ EXPECT_EQ(seal_mkdir_child(dir, "alias", 0700, true), -EEXIST);
+ ASSERT_EQ(umount(target), 0);
+
+ /* A file bound over a file is no more shirudo's than a directory. */
+ close(seal_create_child(dir, "one", O_WRONLY | O_CREAT | O_EXCL, 0600));
+ close(seal_create_child(dir, "two", O_WRONLY | O_CREAT | O_EXCL, 0600));
+ ASSERT_TRUE(strsprintf(source, sizeof(source), "%s/one", dir_path) > 0);
+ ASSERT_TRUE(strsprintf(target, sizeof(target), "%s/two", dir_path) > 0);
+ ASSERT_EQ(mount(source, target, NULL, MS_BIND, NULL), 0);
+ EXPECT_EQ(seal_create_child(dir, "two", O_RDWR | O_CREAT | O_TRUNC,
+ 0600), -EXDEV);
+ EXPECT_EQ(seal_open_child(dir, "two", O_RDONLY), -EXDEV);
+ ASSERT_EQ(umount(target), 0);
+ remove_dir();
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(mkdir_child_makes_or_takes_one_directory),
+ UNIT_CASE(create_child_makes_or_takes_one_regular_file),
+ UNIT_CASE(create_child_takes_nothing_but_a_regular_file),
+ UNIT_CASE(a_child_on_another_mount_is_refused),
+ {}
+};
+
+UNIT_SUITE(seal, cases);
diff --git a/util/io.c b/util/io.c
index d5ac713a..9bf88af5 100644
--- a/util/io.c
+++ b/util/io.c
@@ -130,10 +130,12 @@ int append_line(int fd, const char *path, const char *line, size_t len)
* mounts or links elsewhere, /proc/<pid>/fd before an exec say, is
* followed.
*/
-static int open_resolved(int dirfd, const char *rel, int flags, __u64 resolve)
+static int open_resolved(int dirfd, const char *rel, int flags, mode_t mode,
+ __u64 resolve)
{
struct open_how how = {
.flags = (__u64)flags | O_CLOEXEC,
+ .mode = flags & O_CREAT ? mode : 0,
.resolve = RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS |
RESOLVE_NO_MAGICLINKS | resolve,
};
@@ -147,12 +149,19 @@ static int open_resolved(int dirfd, const char *rel, int flags, __u64 resolve)
int open_beneath(int dirfd, const char *rel, int flags)
{
- return open_resolved(dirfd, rel, flags, 0);
+ return open_resolved(dirfd, rel, flags, 0, 0);
}
int open_beneath_same_mount(int dirfd, const char *rel, int flags)
{
- return open_resolved(dirfd, rel, flags, RESOLVE_NO_XDEV);
+ return open_resolved(dirfd, rel, flags, 0, RESOLVE_NO_XDEV);
+}
+
+int create_beneath_same_mount(int dirfd, const char *rel, int flags,
+ mode_t mode)
+{
+ return open_resolved(dirfd, rel, flags | O_CREAT | O_EXCL, mode,
+ RESOLVE_NO_XDEV);
}
/* The whole regular file behind the descriptor, from its start, xmalloc'd. */
diff --git a/util/io.h b/util/io.h
index 339fb89d..f2060642 100644
--- a/util/io.h
+++ b/util/io.h
@@ -38,6 +38,9 @@ int read_fd(int fd, const char *label, size_t max, void *out, size_t *len);
int open_beneath(int dirfd, const char *rel, int flags);
/* Also refuse mount crossings, including bind mounts of the same file system. */
int open_beneath_same_mount(int dirfd, const char *rel, int flags);
+/* The same for a file that must not be there yet, made with mode. */
+int create_beneath_same_mount(int dirfd, const char *rel, int flags,
+ mode_t mode);
int read_text_line(FILE *f, char **line, size_t *cap, size_t max);
/*
diff --git a/util/seal.c b/util/seal.c
index 7c9084f3..56369e2e 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -325,6 +325,27 @@ int seal_open_child(int parent, const char *name, int flags)
return fd;
}
+int seal_mkdir_child(int parent, const char *name, mode_t mode, bool exclusive)
+{
+ bool created;
+ int fd, ret;
+
+ if (!is_child_name(name))
+ return -EINVAL;
+ created = !mkdirat(parent, name, mode);
+ if (!created && (errno != EEXIST || exclusive))
+ return -errno;
+ fd = open_beneath_same_mount(parent, name, O_RDONLY | O_DIRECTORY);
+ if (fd < 0)
+ return fd;
+ ret = created ? seal_inherit(parent, fd) : seal_same(parent, fd);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+ return fd;
+}
+
static int seal_open_parent(const char *path, const char **name)
{
const char *slash = strrchr(path, '/');
@@ -587,6 +608,34 @@ int seal_open(const char *path, int flags, mode_t mode)
return fd;
}
+int seal_create_child(int parent, const char *name, int flags, mode_t mode)
+{
+ int fd, ret;
+
+ if (!is_child_name(name))
+ return -EINVAL;
+ fd = open_beneath_same_mount(parent, name, O_NONBLOCK |
+ (flags & ~(O_CREAT | O_EXCL | O_TRUNC)));
+ if (fd >= 0) {
+ ret = flags & O_EXCL ? -EEXIST : seal_regular(fd);
+ if (!ret)
+ ret = seal_same(parent, fd);
+ if (!ret && (flags & O_TRUNC) && ftruncate(fd, 0))
+ ret = -errno;
+ } else if (fd == -ENOENT && (flags & O_CREAT)) {
+ fd = create_beneath_same_mount(parent, name, flags, mode);
+ ret = fd < 0 ? fd : seal_inherit(parent, fd);
+ } else {
+ ret = fd;
+ }
+ if (ret) {
+ if (fd >= 0)
+ close(fd);
+ return ret;
+ }
+ return fd;
+}
+
static int seal_open_file(const char *path, bool sealed)
{
char label[SEAL_LABEL_LEN];
diff --git a/util/seal.h b/util/seal.h
index 59c95691..df06931f 100644
--- a/util/seal.h
+++ b/util/seal.h
@@ -66,6 +66,17 @@ int seal_open_read(const char *path);
int seal_open_state_root(void);
/* One child of parent, on its mount and carrying its label. */
int seal_open_child(int parent, const char *name, int flags);
+/*
+ * The same for what an operation makes below a directory it holds open,
+ * so that a mount root places there meanwhile is refused, not followed.
+ * seal_mkdir_child() makes the directory name, or with exclusive unset
+ * takes the one there, and opens it. seal_create_child() opens the
+ * regular file name as seal_open() opens a path: made with O_CREAT when
+ * it is not there, -EEXIST with O_EXCL when it is, truncated with
+ * O_TRUNC once it is known to be shirudo's own.
+ */
+int seal_mkdir_child(int parent, const char *name, mode_t mode, bool exclusive);
+int seal_create_child(int parent, const char *name, int flags, mode_t mode);
/*
* seal_open_read() of an entry whose directory must carry the label: