Skip to contentSkip to conversation

Follow ups for kptr series

PATCH Applied Kumar Kartikeya Dwivedi 3 participants 9 messages May 11, 2022 – May 13, 2022

Subsystems (5)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v2 cover Follow ups for kptr series

Fix a build time warning, and address comments from Alexei on the merged version [0].

[0]: https://lore.kernel.org/bpf/[email protected]

Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]

* Add Fixes tag to patch 1
* Fix test_progs-noalu32 failure in CI due to different alloc_insn (Alexei)
* Remove per-CPU struct, use global struct (Alexei)

Kumar Kartikeya Dwivedi (4):
bpf: Fix sparse warning for bpf_kptr_xchg_proto
bpf: Prepare prog_test_struct kfuncs for runtime tests selftests/bpf: Add negative C tests for kptrs selftests/bpf: Add tests for kptr_ref refcounting

 include/linux/bpf.h                           |   1 +
 net/bpf/test_run.c                            |  23 +-
 .../selftests/bpf/prog_tests/map_kptr.c       | 108 ++++-
 tools/testing/selftests/bpf/progs/map_kptr.c  | 106 ++++-
 .../selftests/bpf/progs/map_kptr_fail.c       | 418 ++++++++++++++++++
 .../testing/selftests/bpf/verifier/map_kptr.c |   4 +-
 6 files changed, 649 insertions(+), 11 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr_fail.c
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH v2 1/4 bpf: Fix sparse warning for bpf_kptr_xchg_proto

Kernel Test Robot complained about missing static storage class annotation for bpf_kptr_xchg_proto variable.

sparse: symbol 'bpf_kptr_xchg_proto' was not declared. Should it be static?

This caused by missing extern definition in the header. Add it to suppress the sparse warning.

Fixes c0a5a21c25f3 ("bpf: Allow storing referenced kptr in map")
---
 include/linux/bpf.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/bpf.h +1 -0
index aba7ded56436..3ded8711457f 100644
@@ -2246,6 +2246,7 @@ extern const struct bpf_func_proto bpf_find_vma_proto;
 extern const struct bpf_func_proto bpf_loop_proto;
 extern const struct bpf_func_proto bpf_strncmp_proto;
 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
+extern const struct bpf_func_proto bpf_kptr_xchg_proto;
 
 const struct bpf_func_proto *tracing_prog_func_proto(
   enum bpf_func_id func_id, const struct bpf_prog *prog);
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH v2 2/4 bpf: Prepare prog_test_struct kfuncs for runtime tests

In reply to Kumar Kartikeya Dwivedi

In an effort to actually test the refcounting logic at runtime, add a refcount_t member to prog_test_ref_kfunc and use it in selftests to verify and test the whole logic more exhaustively.

The kfunc calls for prog_test_member do not require runtime refcounting, as they are only used for verifier selftests, not during runtime execution. Hence, their implementation now has a WARN_ON_ONCE as it is not meant to be reachable code at runtime. It is strictly used in tests triggering failure cases in the verifier. bpf_kfunc_call_memb_release is called from map free path, since prog_test_member is embedded in map value for some verifier tests, so we skip WARN_ON_ONCE for it.

---
 net/bpf/test_run.c                            | 23 ++++++++++++++-----
 .../testing/selftests/bpf/verifier/map_kptr.c |  4 ++--
 2 files changed, 19 insertions(+), 8 deletions(-)
net/bpf/test_run.c +17 -6
index 7a1579c91432..4d08cca771c7 100644
@@ -564,31 +564,36 @@ struct prog_test_ref_kfunc {
 	int b;
 	struct prog_test_member memb;
 	struct prog_test_ref_kfunc *next;
+	refcount_t cnt;
 };
 
 static struct prog_test_ref_kfunc prog_test_struct = {
 	.a = 42,
 	.b = 108,
 	.next = &prog_test_struct,
+	.cnt = REFCOUNT_INIT(1),
 };
 
 noinline struct prog_test_ref_kfunc *
 bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
 {
-	/* randomly return NULL */
-	if (get_jiffies_64() % 2)
-		return NULL;
+	refcount_inc(&prog_test_struct.cnt);
 	return &prog_test_struct;
 }
 
 noinline struct prog_test_member *
 bpf_kfunc_call_memb_acquire(void)
 {
-	return &prog_test_struct.memb;
+	WARN_ON_ONCE(1);
+	return NULL;
 }
 
 noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
 {
+	if (!p)
+		return;
+
+	refcount_dec(&p->cnt);
 }
 
 noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p)
