BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
@ 2022-02-17 18:02 Yucong Sun
  2022-02-17 18:32 ` Martin KaFai Lau
  2022-02-17 19:10 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 6+ messages in thread
From: Yucong Sun @ 2022-02-17 18:02 UTC (permalink / raw)
  To: bpf; +Cc: andrii, fallentree, sunyucong

Initialize obj to null and skip closing if null.

Signed-off-by: Yucong Sun <fallentree@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/core_reloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
index baf53c23c08d..7211243a22c3 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
@@ -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);
 	}
 }
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
  2022-02-17 18:02 [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails Yucong Sun
@ 2022-02-17 18:32 ` Martin KaFai Lau
  2022-02-17 18:55   ` sunyucong
  2022-02-17 19:10 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 6+ messages in thread
From: Martin KaFai Lau @ 2022-02-17 18:32 UTC (permalink / raw)
  To: Yucong Sun; +Cc: bpf, andrii, sunyucong

On Thu, Feb 17, 2022 at 10:02:10AM -0800, Yucong Sun wrote:
> Initialize obj to null and skip closing if null.
> 
> Signed-off-by: Yucong Sun <fallentree@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/core_reloc.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc.c b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> index baf53c23c08d..7211243a22c3 100644
> --- a/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc.c
> @@ -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);
Should it be:
		bpf_object__close(obj);
		obj = NULL:

>  	}
>  }
>  
> -- 
> 2.30.2
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
  2022-02-17 18:32 ` Martin KaFai Lau
@ 2022-02-17 18:55   ` sunyucong
  2022-02-17 18:57     ` Andrii Nakryiko
  0 siblings, 1 reply; 6+ messages in thread
From: sunyucong @ 2022-02-17 18:55 UTC (permalink / raw)
  To: Martin KaFai Lau; +Cc: Yucong Sun, bpf, Andrii Nakryiko

> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
  2022-02-17 18:55   ` sunyucong
@ 2022-02-17 18:57     ` Andrii Nakryiko
  2022-02-17 19:07       ` Andrii Nakryiko
  0 siblings, 1 reply; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-17 18:57 UTC (permalink / raw)
  To: sunyucong@gmail.com; +Cc: Martin KaFai Lau, Yucong Sun, bpf, Andrii Nakryiko

On Thu, Feb 17, 2022 at 10:55 AM sunyucong@gmail.com
<sunyucong@gmail.com> wrote:
>
> > 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
  2022-02-17 18:57     ` Andrii Nakryiko
@ 2022-02-17 19:07       ` Andrii Nakryiko
  0 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-02-17 19:07 UTC (permalink / raw)
  To: sunyucong@gmail.com; +Cc: Martin KaFai Lau, Yucong Sun, bpf, Andrii Nakryiko

On Thu, Feb 17, 2022 at 10:57 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Feb 17, 2022 at 10:55 AM sunyucong@gmail.com
> <sunyucong@gmail.com> wrote:
> >
> > > 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.

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!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails
  2022-02-17 18:02 [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails Yucong Sun
  2022-02-17 18:32 ` Martin KaFai Lau
@ 2022-02-17 19:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-02-17 19:10 UTC (permalink / raw)
  To: Yucong Sun; +Cc: bpf, andrii, sunyucong

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 17 Feb 2022 10:02:10 -0800 you wrote:
> Initialize obj to null and skip closing if null.
> 
> Signed-off-by: Yucong Sun <fallentree@fb.com>
> ---
>  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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-02-17 19:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-17 18:02 [PATCH bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails Yucong Sun
2022-02-17 18:32 ` Martin KaFai Lau
2022-02-17 18:55   ` sunyucong
2022-02-17 18:57     ` Andrii Nakryiko
2022-02-17 19:07       ` Andrii Nakryiko
2022-02-17 19:10 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox