Skip to content

eBPF-based security platform

upatch: read the archive as sealed state where it is run or measured

Daniel Borkmann9554062cb426
diff --git a/Documentation/telemetry.rst b/Documentation/telemetry.rst
index 4444cce9..c8ccde5c 100644
--- a/Documentation/telemetry.rst
+++ b/Documentation/telemetry.rst
@@ -103,7 +103,9 @@ there is no decoder search path and nothing to drop in.
The decoder runs under LuaJIT with the standard library and an
instruction budget, and is loaded as source text alone: LuaJIT verifies
-no bytecode it loads, so a bytecode chunk is refused. Its entry point receives one table::
+no bytecode it loads, so a bytecode chunk is refused. The archived
+artifact it is read from is opened as sealed state, so an entry under
+the archive that is not shirudo's own is refused rather than run. Its entry point receives one table::
input.schema, input.upatch, input.map
input.startTimeUnixNano, input.timeUnixNano
diff --git a/Makefile.shirudo b/Makefile.shirudo
index dc879c67..9a15bbe0 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -811,7 +811,7 @@ testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializ
testing/helper/test-upatch-decode: testing/helper/test-upatch-decode.c \
util/upatch_decode.o util/upatch.o util/malloc.o util/hex.o \
- util/string.o
+ util/string.o util/seal.o util/io.o util/log.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 2f9b7cb1..972033a6 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2906,14 +2906,19 @@ int shirudo_upatch_purge(const char *name, const char *reason)
char persisted[PATH_MAX];
struct shirudo_upatch u;
bool loaded;
- int rc;
+ int fd, rc;
if (!safe_upatch_name(name))
return -EINVAL;
if (strsprintf(persisted, sizeof(persisted), "%s/%s.upatch",
SHIRUDO_UPATCHES_DIR, name) < 0)
return -ENAMETOOLONG;
- rc = upatch_open(persisted, &u);
+ rc = seal_open_read(persisted);
+ if (rc < 0)
+ return rc;
+ fd = rc;
+ rc = upatch_open_fd(fd, &u);
+ close(fd);
if (rc)
return rc;
memcpy(payload, u.hdr.payload_sha256, sizeof(payload));
diff --git a/util/upatch.c b/util/upatch.c
index ea8ab285..696c4231 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -289,35 +289,22 @@ int upatch_open_memory(const void *data, size_t size,
return upatch_parse(data, size, out);
}
-int upatch_open(const char *path, struct shirudo_upatch *out)
+int upatch_open_fd(int fd, struct shirudo_upatch *out)
{
struct stat st;
void *m;
- int fd;
int rc;
memset(out, 0, sizeof(*out));
-
- fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
+ if (fstat(fd, &st) != 0)
return -errno;
- if (fstat(fd, &st) != 0) {
- rc = -errno;
- close(fd);
- return rc;
- }
- if (!S_ISREG(st.st_mode) || st.st_size <= 0) {
- close(fd);
+ if (!S_ISREG(st.st_mode) || st.st_size <= 0)
return -EINVAL;
- }
/* refuse pathologically-large inputs; .upatch is small */
- if ((uintmax_t)st.st_size > SHIRUDO_UPATCH_FILE_MAX) {
- close(fd);
+ if ((uintmax_t)st.st_size > SHIRUDO_UPATCH_FILE_MAX)
return -EFBIG;
- }
m = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- close(fd);
if (m == MAP_FAILED)
return -errno;
@@ -330,6 +317,19 @@ int upatch_open(const char *path, struct shirudo_upatch *out)
return 0;
}
+int upatch_open(const char *path, struct shirudo_upatch *out)
+{
+ int fd, rc;
+
+ memset(out, 0, sizeof(*out));
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ rc = upatch_open_fd(fd, out);
+ close(fd);
+ return rc;
+}
+
void upatch_close(struct shirudo_upatch *u)
{
if (u->map && u->mapped)
diff --git a/util/upatch.h b/util/upatch.h
index 15b7b74d..ec054605 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -51,6 +51,7 @@ struct upatch_decoder_view {
* decides whether to emit user-facing diagnostics.
*/
int upatch_open(const char *path, struct shirudo_upatch *out);
+int upatch_open_fd(int fd, struct shirudo_upatch *out);
int upatch_open_memory(const void *data, size_t size,
struct shirudo_upatch *out);
void upatch_close(struct shirudo_upatch *u);
diff --git a/util/upatch_decode.c b/util/upatch_decode.c
index 393f5a94..08ae9482 100644
--- a/util/upatch_decode.c
+++ b/util/upatch_decode.c
@@ -73,6 +73,7 @@ int upatch_decode_map_file_otel(const char *path,
#include <stdio.h>
#include <string.h>
#include <time.h>
+#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
@@ -87,6 +88,7 @@ int upatch_decode_map_file_otel(const char *path,
#include "macros.h"
#include "malloc.h"
#include "paths.h"
+#include "seal.h"
#include "upatch.h"
#include "upatch_decode.h"
#include "upatch_maps.h"
@@ -1291,7 +1293,29 @@ static int decoder_matches(const struct upatch_decoder_view *d,
return 1;
}
-static int decode_map_file(const char *path, const struct shirudo_map_view *v,
+/*
+ * The archived artifact the decoders come from, opened as sealed state
+ * when archived: an entry under the archive that is not shirudo's own,
+ * brought in while the directory's mark was gone, is refused rather
+ * than run, signed or not.
+ */
+static int archive_open(const char *path, bool archived,
+ struct shirudo_upatch *u)
+{
+ int fd, rc;
+
+ if (!archived)
+ return upatch_open(path, u);
+ fd = seal_open_read(path);
+ if (fd < 0)
+ return fd;
+ rc = upatch_open_fd(fd, u);
+ close(fd);
+ return rc;
+}
+
+static int decode_map_open(const char *path, bool archived,
+ const struct shirudo_map_view *v,
bool otel, size_t max_bytes,
char **out, size_t *out_len)
{
@@ -1305,7 +1329,7 @@ static int decode_map_file(const char *path, const struct shirudo_map_view *v,
*out_len = 0;
if (!upatch_name_valid(v->upatch))
return -EINVAL;
- rc = upatch_open(path, &u);
+ rc = archive_open(path, archived, &u);
if (rc)
return rc == -ENOENT ? -ENOENT : rc;
rc = -ENODATA;
@@ -1323,6 +1347,13 @@ static int decode_map_file(const char *path, const struct shirudo_map_view *v,
return rc;
}
+static int decode_map_file(const char *path, const struct shirudo_map_view *v,
+ bool otel, size_t max_bytes,
+ char **out, size_t *out_len)
+{
+ return decode_map_open(path, false, v, otel, max_bytes, out, out_len);
+}
+
int upatch_decode_map_signal(const struct shirudo_map_view *v,
__u32 *signal_out)
{
@@ -1336,7 +1367,7 @@ int upatch_decode_map_signal(const struct shirudo_map_view *v,
if (strsprintf(path, sizeof(path), "%s/%s.upatch", SHIRUDO_UPATCHES_DIR,
v->upatch) < 0)
return -ENAMETOOLONG;
- rc = upatch_open(path, &u);
+ rc = archive_open(path, true, &u);
if (rc)
return rc == -ENOENT ? -ENOENT : rc;
rc = -ENODATA;
@@ -1364,7 +1395,7 @@ static int decode_map(const struct shirudo_map_view *v, bool otel,
if (strsprintf(path, sizeof(path), "%s/%s.upatch", SHIRUDO_UPATCHES_DIR,
v->upatch) < 0)
return -ENAMETOOLONG;
- return decode_map_file(path, v, otel, max_bytes, out, out_len);
+ return decode_map_open(path, true, v, otel, max_bytes, out, out_len);
}
int upatch_decode_map_json(const struct shirudo_map_view *v,