@@ -597,12 +602,18 @@ noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p)
 
 noinline void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
 {
+	WARN_ON_ONCE(1);
 }
 
 noinline struct prog_test_ref_kfunc *
-bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **p, int a, int b)
+bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b)
 {
-	return &prog_test_struct;
+	struct prog_test_ref_kfunc *p = READ_ONCE(*pp);
+
+	if (!p)
+		return NULL;
+	refcount_inc(&p->cnt);
+	return p;
 }
 
 struct prog_test_pass1 {
tools/testing/selftests/bpf/verifier/map_kptr.c +2 -2
index 9113834640e6..6914904344c0 100644
@@ -212,13 +212,13 @@
 	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
 	BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
 	BPF_EXIT_INSN(),
-	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 24),
+	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 32),
 	BPF_EXIT_INSN(),
 	},
 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	.fixup_map_kptr = { 1 },
 	.result = REJECT,
-	.errstr = "access beyond struct prog_test_ref_kfunc at off 24 size 8",
+	.errstr = "access beyond struct prog_test_ref_kfunc at off 32 size 8",
 },
 {
 	"map_kptr: unref: inherit PTR_UNTRUSTED on struct walk",
Signature · 1 line
2.35.1
author maintainer
#3 lore raw

PATCH v2 3/4 selftests/bpf: Add negative C tests for kptrs

In reply to Kumar Kartikeya Dwivedi

This uses the newly added SEC("?foo") naming to disable autoload of programs, and then loads them one by one for the object and verifies that loading fails and matches the returned error string from verifier. This is similar to already existing verifier tests but provides coverage for BPF C.

---
 .../selftests/bpf/prog_tests/map_kptr.c       |  87 +++-
 .../selftests/bpf/progs/map_kptr_fail.c       | 418 ++++++++++++++++++
 2 files changed, 504 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr_fail.c
tools/testing/selftests/bpf/prog_tests/map_kptr.c +86 -1
index 9e2fbda64a65..00819277cb17 100644
@@ -2,8 +2,86 @@
 #include <test_progs.h>
 
 #include "map_kptr.skel.h"
+#include "map_kptr_fail.skel.h"
 
-void test_map_kptr(void)
+static char log_buf[1024 * 1024];
+
+struct {
+	const char *prog_name;
+	const char *err_msg;
+} map_kptr_fail_tests[] = {
+	{ "size_not_bpf_dw", "kptr access size must be BPF_DW" },
+	{ "non_const_var_off", "kptr access cannot have variable offset" },
+	{ "non_const_var_off_kptr_xchg", "R1 doesn't have constant offset. kptr has to be" },
+	{ "misaligned_access_write", "kptr access misaligned expected=8 off=7" },
+	{ "misaligned_access_read", "kptr access misaligned expected=8 off=1" },
+	{ "reject_var_off_store", "variable untrusted_ptr_ access var_off=(0x0; 0x1e0)" },
+	{ "reject_bad_type_match", "invalid kptr access, R1 type=untrusted_ptr_prog_test_ref_kfunc" },
+	{ "marked_as_untrusted_or_null", "R1 type=untrusted_ptr_or_null_ expected=percpu_ptr_" },
+	{ "correct_btf_id_check_size", "access beyond struct prog_test_ref_kfunc at off 32 size 4" },
+	{ "inherit_untrusted_on_walk", "R1 type=untrusted_ptr_ expected=percpu_ptr_" },
+	{ "reject_kptr_xchg_on_unref", "off=8 kptr isn't referenced kptr" },
+	{ "reject_kptr_get_no_map_val", "arg#0 expected pointer to map value" },
+	{ "reject_kptr_get_no_null_map_val", "arg#0 expected pointer to map value" },
+	{ "reject_kptr_get_no_kptr", "arg#0 no referenced kptr at map value offset=0" },
+	{ "reject_kptr_get_on_unref", "arg#0 no referenced kptr at map value offset=8" },
+	{ "reject_kptr_get_bad_type_match", "kernel function bpf_kfunc_call_test_kptr_get args#0" },
+	{ "mark_ref_as_untrusted_or_null", "R1 type=untrusted_ptr_or_null_ expected=percpu_ptr_" },
+	{ "reject_untrusted_store_to_ref", "store to referenced kptr disallowed" },
+	{ "reject_bad_type_xchg", "invalid kptr access, R2 type=ptr_prog_test_ref_kfunc expected=ptr_prog_test_member" },
+	{ "reject_untrusted_xchg", "R2 type=untrusted_ptr_ expected=ptr_" },
+	{ "reject_member_of_ref_xchg", "invalid kptr access, R2 type=ptr_prog_test_ref_kfunc" },
+	{ "reject_indirect_helper_access", "kptr cannot be accessed indirectly by helper" },
+	{ "reject_indirect_global_func_access", "kptr cannot be accessed indirectly by helper" },
+	{ "kptr_xchg_ref_state", "Unreleased reference id=5 alloc_insn=" },
+	{ "kptr_get_ref_state", "Unreleased reference id=3 alloc_insn=" },
+};
+
+static void test_map_kptr_fail_prog(const char *prog_name, const char *err_msg)
+{
+	LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
+						.kernel_log_size = sizeof(log_buf),
+						.kernel_log_level = 1);
+	struct map_kptr_fail *skel;
+	struct bpf_program *prog;
+	int ret;
+
+	skel = map_kptr_fail__open_opts(&opts);
+	if (!ASSERT_OK_PTR(skel, "map_kptr_fail__open_opts"))
+		return;
+
+	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
+	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
+		goto end;
+
+	bpf_program__set_autoload(prog, true);
+
+	ret = map_kptr_fail__load(skel);
+	if (!ASSERT_ERR(ret, "map_kptr__load must fail"))
+		goto end;
+
+	if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) {
+		fprintf(stderr, "Expected: %s\n", err_msg);
+		fprintf(stderr, "Verifier: %s\n", log_buf);
+	}
+
+end:
+	map_kptr_fail__destroy(skel);
+}
+
+static void test_map_kptr_fail(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(map_kptr_fail_tests); i++) {
+		if (!test__start_subtest(map_kptr_fail_tests[i].prog_name))
+			continue;
+		test_map_kptr_fail_prog(map_kptr_fail_tests[i].prog_name,
+					map_kptr_fail_tests[i].err_msg);
+	}
+}
+
+static void test_map_kptr_success(void)
 {
 	struct map_kptr *skel;
 	int key = 0, ret;
@@ -35,3 +113,10 @@ void test_map_kptr(void)
 
 	map_kptr__destroy(skel);
 }
+
+void test_map_kptr(void)
+{
+	if (test__start_subtest("success"))
+		test_map_kptr_success();
+	test_map_kptr_fail();
+}
tools/testing/selftests/bpf/progs/map_kptr_fail.c new file +418 -0

418 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#7 lore raw

PATCH v2 4/4 selftests/bpf: Add tests for kptr_ref refcounting

In reply to Kumar Kartikeya Dwivedi

Check at runtime how various operations for kptr_ref affect its refcount and verify against the actual count.

---
 .../selftests/bpf/prog_tests/map_kptr.c       |  27 ++++-
 tools/testing/selftests/bpf/progs/map_kptr.c  | 106 +++++++++++++++++-
 2 files changed, 128 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/map_kptr.c +24 -3
index 00819277cb17..8142dd9eff4c 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
+#include <network_helpers.h>
 
 #include "map_kptr.skel.h"
 #include "map_kptr_fail.skel.h"
@@ -81,8 +82,13 @@ static void test_map_kptr_fail(void)
 	}
 }
 
-static void test_map_kptr_success(void)
+static void test_map_kptr_success(bool test_run)
 {
+	LIBBPF_OPTS(bpf_test_run_opts, opts,
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.repeat = 1,
+	);
 	struct map_kptr *skel;
 	int key = 0, ret;
 	char buf[24];
@@ -91,6 +97,16 @@ static void test_map_kptr_success(void)
 	if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
 		return;
 
+	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref), &opts);
+	ASSERT_OK(ret, "test_map_kptr_ref refcount");
+	ASSERT_OK(opts.retval, "test_map_kptr_ref retval");
+	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref2), &opts);
+	ASSERT_OK(ret, "test_map_kptr_ref2 refcount");
+	ASSERT_OK(opts.retval, "test_map_kptr_ref2 retval");
+
+	if (test_run)
+		return;
+
 	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.array_map), &key, buf, 0);
 	ASSERT_OK(ret, "array_map update");
 	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.array_map), &key, buf, 0);
