Re: [PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

2021-04-15 Thread Florent Revest
On Thu, Apr 15, 2021 at 12:16 AM Andrii Nakryiko
 wrote:
>
> On Wed, Apr 14, 2021 at 2:21 AM Florent Revest  wrote:
> >
> > On Wed, Apr 14, 2021 at 1:21 AM Andrii Nakryiko
> >  wrote:
> > >
> > > On Mon, Apr 12, 2021 at 8:38 AM Florent Revest  
> > > wrote:
> > > >
> > > > This exercises most of the format specifiers.
> > > >
> > > > Signed-off-by: Florent Revest 
> > > > Acked-by: Andrii Nakryiko 
> > > > ---
> > >
> > > As I mentioned on another patch, we probably need negative tests even
> > > more than positive ones.
> >
> > Agreed.
> >
> > > I think an easy and nice way to do this is to have a separate BPF
> > > skeleton where fmt string and arguments are provided through read-only
> > > global variables, so that user-space can re-use the same BPF skeleton
> > > to simulate multiple cases. BPF program itself would just call
> > > bpf_snprintf() and store the returned result.
> >
> > Ah, great idea! I was thinking of having one skeleton for each but it
> > would be a bit much indeed.
> >
> > Because the format string needs to be in a read only map though, I
> > hope it can be modified from userspace before loading. I'll try it out
> > and see :) if it doesn't work I'll just use more skeletons
>
> You need read-only variables (const volatile my_type). Their contents
> are statically verified by BPF verifier, yet user-space can pre-setup
> it at runtime.

Thanks :) v4 has negative fmt tests


Re: [PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

2021-04-14 Thread Andrii Nakryiko
On Wed, Apr 14, 2021 at 2:21 AM Florent Revest  wrote:
>
> On Wed, Apr 14, 2021 at 1:21 AM Andrii Nakryiko
>  wrote:
> >
> > On Mon, Apr 12, 2021 at 8:38 AM Florent Revest  wrote:
> > >
> > > This exercises most of the format specifiers.
> > >
> > > Signed-off-by: Florent Revest 
> > > Acked-by: Andrii Nakryiko 
> > > ---
> >
> > As I mentioned on another patch, we probably need negative tests even
> > more than positive ones.
>
> Agreed.
>
> > I think an easy and nice way to do this is to have a separate BPF
> > skeleton where fmt string and arguments are provided through read-only
> > global variables, so that user-space can re-use the same BPF skeleton
> > to simulate multiple cases. BPF program itself would just call
> > bpf_snprintf() and store the returned result.
>
> Ah, great idea! I was thinking of having one skeleton for each but it
> would be a bit much indeed.
>
> Because the format string needs to be in a read only map though, I
> hope it can be modified from userspace before loading. I'll try it out
> and see :) if it doesn't work I'll just use more skeletons

You need read-only variables (const volatile my_type). Their contents
are statically verified by BPF verifier, yet user-space can pre-setup
it at runtime.

>
> > Whether we need to validate the verifier log is up to debate (though
> > it's not that hard to do by overriding libbpf_print_fn() callback),
> > I'd be ok at least knowing that some bad format strings are rejected
> > and don't crash the kernel.
>
> Alright :)
>
> >
> > >  .../selftests/bpf/prog_tests/snprintf.c   | 81 +++
> > >  .../selftests/bpf/progs/test_snprintf.c   | 74 +
> > >  2 files changed, 155 insertions(+)
> > >  create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c
> > >  create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c
> > >
> >
> > [...]


Re: [PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

2021-04-14 Thread Florent Revest
On Wed, Apr 14, 2021 at 1:21 AM Andrii Nakryiko
 wrote:
>
> On Mon, Apr 12, 2021 at 8:38 AM Florent Revest  wrote:
> >
> > This exercises most of the format specifiers.
> >
> > Signed-off-by: Florent Revest 
> > Acked-by: Andrii Nakryiko 
> > ---
>
> As I mentioned on another patch, we probably need negative tests even
> more than positive ones.

Agreed.

> I think an easy and nice way to do this is to have a separate BPF
> skeleton where fmt string and arguments are provided through read-only
> global variables, so that user-space can re-use the same BPF skeleton
> to simulate multiple cases. BPF program itself would just call
> bpf_snprintf() and store the returned result.

Ah, great idea! I was thinking of having one skeleton for each but it
would be a bit much indeed.

Because the format string needs to be in a read only map though, I
hope it can be modified from userspace before loading. I'll try it out
and see :) if it doesn't work I'll just use more skeletons

