Skip to content

eBPF-based security platform

upatch: report the runs the kernel skipped of kprobe multi links

Jiri Olsa602fa0c72488
diff --git a/builtin-status.c b/builtin-status.c
index 05aaef93..934ef794 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -1632,6 +1632,8 @@ static size_t build_upatches_table(const struct status_view *v,
upatch_maps_list_one(name, &list, &nm);
r->n_maps = (__u32)nm;
map_summary_free(list, nm);
+ r->missed_ret = upatch_missed_runs(name, &r->missed_counted,
+ &r->missed_runs);
r->age_sec = upatch_age_sec(r->loaded_at);
}
btf__free(vbtf);
diff --git a/util/upatch_resolve.c b/util/upatch_resolve.c
index f7fff5ef..686d4d34 100644
--- a/util/upatch_resolve.c
+++ b/util/upatch_resolve.c
@@ -340,6 +340,114 @@ int upatch_resolve_location(struct btf *vbtf, const char *name,
return ret;
}
+bool upatch_link_counts_misses(const struct bpf_link_info *info)
+{
+ switch (info->type) {
+ case BPF_LINK_TYPE_KPROBE_MULTI:
+ return true;
+ default:
+ return false;
+ }
+}
+
+__u64 upatch_link_missed(const struct bpf_link_info *info)
+{
+ if (info->type == BPF_LINK_TYPE_KPROBE_MULTI)
+ return info->kprobe_multi.missed;
+ return 0;
+}
+
+/* 1 for a link of a kind whose skipped runs the kernel does not count. */
+static int link_misses(int fd, struct upatch_link_misses *m)
+{
+ struct bpf_link_info info = {};
+ struct bpf_prog_info prog = {};
+ __u32 len = sizeof(info);
+ int ret;
+
+ ret = bpf_link_get_info_by_fd(fd, &info, &len);
+ if (ret)
+ return ret;
+ if (!upatch_link_counts_misses(&info))
+ return 1;
+ ret = prog_info_by_id(info.prog_id, &prog);
+ if (ret)
+ return ret;
+ m->missed = prog.recursion_misses + upatch_link_missed(&info);
+ return 0;
+}
+
+struct misses_walk {
+ int (*cb)(const struct upatch_link_misses *m, void *ctx);
+ void *ctx;
+};
+
+static int read_link_misses(__u32 index, const char *pin, void *ctx)
+{
+ struct upatch_link_misses m = {};
+ struct misses_walk *w = ctx;
+ int fd, ret;
+
+ fd = bpf_obj_get(pin);
+ if (fd < 0)
+ return -errno;
+ ret = link_misses(fd, &m);
+ close(fd);
+ if (ret)
+ return min(ret, 0);
+ return w->cb(&m, w->ctx);
+}
+
+/* One link is pinned as "link", several as "link_<n>". */
+static int walk_link_misses(const char *name, struct misses_walk *w)
+{
+ struct upatch_pins pins;
+ char pin[PATH_MAX];
+ int ret;
+
+ ret = upatch_pins_read(name, &pins);
+ if (ret)
+ return ret;
+ if (pins.has_link) {
+ if (upatch_pin_path(pin, sizeof(pin), name, "link", NULL))
+ return -ENAMETOOLONG;
+ ret = read_link_misses(0, pin, w);
+ if (ret)
+ return ret;
+ }
+ return upatch_each_link(name, read_link_misses, w);
+}
+
+struct misses_sum {
+ __u32 nr_counted;
+ __u64 missed;
+};
+
+static int add_link_misses(const struct upatch_link_misses *m, void *ctx)
+{
+ struct misses_sum *sum = ctx;
+
+ sum->nr_counted++;
+ sum->missed += m->missed;
+ return 0;
+}
+
+int upatch_missed_runs(const char *name, __u32 *nr_counted, __u64 *missed)
+{
+ struct misses_sum sum = {};
+ struct misses_walk w = { .cb = add_link_misses, .ctx = &sum };
+ int ret;
+
+ *nr_counted = 0;
+ *missed = 0;
+ ret = walk_link_misses(name, &w);
+ if (ret)
+ return ret;
+ *nr_counted = sum.nr_counted;
+ *missed = sum.missed;
+ return 0;
+}
+
void upatch_loaded_at(const char *name, char *out, size_t cap)
{
struct upatch_pins pins;
diff --git a/util/upatch_resolve.h b/util/upatch_resolve.h
index e346360f..09e39396 100644
--- a/util/upatch_resolve.h
+++ b/util/upatch_resolve.h
@@ -8,6 +8,7 @@
#ifndef SHIRUDO_UTIL_UPATCH_RESOLVE_H
#define SHIRUDO_UTIL_UPATCH_RESOLVE_H
+#include <stdbool.h>
#include <stddef.h>
#include <shirudo/types.h>
@@ -40,6 +41,33 @@ int upatch_first_prog_id(const char *name, __u32 *out);
int upatch_resolve_location(struct btf *vbtf, const char *name,
char *out, size_t cap);
+/*
+ * The runs the kernel skipped of one program of an upatch: the program's
+ * recursion_misses and what its kprobe multi link counts itself.
+ */
+struct upatch_link_misses {
+ __u64 missed;
+};
+
+/*
+ * Whether the runs the kernel skips of the program behind this link are
+ * reported: kprobe.multi and kretprobe.multi. The kernel skips no lsm,
+ * xdp, cgroup_skb or netfilter run, and counts none it skips of the
+ * uprobe kinds.
+ */
+bool upatch_link_counts_misses(const struct bpf_link_info *info);
+
+/* What the link counts itself, beside its program's recursion_misses. */
+__u64 upatch_link_missed(const struct bpf_link_info *info);
+
+/*
+ * The sum over the links of name that upatch_link_counts_misses(), with
+ * the number of links it counted: 0 for an upatch of no kind the kernel
+ * counts, whose missed is then 0 without meaning that nothing was
+ * skipped. A link or program that cannot be read fails with its -errno.
+ */
+int upatch_missed_runs(const char *name, __u32 *nr_counted, __u64 *missed);
+
void upatch_loaded_at(const char *name, char *out, size_t cap);
/* Empty on any failure. */
diff --git a/util/upatch_table.c b/util/upatch_table.c
index f9d7c17d..dbf9b3a2 100644
--- a/util/upatch_table.c
+++ b/util/upatch_table.c
@@ -10,6 +10,7 @@
#include <time.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "json.h"
#include "timefmt.h"
@@ -26,6 +27,17 @@ __s64 upatch_age_sec(const char *ts)
return (__s64)(time(NULL) - timegm(&tm));
}
+/* "?" when they could not be read, "-" for an upatch of no counted kind. */
+static const char *missed_text(const struct upatch_table_row *r, char *buf,
+ size_t cap)
+{
+ if (r->missed_ret)
+ return "?";
+ if (!r->missed_counted)
+ return "-";
+ return strsprintf(buf, cap, "%llu", r->missed_runs) < 0 ? "?" : buf;
+}
+
void upatch_table_print(const struct upatch_table_row *rows,
size_t n)
{
@@ -36,18 +48,19 @@ void upatch_table_print(const struct upatch_table_row *rows,
printf(" none\n");
return;
}
- printf(" %-24s %-28s %5s %-28s %s\n",
- "NAME", "LOCATION", "MAPS", "STATUS", "AGE");
+ printf(" %-24s %-28s %5s %6s %-28s %s\n",
+ "NAME", "LOCATION", "MAPS", "MISSED", "STATUS", "AGE");
for (i = 0; i < n; i++) {
const struct upatch_table_row *r = &rows[i];
- char age[16] = "?";
+ char age[16] = "?", missed[24];
if (r->age_sec >= 0)
format_duration((long)r->age_sec,
age, sizeof(age));
- printf(" %-24s %-28s %5u %-28s %s\n", r->name,
- r->location[0] ? r->location : "-",
- r->n_maps, r->status[0] ? r->status : "success", age);
+ printf(" %-24s %-28s %5u %6s %-28s %s\n", r->name,
+ r->location[0] ? r->location : "-", r->n_maps,
+ missed_text(r, missed, sizeof(missed)),
+ r->status[0] ? r->status : "success", age);
}
}
@@ -67,6 +80,10 @@ void upatch_table_emit_json(struct json_emit *e,
json_kv_string(e, "status",
r->status[0] ? r->status : "success");
json_kv_int(e, "n_maps", (__s64)r->n_maps);
+ if (!r->missed_ret && r->missed_counted)
+ json_kv_u64(e, "missed_runs", r->missed_runs);
+ else
+ json_kv_null(e, "missed_runs");
json_kv_string(e, "loaded_at", r->loaded_at);
if (r->age_sec >= 0)
json_kv_int(e, "age_sec", r->age_sec);
diff --git a/util/upatch_table.h b/util/upatch_table.h
index ea94f475..ee48f09f 100644
--- a/util/upatch_table.h
+++ b/util/upatch_table.h
@@ -23,6 +23,9 @@ struct upatch_table_row {
char loaded_at[32]; /* ISO-8601 UTC, "" if unknown */
char status[64]; /* success or a tree skip reason */
__u32 n_maps;
+ int missed_ret; /* of upatch_missed_runs() */
+ __u32 missed_counted; /* links it counted, 0 for none */
+ __u64 missed_runs;
__s64 age_sec; /* derived from loaded_at; -1 if unknown */
char payload_sha256[65]; /* "" if unavailable; JSON only */
};