eBPF-based security platform
upatch: name a numbered link pin in one place
John Fastabend8c1da557a3f6
diff --git a/builtin-upatch.c b/builtin-upatch.cindex 69bc9fef..0861244a 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1313,22 +1313,24 @@ static void count_attach_progs(const struct prog_spec *specs,*out = n;}+/* entry in dir: 0 or -ENAMETOOLONG, as upatch_link_pin() returns. */+static int path_in(char *out, size_t sz, const char *dir, const char *entry)+{+ return strsprintf(out, sz, "%s/%s", dir, entry) < 0 ? -ENAMETOOLONG : 0;+}+static int prog_pin_path(char *pin, size_t sz, const char *dir,const char *serializers_dir,const struct prog_spec *spec,__u32 attach_count, __u32 attach_idx){- int n;-if (spec->role == UPATCH_PROG_ROLE_SERIALIZER)- n = strsprintf(pin, sz, "%s/%s", serializers_dir, spec->map);- else if (spec->role == UPATCH_PROG_ROLE_TEST_RUN)- n = strsprintf(pin, sz, "%s/test", dir);- else if (attach_count == 1)- n = strsprintf(pin, sz, "%s/link", dir);- else- n = strsprintf(pin, sz, "%s/link_%u", dir, attach_idx);- return n < 0 ? -ENAMETOOLONG : 0;+ return path_in(pin, sz, serializers_dir, spec->map);+ if (spec->role == UPATCH_PROG_ROLE_TEST_RUN)+ return path_in(pin, sz, dir, "test");+ if (attach_count == 1)+ return path_in(pin, sz, dir, "link");+ return upatch_link_pin(pin, sz, dir, attach_idx);}/*@@ -1410,7 +1412,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,continue;if (*pinned == UINT32_MAX)return -EOVERFLOW;- if (strsprintf(pin, sizeof(pin), "%s/link_%u", dir, *pinned) < 0)+ if (upatch_link_pin(pin, sizeof(pin), dir, *pinned))return -ENAMETOOLONG;ret = xdp_attach_one(prog_fd, spec, dev, pin);if (ret == XDP_ERR_NO_NATIVE) {@@ -1564,11 +1566,10 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,ret = -EOVERFLOW;goto out;}- if (strsprintf(pin, sizeof(pin), "%s/link_%u", dir,- pinned_links) < 0) {- ret = -ENAMETOOLONG;+ ret = upatch_link_pin(pin, sizeof(pin), dir,+ pinned_links);+ if (ret)goto out;- }}link_fd = prog_attach_for_spec(inner_fd, spec);@@ -1630,8 +1631,8 @@ out:if (ret) {while (pinned_links) {pinned_links--;- if (strsprintf(pin, sizeof(pin), "%s/link_%u", dir,- pinned_links) >= 0)+ if (!upatch_link_pin(pin, sizeof(pin), dir,+ pinned_links))unlink(pin);}attach_seen = 0;@@ -4400,7 +4401,7 @@ static int reconcile_attach(int prog_fd, const char *name,if (*next == UINT32_MAX ||upatch_pin_path(dir, sizeof(dir), name, NULL) ||- strsprintf(pin, sizeof(pin), "%s/link_%u", dir, *next) < 0) {+ upatch_link_pin(pin, sizeof(pin), dir, *next)) {rep_fail(rep, "link_create", "unknown", ENAMETOOLONG,"no pin name left for the link");return 1;diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.cindex 2b2bde35..28844c19 100644--- a/testing/unit/upatch_pins.c+++ b/testing/unit/upatch_pins.c@@ -113,6 +113,20 @@ static void xdp_program_pins_have_one_name(void)-ENAMETOOLONG);}+static void link_pins_read_back_as_links(void)+{+ struct upatch_pins pins = {};+ char pin[64], small[8];++ ASSERT_EQ(upatch_link_pin(pin, sizeof(pin), "/d", 4294967295U), 0);+ EXPECT_STREQ(pin, "/d/link_4294967295");+ upatch_pins_note(&pins, pin + strlen("/d/"));+ EXPECT_UEQ(pins.nr_links, 1);+ EXPECT_UEQ(pins.last_link, 4294967295U);+ EXPECT_EQ(upatch_link_pin(small, sizeof(small), "/d", 10),+ -ENAMETOOLONG);+}+static int count_pinned(const char *name, void *ctx){(*(int *)ctx)++;@@ -136,6 +150,7 @@ static const struct unit_case cases[] = {UNIT_CASE(complete_means_every_link),UNIT_CASE(absent_upatch_has_no_pins),UNIT_CASE(xdp_program_pins_have_one_name),+ UNIT_CASE(link_pins_read_back_as_links),UNIT_CASE(no_pin_directory_is_no_upatch),{}};diff --git a/util/paths.h b/util/paths.hindex a4eb1ed7..d445f717 100644--- a/util/paths.h+++ b/util/paths.h@@ -41,6 +41,7 @@#define RUN_DIR "/run/shirudo"#define BPFFS_DIR RUN_DIR "/bpffs"#define XDP_PROG_PIN_PREFIX "xdp_prog_"+#define LINK_PIN_PREFIX "link_"/* The init record written from the initrd, until the root adopts it. */#define EVENTLOG_PENDING_PATH RUN_DIR "/eventlog.pending"/* On a target with a config image these are the image's, opened from its root. */diff --git a/util/upatch_pins.c b/util/upatch_pins.cindex 59e86785..3ff023ac 100644--- a/util/upatch_pins.c+++ b/util/upatch_pins.c@@ -39,7 +39,7 @@ void upatch_pins_note(struct upatch_pins *pins, const char *entry)if (!strcmp(entry, "link")) {pins->has_link = true;- } else if (pin_index(entry, "link_", &index)) {+ } else if (pin_index(entry, LINK_PIN_PREFIX, &index)) {if (!pins->nr_links || index < pins->first_link)pins->first_link = index;if (!pins->nr_links || index > pins->last_link)@@ -116,7 +116,7 @@ int upatch_each_link(const char *name,while (!ret && (e = readdir(d))) {__u32 index;- if (!pin_index(e->d_name, "link_", &index))+ if (!pin_index(e->d_name, LINK_PIN_PREFIX, &index))continue;if (strsprintf(pin, sizeof(pin), "%s/%s", dir, e->d_name) < 0)ret = -ENAMETOOLONG;@@ -127,6 +127,12 @@ int upatch_each_link(const char *name,return ret;}+int upatch_link_pin(char *out, size_t cap, const char *dir, __u32 index)+{+ return strsprintf(out, cap, "%s/" LINK_PIN_PREFIX "%u", dir, index) < 0 ?+ -ENAMETOOLONG : 0;+}+int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index){char entry[32];diff --git a/util/upatch_pins.h b/util/upatch_pins.hindex b5d2a6cb..45826f77 100644--- a/util/upatch_pins.h+++ b/util/upatch_pins.h@@ -45,6 +45,9 @@ int upatch_each_link(const char *name,int (*cb)(__u32 index, const char *pin, void *ctx),void *ctx);+/* Numbered link index in the pin directory dir: 0 or -ENAMETOOLONG. */+int upatch_link_pin(char *out, size_t cap, const char *dir, __u32 index);+/* The pin that keeps XDP program index of name loaded: 0 or -ENAMETOOLONG. */int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index);