eBPF-based security platform
seal: open a path the caller names for output as root's, not as shirudo's
Daniel Borkmann76133e246e10
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 38a28799..a2b09fea 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -328,6 +328,11 @@ the caller's committed task authority. Runtime and config files use dm-verityand receive no seal writes.Only an authorized caller may set the exact seal xattr, including on anunlabelled inode. A binary carrying a copied label gains no authority.+A path the caller names for shirudo to write (``--report``, ``--out``,+``--json``, a ``bpm pull``) is opened by ``seal_output_open()``: a+regular file without a label, not created under a labelled directory,+not reached through a symbolic link, so root cannot point the authority+at sealed state.A label of any size, or an error reading it, requires caller authority,except for ``ENODATA``, ``EOPNOTSUPP``, ``EACCES`` and ``EPERM``. The latterdiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 9398c43d..f2d4f8a5 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -164,6 +164,12 @@ T3 Tampering with shirudo's files and processparent directory open to populate and retain that mark. Directoryeviction and foreign link or rename operations remain subject toresidual risk 15; existing writable references are not revoked.+ The authority the seal grants shirudo is not root's to aim: a path+ root names for shirudo to write, a report, an export, evidence or+ a download, is opened as output, never a labelled inode, never+ created under a labelled directory and never through a symbolic+ link, and a registry download goes into the descriptor shirudo+ opened rather than a name reopened later.Ptrace and perf allocation checks deny against tasks already markedwhen those hooks run. Concurrent perf attachment and exec remain aseparate, untested `race investigation <https://github.com/shirudo-dev/shirudo/issues/11>`__.diff --git a/builtin-attest.c b/builtin-attest.cindex 7e3b2093..c50c5bff 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -34,6 +34,7 @@#include "util/macros.h"#include "util/malloc.h"#include "util/paths.h"+#include "util/seal.h"#include "util/upatch.h"#define EVIDENCE_MAX SHIRUDO_JSON_MAX_BYTES@@ -711,15 +712,20 @@ static int cmd_attest_challenge(int argc, const char **argv)goto out;}{- int fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,- 0600);+ int fd = seal_output_open(out_path,+ O_WRONLY | O_CREAT | O_TRUNC, 0600);- if (fd < 0 || write_complete(fd, doc, len) ||+ if (fd < 0) {+ pr_err("attest challenge: open %s: %s", out_path,+ strerror(-fd));+ ret = 1;+ goto out;+ }+ if (write_complete(fd, doc, len) ||write_complete(fd, "\n", 1) || close(fd)) {pr_err("attest challenge: write %s: %s", out_path,strerror(errno));- if (fd >= 0)- close(fd);+ close(fd);ret = 1;goto out;}@@ -806,15 +812,17 @@ static int cmd_attest_evidence(int argc, const char **argv)return 1;}if (out_path) {- int fd = open(out_path,- O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);-- if (fd < 0 || write_complete(fd, doc, len) ||- write_complete(fd, "\n", 1) || close(fd)) {+ int fd = seal_output_open(out_path,+ O_WRONLY | O_CREAT | O_TRUNC, 0600);++ if (fd < 0) {+ pr_err("attest: open %s: %s", out_path, strerror(-fd));+ rc = fd;+ } else if (write_complete(fd, doc, len) ||+ write_complete(fd, "\n", 1) || close(fd)) {pr_err("attest: write %s: %s", out_path,strerror(errno));- if (fd >= 0)- close(fd);+ close(fd);rc = -EIO;}} else if (fwrite(doc, 1, len, stdout) != len ||diff --git a/builtin-bpm.c b/builtin-bpm.cindex 1afeab89..5d2647d0 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -127,7 +127,13 @@ out:return rc;}-static int download_package(const char *raw, const char *destination)+/*+ * Into the file open on fd at destination, or, without one, a file in+ * the working directory named after the package. The name comes from+ * the registry, so it is one component ending in .bee, opened as output+ * root may ask for: never a sealed file nor one reached through a link.+ */+static int download_package(const char *raw, const char *destination, int fd){struct bpm_oci_client c = {};struct bpm_descriptor layers[8];@@ -135,7 +141,7 @@ static int download_package(const char *raw, const char *destination)char repository[BPM_MAX_ENDPOINT], tag[BPM_MAX_NAME];char url[PATH_MAX], filename[PATH_MAX], title[PATH_MAX];char digest[65], package_name[BPM_MAX_NAME] = {};- int n_layers = 0, rc;+ int n_layers = 0, out = fd, rc;rc = bpm_oci_parse_pull_ref(raw, &c, repository, sizeof(repository),tag, sizeof(tag));@@ -185,26 +191,30 @@ static int download_package(const char *raw, const char *destination)goto out;}}- rc = bpm_oci_get_file(&c, url, BPM_PACKAGE_MEDIA, filename,+ if (!destination) {+ out = seal_output_open(filename,+ O_WRONLY | O_CREAT | O_TRUNC,+ 0644);+ if (out < 0) {+ rc = out;+ goto out;+ }+ }+ rc = bpm_oci_get_file(&c, url, BPM_PACKAGE_MEDIA, out,layers[i].size);- if (rc)- goto out;- if (sha256_file_hex(filename, digest, sizeof(digest)) ||- !bpm_digest_matches(layers[i].digest, digest)) {+ if (!destination)+ close(out);+ if (!rc && (sha256_file_hex(filename, digest, sizeof(digest)) ||+ !bpm_digest_matches(layers[i].digest, digest)))rc = -EKEYREJECTED;- unlink(filename);- goto out;- }- if (layers[i].size >= 0) {+ if (!rc && layers[i].size >= 0) {struct stat st;- if (stat(filename, &st) || st.st_size != layers[i].size) {+ if (stat(filename, &st) || st.st_size != layers[i].size)rc = -EKEYREJECTED;- unlink(filename);- goto out;- }}- rc = 0;+ if (rc && !destination)+ unlink(filename);goto out;}rc = -EINVAL;@@ -215,7 +225,7 @@ out:static int pull_package(const char *raw){- return download_package(raw, NULL);+ return download_package(raw, NULL, -1);}static int copy_file_ex(const char *src, const char *dst, mode_t mode,@@ -407,9 +417,9 @@ static int print_package_readme(const char *source)fd = mkstemp(temporary);if (fd < 0)return -errno;- close(fd);archive = temporary;- rc = download_package(source, archive);+ rc = download_package(source, archive, fd);+ close(fd);if (rc)goto out;}@@ -498,8 +508,8 @@ static int bpm_info_archive(const char *source, char temporary[])fd = mkstemp(temporary);if (fd < 0)return -errno;+ rc = download_package(source, temporary, fd);close(fd);- rc = download_package(source, temporary);if (rc)unlink(temporary);return rc;@@ -725,8 +735,8 @@ static int key_source_file(const char *source, char *temp, size_t temp_sz)fd = mkstemp(archive);if (fd < 0)return -errno;+ rc = download_package(source, archive, fd);close(fd);- rc = download_package(source, archive);if (!rc)rc = extract_package_cert(archive, temp, temp_sz);unlink(archive);@@ -1081,7 +1091,7 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_enstruct bpm_buffer manifest = {};char url[PATH_MAX], archive[PATH_MAX] = {}, dir[PATH_MAX], package_dir[PATH_MAX];char digest[65];- int rc;+ int fd, rc;DIR *d;struct dirent *de;bool found_package = false, extracted = false;@@ -1156,10 +1166,19 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_enrc = -ENAMETOOLONG;goto out;}- rc = bpm_oci_get_file(&c, url, BPM_PACKAGE_MEDIA, archive,+ fd = seal_open(archive, O_WRONLY | O_CREAT | O_TRUNC |+ O_NOFOLLOW, 0600);+ if (fd < 0) {+ rc = fd;+ goto out;+ }+ rc = bpm_oci_get_file(&c, url, BPM_PACKAGE_MEDIA, fd,layers[i].size);- if (rc)+ close(fd);+ if (rc) {+ unlink(archive);goto out;+ }if (sha256_file_hex(archive, digest, sizeof(digest)) ||!bpm_digest_matches(layers[i].digest, digest)) {rc = -EKEYREJECTED;@@ -1650,8 +1669,20 @@ static int tree_write_report(const char *path, const struct shirudo_tree_state *json_obj_close(&e);if (e.err)return e.err;- if (path)- return seal_write_file(path, buf, e.len, 0600);+ if (path) {+ int fd, rc;++ fd = seal_output_open(path, O_WRONLY | O_CREAT | O_TRUNC,+ 0600);+ if (fd < 0)+ return fd;+ rc = write_complete(fd, buf, e.len);+ if (!rc)+ rc = write_complete(fd, "\n", 1);+ if (close(fd) && !rc)+ rc = -errno;+ return rc;+ }printf("%s\n", buf);return 0;}diff --git a/builtin-upatch.c b/builtin-upatch.cindex 367d9e68..305efe5b 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2275,9 +2275,9 @@ static int append_json_record(const char *path, const char *json, size_t len)if (!path || !path[0] || !json)return -EINVAL;- fd = open(path, O_RDWR | O_CREAT | O_APPEND | O_CLOEXEC, 0644);+ fd = seal_output_open(path, O_RDWR | O_CREAT | O_APPEND, 0644);if (fd < 0)- return -errno;+ return fd;while (flock(fd, LOCK_EX) != 0) {if (errno != EINTR) {rc = -errno;diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex fd1e6ae7..b8fc6981 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -68,6 +68,7 @@ for t in \tamperproof/t1132-seal-scratch.sh \tamperproof/t1125-key-permission.sh \tamperproof/t1140-verity-authority.sh \+ tamperproof/t1141-output-paths.sh \tamperproof/t1142-archive-restore.sh \tamperproof/t1143-attach-scope.sh \tamperproof/t1144-signed-metadata.sh \diff --git a/testing/t0056-output-paths.sh b/testing/t0056-output-paths.shnew file mode 100755index 00000000..0fcdff89--- /dev/null+++ b/testing/t0056-output-paths.sh@@ -0,0 +1,47 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t0056 -- a path the caller names for shirudo to write is opened as+# 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 t1141). Driven through the report a+# failed load writes and the JSON a map listing appends.++cd "$(dirname "$0")"+. ./test-lib.sh++S=../shirudo++WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT+printf 'keep\n' >"$WORK/target"+ln -s "$WORK/target" "$WORK/link"+mkdir "$WORK/dir"++test_expect_success 'a report is not written through a symbolic link' '+ ! $S upatch load "$WORK/missing.upatch" --no-precheck \+ --report="$WORK/link" >/dev/null 2>"$WORK/err" &&+ grep -q "report: open .*symbolic links" "$WORK/err" &&+ [ "$(cat "$WORK/target")" = keep ]+'++test_expect_success 'a report is not written over a directory' '+ ! $S upatch load "$WORK/missing.upatch" --no-precheck \+ --report="$WORK/dir" >/dev/null 2>"$WORK/err" &&+ grep -q "^report: open" "$WORK/err" &&+ [ -d "$WORK/dir" ]+'++test_expect_success 'a report is written to a plain path' '+ ! $S upatch load "$WORK/missing.upatch" --no-precheck \+ --report="$WORK/report.json" >/dev/null 2>&1 &&+ grep -q "\"category\":\"parser_rejected\"" "$WORK/report.json"+'++test_expect_success 'a map listing is not appended through a symbolic link' '+ ! $S upatch map --json="$WORK/link" >/dev/null 2>"$WORK/err" &&+ [ "$(cat "$WORK/target")" = keep ]+'++test_donediff --git a/testing/tamperproof/t1141-output-paths.sh b/testing/tamperproof/t1141-output-paths.shnew file mode 100755index 00000000..68444df2--- /dev/null+++ b/testing/tamperproof/t1141-output-paths.sh@@ -0,0 +1,68 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1141 -- a path root names for shirudo to write. The seal lets the+# runtime task write what it denies everyone else, so a --report, --out+# or --json path pointed at sealed state, directly or through a symbolic+# link, must be refused rather than clobber the eventlog or plant a file+# the next boot restores. Non-destructive: every refusal leaves the state+# as it was.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++EVENTLOG=/var/lib/shirudo/eventlog+ARCHIVE=/var/lib/shirudo/upatches+PLANTED="$ARCHIVE/t1141-planted.upatch"+LINK=/tmp/t1141-eventlog.link+[ -s "$EVENTLOG" ] || test_skip_all "no eventlog at $EVENTLOG"++restore() {+ rm -f "$LINK"+}+trap restore EXIT++BEFORE=$(sha256sum "$EVENTLOG" | cut -d" " -f1)++test_expect_success 'a load report is not written over the eventlog' '+ ! "$SHIRUDO_BIN" upatch load /nonexistent.upatch --no-precheck \+ --report="$EVENTLOG" >/dev/null 2>"$LINK.err" &&+ grep -q "refusing to write sealed state" "$LINK.err" &&+ [ "$(sha256sum "$EVENTLOG" | cut -d" " -f1)" = "$BEFORE" ]+'++test_expect_success 'a load report is not written through a link to the eventlog' '+ ln -sf "$EVENTLOG" "$LINK" &&+ ! "$SHIRUDO_BIN" upatch load /nonexistent.upatch --no-precheck \+ --report="$LINK" >/dev/null 2>"$LINK.err" &&+ grep -q "symbolic links" "$LINK.err" &&+ [ "$(sha256sum "$EVENTLOG" | cut -d" " -f1)" = "$BEFORE" ]+'++test_expect_success 'a load report does not plant a file in the archive' '+ ! "$SHIRUDO_BIN" upatch load /nonexistent.upatch --no-precheck \+ --report="$PLANTED" >/dev/null 2>"$LINK.err" &&+ grep -q "refusing to write sealed state" "$LINK.err" &&+ [ ! -e "$PLANTED" ]+'++test_expect_success 'a map listing is not appended to the eventlog' '+ ! "$SHIRUDO_BIN" upatch map --json="$EVENTLOG" >/dev/null 2>&1 &&+ [ "$(sha256sum "$EVENTLOG" | cut -d" " -f1)" = "$BEFORE" ]+'++test_expect_success 'evidence is not written over the eventlog' '+ ! "$SHIRUDO_BIN" attest --nonce=00 --out="$EVENTLOG" \+ >/dev/null 2>"$LINK.err" &&+ grep -q "refusing to write sealed state" "$LINK.err" &&+ [ "$(sha256sum "$EVENTLOG" | cut -d" " -f1)" = "$BEFORE" ]+'++test_expect_success 'the eventlog still replays' '+ "$SHIRUDO_BIN" status --json 2>/dev/null |+ python3 -c "import json,sys; d=json.load(sys.stdin); sys.exit(0 if d[\"tpm\"][\"chain_matches_eventlog\"] else 1)"+'++test_donediff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex e9ae7c5d..8ad2f223 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -1752,14 +1752,11 @@ static int build_download_package(const char *reference, char *path)rc = -errno;goto out;}- if (close(fd)) {+ rc = bpm_oci_get_file(&client, url, BPM_PACKAGE_MEDIA, fd,+ layers[i].size);+ if (close(fd) && !rc)rc = -errno;- fd = -1;- goto out;- }fd = -1;- rc = bpm_oci_get_file(&client, url, BPM_PACKAGE_MEDIA, path,- layers[i].size);if (rc)goto out;rc = build_read_file(path, &body, &body_len, BPM_BUILD_MAX_FILE);diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 6c6d25c1..5989e759 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -121,7 +121,8 @@ static size_t bpm_write_memory(void *ptr, size_t size, size_t nmemb, void *ud)/* A download bounded by the size its manifest declares. */struct bpm_file_sink {- FILE *fp;+ int fd;+ int err;__u64 room;bool overflow;};@@ -136,7 +137,17 @@ static size_t bpm_write_file(void *ptr, size_t size, size_t nmemb, void *ud)return 0;}s->room -= n;- return fwrite(ptr, 1, n, s->fp);+ s->err = write_complete(s->fd, ptr, n);+ return s->err ? 0 : n;+}++/* The descriptor is the caller's: a body is restarted in place. */+static int sink_restart(struct bpm_file_sink *s, int fd, __u64 room)+{+ if (ftruncate(fd, 0) || lseek(fd, 0, SEEK_SET) < 0)+ return -errno;+ *s = (struct bpm_file_sink){ .fd = fd, .room = room };+ return 0;}static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)@@ -731,70 +742,55 @@ int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,}int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,- const char *accept, const char *path, __s64 size)+ const char *accept, int fd, __s64 size){struct bpm_http_response r = {};struct bpm_file_sink sink = {};- FILE *fp;char auth[BPM_OCI_AUTH_VALUE_SIZE];int rc;- if (size < 0)+ if (size < 0 || fd < 0)return -EINVAL;- fp = fopen(path, "wb");- if (!fp)- return -errno;- sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };+ rc = sink_restart(&sink, fd, (__u64)size);+ if (rc)+ return rc;rc = curl_once(c, "GET", url, accept, NULL, &r, &sink, true);if (!rc && r.status == 401 && r.www_auth[0] && !strncasecmp(r.www_auth, "Bearer", 6)) {- fclose(fp);- unlink(path);- fp = fopen(path, "wb");- if (!fp) {- rc = -errno;- bpm_buffer_free(&r.body);- return rc;- }- sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };- rc = get_bearer_token(c, r.www_auth);+ rc = sink_restart(&sink, fd, (__u64)size);+ if (!rc)+ rc = get_bearer_token(c, r.www_auth);if (!rc && strsprintf(auth, sizeof(auth), "Bearer %s",c->token) < 0)rc = -E2BIG;if (!rc) {- memset(&r, 0, sizeof(r));+ bpm_buffer_free(&r.body);+ r = (struct bpm_http_response){};rc = curl_once(c, "GET", url, accept, auth, &r, &sink, true);}} else if (!rc && r.status == 401 && r.www_auth[0] &&!strncasecmp(r.www_auth, "Basic", 5) && c->username[0]) {char encoded[BPM_OCI_BASIC_ENCODED_SIZE];- fclose(fp);- unlink(path);- fp = fopen(path, "wb");- if (!fp) {- rc = -errno;- bpm_buffer_free(&r.body);- return rc;- }- sink = (struct bpm_file_sink){ .fp = fp, .room = (__u64)size };- rc = encode_basic(c->username, c->password, encoded, sizeof(encoded));+ rc = sink_restart(&sink, fd, (__u64)size);+ if (!rc)+ rc = encode_basic(c->username, c->password, encoded,+ sizeof(encoded));if (!rc && strsprintf(auth, sizeof(auth), "Basic %s",encoded) < 0)rc = -E2BIG;if (!rc) {- memset(&r, 0, sizeof(r));+ bpm_buffer_free(&r.body);+ r = (struct bpm_http_response){};rc = curl_once(c, "GET", url, accept, auth, &r, &sink, true);}}if (sink.overflow)rc = -EFBIG;+ if (!rc && sink.err)+ rc = sink.err;if (!rc && (r.status < 200 || r.status >= 300))rc = r.status == 404 ? -ENOENT : -EACCES;- if (fclose(fp) && !rc)- rc = -EIO;bpm_buffer_free(&r.body);- if (rc)- unlink(path);return rc;}diff --git a/util/bpm_oci.h b/util/bpm_oci.hindex 9b9a34cf..451effa4 100644--- a/util/bpm_oci.h+++ b/util/bpm_oci.h@@ -118,9 +118,13 @@ int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,const char *accept, struct bpm_buffer *out);int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,const char *accept, struct bpm_buffer *out);-/* At most size bytes, the size the manifest declares; -EFBIG beyond. */+/*+ * Into the caller's descriptor, emptied first: at most size bytes, the+ * size the manifest declares, -EFBIG beyond. What a failure leaves in+ * the file is the caller's to remove.+ */int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,- const char *accept, const char *path, __s64 size);+ const char *accept, int fd, __s64 size);int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,const void *body, size_t body_len, const char *media,struct bpm_descriptor *out);diff --git a/util/report.c b/util/report.cindex 2a94d79c..de710380 100644--- a/util/report.c+++ b/util/report.c@@ -6,18 +6,22 @@*/#include <errno.h>+#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/utsname.h>+#include <unistd.h>#include <shirudo/string.h>+#include "io.h"#include "json.h"#include "log.h"#include "malloc.h"#include "paths.h"#include "report.h"+#include "seal.h"int report_emit(const char *path, const struct shirudo_op_report *r){@@ -28,9 +32,8 @@ int report_emit(const char *path, const struct shirudo_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, rc = 0;char *buf;- FILE *f;- int rc = 0;if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",SHIRUDO_NV_INDEX) < 0)@@ -93,15 +96,16 @@ int report_emit(const char *path, const struct shirudo_op_report *r)xfree(buf, cap);return 0;}- f = fopen(path, "w");- if (!f) {- rc = -errno;- pr_err("report: open %s: %s", path, strerror(errno));+ fd = seal_output_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);+ if (fd < 0) {+ pr_err("report: open %s: %s", path, strerror(-fd));xfree(buf, cap);- return rc;+ return fd;}- rc = fprintf(f, "%s\n", buf) < 0 ? -EIO : 0;- if (fclose(f) != 0 && !rc)+ rc = write_complete(fd, buf, strlen(buf));+ if (!rc)+ rc = write_complete(fd, "\n", 1);+ if (close(fd) != 0 && !rc)rc = -errno;if (rc)pr_err("report: write %s: %s", path, strerror(-rc));diff --git a/util/seal.c b/util/seal.cindex 32b9b661..fc294acf 100644--- a/util/seal.c+++ b/util/seal.c@@ -177,6 +177,60 @@ int seal_inherit_path(const char *path)return 0;}+/* 1 for a label of any size, 0 for none, -errno when unreadable. */+static int seal_fsealed(int fd)+{+ if (fgetxattr(fd, SHIRUDO_XATTR_NAME, NULL, 0) >= 0)+ return 1;+ return errno == ENODATA || errno == ENOTSUP ? 0 : -errno;+}++int seal_output_open(const char *path, int flags, mode_t mode)+{+ const char *name;+ struct stat st;+ int dfd, fd, rc;++ dfd = seal_open_parent(path, &name);+ if (dfd < 0)+ return dfd;+ rc = seal_fsealed(dfd);+ if (rc)+ goto out_dir;+ fd = openat(dfd, name, (flags & ~O_TRUNC) | O_NOFOLLOW | O_CLOEXEC,+ mode);+ if (fd < 0) {+ rc = -errno;+ goto out_dir;+ }+ if (fstat(fd, &st)) {+ rc = -errno;+ goto out_fd;+ }+ if (!S_ISREG(st.st_mode)) {+ rc = -EINVAL;+ goto out_fd;+ }+ rc = seal_fsealed(fd);+ if (rc)+ goto out_fd;+ if ((flags & O_TRUNC) && ftruncate(fd, 0)) {+ rc = -errno;+ goto out_fd;+ }+ close(dfd);+ return fd;+out_fd:+ close(fd);+out_dir:+ close(dfd);+ if (rc > 0) {+ pr_err("%s: refusing to write sealed state", path);+ rc = -EPERM;+ }+ return rc;+}+int seal_open(const char *path, int flags, mode_t mode){const char *name;diff --git a/util/seal.h b/util/seal.hindex fd1ffd09..b83ff563 100644--- a/util/seal.h+++ b/util/seal.h@@ -49,6 +49,16 @@ int seal_mkdir_p(const char *path, mode_t mode);int seal_write_file(const char *path, const void *data, size_t len,mode_t mode);+/*+ * Open a file the caller names for shirudo to write: a report, a+ * download, an export. Root may run shirudo with any path, and the seal+ * lets shirudo write what it denies everyone else, so the file must be+ * a regular one that carries no label, not be created under a labelled+ * directory, and not be reached through a symbolic link. The truncation+ * waits until the inode has been checked. -EPERM for a sealed target.+ */+int seal_output_open(const char *path, int flags, mode_t mode);+/* Give the inode behind fd the label of its directory, open as dfd. */int seal_inherit(int dfd, int fd);