From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>, Hao Luo <haoluo@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Matthew Wilcox <willy@infradead.org>
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-perf-users@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Stanislav Fomichev <sdf@google.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Namhyung Kim <namhyung@gmail.com>
Subject: [PATCH RFC v2 bpf-next 8/9] selftests/bpf: Add inode_build_id test
Date: Tue, 28 Feb 2023 10:32:05 +0100 [thread overview]
Message-ID: <20230228093206.821563-9-jolsa@kernel.org> (raw)
In-Reply-To: <20230228093206.821563-1-jolsa@kernel.org>
The test attaches bpf program to sched_process_exec tracepoint
and gets build of executed file from bprm->file->f_inode object.
We use urandom_read as the test program and in addition we also
attach uprobe to liburandom_read.so:urandlib_read_without_sema
and retrieve and check build id of that shared library.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../selftests/bpf/prog_tests/inode_build_id.c | 68 +++++++++++++++++++
.../selftests/bpf/progs/inode_build_id.c | 62 +++++++++++++++++
tools/testing/selftests/bpf/test_progs.h | 10 +++
3 files changed, 140 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/inode_build_id.c
create mode 100644 tools/testing/selftests/bpf/progs/inode_build_id.c
diff --git a/tools/testing/selftests/bpf/prog_tests/inode_build_id.c b/tools/testing/selftests/bpf/prog_tests/inode_build_id.c
new file mode 100644
index 000000000000..d0add90f187d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/inode_build_id.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <unistd.h>
+#include <test_progs.h>
+#include "inode_build_id.skel.h"
+#include "trace_helpers.h"
+
+void test_inode_build_id(void)
+{
+ int go[2], err, child_pid, child_status, c = 1, sz;
+ char build_id[BPF_BUILD_ID_SIZE];
+ struct inode_build_id *skel;
+
+ skel = inode_build_id__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "inode_build_id__open_and_load"))
+ return;
+
+ if (!ASSERT_OK(pipe(go), "pipe"))
+ goto out;
+
+ child_pid = fork();
+ if (child_pid < 0)
+ goto out;
+
+ /* child */
+ if (child_pid == 0) {
+ /* wait for parent's pid update */
+ err = read(go[0], &c, 1);
+ if (!ASSERT_EQ(err, 1, "child_read_pipe"))
+ exit(err);
+
+ execle("./urandom_read", "urandom_read", NULL, NULL);
+ exit(errno);
+ }
+
+ /* parent, update child's pid and kick it */
+ skel->bss->pid = child_pid;
+
+ err = inode_build_id__attach(skel);
+ if (!ASSERT_OK(err, "inode_build_id__attach"))
+ goto out;
+
+ err = write(go[1], &c, 1);
+ if (!ASSERT_EQ(err, 1, "child_write_pipe"))
+ goto out;
+
+ /* wait for child to exit */
+ waitpid(child_pid, &child_status, 0);
+ if (!ASSERT_EQ(WEXITSTATUS(child_status), 0, "child_exit_value"))
+ goto out;
+
+ sz = read_build_id("./urandom_read", build_id);
+ if (!ASSERT_GT(sz, 0, "read_build_id"))
+ goto out;
+
+ ASSERT_EQ(skel->bss->build_id_bin_size, sz, "build_id_bin_size");
+ ASSERT_MEMEQ(skel->bss->build_id_bin, build_id, sz, "build_id_bin");
+
+ sz = read_build_id("./liburandom_read.so", build_id);
+ if (!ASSERT_GT(sz, 0, "read_build_id"))
+ goto out;
+
+ ASSERT_EQ(skel->bss->build_id_lib_size, sz, "build_id_lib_size");
+ ASSERT_MEMEQ(skel->bss->build_id_lib, build_id, sz, "build_id_lib");
+
+out:
+ inode_build_id__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/inode_build_id.c b/tools/testing/selftests/bpf/progs/inode_build_id.c
new file mode 100644
index 000000000000..eceb215b56b8
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/inode_build_id.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include "err.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <linux/string.h>
+
+char _license[] SEC("license") = "GPL";
+
+int pid;
+
+u32 build_id_bin_size;
+u32 build_id_lib_size;
+
+char build_id_bin[20];
+char build_id_lib[20];
+
+static int store_build_id(struct inode *inode, char *build_id, u32 *sz)
+{
+ struct build_id *bid;
+
+ bid = inode->i_build_id;
+ if (IS_ERR_OR_NULL(bid))
+ return 0;
+ *sz = bid->sz;
+ if (bid->sz > sizeof(bid->data))
+ return 0;
+ __builtin_memcpy(build_id, bid->data, sizeof(bid->data));
+ return 0;
+}
+
+SEC("tp_btf/sched_process_exec")
+int BPF_PROG(prog, struct task_struct *p, pid_t old_pid, struct linux_binprm *bprm)
+{
+ int cur_pid = bpf_get_current_pid_tgid() >> 32;
+
+ if (pid != cur_pid)
+ return 0;
+ if (!bprm->file || !bprm->file->f_inode)
+ return 0;
+ return store_build_id(bprm->file->f_inode, build_id_bin, &build_id_bin_size);
+}
+
+static long check_vma(struct task_struct *task, struct vm_area_struct *vma,
+ void *data)
+{
+ if (!vma || !vma->vm_file || !vma->vm_file->f_inode)
+ return 0;
+ return store_build_id(vma->vm_file->f_inode, build_id_lib, &build_id_lib_size);
+}
+
+SEC("uprobe/liburandom_read.so:urandlib_read_without_sema")
+int BPF_UPROBE(urandlib_read_without_sema)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ int cur_pid = bpf_get_current_pid_tgid() >> 32;
+
+ if (pid != cur_pid)
+ return 0;
+ return bpf_find_vma(task, ctx->ip, check_vma, NULL, 0);
+}
diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 3825c2797a4b..8156d6d4cb3b 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -310,6 +310,16 @@ int test__join_cgroup(const char *path);
___ok; \
})
+#define ASSERT_MEMEQ(actual, expected, sz, name) ({ \
+ static int duration = 0; \
+ const char *___act = actual; \
+ const char *___exp = expected; \
+ bool ___ok = memcmp(___act, ___exp, sz) == 0; \
+ CHECK(!___ok, (name), \
+ "unexpected %s does not match\n", (name)); \
+ ___ok; \
+})
+
#define ASSERT_STRNEQ(actual, expected, len, name) ({ \
static int duration = 0; \
const char *___act = actual; \
--
2.39.2
next prev parent reply other threads:[~2023-02-28 9:34 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-28 9:31 [RFC v2 bpf-next 0/9] mm/bpf/perf: Store build id in inode object Jiri Olsa
2023-02-28 9:31 ` [PATCH RFC v2 bpf-next 1/9] mm: " Jiri Olsa
2023-02-28 19:13 ` Andrew Morton
2023-03-01 8:16 ` Jiri Olsa
2023-02-28 9:31 ` [PATCH RFC v2 bpf-next 2/9] bpf: Use file's inode object build id in stackmap Jiri Olsa
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 3/9] perf: Use file object build id in perf_event_mmap_event Jiri Olsa
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 4/9] libbpf: Allow to resolve binary path in current directory Jiri Olsa
2023-03-08 1:19 ` Andrii Nakryiko
2023-03-08 13:48 ` Jiri Olsa
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 5/9] selftests/bpf: Add read_buildid function Jiri Olsa
2023-03-08 1:22 ` Andrii Nakryiko
2023-03-08 13:49 ` Jiri Olsa
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 6/9] selftests/bpf: Add err.h header Jiri Olsa
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 7/9] selftests/bpf: Replace extract_build_id with read_build_id Jiri Olsa
2023-03-08 1:26 ` Andrii Nakryiko
2023-03-08 13:51 ` Jiri Olsa
2023-02-28 9:32 ` Jiri Olsa [this message]
2023-02-28 9:32 ` [PATCH RFC v2 bpf-next 9/9] selftests/bpf: Add iter_task_vma_buildid test Jiri Olsa
2023-03-08 1:32 ` Andrii Nakryiko
2023-03-08 14:00 ` Jiri Olsa
2023-02-28 22:07 ` [RFC v2 bpf-next 0/9] mm/bpf/perf: Store build id in inode object Dave Chinner
2023-03-01 15:41 ` Arnaldo Carvalho de Melo
2023-03-02 8:41 ` Jiri Olsa
2023-03-02 8:35 ` Jiri Olsa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230228093206.821563-9-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=acme@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@gmail.com \
--cc=peterz@infradead.org \
--cc=sdf@google.com \
--cc=songliubraving@fb.com \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.