Skip to content

eBPF-based security platform

upatch: replace a dead load's pins, read errno first, align the loader

Daniel Borkmann227b9ce557a1
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 7219abec..0c480361 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1389,8 +1389,16 @@ static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,
strerror(-rc));
goto out;
}
- if (stat(pin, &st) == 0)
- continue; /* idempotent re-run */
+ /*
+ * A pin a load left when it died before its link is
+ * bound to maps nothing reaches any more: replace it.
+ */
+ if (stat(pin, &st) == 0 && unlink(pin)) {
+ rc = -errno;
+ pr_err("unlink stale serializer pin %s: %s", pin,
+ strerror(-rc));
+ goto out;
+ }
rc = obj_pin(inner_fd, pin);
if (rc) {
pr_err("pin serializer %s: %s", pin,
@@ -1405,8 +1413,12 @@ static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,
struct stat st;
test_fd = inner_fd;
- if (stat(pin, &st) == 0)
- continue;
+ if (stat(pin, &st) == 0 && unlink(pin)) {
+ rc = -errno;
+ pr_err("unlink stale test-run pin %s: %s", pin,
+ strerror(-rc));
+ goto out;
+ }
rc = obj_pin(inner_fd, pin);
if (rc) {
pr_err("pin test-run program %s: %s", pin,
@@ -1884,9 +1896,11 @@ static int upatch_apply(const char *path, bool verify_only,
/* The kernel refuses a signed load whose bound map is not frozen. */
rc = loader_map_freeze(map_fd);
if (rc) {
- pr_err("__loader.map freeze: %s", strerror(errno));
- rep_fail(rep, "signed_loader_load", cat_from_load_errno(errno),
- errno, "__loader.map freeze failed");
+ int err = errno;
+
+ pr_err("__loader.map freeze: %s", strerror(err));
+ rep_fail(rep, "signed_loader_load", cat_from_load_errno(err),
+ err, "__loader.map freeze failed");
rc = 1;
goto out;
}
@@ -1959,10 +1973,12 @@ static int upatch_apply(const char *path, bool verify_only,
rc = prog_test_run(prog_fd, cx, (__u32)ctx_sz, &retval);
if (rc) {
- pr_err("BPF_PROG_TEST_RUN: %s", strerror(errno));
+ int err = errno;
+
+ pr_err("BPF_PROG_TEST_RUN: %s", strerror(err));
if (vlog[0])
fprintf(stderr, "loader runtime log:\n%s\n", vlog);
- rep_fail(rep, "payload_load", "verifier_rejected", errno,
+ rep_fail(rep, "payload_load", "verifier_rejected", err,
"BPF_PROG_TEST_RUN failed");
report_set_log(rep, vlog);
rc = 1;
diff --git a/util/upatch.c b/util/upatch.c
index 696c4231..06606814 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -42,6 +42,8 @@ static int parse_header(const __u8 *m, size_t n,
p += 4;
memcpy(out->name, p, sizeof(out->name));
+ if (!memchr(out->name, '\0', sizeof(out->name)))
+ return -EINVAL;
p += sizeof(out->name);
memcpy(out->payload_sha256, p, sizeof(out->payload_sha256));
@@ -96,6 +98,7 @@ static int validate_bounds(const struct shirudo_upatch_header *h, size_t n)
if (h->type != SHIRUDO_UPATCH_TYPE_BPF)
return -EINVAL;
if (h->loader_insns_off < SHIRUDO_UPATCH_HEADER_SIZE ||
+ h->loader_insns_off % 8 || h->loader_insns_len % 8 ||
!section_inside(h->loader_insns_off, h->loader_insns_len, n))
return -EINVAL;
if (!section_inside(h->signature_off, h->signature_len, n))