linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] tools/bpf: only set obj->skeleton without err
@ 2022-01-08  8:40 Wei Fu
  2022-01-10  2:04 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Wei Fu @ 2022-01-08  8:40 UTC (permalink / raw)
  To: ast, daniel, andrii, kafai, songliubraving, yhs, john.fastabend, kpsingh
  Cc: bpf, netdev, linux-kernel, Wei Fu

After `bpftool gen skeleton`, the ${bpf_app}.skel.h will provide that
${bpf_app_name}__open helper to load bpf. If there is some error
like ENOMEM, the ${bpf_app_name}__open will rollback(free) the allocated
object, including `bpf_object_skeleton`.

Since the ${bpf_app_name}__create_skeleton set the obj->skeleton first
and not rollback it when error, it will cause double-free in
${bpf_app_name}__destory at ${bpf_app_name}__open. Therefore, we should
set the obj->skeleton before return 0;

Signed-off-by: Wei Fu <fuweid89@gmail.com>
---
 tools/bpf/bpftool/gen.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 5c18351290f0..e61e08f524da 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -928,7 +928,6 @@ static int do_skeleton(int argc, char **argv)
 			s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
 			if (!s)						    \n\
 				goto err;				    \n\
-			obj->skeleton = s;				    \n\
 									    \n\
 			s->sz = sizeof(*s);				    \n\
 			s->name = \"%1$s\";				    \n\
@@ -1001,6 +1000,8 @@ static int do_skeleton(int argc, char **argv)
 									    \n\
 			s->data = (void *)%2$s__elf_bytes(&s->data_sz);	    \n\
 									    \n\
+			obj->skeleton = s;				    \n\
+									    \n\
 			return 0;					    \n\
 		err:							    \n\
 			bpf_object__destroy_skeleton(s);		    \n\
-- 
2.25.1


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

* Re: [PATCH bpf] tools/bpf: only set obj->skeleton without err
  2022-01-08  8:40 [PATCH bpf] tools/bpf: only set obj->skeleton without err Wei Fu
@ 2022-01-10  2:04 ` Andrii Nakryiko
  2022-01-11 17:09   ` Wei Fu
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2022-01-10  2:04 UTC (permalink / raw)
  To: Wei Fu
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin Lau,
	Song Liu, Yonghong Song, john fastabend, KP Singh, bpf,
	Networking, open list

On Sat, Jan 8, 2022 at 12:40 AM Wei Fu <fuweid89@gmail.com> wrote:
>
> After `bpftool gen skeleton`, the ${bpf_app}.skel.h will provide that
> ${bpf_app_name}__open helper to load bpf. If there is some error
> like ENOMEM, the ${bpf_app_name}__open will rollback(free) the allocated
> object, including `bpf_object_skeleton`.
>
> Since the ${bpf_app_name}__create_skeleton set the obj->skeleton first
> and not rollback it when error, it will cause double-free in
> ${bpf_app_name}__destory at ${bpf_app_name}__open. Therefore, we should
> set the obj->skeleton before return 0;
>
> Signed-off-by: Wei Fu <fuweid89@gmail.com>
> ---
>  tools/bpf/bpftool/gen.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>

Great catch! Added (please add it yourself in the future):

Fixes: 5dc7a8b21144 ("bpftool, selftests/bpf: Embed object file inside
skeleton")

Also reworded the subject a bit. Pushed to bpf-next.

> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index 5c18351290f0..e61e08f524da 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -928,7 +928,6 @@ static int do_skeleton(int argc, char **argv)
>                         s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
>                         if (!s)                                             \n\
>                                 goto err;                                   \n\
> -                       obj->skeleton = s;                                  \n\
>                                                                             \n\
>                         s->sz = sizeof(*s);                                 \n\
>                         s->name = \"%1$s\";                                 \n\
> @@ -1001,6 +1000,8 @@ static int do_skeleton(int argc, char **argv)
>                                                                             \n\
>                         s->data = (void *)%2$s__elf_bytes(&s->data_sz);     \n\
>                                                                             \n\
> +                       obj->skeleton = s;                                  \n\
> +                                                                           \n\
>                         return 0;                                           \n\
>                 err:                                                        \n\
>                         bpf_object__destroy_skeleton(s);                    \n\
> --
> 2.25.1
>

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

* Re: [PATCH bpf] tools/bpf: only set obj->skeleton without err
  2022-01-10  2:04 ` Andrii Nakryiko
@ 2022-01-11 17:09   ` Wei Fu
  0 siblings, 0 replies; 3+ messages in thread
From: Wei Fu @ 2022-01-11 17:09 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: andrii, ast, bpf, daniel, john.fastabend, kafai, kpsingh,
	linux-kernel, netdev, songliubraving, yhs, fuweid89

On Sun, Jan 09, 2022 at 06:04:45PM -0800, Andrii Nakryiko wrote:
> On Sat, Jan 8, 2022 at 12:40 AM Wei Fu <fuweid89@gmail.com> wrote:
> >
> > After `bpftool gen skeleton`, the ${bpf_app}.skel.h will provide that
> > ${bpf_app_name}__open helper to load bpf. If there is some error
> > like ENOMEM, the ${bpf_app_name}__open will rollback(free) the allocated
> > object, including `bpf_object_skeleton`.
> >
> > Since the ${bpf_app_name}__create_skeleton set the obj->skeleton first
> > and not rollback it when error, it will cause double-free in
> > ${bpf_app_name}__destory at ${bpf_app_name}__open. Therefore, we should
> > set the obj->skeleton before return 0;
> >
> > Signed-off-by: Wei Fu <fuweid89@gmail.com>
> > ---
> >  tools/bpf/bpftool/gen.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> 
> Great catch! Added (please add it yourself in the future):
> 
> Fixes: 5dc7a8b21144 ("bpftool, selftests/bpf: Embed object file inside
> skeleton")
> 
> Also reworded the subject a bit. Pushed to bpf-next.

Sure! Thanks for the review.

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

end of thread, other threads:[~2022-01-11 17:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-08  8:40 [PATCH bpf] tools/bpf: only set obj->skeleton without err Wei Fu
2022-01-10  2:04 ` Andrii Nakryiko
2022-01-11 17:09   ` Wei Fu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).