BPF List
 help / color / mirror / Atom feed
From: Andrei Matei <andreimatei1@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: Andrei Matei <andreimatei1@gmail.com>
Subject: [PATCH bpf-next v2 5/5] selftest/bpf: add test for var-offset stack access
Date: Sun, 24 Jan 2021 14:49:09 -0500	[thread overview]
Message-ID: <20210124194909.453844-6-andreimatei1@gmail.com> (raw)
In-Reply-To: <20210124194909.453844-1-andreimatei1@gmail.com>

Add a higher-level test (C BPF program) for the new functionality -
variable access stack reads and writes.

Signed-off-by: Andrei Matei <andreimatei1@gmail.com>
---
 .../selftests/bpf/prog_tests/stack_var_off.c  | 56 +++++++++++++++++++
 .../selftests/bpf/progs/test_stack_var_off.c  | 43 ++++++++++++++
 2 files changed, 99 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/stack_var_off.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_stack_var_off.c

diff --git a/tools/testing/selftests/bpf/prog_tests/stack_var_off.c b/tools/testing/selftests/bpf/prog_tests/stack_var_off.c
new file mode 100644
index 000000000000..c4c47fb0f0af
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/stack_var_off.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "test_stack_var_off.skel.h"
+
+int dummy;
+
+noinline void uprobed_function(char *s, int len)
+{
+	/* Do something to keep the compiler from removing the function.
+	 */
+	dummy++;
+}
+
+void test_stack_var_off(void)
+{
+	int duration = 0;
+	struct bpf_link *uprobe_link;
+	struct test_stack_var_off *skel;
+	size_t uprobe_offset;
+	ssize_t base_addr;
+	char s[100];
+
+	base_addr = get_base_addr();
+	if (CHECK(base_addr < 0, "get_base_addr",
+		  "failed to find base addr: %zd", base_addr))
+		return;
+	uprobe_offset = (size_t)&uprobed_function - base_addr;
+
+	skel = test_stack_var_off__open_and_load();
+	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
+		return;
+	if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n"))
+		goto cleanup;
+
+	uprobe_link = bpf_program__attach_uprobe(skel->progs.uprobe,
+						 false /* retprobe */,
+						 0 /* self pid */,
+						 "/proc/self/exe",
+						 uprobe_offset);
+	if (CHECK(IS_ERR(uprobe_link), "attach_uprobe",
+		  "err %ld\n", PTR_ERR(uprobe_link)))
+		goto cleanup;
+	skel->links.uprobe = uprobe_link;
+
+	/* trigger uprobe */
+	s[0] = 1;
+	s[1] = 10;
+	uprobed_function(&s[0], 2);
+
+	if (CHECK(skel->bss->uprobe_res != 10, "check_uprobe_res",
+		  "wrong uprobe res: %d\n", skel->bss->uprobe_res))
+		goto cleanup;
+
+cleanup:
+	test_stack_var_off__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_stack_var_off.c b/tools/testing/selftests/bpf/progs/test_stack_var_off.c
new file mode 100644
index 000000000000..44f982684541
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_stack_var_off.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2017 Facebook
+
+#include <linux/ptrace.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+int uprobe_res;
+
+SEC("uprobe/func")
+int BPF_KPROBE(uprobe, char *s, int len)
+{
+	/* This BPF program performs variable-offset reads and writes on a
+	 * stack-allocated buffer.
+	 */
+	char buf[16];
+	unsigned long idx;
+	char out;
+
+	/* Zero-out the buffer so we can read anywhere inside it. */
+	__builtin_memset(&buf, 0, 16);
+	/* Copy the contents of s from user-space. */
+	len &= 0xf;
+	if (bpf_probe_read_user(&buf, len, s)) {
+		bpf_printk("error reading user mem\n");
+		return 1;
+	}
+	/* Index into the buffer at an unknown offset that comes from the
+	 * buffer itself. This is a variable-offset stack read.
+	 */
+	idx = buf[0];
+	idx &= 0xf;
+	out = buf[idx];
+	/* Append something to the buffer. The position where we append it
+	 * is unknown. This is a variable-offset stack write.
+	 */
+	buf[len] = buf[idx];
+	uprobe_res = out;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.27.0


  parent reply	other threads:[~2021-01-24 19:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 19:49 [PATCH bpf-next v2 0/5] bpf: allow variable-offset stack access Andrei Matei
2021-01-24 19:49 ` [PATCH bpf-next v2 1/5] " Andrei Matei
2021-01-27 22:58   ` Alexei Starovoitov
2021-01-30 22:55     ` Andrei Matei
2021-02-02  0:22       ` Alexei Starovoitov
2021-02-07  1:11     ` Andrei Matei
2021-01-24 19:49 ` [PATCH bpf-next v2 2/5] selftest/bpf: adjust expected verifier errors Andrei Matei
2021-01-24 19:49 ` [PATCH bpf-next v2 3/5] selftest/bpf: verifier tests for var-off access Andrei Matei
2021-01-24 19:49 ` [PATCH bpf-next v2 4/5] selftest/bpf: move utility function to tests header Andrei Matei
2021-01-26  2:33   ` Andrii Nakryiko
2021-01-24 19:49 ` Andrei Matei [this message]
2021-01-26  2:37   ` [PATCH bpf-next v2 5/5] selftest/bpf: add test for var-offset stack access Andrii Nakryiko
2021-02-07  1:11     ` Andrei Matei

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=20210124194909.453844-6-andreimatei1@gmail.com \
    --to=andreimatei1@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox