Skip to contentSkip to conversation

selftests/bpf: Add test for reg2btf_ids out of bounds access

PATCH Applied Kumar Kartikeya Dwivedi 3 participants 4 messages Feb 20, 2022 – Feb 22, 2022

Subsystems (3)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: Add test for reg2btf_ids out of bounds access

This test tries to pass a PTR_TO_BTF_ID_OR_NULL to the release function, which would trigger a out of bounds access without the fix in commit 45ce4b4f9009 ("bpf: Fix crash due to out of bounds access into reg2btf_ids.") but after the fix, it should only index using base_type(reg->type), which should be less than __BPF_REG_TYPE_MAX, and also not permit any type flags to be set for the reg->type.

---
 tools/testing/selftests/bpf/verifier/calls.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/testing/selftests/bpf/verifier/calls.c +19 -0
index 829be2b9e08e..0a8ea60c2a80 100644
@@ -96,6 +96,25 @@
 		{ "bpf_kfunc_call_test_mem_len_fail1", 2 },
 	},
 },
+{
+	"calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX",
+	.insns = {
+	BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
+	BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
+	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+	BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	.result = REJECT,
+	.errstr = "arg#0 pointer type STRUCT prog_test_ref_kfunc must point",
+	.fixup_kfunc_btf_id = {
+		{ "bpf_kfunc_call_test_acquire", 3 },
+		{ "bpf_kfunc_call_test_release", 5 },
+	},
+},
 {
 	"calls: basic sanity",
 	.insns = {

--
2.35.1

reviewer
#1 lore raw
Quoted 25 lines · Kumar Kartikeya Dwivedi

This test tries to pass a PTR_TO_BTF_ID_OR_NULL to the release function, which would trigger a out of bounds access without the fix in commit 45ce4b4f9009 ("bpf: Fix crash due to out of bounds access into reg2btf_ids.") but after the fix, it should only index using base_type(reg->type), which should be less than __BPF_REG_TYPE_MAX, and also not permit any type flags to be set for the reg->type.

---
 tools/testing/selftests/bpf/verifier/calls.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/testing/selftests/bpf/verifier/calls.c +4 -0
index 829be2b9e08e..0a8ea60c2a80 100644
@@ -96,6 +96,25 @@
                { "bpf_kfunc_call_test_mem_len_fail1", 2 },
        },
 },
+{
+       "calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX",
+       .insns = {
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),

tools/testing/selftests/bpf/verifier/calls.c line 112

Quoted 10 lines
tools/testing/selftests/bpf/verifier/calls.c +10 -0
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
+       BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       .result = REJECT,
+       .errstr = "arg#0 pointer type STRUCT prog_test_ref_kfunc must point",

Why do we stop errstr at "must point"?

Quoted 11 lines
tools/testing/selftests/bpf/verifier/calls.c +5 -1
+       .fixup_kfunc_btf_id = {
+               { "bpf_kfunc_call_test_acquire", 3 },
+               { "bpf_kfunc_call_test_release", 5 },
+       },
+},
 {
        "calls: basic sanity",
        .insns = {
--

2.35.1

author maintainer
#2 lore raw
Quoted 31 lines · Song Liu
Quoted 29 lines · Kumar Kartikeya Dwivedi

This test tries to pass a PTR_TO_BTF_ID_OR_NULL to the release function, which would trigger a out of bounds access without the fix in commit 45ce4b4f9009 ("bpf: Fix crash due to out of bounds access into reg2btf_ids.") but after the fix, it should only index using base_type(reg->type), which should be less than __BPF_REG_TYPE_MAX, and also not permit any type flags to be set for the reg->type.

---
 tools/testing/selftests/bpf/verifier/calls.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/testing/selftests/bpf/verifier/calls.c +8 -0
index 829be2b9e08e..0a8ea60c2a80 100644
@@ -96,6 +96,25 @@
                { "bpf_kfunc_call_test_mem_len_fail1", 2 },
        },
 },
+{
+       "calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX",
+       .insns = {
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
+       BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
Quoted 9 lines

tools/testing/selftests/bpf/verifier/calls.c line 112

Quoted 6 lines
tools/testing/selftests/bpf/verifier/calls.c +6 -0
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       .result = REJECT,
+       .errstr = "arg#0 pointer type STRUCT prog_test_ref_kfunc must point",

Why do we stop errstr at "must point"?

The complete string is too long, I think it matches using strncmp.

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/bpf/verifier/calls.c +5 -1
+       .fixup_kfunc_btf_id = {
+               { "bpf_kfunc_call_test_acquire", 3 },
+               { "bpf_kfunc_call_test_release", 5 },
+       },
+},
 {
        "calls: basic sanity",
        .insns = {
--

2.35.1

--
Kartikeya

#3 lore raw
In reply to Kumar Kartikeya Dwivedi
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

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

Quoted 8 lines · On Sun, 20 Feb 2022 08:01:38 +0530 you wrote:

This test tries to pass a PTR_TO_BTF_ID_OR_NULL to the release function, which would trigger a out of bounds access without the fix in commit 45ce4b4f9009 ("bpf: Fix crash due to out of bounds access into reg2btf_ids.") but after the fix, it should only index using base_type(reg->type), which should be less than __BPF_REG_TYPE_MAX, and also not permit any type flags to be set for the reg->type.

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: Add test for reg2btf_ids out of bounds access

    https://git.kernel.org/bpf/bpf-next/c/13c6a37d409d

You are awesome, thank you!

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