Skip to contentSkip to conversation

selftests/bpf: fix crash in core_reloc when bpftool btfgen fails

PATCH Applied Yucong Sun 5 participants 6 messages Feb 17, 2022

Subsystems (3)
lore
Share & tools
lore

Yucong Sun

author
#0 lore raw

PATCH 1/1 selftests/bpf: fix crash in core_reloc when bpftool btfgen fails

Initialize obj to null and skip closing if null.

---
 tools/testing/selftests/bpf/prog_tests/core_reloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +3 -2
index baf53c23c08d..7211243a22c3 100644
@@ -861,7 +861,7 @@ static void run_core_reloc_tests(bool use_btfgen)
 	struct bpf_link *link = NULL;
 	struct bpf_map *data_map;
 	struct bpf_program *prog;
-	struct bpf_object *obj;
+	struct bpf_object *obj = NULL;
 	uint64_t my_pid_tgid;
 	struct data *data;
 	void *mmap_data = NULL;
@@ -992,7 +992,8 @@ static void run_core_reloc_tests(bool use_btfgen)
 		remove(btf_file);
 		bpf_link__destroy(link);
 		link = NULL;
-		bpf_object__close(obj);
+		if (obj)
+			bpf_object__close(obj);
 	}
 }
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 21 lines · Yucong Sun

Initialize obj to null and skip closing if null.

---
 tools/testing/selftests/bpf/prog_tests/core_reloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +1 -1
index baf53c23c08d..7211243a22c3 100644
@@ -861,7 +861,7 @@ static void run_core_reloc_tests(bool use_btfgen)
 	struct bpf_link *link = NULL;
 	struct bpf_map *data_map;
 	struct bpf_program *prog;
-	struct bpf_object *obj;
+	struct bpf_object *obj = NULL;
 	uint64_t my_pid_tgid;
 	struct data *data;
 	void *mmap_data = NULL;

tools/testing/selftests/bpf/prog_tests/core_reloc.c static void run_core_reloc_tests(bool use_btfgen) · line 996

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -1
@@ -992,7 +992,8 @@ static void run_core_reloc_tests(bool use_btfgen)
 		remove(btf_file);
 		bpf_link__destroy(link);
 		link = NULL;
-		bpf_object__close(obj);
+		if (obj)
+			bpf_object__close(obj);

Should it be:

		bpf_object__close(obj);
		obj = NULL:
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/core_reloc.c +0 -0
 	}
 }
 
Signature · 1 line
2.30.2
#3 lore raw
Quoted 10 lines · On Thu, Feb 17, 2022 at 10:55 AM [email protected] <[email protected]>…
Quoted 4 lines

Should it be:
bpf_object__close(obj);
obj = NULL:

No, the actual crash is caused by this line:
https://github.com/kernel-patches/bpf/blob/bpf-next/tools/testing/selftests/bpf/prog_tests/core_reloc.c#L896

When run_btfgen fails, the obj contains uninitialized data and then bpf_object__close(obj) crashes.

Martin's point is that you have to NULL out obj so that on the next iteration this doesn't crash again. I'll fix it up while applying.

#4 lore raw
Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · On Thu, Feb 17, 2022 at 10:55 AM [email protected] <[email protected]>…
Quoted 4 lines

Should it be:
bpf_object__close(obj);
obj = NULL:

No, the actual crash is caused by this line:
https://github.com/kernel-patches/bpf/blob/bpf-next/tools/testing/selftests/bpf/prog_tests/core_reloc.c#L896

Quoted 6 lines
Quoted 2 lines

When run_btfgen fails, the obj contains uninitialized data and then bpf_object__close(obj) crashes.

Martin's point is that you have to NULL out obj so that on the next iteration this doesn't crash again. I'll fix it up while applying.

But I actually ended up replacing two goto cleanup with continue (there is no clean up to do). And adjusted commit message to reflect that. Applied to bpf-next, thanks for the fix!

#5 lore raw
In reply to Yucong Sun
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

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

Quoted 6 lines · On Thu, 17 Feb 2022 10:02:10 -0800 you wrote:

Initialize obj to null and skip closing if null.

---
 tools/testing/selftests/bpf/prog_tests/core_reloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Here is the summary with links:
- [bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails

    https://git.kernel.org/bpf/bpf-next/c/b75dacaac465

You are awesome, thank you!

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