diff mbox series

[bpf-next,v2,5/5] selftest/bpf: add test for var-offset stack access

Message ID 20210124194909.453844-6-andreimatei1@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: allow variable-offset stack access | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 10 maintainers not CCed: shuah@kernel.org netdev@vger.kernel.org songliubraving@fb.com linux-kselftest@vger.kernel.org andrii@kernel.org daniel@iogearbox.net kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com yhs@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 2 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Andrei Matei Jan. 24, 2021, 7:49 p.m. UTC
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

Comments

Andrii Nakryiko Jan. 26, 2021, 2:37 a.m. UTC | #1
On Sun, Jan 24, 2021 at 11:54 AM Andrei Matei <andreimatei1@gmail.com> wrote:
>
> 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);

I don't think uprobe() is essential to this test and just obscured
what is being tested. I'd just use a global variable to pass whatever
input data you need and use usleep(1), just like lots of other tests.

> +
> +       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
>
Andrei Matei Feb. 7, 2021, 1:11 a.m. UTC | #2
done in v3. Thanks!

On Mon, Jan 25, 2021 at 9:37 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Jan 24, 2021 at 11:54 AM Andrei Matei <andreimatei1@gmail.com> wrote:
> >
> > 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);
>
> I don't think uprobe() is essential to this test and just obscured
> what is being tested. I'd just use a global variable to pass whatever
> input data you need and use usleep(1), just like lots of other tests.
>
> > +
> > +       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
> >
diff mbox series

Patch

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";