diff mbox series

[bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails

Message ID 20220217180210.2981502-1-fallentree@fb.com (mailing list archive)
State Accepted
Commit b75dacaac4650478ed5a9d33975b91b99016daff
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: fix crash in core_reloc when bpftool btfgen fails | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 12 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org mauricio@kinvolk.io daniel@iogearbox.net john.fastabend@gmail.com kafai@fb.com shuah@kernel.org songliubraving@fb.com ast@kernel.org kuifeng@fb.com yhs@fb.com netdev@vger.kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 17 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR pending PR summary
bpf/vmtest-bpf-next pending VM_Test

Commit Message

Yucong Sun Feb. 17, 2022, 6:02 p.m. UTC
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(-)

Comments

Martin KaFai Lau Feb. 17, 2022, 6:32 p.m. UTC | #1
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
>
sunyucong@gmail.com Feb. 17, 2022, 6:55 p.m. UTC | #2
> 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.
Andrii Nakryiko Feb. 17, 2022, 6:57 p.m. UTC | #3
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.
Andrii Nakryiko Feb. 17, 2022, 7:07 p.m. UTC | #4
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!
patchwork-bot+netdevbpf@kernel.org Feb. 17, 2022, 7:10 p.m. UTC | #5
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!
diff mbox series

Patch

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);
 	}
 }