Skip to contentSkip to conversation

tools/bpf: only set obj->skeleton without err

PATCH In review Wei Fu 2 participants 3 messages Jan 8, 2022 – Jan 11, 2022

Subsystems (4)
lore
Share & tools
lore

Wei Fu

author
#0 lore raw

PATCH 1/1 tools/bpf: only set obj->skeleton without err

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;

---
 tools/bpf/bpftool/gen.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/bpf/bpftool/gen.c +2 -1
index 5c18351290f0..e61e08f524da 100644
@@ -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\
Signature · 1 line
2.25.1
#1 lore raw
Quoted 11 lines · 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;

Quoted 5 lines
---
 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.

Quoted 24 lines
tools/bpf/bpftool/gen.c +2 -2
index 5c18351290f0..e61e08f524da 100644
@@ -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

author
#2 lore raw
Quoted 18 lines · Andrii Nakryiko
Quoted 11 lines · 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;

Quoted 5 lines
---
 tools/bpf/bpftool/gen.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 6 lines

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.