diff mbox series

[bpf-next] libbpf: Error out when binary_path is NULL for uprobe and USDT

Message ID 20220712025745.2703995-1-hengqi.chen@gmail.com (mailing list archive)
State Accepted
Commit 8ed2f5a6f385b5fff313208b90ea83f7121bd909
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: Error out when binary_path is NULL for uprobe and USDT | 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 10 maintainers not CCed: haoluo@google.com song@kernel.org daniel@iogearbox.net ast@kernel.org martin.lau@linux.dev yhs@fb.com john.fastabend@gmail.com jolsa@kernel.org sdf@google.com kpsingh@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/check_selftest success No net selftest shell script
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, 31 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-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Hengqi Chen July 12, 2022, 2:57 a.m. UTC
binary_path is a required non-null parameter for bpf_program__attach_usdt
and bpf_program__attach_uprobe_opts. Check it against NULL to prevent
coredump on strchr.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 tools/lib/bpf/libbpf.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

--
2.30.2

Comments

Jiri Olsa July 12, 2022, 7:58 a.m. UTC | #1
On Tue, Jul 12, 2022 at 10:57:45AM +0800, Hengqi Chen wrote:
> binary_path is a required non-null parameter for bpf_program__attach_usdt
> and bpf_program__attach_uprobe_opts. Check it against NULL to prevent
> coredump on strchr.

binary_path seems to be mandatory so LGTM, cc-ing Alan to be sure ;-)

thanks,
jirka

> 
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> ---
>  tools/lib/bpf/libbpf.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index cb49408eb298..72548798126b 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -10545,7 +10545,10 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
>  	ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
>  	pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
> 
> -	if (binary_path && !strchr(binary_path, '/')) {
> +	if (!binary_path)
> +		return libbpf_err_ptr(-EINVAL);
> +
> +	if (!strchr(binary_path, '/')) {
>  		err = resolve_full_path(binary_path, full_binary_path,
>  					sizeof(full_binary_path));
>  		if (err) {
> @@ -10559,11 +10562,6 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
>  	if (func_name) {
>  		long sym_off;
> 
> -		if (!binary_path) {
> -			pr_warn("prog '%s': name-based attach requires binary_path\n",
> -				prog->name);
> -			return libbpf_err_ptr(-EINVAL);
> -		}
>  		sym_off = elf_find_func_offset(binary_path, func_name);
>  		if (sym_off < 0)
>  			return libbpf_err_ptr(sym_off);
> @@ -10711,6 +10709,9 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
>  		return libbpf_err_ptr(-EINVAL);
>  	}
> 
> +	if (!binary_path)
> +		return libbpf_err_ptr(-EINVAL);
> +
>  	if (!strchr(binary_path, '/')) {
>  		err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
>  		if (err) {
> --
> 2.30.2
Andrii Nakryiko July 13, 2022, 6:59 p.m. UTC | #2
On Tue, Jul 12, 2022 at 12:58 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Tue, Jul 12, 2022 at 10:57:45AM +0800, Hengqi Chen wrote:
> > binary_path is a required non-null parameter for bpf_program__attach_usdt
> > and bpf_program__attach_uprobe_opts. Check it against NULL to prevent
> > coredump on strchr.
>
> binary_path seems to be mandatory so LGTM, cc-ing Alan to be sure ;-)
>

Right, what will happen for attach_uprobe with NULL binary_path is
that it will be passed as zero to perf_event_create() and kernel will
reject it with -EINVAL. So this looks all correct to me, applying to
bpf-next.

Thanks for the fix!

> thanks,
> jirka
>
> >
> > Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> > ---
> >  tools/lib/bpf/libbpf.c | 13 +++++++------
> >  1 file changed, 7 insertions(+), 6 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index cb49408eb298..72548798126b 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -10545,7 +10545,10 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
> >       ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
> >       pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
> >
> > -     if (binary_path && !strchr(binary_path, '/')) {
> > +     if (!binary_path)
> > +             return libbpf_err_ptr(-EINVAL);
> > +
> > +     if (!strchr(binary_path, '/')) {
> >               err = resolve_full_path(binary_path, full_binary_path,
> >                                       sizeof(full_binary_path));
> >               if (err) {
> > @@ -10559,11 +10562,6 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
> >       if (func_name) {
> >               long sym_off;
> >
> > -             if (!binary_path) {
> > -                     pr_warn("prog '%s': name-based attach requires binary_path\n",
> > -                             prog->name);
> > -                     return libbpf_err_ptr(-EINVAL);
> > -             }
> >               sym_off = elf_find_func_offset(binary_path, func_name);
> >               if (sym_off < 0)
> >                       return libbpf_err_ptr(sym_off);
> > @@ -10711,6 +10709,9 @@ struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
> >               return libbpf_err_ptr(-EINVAL);
> >       }
> >
> > +     if (!binary_path)
> > +             return libbpf_err_ptr(-EINVAL);
> > +
> >       if (!strchr(binary_path, '/')) {
> >               err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
> >               if (err) {
> > --
> > 2.30.2
patchwork-bot+netdevbpf@kernel.org July 13, 2022, 7:10 p.m. UTC | #3
Hello:

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

On Tue, 12 Jul 2022 10:57:45 +0800 you wrote:
> binary_path is a required non-null parameter for bpf_program__attach_usdt
> and bpf_program__attach_uprobe_opts. Check it against NULL to prevent
> coredump on strchr.
> 
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> ---
>  tools/lib/bpf/libbpf.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> [...]

Here is the summary with links:
  - [bpf-next] libbpf: Error out when binary_path is NULL for uprobe and USDT
    https://git.kernel.org/bpf/bpf-next/c/8ed2f5a6f385

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index cb49408eb298..72548798126b 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10545,7 +10545,10 @@  bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 	ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
 	pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);

-	if (binary_path && !strchr(binary_path, '/')) {
+	if (!binary_path)
+		return libbpf_err_ptr(-EINVAL);
+
+	if (!strchr(binary_path, '/')) {
 		err = resolve_full_path(binary_path, full_binary_path,
 					sizeof(full_binary_path));
 		if (err) {
@@ -10559,11 +10562,6 @@  bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
 	if (func_name) {
 		long sym_off;

-		if (!binary_path) {
-			pr_warn("prog '%s': name-based attach requires binary_path\n",
-				prog->name);
-			return libbpf_err_ptr(-EINVAL);
-		}
 		sym_off = elf_find_func_offset(binary_path, func_name);
 		if (sym_off < 0)
 			return libbpf_err_ptr(sym_off);
@@ -10711,6 +10709,9 @@  struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
 		return libbpf_err_ptr(-EINVAL);
 	}

+	if (!binary_path)
+		return libbpf_err_ptr(-EINVAL);
+
 	if (!strchr(binary_path, '/')) {
 		err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
 		if (err) {