@@ -116,7 +132,12 @@ static void test_map_kptr_success(void)
 
 void test_map_kptr(void)
 {
-	if (test__start_subtest("success"))
-		test_map_kptr_success();
+	if (test__start_subtest("success")) {
+		test_map_kptr_success(false);
+		/* Do test_run twice, so that we see refcount going back to 1
+		 * after we leave it in map from first iteration.
+		 */
+		test_map_kptr_success(true);
+	}
 	test_map_kptr_fail();
 }
tools/testing/selftests/bpf/progs/map_kptr.c +104 -2

122 lines not on this page · raw message

Signature · 1 line
2.35.1
#8 lore raw
In reply to Kumar Kartikeya Dwivedi
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Thu, 12 May 2022 01:16:50 +0530 you wrote:

Fix a build time warning, and address comments from Alexei on the merged version [0].

[0]: https://lore.kernel.org/bpf/[email protected]

Changelog:

[...]

Here is the summary with links:
- [bpf-next,v2,1/4] bpf: Fix sparse warning for bpf_kptr_xchg_proto

    https://git.kernel.org/bpf/bpf-next/c/5b74c690e1c5
  - [bpf-next,v2,2/4] bpf: Prepare prog_test_struct kfuncs for runtime tests
    https://git.kernel.org/bpf/bpf-next/c/5cdccadcac26
  - [bpf-next,v2,3/4] selftests/bpf: Add negative C tests for kptrs
    https://git.kernel.org/bpf/bpf-next/c/04accf794bb2
  - [bpf-next,v2,4/4] selftests/bpf: Add tests for kptr_ref refcounting
    https://git.kernel.org/bpf/bpf-next/c/0ef6740e9777

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#4 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Add negative C tests for kptrs

In reply to Kumar Kartikeya Dwivedi
Quoted 14 lines · Kumar Kartikeya Dwivedi

This uses the newly added SEC("?foo") naming to disable autoload of programs, and then loads them one by one for the object and verifies that loading fails and matches the returned error string from verifier. This is similar to already existing verifier tests but provides coverage for BPF C.

---
 .../selftests/bpf/prog_tests/map_kptr.c       |  87 +++-
 .../selftests/bpf/progs/map_kptr_fail.c       | 418 ++++++++++++++++++
 2 files changed, 504 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr_fail.c

[...]

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +2 -0
+
+static void test_map_kptr_success(void)
 {
        struct map_kptr *skel;
        int key = 0, ret;

tools/testing/selftests/bpf/prog_tests/map_kptr.c void test_map_kptr(void) · line 121

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +6 -0
@@ -35,3 +113,10 @@ void test_map_kptr(void)
 
        map_kptr__destroy(skel);
 }
