Skip to content

eBPF-based security platform

seal: create shirudo's files under their opened directory

Daniel Borkmann3572990954c0
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 6cd04eeb..17028648 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -311,7 +311,11 @@ inode storage from the hooks that hold a trusted handle on it:
``file_open`` of the directory, ``inode_post_setxattr`` and
``inode_post_removexattr`` on it, and the creation of a directory
under a marked parent, which marks the child in turn. The label a new
-inode gets is its parent's.
+inode gets is its parent's. shirudo's own writers (``util/seal.c``)
+open the directory before creating in it, so that it is marked and its
+inode stays in memory for the duration, create relative to it, and put
+the label on afterwards where the kernel could not, with no seal
+attached or on a file system that takes no xattrs at creation.
Process hardening. ``util/harden.c`` runs at startup on the target:
``PR_SET_DUMPABLE`` off, ``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` to
diff --git a/Documentation/operations.rst b/Documentation/operations.rst
index 6518b329..a4b7770a 100644
--- a/Documentation/operations.rst
+++ b/Documentation/operations.rst
@@ -59,9 +59,10 @@ State lives in two places. ``/var/lib/shirudo`` is persistent:
``keys/`` (the primary certificate, the trusted set with its vouches,
the revoked certificates), ``eventlog``, the ``upatches/`` archive
``init`` restores at boot, ``bpm/`` (indexes, cached packages, tree
-state) and
-``locks/``, all labelled with the seal xattr so that only shirudo can
-change them. ``/run/shirudo`` is per boot: the private bpffs with the
+state) and ``locks/``, all labelled with the seal xattr so that only
+shirudo can change them, each from the instant it is created: a new
+inode gets the label of the directory it is created in.
+``/run/shirudo`` is per boot: the private bpffs with the
LSM pins, the frozen identity map and the pending init record between
the initrd run and the root's.
diff --git a/Makefile.shirudo b/Makefile.shirudo
index f6db731c..a769b9e5 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -372,6 +372,7 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o \
util/log.o util/json.o util/loader.o \
util/io.o util/malloc.o util/paths.o \
util/elf_target.o util/hex.o util/keys.o \
+ util/seal.o \
util/crypto_openssl.o util/tls_openssl.o \
util/bpm_oci.o util/bpm_package.o \
util/bpm_tree.o $(VERSION_OBJ) \
@@ -586,9 +587,9 @@ testing/helper/test-kernel-abi: testing/helper/test-kernel-abi.c util/kernel_abi
testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.o \
util/upatch_maps_otel.o util/crypto_openssl.o \
- util/tls_openssl.o util/serializer.o \
+ util/tls_openssl.o util/serializer.o util/seal.o \
util/upatch_decode.o util/upatch_resolve.o \
- util/upatch.o util/json.o \
+ util/upatch.o util/json.o util/log.o \
util/io.o util/malloc.o $(LIBBPF_A)
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)
@@ -604,8 +605,9 @@ testing/helper/test-upatch-prog-metadata: testing/helper/test-upatch-prog-metada
testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializer-pages.c \
util/upatch_maps.o util/upatch_resolve.o \
- util/serializer.o \
- util/json.o util/malloc.o
+ util/serializer.o util/seal.o \
+ util/json.o util/malloc.o util/io.o \
+ util/log.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 98248c33..14654e90 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -214,9 +214,9 @@ static int copy_file_ex(const char *src, const char *dst, mode_t mode,
in = open(src, O_RDONLY | O_CLOEXEC);
if (in < 0) return -errno;
- out = open(dst, O_WRONLY | O_CREAT | O_CLOEXEC |
- (exclusive ? O_EXCL : O_TRUNC), mode);
- if (out < 0) { rc = -errno; close(in); return rc; }
+ out = shirudo_seal_open(dst, O_WRONLY | O_CREAT |
+ (exclusive ? O_EXCL : O_TRUNC), mode);
+ if (out < 0) { close(in); return out; }
while ((n = read(in, buf, sizeof(buf))) > 0) {
rc = shirudo_write_all(out, buf, (size_t)n);
if (rc)
@@ -234,21 +234,6 @@ static int copy_file(const char *src, const char *dst, mode_t mode)
return copy_file_ex(src, dst, mode, false);
}
-static int atomic_write(const char *path, const void *data, size_t len, mode_t mode)
-{
- char tmp[PATH_MAX];
- int fd, rc = 0;
- if (snprintf(tmp, sizeof(tmp), "%s.tmp.%ld", path, (long)getpid()) >= (int)sizeof(tmp)) return -ENAMETOOLONG;
- fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
- if (fd < 0) return -errno;
- rc = shirudo_write_all(fd, data, len);
- if (!rc && fsync(fd)) rc = -errno;
- close(fd);
- if (!rc && rename(tmp, path)) rc = -errno;
- if (rc) unlink(tmp);
- return rc;
-}
-
static int source_path(const char *name, char *out, size_t out_sz)
{
if (!bpm_name_ok(name)) return -EINVAL;
@@ -286,15 +271,15 @@ static int save_source(const struct bpm_source *s)
int rc = source_path(s->name, path, sizeof(path));
if (rc) return rc;
if (snprintf(data, sizeof(data), "endpoint = %s\nkey_file = %s\n", s->endpoint, s->key_file) >= (int)sizeof(data)) return -E2BIG;
- return atomic_write(path, data, strlen(data), 0644);
+ return shirudo_seal_write_file(path, data, strlen(data), 0644);
}
static int with_bpm_lock(void)
{
int fd;
{ int rc = bpm_mkdir_p(SHIRUDO_LOCKS_DIR, 0700); if (rc) return rc; }
- fd = open(SHIRUDO_BPM_LOCK_PATH, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
- if (fd < 0) return -errno;
+ fd = shirudo_seal_open(SHIRUDO_BPM_LOCK_PATH, O_RDWR | O_CREAT, 0600);
+ if (fd < 0) return fd;
if (flock(fd, LOCK_EX)) { int rc = -errno; close(fd); return rc; }
return fd;
}
@@ -501,13 +486,14 @@ static int install_key_file(const char *src)
if (rc)
return rc;
}
- if (!active && symlink(dst, SHIRUDO_CERT_PATH)) {
+ if (active)
+ return 0;
+ if (symlink(dst, SHIRUDO_CERT_PATH)) {
if (errno != EEXIST)
return -errno;
- if (cert_matches_active(src))
- return -EPERM;
+ return cert_matches_active(src) ? -EPERM : 0;
}
- return 0;
+ return shirudo_seal_inherit_path(SHIRUDO_CERT_PATH);
}
static int extract_package_cert(const char *archive, char *out, size_t out_sz)
@@ -699,7 +685,9 @@ static int state_write(const struct bpm_state_entry *st, int n)
shirudo_json_obj_close(&e);
if (e.err) return -E2BIG;
rc = bpm_mkdir_p(SHIRUDO_BPM_VAR_DIR, 0700);
- if (!rc) rc = atomic_write(SHIRUDO_BPM_STATE_PATH, buf, e.len, 0600);
+ if (!rc)
+ rc = shirudo_seal_write_file(SHIRUDO_BPM_STATE_PATH, buf, e.len,
+ 0600);
return rc;
}
@@ -785,7 +773,7 @@ static int repo_update_one(const char *name)
if (snprintf(dir, sizeof(dir), "%s/%s", SHIRUDO_BPM_REPOS_DIR, name) >= (int)sizeof(dir)) { rc = -ENAMETOOLONG; goto out; }
if ((rc = bpm_mkdir_p(dir, 0700))) goto out;
if ((rc = cache_path(name, cache, sizeof(cache)))) goto out;
- rc = atomic_write(cache, index.data, index.len, 0600);
+ rc = shirudo_seal_write_file(cache, index.data, index.len, 0600);
out:
bpm_buffer_free(&manifest);
bpm_buffer_free(&index);
@@ -1193,10 +1181,9 @@ static int tree_pull_artifact(const struct shirudo_tree_operation *op,
rc = -errno;
goto out;
}
- rc = atomic_write(cache, blob.data, blob.len, 0600);
+ rc = shirudo_seal_write_file(cache, blob.data, blob.len, 0600);
if (rc)
goto out;
- (void)shirudo_seal_label(cache);
bpm_copy_string(path, path_sz, cache);
rc = tree_verify_upatch(path, op->artifact.payload_sha256, op->id,
!strcmp(op->op, "delete"));
@@ -1234,7 +1221,7 @@ static int tree_write_report(const char *path, const struct shirudo_tree_state *
if (e.err)
return e.err;
if (path)
- return atomic_write(path, buf, e.len, 0600);
+ return shirudo_seal_write_file(path, buf, e.len, 0600);
printf("%s\n", buf);
return 0;
}
@@ -1243,7 +1230,6 @@ static int tree_write_pending(const struct shirudo_tree_operation *op)
{
char buf[1024];
struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
- int rc;
shirudo_json_obj_open(&e);
shirudo_json_kv_int(&e, "schema", 1);
@@ -1255,11 +1241,8 @@ static int tree_write_pending(const struct shirudo_tree_operation *op)
return e.err;
if (bpm_mkdir_p(SHIRUDO_BPM_TREE_VAR_DIR, 0700))
return -errno;
- rc = atomic_write(SHIRUDO_BPM_TREE_PENDING_PATH, buf, e.len, 0600);
- if (rc)
- return rc;
- (void)shirudo_seal_label(SHIRUDO_BPM_TREE_PENDING_PATH);
- return 0;
+ return shirudo_seal_write_file(SHIRUDO_BPM_TREE_PENDING_PATH, buf,
+ e.len, 0600);
}
static int tree_status_write(const char *id, const char *status)
@@ -1275,7 +1258,7 @@ static int tree_status_write(const char *id, const char *status)
if (snprintf(path, sizeof(path), "%s/%s",
SHIRUDO_BPM_TREE_STATUS_DIR, id) >= (int)sizeof(path))
return -ENAMETOOLONG;
- return atomic_write(path, status, strlen(status), 0600);
+ return shirudo_seal_write_file(path, status, strlen(status), 0600);
}
static void tree_status_clear(const char *id)
diff --git a/builtin-keys.c b/builtin-keys.c
index b08a9a32..6a8d41df 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -39,6 +39,7 @@
#include "util/macros.h"
#include "util/malloc.h"
#include "util/paths.h"
+#include "util/seal.h"
#include "util/upatch.h"
static void openssl_complain(const char *where)
@@ -948,10 +949,10 @@ static int keys_revoke(int argc, const char **argv)
}
/* The marker first: whatever happens next, no boot enrols it again. */
- if (mkdir(SHIRUDO_KEYS_REVOKED_DIR, 0700) && errno != EEXIST) {
+ rc = shirudo_seal_mkdir_p(SHIRUDO_KEYS_REVOKED_DIR, 0700);
+ if (rc) {
pr_err("keys revoke: mkdir %s: %s", SHIRUDO_KEYS_REVOKED_DIR,
- strerror(errno));
- rc = -errno;
+ strerror(-rc));
goto out;
}
rc = shirudo_keys_path_for(SHIRUDO_KEYS_REVOKED_DIR, fp, ".der",
diff --git a/builtin-upatch.c b/builtin-upatch.c
index dd4a5adb..0c708923 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2554,11 +2554,11 @@ static int copy_file_atomic(const char *src, const char *dst)
in = open(src, O_RDONLY | O_CLOEXEC);
if (in < 0)
return -errno;
- out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
+ out = shirudo_seal_open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW,
+ 0644);
if (out < 0) {
- rc = -errno;
close(in);
- return rc;
+ return out;
}
while ((r = read(in, buf, sizeof(buf))) > 0) {
rc = shirudo_write_all(out, buf, (size_t)r);
@@ -2601,7 +2601,7 @@ static int persist_upatch(const char *src, const char *name)
struct stat ss, ds;
int rc;
- rc = mkdir_if_absent(SHIRUDO_UPATCHES_DIR, 0700);
+ rc = shirudo_seal_mkdir_p(SHIRUDO_UPATCHES_DIR, 0700);
if (rc) {
pr_err("mkdir %s: %s", SHIRUDO_UPATCHES_DIR, strerror(-rc));
return rc;
@@ -2614,14 +2614,7 @@ static int persist_upatch(const char *src, const char *name)
ss.st_ino == ds.st_ino && ss.st_dev == ds.st_dev)
return 0; /* already the installed copy */
- rc = copy_file_atomic(src, dst);
- if (rc)
- return rc;
- /* Bring the new file under the seal LSM. */
- if (shirudo_seal_label(dst) != 0)
- pr_warn("upatch: could not seal-label %s: %s", dst,
- strerror(errno));
- return 0;
+ return copy_file_atomic(src, dst);
}
/*
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 8931f2b4..e1ff63b2 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -23,6 +23,7 @@
#include "bpm_oci.h"
#include "crypto.h"
+#include "seal.h"
#include "json.h"
#include "log.h"
#include "macros.h"
@@ -45,23 +46,7 @@ void bpm_copy_string(char *dst, size_t dst_sz, const char *src)
int bpm_mkdir_p(const char *path, mode_t mode)
{
- char tmp[PATH_MAX];
- char *p;
-
- if (!path || strlen(path) >= sizeof(tmp))
- return -ENAMETOOLONG;
- snprintf(tmp, sizeof(tmp), "%s", path);
- for (p = tmp + 1; *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(tmp, mode) && errno != EEXIST)
- return -errno;
- *p = '/';
- }
- if (mkdir(tmp, mode) && errno != EEXIST)
- return -errno;
- return 0;
+ return shirudo_seal_mkdir_p(path, mode);
}
static bool bpm_component_ok(const char *s, bool plus)
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 2a85a807..3a08b60b 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -80,7 +80,6 @@ void bpm_trim(const char *in, char *out, size_t out_sz);
bool bpm_name_ok(const char *s);
bool bpm_version_ok(const char *s);
bool bpm_package_name_ok(const char *s);
-int bpm_mkdir_p(const char *path, mode_t mode);
/* Whole file into an xmalloc'd buffer of out_len + 1 bytes (NUL added). */
int bpm_read_file(const char *path, unsigned char **out, size_t *out_len,
size_t max);
@@ -109,6 +108,8 @@ int bpm_oci_request(struct bpm_oci_client *c, const char *method,
/* 2xx fills out; 404 is -ENOENT, other failures -EACCES. */
int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
const char *accept, struct bpm_buffer *out);
+/* mkdir -p under the state tree: born sealed as the parent is. */
+int bpm_mkdir_p(const char *path, mode_t mode);
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
const char *accept, const char *path);
int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 680db410..2c20649f 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -32,27 +32,6 @@
#include "paths.h"
#include "seal.h"
-static int tree_mkdir_p(const char *path, mode_t mode)
-{
- char tmp[PATH_MAX];
- char *p;
-
- if (!path || strlen(path) >= sizeof(tmp))
- return -ENAMETOOLONG;
- strcpy(tmp, path);
- for (p = tmp + 1; *p; p++) {
- if (*p != '/')
- continue;
- *p = '\0';
- if (mkdir(tmp, mode) && errno != EEXIST)
- return -errno;
- *p = '/';
- }
- if (mkdir(tmp, mode) && errno != EEXIST)
- return -errno;
- return 0;
-}
-
static int kv_add(struct shirudo_tree_kv *dst, int *n, int max,
const char *key, const char *value)
{
@@ -459,38 +438,20 @@ int shirudo_tree_state_read(struct shirudo_tree_state *out)
static int tree_atomic_write(const char *path, const void *data, size_t len)
{
- char tmp[PATH_MAX];
- char dir[PATH_MAX];
const char *slash = strrchr(path, '/');
- int fd, dirfd = -1, rc;
- if (!slash || (size_t)(slash - path) >= sizeof(dir)) return -EINVAL;
- memcpy(dir, path, slash - path); dir[slash - path] = '\0';
- {
- int mkdir_rc = tree_mkdir_p(dir, 0700);
- if (mkdir_rc) {
- pr_err("tree state mkdir %s rc=%d errno=%d", dir, mkdir_rc, errno);
- return mkdir_rc;
- }
- }
- if (snprintf(tmp, sizeof(tmp), "%s/.tmp.%ld", dir, (long)getpid()) >= (int)sizeof(tmp)) return -ENAMETOOLONG;
- fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
- if (fd < 0) return -errno;
- rc = shirudo_write_all(fd, data, len);
- if (!rc && fsync(fd)) rc = -errno;
- if (close(fd) && !rc) rc = -errno;
- if (!rc && rename(tmp, path)) rc = -errno;
- if (!rc) {
- dirfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
- if (dirfd < 0)
- rc = -errno;
- else if (fsync(dirfd))
- rc = -errno;
- if (dirfd >= 0 && close(dirfd) && !rc)
- rc = -errno;
+ char dir[PATH_MAX];
+ int rc;
+
+ if (!slash || (size_t)(slash - path) >= sizeof(dir))
+ return -EINVAL;
+ memcpy(dir, path, slash - path);
+ dir[slash - path] = '\0';
+ rc = shirudo_seal_mkdir_p(dir, 0700);
+ if (rc) {
+ pr_err("tree state mkdir %s: %s", dir, strerror(-rc));
+ return rc;
}
- if (rc) unlink(tmp);
- if (!rc) (void)shirudo_seal_label(path);
- return rc;
+ return shirudo_seal_write_file(path, data, len, 0600);
}
int shirudo_tree_state_write(const struct shirudo_tree_state *s)
@@ -527,17 +488,17 @@ int shirudo_tree_event_append(int64_t seq, const char *operation_digest,
shirudo_json_kv_string(&e, "projection_root", root);
shirudo_json_obj_close(&e);
if (e.err) return e.err;
- {
- int mkdir_rc = tree_mkdir_p(SHIRUDO_BPM_TREE_VAR_DIR, 0700);
- if (mkdir_rc) return mkdir_rc;
- }
- fd = open(SHIRUDO_BPM_TREE_EVENTLOG_PATH, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);
- if (fd < 0) return -errno;
+ rc = shirudo_seal_mkdir_p(SHIRUDO_BPM_TREE_VAR_DIR, 0700);
+ if (rc)
+ return rc;
+ fd = shirudo_seal_open(SHIRUDO_BPM_TREE_EVENTLOG_PATH,
+ O_WRONLY | O_CREAT | O_APPEND, 0600);
+ if (fd < 0)
+ return fd;
rc = shirudo_write_all(fd, buf, e.len);
if (!rc) rc = shirudo_write_all(fd, "\n", 1);
if (!rc && fsync(fd)) rc = -errno;
if (close(fd) && !rc) rc = -errno;
- if (!rc) (void)shirudo_seal_label(SHIRUDO_BPM_TREE_EVENTLOG_PATH);
return rc;
}
diff --git a/util/eventlog.c b/util/eventlog.c
index bac648fd..add713e5 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -49,10 +49,10 @@ int shirudo_eventlog_append_to(const char *path, const char *json_line)
size_t len = strlen(json_line);
int fd, rc;
- fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);
+ fd = shirudo_seal_open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);
if (fd < 0) {
- pr_err("eventlog: open %s: %s", path, strerror(errno));
- return -errno;
+ pr_err("eventlog: open %s: %s", path, strerror(-fd));
+ return fd;
}
rc = shirudo_write_all(fd, json_line, len);
@@ -67,10 +67,6 @@ int shirudo_eventlog_append_to(const char *path, const char *json_line)
pr_err("eventlog: append: %s", strerror(-rc));
return rc;
}
- /* Keep the eventlog sealed (idempotent; no-op if seal not yet up). */
- if (shirudo_seal_label(path) != 0)
- pr_warn("eventlog: could not seal-label %s: %s", path,
- strerror(errno));
return 0;
}
diff --git a/util/keys.c b/util/keys.c
index cd165c03..050bc4b3 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -20,6 +20,7 @@
#include "keys.h"
#include "malloc.h"
#include "paths.h"
+#include "seal.h"
/* Files under trusted/ a load looks at, members or not. */
#define KEYS_SCAN_MAX 64
@@ -431,24 +432,7 @@ void shirudo_keys_set_digest(const struct shirudo_trusted_set *set,
/* Write whole, then rename into place; a stale .tmp is overwritten. */
int shirudo_keys_write_file(const char *path, const void *data, size_t len)
{
- char tmp[PATH_MAX];
- int fd, rc;
-
- if (snprintf(tmp, sizeof(tmp), "%s.tmp", path) >= (int)sizeof(tmp))
- return -ENAMETOOLONG;
- fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
- if (fd < 0)
- return -errno;
- rc = shirudo_write_all(fd, data, len);
- if (!rc && fsync(fd))
- rc = -errno;
- if (close(fd) && !rc)
- rc = -errno;
- if (!rc && rename(tmp, path))
- rc = -errno;
- if (rc)
- unlink(tmp);
- return rc;
+ return shirudo_seal_write_file(path, data, len, 0644);
}
int shirudo_keys_install(const char *keys_dir, const unsigned char *der,
@@ -466,8 +450,9 @@ int shirudo_keys_install(const char *keys_dir, const unsigned char *der,
if (snprintf(dir, sizeof(dir), "%s/trusted", keys_dir) >=
(int)sizeof(dir))
return -ENAMETOOLONG;
- if (mkdir(dir, 0700) && errno != EEXIST)
- return -errno;
+ rc = shirudo_seal_mkdir_p(dir, 0700);
+ if (rc)
+ return rc;
rc = shirudo_keys_path_for(dir, fingerprint, ".der", out, cap);
if (!rc)
rc = shirudo_keys_path_for(dir, fingerprint, ".vouch", vouch,
diff --git a/util/seal.c b/util/seal.c
index d03a054b..da2d2228 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -6,18 +6,24 @@
*/
#include <errno.h>
+#include <fcntl.h>
#include <ftw.h>
#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <unistd.h>
+#include "io.h"
#include "log.h"
#include "paths.h"
#include "seal.h"
+#define SEAL_LABEL_LEN (sizeof(SHIRUDO_SEAL_LABEL) - 1)
+
int shirudo_seal_label(const char *path)
{
if (lsetxattr(path, SHIRUDO_XATTR_NAME, SHIRUDO_SEAL_LABEL,
@@ -72,3 +78,195 @@ int shirudo_seal_label_tree(void)
return -errno;
return 0;
}
+
+/*
+ * The length of an inode's label, 0 when it carries none or one of
+ * another size, which the seal takes for none, -errno for a failed read.
+ */
+static ssize_t seal_label_len(ssize_t n)
+{
+ if (n >= 0)
+ return n == (ssize_t)SEAL_LABEL_LEN ? n : 0;
+ if (errno == ENODATA || errno == ENOTSUP || errno == ERANGE)
+ return 0;
+ return -errno;
+}
+
+static ssize_t seal_fget(int fd, char label[SEAL_LABEL_LEN])
+{
+ return seal_label_len(fgetxattr(fd, SHIRUDO_XATTR_NAME, label,
+ SEAL_LABEL_LEN));
+}
+
+static ssize_t seal_lget(const char *path, char label[SEAL_LABEL_LEN])
+{
+ return seal_label_len(lgetxattr(path, SHIRUDO_XATTR_NAME, label,
+ SEAL_LABEL_LEN));
+}
+
+/* Open the directory of path to create in it; *name is the last component. */
+static int seal_open_parent(const char *path, const char **name)
+{
+ const char *slash = strrchr(path, '/');
+ char dir[PATH_MAX];
+ size_t len;
+ int fd;
+
+ if (!slash) {
+ *name = path;
+ fd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ } else {
+ len = slash == path ? 1 : (size_t)(slash - path);
+ if (len >= sizeof(dir))
+ return -ENAMETOOLONG;
+ memcpy(dir, path, len);
+ dir[len] = '\0';
+ *name = slash + 1;
+ fd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ }
+ if (fd < 0)
+ return -errno;
+ if (!**name) {
+ close(fd);
+ return -EINVAL;
+ }
+ return fd;
+}
+
+int shirudo_seal_inherit(int dfd, int fd)
+{
+ char parent[SEAL_LABEL_LEN], own[SEAL_LABEL_LEN];
+ ssize_t plen, olen;
+
+ plen = seal_fget(dfd, parent);
+ if (plen <= 0)
+ return (int)plen;
+ olen = seal_fget(fd, own);
+ if (olen < 0)
+ return (int)olen;
+ if (olen == plen && !memcmp(parent, own, (size_t)plen))
+ return 0;
+ if (fsetxattr(fd, SHIRUDO_XATTR_NAME, parent, (size_t)plen, 0))
+ return -errno;
+ return 0;
+}
+
+int shirudo_seal_inherit_path(const char *path)
+{
+ char parent[SEAL_LABEL_LEN], own[SEAL_LABEL_LEN];
+ ssize_t plen, olen;
+ const char *name;
+ int dfd;
+
+ dfd = seal_open_parent(path, &name);
+ if (dfd < 0)
+ return dfd;
+ plen = seal_fget(dfd, parent);
+ close(dfd);
+ if (plen <= 0)
+ return (int)plen;
+ olen = seal_lget(path, own);
+ if (olen < 0)
+ return (int)olen;
+ if (olen == plen && !memcmp(parent, own, (size_t)plen))
+ return 0;
+ if (lsetxattr(path, SHIRUDO_XATTR_NAME, parent, (size_t)plen, 0))
+ return -errno;
+ return 0;
+}
+
+int shirudo_seal_open(const char *path, int flags, mode_t mode)
+{
+ const char *name;
+ int dfd, fd, rc;
+
+ dfd = seal_open_parent(path, &name);
+ if (dfd < 0)
+ return dfd;
+ fd = openat(dfd, name, flags | O_CLOEXEC, mode);
+ if (fd < 0) {
+ rc = -errno;
+ close(dfd);
+ return rc;
+ }
+ rc = shirudo_seal_inherit(dfd, fd);
+ close(dfd);
+ if (rc) {
+ close(fd);
+ return rc;
+ }
+ return fd;
+}
+
+int shirudo_seal_mkdir_p(const char *path, mode_t mode)
+{
+ char buf[PATH_MAX], *comp, *save;
+ int dfd, nfd, rc = 0;
+ bool created;
+
+ if (snprintf(buf, sizeof(buf), "%s", path) >= (int)sizeof(buf))
+ return -ENAMETOOLONG;
+ dfd = open(buf[0] == '/' ? "/" : ".",
+ O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (dfd < 0)
+ return -errno;
+ for (comp = strtok_r(buf, "/", &save); comp;
+ comp = strtok_r(NULL, "/", &save)) {
+ created = mkdirat(dfd, comp, mode) == 0;
+ if (!created && errno != EEXIST) {
+ rc = -errno;
+ break;
+ }
+ nfd = openat(dfd, comp, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (nfd < 0) {
+ rc = -errno;
+ break;
+ }
+ if (created)
+ rc = shirudo_seal_inherit(dfd, nfd);
+ close(dfd);
+ dfd = nfd;
+ if (rc)
+ break;
+ }
+ close(dfd);
+ return rc;
+}
+
+int shirudo_seal_write_file(const char *path, const void *data, size_t len,
+ mode_t mode)
+{
+ char tmp[NAME_MAX + 1];
+ const char *name;
+ int dfd, fd, rc;
+
+ dfd = seal_open_parent(path, &name);
+ if (dfd < 0)
+ return dfd;
+ if (snprintf(tmp, sizeof(tmp), "%s.tmp", name) >= (int)sizeof(tmp)) {
+ close(dfd);
+ return -ENAMETOOLONG;
+ }
+ fd = openat(dfd, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC |
+ O_NOFOLLOW, mode);
+ if (fd < 0) {
+ rc = -errno;
+ close(dfd);
+ return rc;
+ }
+ rc = shirudo_seal_inherit(dfd, fd);
+ if (!rc)
+ rc = shirudo_write_all(fd, data, len);
+ if (!rc && fsync(fd))
+ rc = -errno;
+ if (close(fd) && !rc)
+ rc = -errno;
+ if (!rc && renameat(dfd, tmp, dfd, name))
+ rc = -errno;
+ if (!rc && fsync(dfd))
+ rc = -errno;
+ if (rc)
+ unlinkat(dfd, tmp, 0);
+ close(dfd);
+ return rc;
+}
diff --git a/util/seal.h b/util/seal.h
index 76cefcda..f63cb9dd 100644
--- a/util/seal.h
+++ b/util/seal.h
@@ -8,6 +8,9 @@
#ifndef SHIRUDO_SEAL_H
#define SHIRUDO_SEAL_H
+#include <stddef.h>
+#include <sys/types.h>
+
/*
* The label value: SHA-256 of the xattr name, 64 hex characters, the
* same for every build. bpf/seal.c takes the label, whatever its value,
@@ -33,4 +36,35 @@ int shirudo_seal_label(const char *path);
*/
int shirudo_seal_label_tree(void);
+/*
+ * Creating under a sealed directory. The directory decides: an inode
+ * created under a labelled directory carries that directory's label,
+ * which the seal writes inside the transaction that creates it, on the
+ * strength of the mark the directory's open leaves. So these helpers
+ * open the directory first and hold it open across the creation, which
+ * also keeps its inode and the mark on it in memory, and put the label
+ * on afterwards where the kernel did not: no seal attached, a file
+ * system that takes no xattrs at creation. All return -errno on
+ * failure, a sealed inode that could not be labelled included.
+ */
+
+/* Open or create path under its directory. Returns the fd. */
+int shirudo_seal_open(const char *path, int flags, mode_t mode);
+
+/* mkdir -p, every directory created labelled as its parent is. */
+int shirudo_seal_mkdir_p(const char *path, mode_t mode);
+
+/*
+ * Replace path by data atomically: written to <path>.tmp beside it,
+ * labelled and synced, renamed over path, the directory synced.
+ */
+int shirudo_seal_write_file(const char *path, const void *data, size_t len,
+ mode_t mode);
+
+/* Give the inode behind fd the label of its directory, open as dfd. */
+int shirudo_seal_inherit(int dfd, int fd);
+
+/* The same by path, without following a symlink. */
+int shirudo_seal_inherit_path(const char *path);
+
#endif /* SHIRUDO_SEAL_H */
diff --git a/util/serializer.c b/util/serializer.c
index 78675e6e..78ceef7b 100644
--- a/util/serializer.c
+++ b/util/serializer.c
@@ -10,6 +10,7 @@
#include <unistd.h>
#include "paths.h"
+#include "seal.h"
#include "serializer.h"
int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id)
@@ -20,8 +21,9 @@ int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id)
if (!dir || !dir[0] || map_id == 0)
return -EINVAL;
- if (mkdir(dir, 0700) != 0 && errno != EEXIST)
- return -errno;
+ rc = shirudo_seal_mkdir_p(dir, 0700);
+ if (rc)
+ return rc;
dir_fd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
if (dir_fd < 0)
return -errno;
@@ -31,9 +33,17 @@ int shirudo_serializer_lock_acquire_at(const char *dir, uint32_t map_id)
return -ENAMETOOLONG;
}
fd = openat(dir_fd, name, O_CREAT | O_RDWR | O_CLOEXEC | O_NOFOLLOW, 0600);
+ if (fd < 0) {
+ rc = -errno;
+ close(dir_fd);
+ return rc;
+ }
+ rc = shirudo_seal_inherit(dir_fd, fd);
close(dir_fd);
- if (fd < 0)
- return -errno;
+ if (rc) {
+ close(fd);
+ return rc;
+ }
if (fstat(fd, &st) != 0) {
rc = -errno;
close(fd);