eBPF-based security platform
seal: remove a failed output through the directory it was opened in
Daniel Borkmann59f740b23593
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 8a8a168e..f455fe49 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -286,7 +286,8 @@ signed config image.* - ``t6006-harden``- Memory locking at startup, failing closed* - ``t6007-output-paths``- - Output paths: no symbolic links, directories or devices+ - Output paths: no symbolic links, directories or devices; a failed+ write removes only the file written, where it was opened* - ``t6101-mode-objects``- Device, memory and config-root guards are pinned* - ``t6102-siggate``diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex af417597..7b0ac272 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -739,7 +739,11 @@ no entry without the label for its own: ``init`` ignores such an entryrather than labeling, following or restoring it. A path that rootnames for shirudo to write, a report, an export, evidence or adownload, must be a regular file without the label that is not reached-through a symbolic link and not created under a labeled directory. The+through a symbolic link and not created under a labeled directory. When+root makes the write fail, shirudo removes the file through the+directory it opened it in, and only while the name still holds the+inode it wrote, never by resolving the path again, where a directory+root swapped for a link would lead it to sealed state (``t6007``). Thecontents of a config image are read from the descriptor of its verifiedroot with ``openat2(2)`` and ``RESOLVE_BENEATH``, never through a``/proc/<pid>/fd`` path that root could mount over.diff --git a/Makefile.shirudo b/Makefile.shirudoindex cb9e8e4a..aa3c85fc 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -645,6 +645,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \testing/helper/test-image-cli \testing/helper/test-image-cleanup \testing/helper/test-mode-objects \+ testing/helper/test-seal-output \testing/helper/test-image-config \testing/helper/test-config-image \testing/helper/test-config-update \@@ -910,6 +911,11 @@ testing/helper/test-mode-objects: testing/helper/test-mode-objects.c \@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lelf -lz+testing/helper/test-seal-output: testing/helper/test-seal-output.c \+ util/seal.o util/io.o util/log.o util/string.o util/malloc.o+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -Wl,--wrap=write_complete+test-helpers: $(TEST_HELPERS)# ----- unit tests -------------------------------------------------diff --git a/testing/functional/tamperproof/t6007-output-paths.sh b/testing/functional/tamperproof/t6007-output-paths.shindex 9b8027cb..967c02e0 100755--- a/testing/functional/tamperproof/t6007-output-paths.sh+++ b/testing/functional/tamperproof/t6007-output-paths.sh@@ -6,12 +6,16 @@# output root may ask for: never through a symbolic link, never a# directory or device, and never a sealed inode (the label needs root,# so that case is the target suite's t6114). Driven through the report a-# failed load writes and the JSON a map listing appends.+# failed load writes and the JSON a map listing appends, and through a+# helper that fails the write of an output and makes root's move at that+# moment: what shirudo removes then is the file it wrote, and never one a+# swapped path leads to.cd "$(dirname "$0")/../..". ./test-lib.shS=../shirudo+H=${SHIRUDO_TEST_SEAL_OUTPUT_HELPER:-./helper/test-seal-output}WORK=$(mktemp -d)trap 'rm -rf "$WORK"' EXIT@@ -44,4 +48,26 @@ test_expect_success 'a map listing is not appended through a symbolic link' '[ "$(cat "$WORK/target")" = keep ]'+test_expect_success 'an output that cannot be written is removed' '+ mkdir "$WORK/fail" &&+ [ "$("$H" fail "$WORK/fail/out.json")" = -5 ] &&+ [ ! -e "$WORK/fail/out.json" ]+'++test_expect_success 'a failed output is removed where it was opened, not where its path leads' '+ mkdir "$WORK/swapped" "$WORK/state" &&+ printf "keep\n" >"$WORK/state/out.json" &&+ [ "$("$H" swap-dir "$WORK/swapped/out.json" "$WORK/state")" = -5 ] &&+ [ -L "$WORK/swapped" ] &&+ [ "$(cat "$WORK/state/out.json")" = keep ] &&+ [ ! -e "$WORK/swapped.moved/out.json" ]+'++test_expect_success 'a failed output does not remove a file put in its place' '+ mkdir "$WORK/replaced" &&+ [ "$("$H" swap-file "$WORK/replaced/out.json")" = -5 ] &&+ [ -e "$WORK/replaced/out.json" ] &&+ [ -e "$WORK/replaced/out.json.moved" ]+'+test_donediff --git a/testing/helper/test-seal-output.c b/testing/helper/test-seal-output.cnew file mode 100644index 00000000..819f795b--- /dev/null+++ b/testing/helper/test-seal-output.c@@ -0,0 +1,86 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * seal_output_create() and seal_output_commit() with every write failing,+ * and root's move made at that moment: test-seal-output <how> <path>+ * [<dir>], how being fail, swap-dir, which puts a symbolic link to dir in+ * place of the path's directory, or swap-file, which puts another file in+ * place of the one being written. Prints what the commit returned.+ */++#include <errno.h>+#include <fcntl.h>+#include <stdio.h>+#include <string.h>+#include <unistd.h>++#include <shirudo/string.h>++#include "util/seal.h"++int __wrap_write_complete(int fd, const void *buf, size_t len);++static const char *how, *path, *dir;++static int swap_dir(void)+{+ char parent[4096], moved[4096 + 8];+ const char *slash = strrchr(path, '/');++ if (!slash ||+ strsprintf(parent, sizeof(parent), "%.*s", (int)(slash - path),+ path) < 0 ||+ strsprintf(moved, sizeof(moved), "%s.moved", parent) < 0 ||+ rename(parent, moved) || symlink(dir, parent))+ return -errno;+ return 0;+}++static int swap_file(void)+{+ char moved[4096 + 8];+ int fd;++ if (strsprintf(moved, sizeof(moved), "%s.moved", path) < 0 ||+ rename(path, moved))+ return -errno;+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);+ if (fd < 0)+ return -errno;+ close(fd);+ return 0;+}++int __wrap_write_complete(int fd, const void *buf, size_t len)+{+ int ret = 0;++ if (!strcmp(how, "swap-dir"))+ ret = swap_dir();+ else if (!strcmp(how, "swap-file"))+ ret = swap_file();+ if (ret)+ fprintf(stderr, "test-seal-output: %s: %s\n", how,+ strerror(-ret));+ return -EIO;+}++int main(int argc, char **argv)+{+ struct seal_output out;+ int ret;++ if (argc < 3) {+ fprintf(stderr, "usage: test-seal-output <how> <path> [<dir>]\n");+ return 2;+ }+ how = argv[1];+ path = argv[2];+ dir = argc > 3 ? argv[3] : "";+ ret = seal_output_create(&out, path, 0600);+ if (!ret)+ ret = seal_output_commit(&out, "{}", 2);+ printf("%d\n", ret);+ return 0;+}diff --git a/util/report.c b/util/report.cindex 1c3485fb..aa4234be 100644--- a/util/report.c+++ b/util/report.c@@ -25,6 +25,7 @@int report_emit(const char *path, const struct op_report *r){+ struct seal_output out;struct json_emit e;struct utsname uts;char nv_index[16];@@ -32,7 +33,7 @@ int report_emit(const char *path, const struct op_report *r)const char *vlog = r->verifier_log ? r->verifier_log : "";size_t loglen = strlen(vlog);size_t cap = loglen * 6 + 16384; /* worst-case escape + slack */- int fd, ret = 0;+ int ret = 0;char *buf;if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",@@ -96,21 +97,15 @@ int report_emit(const char *path, const struct op_report *r)xfree(buf, cap);return 0;}- fd = seal_output_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);- if (fd < 0) {- pr_err("report: open %s: %s", path, strerror(-fd));+ ret = seal_output_create(&out, path, 0644);+ if (ret) {+ pr_err("report: open %s: %s", path, strerror(-ret));xfree(buf, cap);- return fd;+ return ret;}- ret = write_complete(fd, buf, strlen(buf));- if (!ret)- ret = write_complete(fd, "\n", 1);- if (close(fd) != 0 && !ret)- ret = -errno;- if (ret) {+ ret = seal_output_commit(&out, buf, strlen(buf));+ if (ret)pr_err("report: write %s: %s", path, strerror(-ret));- unlink(path);- }xfree(buf, cap);return ret;}diff --git a/util/seal.c b/util/seal.cindex 53f30f76..c622a9b3 100644--- a/util/seal.c+++ b/util/seal.c@@ -304,7 +304,9 @@ static int seal_fsealed(int fd)return errno == ENODATA || errno == ENOTSUP ? 0 : -errno;}-int seal_output_open(const char *path, int flags, mode_t mode)+/* seal_output_open(), keeping the directory open in *dirfd when it is set. */+static int output_open(const char *path, int flags, mode_t mode,+ int *dirfd, const char **out_name){const char *name;struct stat st;@@ -337,7 +339,12 @@ int seal_output_open(const char *path, int flags, mode_t mode)ret = -errno;goto out_fd;}- close(dfd);+ if (dirfd) {+ *dirfd = dfd;+ *out_name = name;+ } else {+ close(dfd);+ }return fd;out_fd:close(fd);@@ -350,6 +357,49 @@ out_dir:return ret;}+int seal_output_open(const char *path, int flags, mode_t mode)+{+ return output_open(path, flags, mode, NULL, NULL);+}++int seal_output_create(struct seal_output *out, const char *path,+ mode_t mode)+{+ int fd;++ fd = output_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode, &out->dfd,+ &out->name);+ if (fd < 0)+ return fd;+ out->fd = fd;+ return 0;+}++static bool same_inode(const struct stat *a, const struct stat *b)+{+ return a->st_dev == b->st_dev && a->st_ino == b->st_ino;+}++int seal_output_commit(struct seal_output *out, const void *buf, size_t len)+{+ struct stat wrote, named;+ bool known;+ int ret;++ ret = write_complete(out->fd, buf, len);+ if (!ret)+ ret = write_complete(out->fd, "\n", 1);+ known = !fstat(out->fd, &wrote);+ if (close(out->fd) && !ret)+ ret = -errno;+ if (ret && known &&+ !fstatat(out->dfd, out->name, &named, AT_SYMLINK_NOFOLLOW) &&+ same_inode(&wrote, &named))+ unlinkat(out->dfd, out->name, 0);+ close(out->dfd);+ return ret;+}+int seal_open(const char *path, int flags, mode_t mode){const char *name;diff --git a/util/seal.h b/util/seal.hindex b135784b..1d0b9268 100644--- a/util/seal.h+++ b/util/seal.h@@ -72,6 +72,25 @@ int seal_write_file(const char *path, const void *data, size_t len,*/int seal_output_open(const char *path, int flags, mode_t mode);+/*+ * A document and its newline written to such a file, truncated first. A+ * file the write does not complete is removed, but through the directory+ * it was opened in and only while its name still holds the inode that+ * was written: root, who picks the path and can make the write fail,+ * could otherwise point it at sealed state for shirudo to remove.+ * seal_output_create() is 0 or what seal_output_open() refused with;+ * seal_output_commit() closes the file and is 0 or -errno.+ */+struct seal_output {+ int fd;+ int dfd;+ const char *name;+};++int seal_output_create(struct seal_output *out, const char *path,+ mode_t mode);+int seal_output_commit(struct seal_output *out, const void *buf, size_t len);+/* Give the inode behind fd the label of its directory, open as dfd. */int seal_inherit(int dfd, int fd);