Skip to contentSkip to conversation

tools: bpftool: fix struct_ops command invalid pointer free

PATCH In review Daniel T. Lee 3 participants 4 messages Apr 10, 2020 – Apr 14, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: bpftool: fix struct_ops command invalid pointer free

From commit 65c93628599d ("bpftool: Add struct_ops support"), a new type of command struct_ops has been added.

This command requires kernel CONFIG_DEBUG_INFO_BTF=y, and for retrieving btf info, get_btf_vmlinux() is used.

When running this command on kernel without BTF debug info, this will lead to 'btf_vmlinux' variable contains invalid(error) pointer. And by this, btf_free() causes a segfault when executing 'bpftool struct_ops'.

This commit adds pointer validation with IS_ERR not to free invalid pointer, and this will fix the segfault issue.

---
 tools/bpf/bpftool/struct_ops.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/bpf/bpftool/struct_ops.c +3 -1
index 2a7befbd11ad..0fe0d584c57e 100644
@@ -591,6 +591,8 @@ int do_struct_ops(int argc, char **argv)
 
 	err = cmd_select(cmds, argc, argv, do_help);
 
-	btf__free(btf_vmlinux);
+	if (!IS_ERR(btf_vmlinux))
+		btf__free(btf_vmlinux);
+
 	return err;
 }
Signature · 1 line
2.26.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Daniel T. Lee

From commit 65c93628599d ("bpftool: Add struct_ops support"), a new type of command struct_ops has been added.

This command requires kernel CONFIG_DEBUG_INFO_BTF=y, and for retrieving btf info, get_btf_vmlinux() is used.

Quoted 8 lines

When running this command on kernel without BTF debug info, this will lead to 'btf_vmlinux' variable contains invalid(error) pointer. And by this, btf_free() causes a segfault when executing 'bpftool struct_ops'.

This commit adds pointer validation with IS_ERR not to free invalid pointer, and this will fix the segfault issue.

Fixes 65c93628599d ("bpftool: Add struct_ops support")
Acked-by Martin KaFai Lau

Thanks for the fix! Please add the Fixes tag in the future.

#2 lore raw

Will do, thanks for letting me know.
Thank you for your time and effort for the review.

Best,
Daniel

Quoted 20 lines · Martin KaFai Lau
Quoted 6 lines · Daniel T. Lee

From commit 65c93628599d ("bpftool: Add struct_ops support"), a new type of command struct_ops has been added.

This command requires kernel CONFIG_DEBUG_INFO_BTF=y, and for retrieving btf info, get_btf_vmlinux() is used.

Quoted 8 lines

When running this command on kernel without BTF debug info, this will lead to 'btf_vmlinux' variable contains invalid(error) pointer. And by this, btf_free() causes a segfault when executing 'bpftool struct_ops'.

This commit adds pointer validation with IS_ERR not to free invalid pointer, and this will fix the segfault issue.

Fixes 65c93628599d ("bpftool: Add struct_ops support")
Acked-by Martin KaFai Lau

Thanks for the fix! Please add the Fixes tag in the future.

maintainer
#3 lore raw
In reply to Martin KaFai Lau
Quoted 11 lines · Martin KaFai Lau
Quoted 10 lines · Daniel T. Lee

From commit 65c93628599d ("bpftool: Add struct_ops support"),
a new type of command struct_ops has been added.

This command requires kernel CONFIG_DEBUG_INFO_BTF=y, and for retrieving btf info, get_btf_vmlinux() is used.

When running this command on kernel without BTF debug info, this will lead to 'btf_vmlinux' variable contains invalid(error) pointer. And by this, btf_free() causes a segfault when executing 'bpftool struct_ops'.

Quoted 6 lines
Quoted 4 lines

This commit adds pointer validation with IS_ERR not to free invalid pointer, and this will fix the segfault issue.

Fixes 65c93628599d ("bpftool: Add struct_ops support")
Acked-by Martin KaFai Lau

Applied & fixed up email, thanks!