Skip to content

eBPF-based security platform

seal: take a lock and append to a sealed log in one place

Daniel Borkmannfce0afbbd7f1
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index e0b81d5a..97945d9b 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -107,7 +107,8 @@ extra check left to the caller, and move one found in a builtin into
| Smaller or larger | `min()`, `max()` (`<shirudo/misc.h>`) |
| Memory | `xmalloc()`, `xcalloc()`, `xreallocarray()`, `xstrndup()`, `xasprintf()`, `xfree(p, size)` (`util/malloc.h`) |
| Secrets | `memzero_explicit()`, `memneq_explicit()` |
-| Files | `read_file()`, `read_fd()`, `write_complete()`, `append_line()`, `lines_len()`, `open_beneath()` (`util/io.h`) |
+| Files | `read_file()`, `read_fd()`, `write_complete()`, `append_line()`, `lines_len()`, `open_beneath()`, `lock_exclusive()` (`util/io.h`) |
+| Sealed state and paths root names | `seal_open()`, `seal_lock()`, `seal_append_line()`, `seal_write_file()`; `seal_output_create()` and `seal_output_commit()` for a report or export (`util/seal.h`) |
| Bytes on the wire | `get_le32()`, `put_be32()`, `le32_to_cpu()` and kin (`<shirudo/byteorder.h>`), `hex_encode()`, `hex_decode()`, `hex_decode_max()`, `sha256_digest_decode()` (`util/hex.h`) |
| JSON | `util/json.h`: `json_parse()` with a schema, `json_walk()`, `struct json_emit` |
| Messages | `pr_err()`, `pr_warn()`, `pr_info()`, `pr_audit()` (`util/log.h`) |
diff --git a/builtin-bpm.c b/builtin-bpm.c
index e512b87c..2512379a 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -456,20 +456,10 @@ static int save_source(const struct bpm_source *s)
static int with_bpm_lock(void)
{
- int fd, ret;
+ int ret;
ret = seal_mkdir_p(LOCKS_DIR, 0700);
- if (ret)
- return ret;
- fd = seal_open(BPM_LOCK_PATH, O_RDWR | O_CREAT, 0600);
- if (fd < 0)
- return fd;
- if (flock(fd, LOCK_EX)) {
- ret = -errno;
- close(fd);
- return ret;
- }
- return fd;
+ return ret ? ret : seal_lock(BPM_LOCK_PATH);
}
static int bpm_fail(const char *action, int ret)
diff --git a/builtin-upatch.c b/builtin-upatch.c
index e292c9df..20db3d97 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2463,12 +2463,9 @@ static int append_json_record(const char *path, const char *json, size_t len)
fd = seal_output_open(path, O_RDWR | O_CREAT | O_APPEND, 0644);
if (fd < 0)
return fd;
- while (flock(fd, LOCK_EX) != 0) {
- if (errno != EINTR) {
- ret = -errno;
- goto out;
- }
- }
+ ret = lock_exclusive(fd);
+ if (ret)
+ goto out;
if (fstat(fd, &st) != 0) {
ret = -errno;
goto out_unlock;
diff --git a/testing/unit/io.c b/testing/unit/io.c
index 6fd05108..40e81c2d 100644
--- a/testing/unit/io.c
+++ b/testing/unit/io.c
@@ -4,6 +4,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
+#include <sys/file.h>
#include <sys/sysmacros.h>
#include <unistd.h>
@@ -95,6 +96,22 @@ static void append_line_takes_one_line_alone(void)
close(fd);
}
+static void lock_exclusive_keeps_others_out(void)
+{
+ int fd = log_with("", 0), other;
+ char path[64];
+
+ ASSERT_TRUE(strsprintf(path, sizeof(path), "/proc/self/fd/%d", fd) > 0);
+ other = open(path, O_RDONLY | O_CLOEXEC);
+ ASSERT_TRUE(other >= 0);
+ EXPECT_EQ(lock_exclusive(fd), 0);
+ EXPECT_EQ(flock(other, LOCK_EX | LOCK_NB), -1);
+ EXPECT_EQ(errno, EWOULDBLOCK);
+ close(fd);
+ EXPECT_EQ(lock_exclusive(other), 0);
+ close(other);
+}
+
static void kernel_dev_is_major_over_20_minor_bits(void)
{
EXPECT_EQ(kernel_dev(makedev(8, 1)), 0x800001U);
@@ -117,6 +134,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(append_line_adds_one_record),
UNIT_CASE(append_line_drops_what_did_not_finish),
UNIT_CASE(append_line_takes_one_line_alone),
+ 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),
{}
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 320cf755..5a0236df 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -14,7 +14,6 @@
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
-#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
@@ -659,7 +658,7 @@ int tree_event_append(__s64 seq, const char *operation_digest,
char buf[1024], root[65];
struct json_emit e = { .buf = buf, .cap = sizeof(buf) };
__u8 digest[32];
- int fd, ret;
+ int ret;
if (sha256_digest_decode(operation_digest, digest))
return -EINVAL;
@@ -675,22 +674,7 @@ int tree_event_append(__s64 seq, const char *operation_digest,
ret = seal_mkdir_p(BPM_TREE_VAR_DIR, 0700);
if (ret)
return ret;
- fd = seal_open(BPM_TREE_EVENTLOG_PATH,
- O_RDWR | O_CREAT | O_APPEND, 0600);
- if (fd < 0)
- return fd;
- /* A partial line would fail every later replay. */
- while (flock(fd, LOCK_EX) != 0) {
- if (errno != EINTR) {
- ret = -errno;
- goto out;
- }
- }
- ret = append_line(fd, BPM_TREE_EVENTLOG_PATH, buf, e.len);
-out:
- if (close(fd) && !ret)
- ret = -errno;
- return ret;
+ return seal_append_line(BPM_TREE_EVENTLOG_PATH, buf, e.len);
}
int tree_event_replay(__u8 out[32])
diff --git a/util/eventlog.c b/util/eventlog.c
index b0d72903..667a4711 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -12,7 +12,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/file.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
@@ -73,28 +72,12 @@ int eventlog_append(const char *json_line)
*/
int eventlog_append_to(const char *path, const char *json_line)
{
- int fd, ret;
+ int ret;
- fd = seal_open(path, O_RDWR | O_CREAT | O_APPEND, 0600);
- if (fd < 0) {
- pr_err("eventlog: open %s: %s", path, strerror(-fd));
- return fd;
- }
- while (flock(fd, LOCK_EX) != 0) {
- if (errno != EINTR) {
- ret = -errno;
- goto out;
- }
- }
- ret = append_line(fd, path, json_line, strlen(json_line));
-out:
- if (close(fd) != 0 && !ret)
- ret = -errno;
- if (ret) {
- pr_err("eventlog: append: %s", strerror(-ret));
- return ret;
- }
- return 0;
+ ret = seal_append_line(path, json_line, strlen(json_line));
+ if (ret)
+ pr_err("eventlog: append to %s: %s", path, strerror(-ret));
+ return ret;
}
int eventlog_would_exceed(__u64 max_bytes)
@@ -327,21 +310,10 @@ static int adopt_pending(void)
int eventlog_lock(void)
{
char path[PATH_MAX];
- int fd, ret;
if (strsprintf(path, sizeof(path), "%s.lock", eventlog_path()) < 0)
return -ENAMETOOLONG;
- fd = seal_open(path, O_RDWR | O_CREAT, 0600);
- if (fd < 0)
- return fd;
- while (flock(fd, LOCK_EX)) {
- if (errno != EINTR) {
- ret = -errno;
- close(fd);
- return ret;
- }
- }
- return fd;
+ return seal_lock(path);
}
void eventlog_unlock(int fd)
diff --git a/util/io.c b/util/io.c
index bd12e6a3..a627faee 100644
--- a/util/io.c
+++ b/util/io.c
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/file.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/syscall.h>
@@ -296,3 +297,12 @@ int bpffs_mounted_at(const char *path)
close(fd);
return ret;
}
+
+int lock_exclusive(int fd)
+{
+ while (flock(fd, LOCK_EX)) {
+ if (errno != EINTR)
+ return -errno;
+ }
+ return 0;
+}
diff --git a/util/io.h b/util/io.h
index efe660a3..28bd3c27 100644
--- a/util/io.h
+++ b/util/io.h
@@ -47,6 +47,9 @@ int read_text_line(FILE *f, char **line, size_t *cap, size_t max);
*/
int check_trusted(int fd, const char *path, bool secret);
+/* An exclusive flock() on fd, waited for through signals. 0 or -errno. */
+int lock_exclusive(int fd);
+
/* A st_dev or st_rdev as the kernel encodes the dev_t in s_dev. */
__u32 kernel_dev(dev_t dev);
diff --git a/util/seal.c b/util/seal.c
index c622a9b3..3c31a360 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -357,6 +357,36 @@ out_dir:
return ret;
}
+int seal_lock(const char *path)
+{
+ int fd, ret;
+
+ fd = seal_open(path, O_RDWR | O_CREAT, 0600);
+ if (fd < 0)
+ return fd;
+ ret = lock_exclusive(fd);
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+ return fd;
+}
+
+int seal_append_line(const char *path, const char *line, size_t len)
+{
+ int fd, ret;
+
+ fd = seal_open(path, O_RDWR | O_CREAT | O_APPEND, 0600);
+ if (fd < 0)
+ return fd;
+ ret = lock_exclusive(fd);
+ if (!ret)
+ ret = append_line(fd, path, line, len);
+ if (close(fd) && !ret)
+ ret = -errno;
+ return ret;
+}
+
int seal_output_open(const char *path, int flags, mode_t mode)
{
return output_open(path, flags, mode, NULL, NULL);
diff --git a/util/seal.h b/util/seal.h
index 1d0b9268..a3ab189f 100644
--- a/util/seal.h
+++ b/util/seal.h
@@ -62,6 +62,19 @@ bool seal_owns(const char *path);
int seal_write_file(const char *path, const void *data, size_t len,
mode_t mode);
+/*
+ * The lock file at path, created sealed when it is not there, held
+ * exclusively until the returned descriptor is closed; or -errno.
+ */
+int seal_lock(const char *path);
+
+/*
+ * line appended as one record to the sealed log at path, created when it
+ * is not there, under the lock that keeps its appenders apart; see
+ * append_line(). 0 or -errno.
+ */
+int seal_append_line(const char *path, const char *line, size_t len);
+
/*
* 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