> Whether we need to validate the verifier log is up to debate (though
> it's not that hard to do by overriding libbpf_print_fn() callback),
> I'd be ok at least knowing that some bad format strings are rejected
> and don't crash the kernel.

Alright :)

>
> >  .../selftests/bpf/prog_tests/snprintf.c   | 81 +++
> >  .../selftests/bpf/progs/test_snprintf.c   | 74 +
> >  2 files changed, 155 insertions(+)
> >  create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c
> >  create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c
> >
>
> [...]


Re: [PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

2021-04-13 Thread Andrii Nakryiko
On Mon, Apr 12, 2021 at 8:38 AM Florent Revest  wrote:
>
> This exercises most of the format specifiers.
>
> Signed-off-by: Florent Revest 
> Acked-by: Andrii Nakryiko 
> ---

As I mentioned on another patch, we probably need negative tests even
more than positive ones.

I think an easy and nice way to do this is to have a separate BPF
skeleton where fmt string and arguments are provided through read-only
global variables, so that user-space can re-use the same BPF skeleton
to simulate multiple cases. BPF program itself would just call
bpf_snprintf() and store the returned result.

Whether we need to validate the verifier log is up to debate (though
it's not that hard to do by overriding libbpf_print_fn() callback),
I'd be ok at least knowing that some bad format strings are rejected
and don't crash the kernel.


>  .../selftests/bpf/prog_tests/snprintf.c   | 81 +++
>  .../selftests/bpf/progs/test_snprintf.c   | 74 +
>  2 files changed, 155 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c
>

[...]


[PATCH bpf-next v3 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

2021-04-12 Thread Florent Revest
This exercises most of the format specifiers.

Signed-off-by: Florent Revest 
Acked-by: Andrii Nakryiko 
---
 .../selftests/bpf/prog_tests/snprintf.c   | 81 +++
 .../selftests/bpf/progs/test_snprintf.c   | 74 +
 2 files changed, 155 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c

diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c 
b/tools/testing/selftests/bpf/prog_tests/snprintf.c
new file mode 100644
index ..3ad1ee885273
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Google LLC. */
+
+#include 
+#include "test_snprintf.skel.h"
+
+#define EXP_NUM_OUT  "-8 9 96 -424242 1337 DABBAD00"
+#define EXP_NUM_RET  sizeof(EXP_NUM_OUT)
+
+#define EXP_IP_OUT   "127.000.000.001 :::::::0001"
+#define EXP_IP_RET   sizeof(EXP_IP_OUT)
+
+/* The third specifier, %pB, depends on compiler inlining so don't check it */
+#define EXP_SYM_OUT  "schedule schedule+0x0/"
+#define MIN_SYM_RET  sizeof(EXP_SYM_OUT)
+
+/* The third specifier, %p, is a hashed pointer which changes on every reboot 
*/
+#define EXP_ADDR_OUT " 0add4e55 "
+#define EXP_ADDR_RET sizeof(EXP_ADDR_OUT "unknownhashedptr")
+
+#define EXP_STR_OUT  "str1 longstr"
+#define EXP_STR_RET  sizeof(EXP_STR_OUT)
+
+#define EXP_OVER_OUT "%over"
+#define EXP_OVER_RET 10
+
+#define EXP_PAD_OUT "4 000"
+#define EXP_PAD_RET 97
+
+#define EXP_NO_ARG_OUT "simple case"
+#define EXP_NO_ARG_RET 12
+
+#define EXP_NO_BUF_RET 29
+
+void test_snprintf(void)
+{
+   char exp_addr_out[] = EXP_ADDR_OUT;
+   char exp_sym_out[]  = EXP_SYM_OUT;
+   struct test_snprintf *skel;
+
+   skel = test_snprintf__open_and_load();
+   if (!ASSERT_OK_PTR(skel, "skel_open"))
+   return;
+
+   if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach"))
+   goto cleanup;
+
+   /* trigger tracepoint */
+   usleep(1);
+
+   ASSERT_STREQ(skel->bss->num_out, EXP_NUM_OUT, "num_out");
+   ASSERT_EQ(skel->bss->num_ret, EXP_NUM_RET, "num_ret");
+
+   ASSERT_STREQ(skel->bss->ip_out, EXP_IP_OUT, "ip_out");
+   ASSERT_EQ(skel->bss->ip_ret, EXP_IP_RET, "ip_ret");
+
+   ASSERT_OK(memcmp(skel->bss->sym_out, exp_sym_out,
+sizeof(exp_sym_out) - 1), "sym_out");
+   ASSERT_LT(MIN_SYM_RET, skel->bss->sym_ret, "sym_ret");
+
+   ASSERT_OK(memcmp(skel->bss->addr_out, exp_addr_out,
+sizeof(exp_addr_out) - 1), "addr_out");
+   ASSERT_EQ(skel->bss->addr_ret, EXP_ADDR_RET, "addr_ret");
+
+   ASSERT_STREQ(skel->bss->str_out, EXP_STR_OUT, "str_out");
+   ASSERT_EQ(skel->bss->str_ret, EXP_STR_RET, "str_ret");
+
+   ASSERT_STREQ(skel->bss->over_out, EXP_OVER_OUT, "over_out");
+   ASSERT_EQ(skel->bss->over_ret, EXP_OVER_RET, "over_ret");
+
+   ASSERT_STREQ(skel->bss->pad_out, EXP_PAD_OUT, "pad_out");
+   ASSERT_EQ(skel->bss->pad_ret, EXP_PAD_RET, "pad_ret");
+
+   ASSERT_STREQ(skel->bss->noarg_out, EXP_NO_ARG_OUT, "no_arg_out");
+   ASSERT_EQ(skel->bss->noarg_ret, EXP_NO_ARG_RET, "no_arg_ret");
+
+   ASSERT_EQ(skel->bss->nobuf_ret, EXP_NO_BUF_RET, "no_buf_ret");
+
+cleanup:
+   test_snprintf__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_snprintf.c 
b/tools/testing/selftests/bpf/progs/test_snprintf.c
new file mode 100644
index ..4c36f355dfca
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_snprintf.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Google LLC. */
+
+#include 
+#include 
+#include 
+#include 
+
+char num_out[64] = {};
+long num_ret = 0;
+
+char ip_out[64] = {};
+long ip_ret = 0;
+
+char sym_out[64] = {};
+long sym_ret = 0;
+
+char addr_out[64] = {};
+long addr_ret = 0;
+
+char str_out[64] = {};
+long str_ret = 0;
+
+char over_out[6] = {};
+long over_ret = 0;
+
+char pad_out[10] = {};
+long pad_ret = 0;
+
+char noarg_out[64] = {};
+long noarg_ret = 0;
+
+long nobuf_ret = 0;
+
+extern const void schedule __ksym;
+
+SEC("raw_tp/sys_enter")
+int handler(const void *ctx)
+{
+   /* Convenient values to pretty-print */
+   const __u8 ex_ipv4[] = {127, 0, 0, 1};
+   const __u8 ex_ipv6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
+   const char str1[] = "str1";
+   const char longstr[] = "longstr";
+
+   /* Integer types */
+   num_ret  = BPF_SNPRINTF(num_out, sizeof(num_out),
+   "%d %u %x %li %llu %lX",
+   -8, 9, 150, -424242, 1337, 0xDABBAD00);
+   /* IP addresses */
+   ip_ret   = BPF_SNPRINTF(ip_out, sizeof(ip_out), "%pi4 %pI6",
+   _ipv4, _ipv6);
+   /* Symbol lookup formatting */
+   sym_ret  = BPF_SNPRINTF(sym_out,