+
+void test_map_kptr(void)
+{
+       if (test__start_subtest("success"))
+               test_map_kptr_success();
+       test_map_kptr_fail();

I think the intent for this was to be another subtest, right? Worth fixing in a follow up?

Quoted 7 lines
(quoted diff) +1 -0
+}
tools/testing/selftests/bpf/progs/map_kptr_fail.c new file +0 -0
new file mode 100644
index 000000000000..05e209b1b12a
@@ -0,0 +1,418 @@

[...]

author maintainer
#5 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Add negative C tests for kptrs

Quoted 26 lines · Andrii Nakryiko
Quoted 14 lines · Kumar Kartikeya Dwivedi

This uses the newly added SEC("?foo") naming to disable autoload of programs, and then loads them one by one for the object and verifies that loading fails and matches the returned error string from verifier. This is similar to already existing verifier tests but provides coverage for BPF C.

---
 .../selftests/bpf/prog_tests/map_kptr.c       |  87 +++-
 .../selftests/bpf/progs/map_kptr_fail.c       | 418 ++++++++++++++++++
 2 files changed, 504 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr_fail.c

[...]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +2 -0
+
+static void test_map_kptr_success(void)
 {
        struct map_kptr *skel;
        int key = 0, ret;
@@ -35,3 +113,10 @@ void test_map_kptr(void)
 
Quoted 12 lines

tools/testing/selftests/bpf/prog_tests/map_kptr.c void test_map_kptr(void) · line 121

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +6 -0
        map_kptr__destroy(skel);
 }
+
+void test_map_kptr(void)
+{
+       if (test__start_subtest("success"))
+               test_map_kptr_success();
+       test_map_kptr_fail();

I think the intent for this was to be another subtest, right? Worth fixing in a follow up?

No, instead I am calling test__start_subtest inside it for each program name that is failing, to make them the subtest. In that case, it should be alright?

Quoted 9 lines
Quoted 7 lines
(quoted diff) +1 -0
+}
tools/testing/selftests/bpf/progs/map_kptr_fail.c new file +0 -0
new file mode 100644
index 000000000000..05e209b1b12a
@@ -0,0 +1,418 @@

[...]

--
Kartikeya

#6 lore raw

Re: [PATCH bpf-next v2 3/4] selftests/bpf: Add negative C tests for kptrs

Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 26 lines · Andrii Nakryiko
Quoted 14 lines · Kumar Kartikeya Dwivedi

This uses the newly added SEC("?foo") naming to disable autoload of programs, and then loads them one by one for the object and verifies that loading fails and matches the returned error string from verifier. This is similar to already existing verifier tests but provides coverage for BPF C.

---
 .../selftests/bpf/prog_tests/map_kptr.c       |  87 +++-
 .../selftests/bpf/progs/map_kptr_fail.c       | 418 ++++++++++++++++++
 2 files changed, 504 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr_fail.c

[...]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +2 -0
+
+static void test_map_kptr_success(void)
 {
        struct map_kptr *skel;
        int key = 0, ret;
@@ -35,3 +113,10 @@ void test_map_kptr(void)
 
Quoted 12 lines

tools/testing/selftests/bpf/prog_tests/map_kptr.c void test_map_kptr(void) · line 121

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/map_kptr.c +6 -0
        map_kptr__destroy(skel);
 }
+
+void test_map_kptr(void)
+{
+       if (test__start_subtest("success"))
+               test_map_kptr_success();
+       test_map_kptr_fail();

I think the intent for this was to be another subtest, right? Worth fixing in a follow up?

Quoted 2 lines

No, instead I am calling test__start_subtest inside it for each program name that is failing, to make them the subtest. In that case, it should be alright?

Ah, ok, it's quite confusing to have test__start_subtest in different functions (though not illegal, I suppose). I'd rather have that test loop right here in test_map_kptr() function, but it's no big deal

Quoted 13 lines
Quoted 10 lines
Quoted 8 lines
(quoted diff) +1 -0
+}
tools/testing/selftests/bpf/progs/map_kptr_fail.c new file +0 -0
new file mode 100644
index 000000000000..05e209b1b12a
@@ -0,0 +1,418 @@

[...]

--
Kartikeya