Skip to content

eBPF-based security platform

upatch: open an XDP program pin in one place

John Fastabendea9c84cb6942
diff --git a/testing/unit/upatch_pins.c b/testing/unit/upatch_pins.c
index 893ba7e9..a5e6b9b2 100644
--- a/testing/unit/upatch_pins.c
+++ b/testing/unit/upatch_pins.c
@@ -113,6 +113,15 @@ static void xdp_program_pins_have_one_name(void)
-ENAMETOOLONG);
}
+static void an_unpinned_xdp_program_does_not_open(void)
+{
+ __u32 id = 0;
+
+ EXPECT_TRUE(upatch_xdp_prog_open("shirudo-unit-no-such-upatch", 0,
+ &id) < 0);
+ EXPECT_EQ(id, 0);
+}
+
static void link_pins_read_back_as_links(void)
{
struct upatch_pins pins = {};
@@ -162,6 +171,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(an_unpinned_xdp_program_does_not_open),
UNIT_CASE(link_pins_read_back_as_links),
UNIT_CASE(payload_of_absent_upatch_is_enoent),
UNIT_CASE(no_pin_directory_is_no_upatch),
diff --git a/testing/unit/upatch_verify.c b/testing/unit/upatch_verify.c
index fb503fe2..f2da5c7a 100644
--- a/testing/unit/upatch_verify.c
+++ b/testing/unit/upatch_verify.c
@@ -144,15 +144,6 @@ static void specs_of_a_missing_archive_are_stale(void)
EXPECT_TRUE(!specs);
}
-static void an_unpinned_xdp_program_does_not_open(void)
-{
- __u32 id = 0;
-
- EXPECT_TRUE(upatch_xdp_prog_open("shirudo-unit-no-such-upatch", 0,
- &id) < 0);
- EXPECT_EQ(id, 0);
-}
-
static int never(const char *name, void *ctx)
{
return 1;
@@ -178,7 +169,6 @@ static const struct unit_case cases[] = {
UNIT_CASE(a_missing_archive_is_stale),
UNIT_CASE(links_of_a_missing_archive_are_stale),
UNIT_CASE(specs_of_a_missing_archive_are_stale),
- UNIT_CASE(an_unpinned_xdp_program_does_not_open),
UNIT_CASE(nothing_pinned_checks_clean),
{}
};
diff --git a/util/upatch_pins.c b/util/upatch_pins.c
index dfe3fe06..d8eb25d3 100644
--- a/util/upatch_pins.c
+++ b/util/upatch_pins.c
@@ -11,6 +11,10 @@
#include <stdint.h>
#include <string.h>
#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <bpf/bpf.h>
+#include <linux/bpf.h>
#include <shirudo/string.h>
#include <shirudo/upatch.h>
@@ -144,6 +148,29 @@ int upatch_xdp_prog_pin(char *out, size_t cap, const char *name, __u32 index)
return 0;
}
+int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id)
+{
+ struct bpf_prog_info info = {};
+ __u32 len = sizeof(info);
+ char pin[PATH_MAX];
+ int fd, ret;
+
+ if (upatch_xdp_prog_pin(pin, sizeof(pin), name, index))
+ return -ENAMETOOLONG;
+ fd = bpf_obj_get(pin);
+ if (fd < 0)
+ return -errno;
+ ret = bpf_prog_get_info_by_fd(fd, &info, &len);
+ if (!ret && info.type != BPF_PROG_TYPE_XDP)
+ ret = -ESTALE;
+ if (ret) {
+ close(fd);
+ return ret;
+ }
+ *id = info.id;
+ return fd;
+}
+
int upatch_pin_payload(const char *name, __u8 out[UPATCH_PAYLOAD_HASH_LEN])
{
char dir[PATH_MAX];
diff --git a/util/upatch_pins.h b/util/upatch_pins.h
index fb5de6b5..80def07b 100644
--- a/util/upatch_pins.h
+++ b/util/upatch_pins.h
@@ -52,6 +52,12 @@ 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);
+/*
+ * The fd of XDP program index pinned for name, its id in *id, or -errno;
+ * -ESTALE when the pin holds a program that is not XDP.
+ */
+int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id);
+
/*
* The payload hash a load set on the pin directory of name: 0,
* -ENAMETOOLONG, -EINVAL when it is not a hash, or the -errno of the read.
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index 707e30de..f48b4b0d 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -88,10 +88,8 @@ static const char *loc_prefix(__u32 prog_type, __u32 attach_type)
int upatch_first_prog_id(const char *name, __u32 *out)
{
struct bpf_link_info linfo = {};
- struct bpf_prog_info info = {};
__u32 len = sizeof(linfo);
struct upatch_pins pins;
- char pin[PATH_MAX];
int fd, ret;
*out = 0;
@@ -111,18 +109,11 @@ int upatch_first_prog_id(const char *name, __u32 *out)
return ret;
if (!pins.nr_xdp_progs)
return -ENOENT;
- if (upatch_xdp_prog_pin(pin, sizeof(pin), name, pins.first_xdp_prog))
- return -ENAMETOOLONG;
- fd = bpf_obj_get(pin);
+ fd = upatch_xdp_prog_open(name, pins.first_xdp_prog, out);
if (fd < 0)
- return -errno;
- len = sizeof(info);
- ret = bpf_prog_get_info_by_fd(fd, &info, &len);
+ return fd;
close(fd);
- if (ret)
- return ret;
- *out = info.id;
- return info.id ? 0 : -EINVAL;
+ return *out ? 0 : -EINVAL;
}
struct xdp_links {
diff --git a/util/upatch_verify.c b/util/upatch_verify.c
index 9d5008b4..4639d703 100644
--- a/util/upatch_verify.c
+++ b/util/upatch_verify.c
@@ -11,8 +11,6 @@
#include <sys/stat.h>
#include <unistd.h>
-#include <bpf/bpf.h>
-#include <bpf/libbpf.h>
#include <linux/bpf.h>
#include <shirudo/misc.h>
@@ -197,29 +195,6 @@ int upatch_archive_specs(const char *name,
return ret;
}
-int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id)
-{
- struct bpf_prog_info info = {};
- __u32 len = sizeof(info);
- char pin[PATH_MAX];
- int fd, ret;
-
- if (upatch_xdp_prog_pin(pin, sizeof(pin), name, index))
- return -ENAMETOOLONG;
- fd = bpf_obj_get(pin);
- if (fd < 0)
- return -errno;
- ret = bpf_prog_get_info_by_fd(fd, &info, &len);
- if (!ret && info.type != BPF_PROG_TYPE_XDP)
- ret = -ESTALE;
- if (ret) {
- close(fd);
- return ret;
- }
- *id = info.id;
- return fd;
-}
-
int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,
__u32 nr_progs, const struct xdp_device *devices,
size_t count,
diff --git a/util/upatch_verify.h b/util/upatch_verify.h
index bfb0b5a2..39ff0e78 100644
--- a/util/upatch_verify.h
+++ b/util/upatch_verify.h
@@ -44,9 +44,6 @@ int upatch_archive_specs(const char *name,
const __u8 payload[UPATCH_PAYLOAD_HASH_LEN],
struct upatch *u, struct prog_spec **specs);
-/* The fd of XDP program index pinned for name, its id in *id, or -errno. */
-int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id);
-
/*
* Calls cb for each device an XDP program of name selects and that does not
* run it, with what the device runs instead as devices were read, 0 for