Skip to content

eBPF-based security platform

util: one whole-file reader

Daniel Borkmann79f63cf687d4
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 273583fc..5547322d 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -535,7 +535,7 @@ testing/helper/test-keys: testing/helper/test-keys.c util/keys.o \
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
testing/helper/test-seal: testing/helper/test-seal.c util/seal.o util/io.o \
- util/log.o
+ util/log.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
@@ -628,7 +628,7 @@ testing/helper/test-upatch-decode: testing/helper/test-upatch-decode.c \
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)
-testing/helper/test-exec-churn: testing/helper/test-exec-churn.c util/io.o
+testing/helper/test-exec-churn: testing/helper/test-exec-churn.c util/io.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
diff --git a/builtin-bpm.c b/builtin-bpm.c
index a67fc999..f838f764 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -669,7 +669,8 @@ static int state_read(struct bpm_state_entry *st, int *n)
struct shirudo_json_field sub[4], root[1];
int64_t unused = 0;
- rc = bpm_read_file(SHIRUDO_BPM_STATE_PATH, &body, &len, 8u * 1024u * 1024u);
+ rc = shirudo_read_file(SHIRUDO_BPM_STATE_PATH, 8u * 1024u * 1024u,
+ &body, &len);
if (rc == -ENOENT) { *n = 0; return 0; }
if (rc) return rc;
sub[0] = (struct shirudo_json_field){ .key = "name", .type = SHIRUDO_JSON_T_STRING, .dst = sink.scratch.name, .dst_cap = sizeof(sink.scratch.name) };
@@ -720,7 +721,7 @@ static int load_cached_index(const char *source, struct bpm_index_entry *entries
rc = cache_path(source, path, sizeof(path));
if (rc)
return rc;
- rc = bpm_read_file(path, &body, &len, 64u * 1024u * 1024u);
+ rc = shirudo_read_file(path, 64u * 1024u * 1024u, &body, &len);
if (rc)
return rc;
rc = bpm_index_parse(body, len, entries, n);
@@ -1318,8 +1319,8 @@ static int tree_read_pending(struct bpm_tree_pending *pending)
pending->operation_digest, sizeof(pending->operation_digest),
0, NULL, 0 },
};
- int rc = bpm_read_file(SHIRUDO_BPM_TREE_PENDING_PATH, &body, &len,
- 64u * 1024u);
+ int rc = shirudo_read_file(SHIRUDO_BPM_TREE_PENDING_PATH, 64u * 1024u,
+ &body, &len);
if (rc)
return rc;
rc = shirudo_json_parse((const char *)body, len, fields, ARRAY_SIZE(fields));
diff --git a/builtin-status.c b/builtin-status.c
index e840df6a..2b5d5631 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -36,6 +36,7 @@
#define SHIRUDO_KERNEL_LOG_MAX 100
#include "util/help.h"
+#include "util/io.h"
#include "util/json.h"
#include "util/macros.h"
#include "util/identity.h"
@@ -194,38 +195,6 @@ static int parse_event_line(const char *line, size_t len, struct ev_proj *ev)
return shirudo_json_parse(line, len, f, ARRAY_SIZE(f));
}
-static int read_eventlog(char **buf, size_t *len)
-{
- struct stat st;
- ssize_t got;
- int fd, rc = 0;
-
- *buf = NULL;
- *len = 0;
- fd = open(shirudo_eventlog_path(), O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) != 0 || st.st_size > (off_t)SHIRUDO_JSON_MAX_BYTES) {
- rc = fd < 0 ? -errno : -EFBIG;
- close(fd);
- return rc;
- }
- *buf = xmalloc((size_t)st.st_size + 1);
- if (!*buf) {
- close(fd);
- return -ENOMEM;
- }
- got = read(fd, *buf, (size_t)st.st_size);
- close(fd);
- if (got != st.st_size) {
- xfree(*buf, (size_t)st.st_size + 1);
- *buf = NULL;
- return -EIO;
- }
- (*buf)[got] = '\0';
- *len = (size_t)st.st_size + 1;
- return 0;
-}
static void emit_eventlog_events(struct shirudo_json_emit *e)
{
@@ -233,7 +202,8 @@ static void emit_eventlog_events(struct shirudo_json_emit *e)
size_t len;
shirudo_json_arr_open(e, "events");
- if (read_eventlog(&buf, &len) != 0) {
+ if (shirudo_read_file(shirudo_eventlog_path(), SHIRUDO_JSON_MAX_BYTES,
+ &buf, &len) != 0) {
shirudo_json_arr_close(e);
return;
}
@@ -261,7 +231,7 @@ static void emit_eventlog_events(struct shirudo_json_emit *e)
}
shirudo_json_obj_close(e);
}
- xfree(buf, len);
+ xfree(buf, len + 1);
shirudo_json_arr_close(e);
}
@@ -281,7 +251,8 @@ static void print_eventlog_table(void)
char *save, *line, *buf;
size_t len;
- if (read_eventlog(&buf, &len) != 0)
+ if (shirudo_read_file(shirudo_eventlog_path(), SHIRUDO_JSON_MAX_BYTES,
+ &buf, &len) != 0)
return;
printf("\nEventlog (audit trail):\n");
printf(" %-20s %-13s %-13s %s\n", "TIME", "TYPE", "DIGEST",
@@ -300,7 +271,7 @@ static void print_eventlog_table(void)
printf(" %-20s %-13s %-13.12s %s\n", ev.ts, ev.type,
ev.digest[0] ? ev.digest : "-", detail);
}
- xfree(buf, len);
+ xfree(buf, len + 1);
}
static void print_system_human(const struct shirudo_sysinfo *si)
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 152df37b..53fc6b89 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -338,8 +338,9 @@ static int push_package(const char *package_path, const char *raw_repository,
return -errno;
if (!S_ISREG(st.st_mode))
return -EINVAL;
- if ((rc = bpm_read_file(package_path, &archive, &archive_len,
- BPM_MAX_HTTP_BODY)))
+ rc = shirudo_read_file(package_path, BPM_MAX_HTTP_BODY, &archive,
+ &archive_len);
+ if (rc)
return rc;
if ((rc = bpm_pkg_load_info(package_path, &info)))
goto out;
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index cc1db687..147968cc 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -105,44 +105,6 @@ static void pack_header(struct shirudo_upatch_header *h,
h->reserved1 = 0;
}
-static int read_small_file(const char *path, char **out, uint32_t *out_len)
-{
- struct stat st;
- ssize_t got;
- char *buf;
- int fd;
-
- *out = NULL;
- *out_len = 0;
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) != 0) {
- int e = -errno;
-
- close(fd);
- return e;
- }
- if (!S_ISREG(st.st_mode) || st.st_size <= 0 ||
- st.st_size > 16 * 1024 * 1024) {
- close(fd);
- return -EINVAL;
- }
- buf = xmalloc((size_t)st.st_size);
- if (!buf) {
- close(fd);
- return -ENOMEM;
- }
- got = read(fd, buf, (size_t)st.st_size);
- close(fd);
- if (got != st.st_size) {
- xfree(buf, (size_t)st.st_size);
- return -EIO;
- }
- *out = buf;
- *out_len = (uint32_t)st.st_size;
- return 0;
-}
static bool unsupported_kprobe_section(const char *sec)
{
@@ -452,7 +414,7 @@ static void free_decoder_set(struct decoder_set *set)
for (i = 0; i < set->nr_items; i++) {
xstrfree(set->items[i].path);
- xfree(set->items[i].source, set->items[i].source_len);
+ xfree(set->items[i].source, set->items[i].source_len + 1);
}
xfree(set->items, (size_t)set->nr_items * sizeof(set->items[0]));
memset(set, 0, sizeof(*set));
@@ -465,8 +427,8 @@ static int decoder_set_add(struct decoder_set *set,
struct decoder_file *items;
struct decoder_file *item;
char *path = NULL;
+ size_t source_len = 0;
char *source = NULL;
- uint32_t source_len = 0;
int err;
err = make_decoder_path(src_path, manifest->source, &path);
@@ -474,7 +436,10 @@ static int decoder_set_add(struct decoder_set *set,
pr_err("decoder source path '%s' is invalid", manifest->source);
return err;
}
- err = read_small_file(path, &source, &source_len);
+ err = shirudo_read_file(path, 16u * 1024u * 1024u, &source,
+ &source_len);
+ if (!err && !source_len)
+ err = -EINVAL;
if (err) {
pr_err("read decoder source %s: %s", path, strerror(-err));
xstrfree(path);
@@ -485,7 +450,7 @@ static int decoder_set_add(struct decoder_set *set,
sizeof(set->items[0]));
if (!items) {
xstrfree(path);
- xfree(source, source_len);
+ xfree(source, source_len + 1);
return -ENOMEM;
}
set->items = items;
@@ -493,7 +458,7 @@ static int decoder_set_add(struct decoder_set *set,
item->manifest = *manifest;
item->path = path;
item->source = source;
- item->source_len = source_len;
+ item->source_len = (uint32_t)source_len;
return 0;
}
diff --git a/util/attest.c b/util/attest.c
index b91c3bea..8cffe1dc 100644
--- a/util/attest.c
+++ b/util/attest.c
@@ -21,6 +21,7 @@
#include "crypto.h"
#include "eventlog.h"
#include "hex.h"
+#include "io.h"
#include "json.h"
#include "log.h"
#include "macros.h"
@@ -1086,40 +1087,6 @@ out:
return rc;
}
-static int read_file(const char *path, char **out, size_t *len, size_t *cap)
-{
- struct stat st;
- ssize_t got;
- int fd;
-
- *out = NULL;
- *len = 0;
- *cap = 0;
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) != 0 || st.st_size <= 0 ||
- st.st_size > (off_t)SHIRUDO_ATTEST_CHALLENGE_MAX) {
- close(fd);
- return -EFBIG;
- }
- *out = xmalloc((size_t)st.st_size + 1);
- if (!*out) {
- close(fd);
- return -ENOMEM;
- }
- got = read(fd, *out, (size_t)st.st_size);
- close(fd);
- if (got != st.st_size) {
- xfree(*out, (size_t)st.st_size + 1);
- *out = NULL;
- return -EIO;
- }
- (*out)[got] = '\0';
- *len = (size_t)got;
- *cap = (size_t)st.st_size + 1;
- return 0;
-}
int shirudo_attest_challenge_read(const char *path,
struct shirudo_attest_challenge *out)
@@ -1142,17 +1109,17 @@ int shirudo_attest_challenge_read(const char *path,
{ "secret", SHIRUDO_JSON_T_STRING, secret_hex,
sizeof(secret_hex), 0, NULL, 0 },
};
- size_t len, cap, ak_len;
+ size_t len, ak_len;
char *doc;
int rc;
memset(out, 0, sizeof(*out));
kind[0] = type[0] = ak_hex[0] = blob_hex[0] = secret_hex[0] = '\0';
- rc = read_file(path, &doc, &len, &cap);
+ rc = shirudo_read_file(path, SHIRUDO_ATTEST_CHALLENGE_MAX, &doc, &len);
if (rc)
return rc;
rc = shirudo_json_parse(doc, len, top, ARRAY_SIZE(top));
- xfree(doc, cap);
+ xfree(doc, len + 1);
if (rc || !top[0].seen || strcmp(kind, "shirudo-credential") ||
!ek_sub[0].seen || !top[2].seen || !top[3].seen || !top[4].seen)
return -EINVAL;
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 3f2ac614..fb46d0fe 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -23,6 +23,7 @@
#include "bpm_oci.h"
#include "crypto.h"
+#include "io.h"
#include "seal.h"
#include "json.h"
#include "log.h"
@@ -224,39 +225,6 @@ static int encode_basic(const char *user, const char *pass,
return 0;
}
-int bpm_read_file(const char *path, unsigned char **out, size_t *out_len,
- size_t max)
-{
- struct stat st;
- unsigned char *p;
- ssize_t n;
- int fd;
-
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) || st.st_size < 0 || (uintmax_t)st.st_size > max) {
- int rc = errno ? -errno : -E2BIG;
- close(fd);
- return rc;
- }
- p = xmalloc((size_t)st.st_size + 1);
- if (!p) {
- close(fd);
- return -ENOMEM;
- }
- n = read(fd, p, (size_t)st.st_size);
- close(fd);
- if (n != st.st_size) {
- xfree(p, (size_t)st.st_size + 1);
- return n < 0 ? -errno : -EIO;
- }
- p[n] = 0;
- *out = p;
- *out_len = (size_t)n;
- return 0;
-}
-
static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
{
const char *authfile = getenv("REGISTRY_AUTH_FILE");
@@ -285,7 +253,7 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
return -ENAMETOOLONG;
authfile = path;
}
- rc = bpm_read_file(authfile, &body, &body_len, 4u * 1024u * 1024u);
+ rc = shirudo_read_file(authfile, 4u * 1024u * 1024u, &body, &body_len);
if (rc) return rc == -ENOENT ? 0 : rc;
/*
* The auth file is local input; require the registry key and auth
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 9b59e5b8..66b8102b 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -78,9 +78,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);
-/* 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);
int bpm_sha256_hex(const void *data, size_t len, char *hex, size_t hex_sz);
/* want is "sha256:<hex>" or bare hex; hex is the computed digest. */
bool bpm_digest_matches(const char *want, const char *hex);
diff --git a/util/eventlog.c b/util/eventlog.c
index 9e49d9aa..9d71ee32 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -81,41 +81,6 @@ int shirudo_eventlog_would_exceed(unsigned long max_bytes)
return cur + SHIRUDO_EVENTLOG_LINE_MAX > max_bytes;
}
-static int read_file(const char *path, char **out, size_t *out_len)
-{
- struct stat st;
- ssize_t got;
- char *buf;
- int fd, rc;
-
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) != 0) {
- rc = -errno;
- close(fd);
- return rc;
- }
- if (st.st_size > (off_t)SHIRUDO_JSON_MAX_BYTES) {
- close(fd);
- return -EFBIG;
- }
- buf = xmalloc((size_t)st.st_size + 1);
- if (!buf) {
- close(fd);
- return -ENOMEM;
- }
- got = read(fd, buf, (size_t)st.st_size);
- close(fd);
- if (got != st.st_size) {
- xfree(buf, (size_t)st.st_size + 1);
- return -EIO;
- }
- buf[got] = '\0';
- *out = buf;
- *out_len = (size_t)st.st_size;
- return 0;
-}
static int read_log(char **out, size_t *out_len)
{
@@ -123,11 +88,13 @@ static int read_log(char **out, size_t *out_len)
char *base = NULL, *pending = NULL, *buf;
int rc, rc_pending;
- rc = read_file(shirudo_eventlog_path(), &base, &base_len);
+ rc = shirudo_read_file(shirudo_eventlog_path(), SHIRUDO_JSON_MAX_BYTES,
+ &base, &base_len);
if (rc && rc != -ENOENT)
return rc;
- rc_pending = read_file(shirudo_eventlog_pending_path(), &pending,
- &pending_len);
+ rc_pending = shirudo_read_file(shirudo_eventlog_pending_path(),
+ SHIRUDO_JSON_MAX_BYTES, &pending,
+ &pending_len);
if (rc_pending && rc_pending != -ENOENT) {
xfree(base, base_len + 1);
return rc_pending;
@@ -249,7 +216,8 @@ static int log_holds(const char *line, size_t len)
size_t blen = 0;
int rc;
- rc = read_file(shirudo_eventlog_path(), &buf, &blen);
+ rc = shirudo_read_file(shirudo_eventlog_path(), SHIRUDO_JSON_MAX_BYTES,
+ &buf, &blen);
if (rc == -ENOENT)
return 0;
if (rc)
@@ -268,7 +236,7 @@ int shirudo_eventlog_adopt_pending(void)
size_t len = 0;
int rc;
- rc = read_file(pending, &buf, &len);
+ rc = shirudo_read_file(pending, SHIRUDO_JSON_MAX_BYTES, &buf, &len);
if (rc == -ENOENT)
return 0;
if (rc)
diff --git a/util/io.c b/util/io.c
index 6a89119d..a3a04850 100644
--- a/util/io.c
+++ b/util/io.c
@@ -2,9 +2,13 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <sys/stat.h>
#include <unistd.h>
#include "io.h"
+#include "malloc.h"
int shirudo_write_all(int fd, const void *buf, size_t len)
{
@@ -25,3 +29,51 @@ int shirudo_write_all(int fd, const void *buf, size_t len)
}
return 0;
}
+
+int shirudo_read_file(const char *path, size_t max, void *out, size_t *len)
+{
+ struct stat st;
+ char *buf, *p;
+ size_t left;
+ int fd, rc;
+
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ if (fstat(fd, &st) != 0) {
+ rc = -errno;
+ goto out;
+ }
+ if (!S_ISREG(st.st_mode)) {
+ rc = -EINVAL;
+ goto out;
+ }
+ if ((uintmax_t)st.st_size > max) {
+ rc = -EFBIG;
+ goto out;
+ }
+ buf = xmalloc((size_t)st.st_size + 1);
+ if (!buf) {
+ rc = -ENOMEM;
+ goto out;
+ }
+ for (p = buf, left = (size_t)st.st_size; left; ) {
+ ssize_t n = read(fd, p, left);
+
+ if (n < 0 && errno == EINTR)
+ continue;
+ if (n <= 0) {
+ rc = n < 0 ? -errno : -EIO;
+ xfree(buf, (size_t)st.st_size + 1);
+ goto out;
+ }
+ p += n;
+ left -= (size_t)n;
+ }
+ *(char **)out = buf;
+ *len = (size_t)st.st_size;
+ rc = 0;
+out:
+ close(fd);
+ return rc;
+}
diff --git a/util/io.h b/util/io.h
index 6b434f56..02875822 100644
--- a/util/io.h
+++ b/util/io.h
@@ -8,4 +8,11 @@
int shirudo_write_all(int fd, const void *buf, size_t len);
+/*
+ * A regular file of at most max bytes, into *out xmalloc'd to *len + 1
+ * bytes and NUL-terminated; out is the address of a char or unsigned
+ * char pointer.
+ */
+int shirudo_read_file(const char *path, size_t max, void *out, size_t *len);
+
#endif /* SHIRUDO_IO_H */