diff mbox series

[bpf-next,1/2] bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules

Message ID 2ec861621e283ba2a54f7e939eafed1c29f77bf4.1669216157.git.vmalik@redhat.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Fix attaching fentry/fexit/fmod_ret/lsm to modules | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix
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 Series has a cover letter
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: 1397 this patch: 1397
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 157 this patch: 157
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: 1387 this patch: 1387
netdev/checkpatch fail ERROR: else should follow close brace '}' WARNING: Missing a blank line after declarations WARNING: line length of 83 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Viktor Malik Nov. 28, 2022, 7:26 a.m. UTC
When attaching fentry/fexit/fmod_ret/lsm to a function located in a
module without specifying the target program, the verifier tries to find
the address to attach to in kallsyms. This is always done by searching
the entire kallsyms, not respecting the module in which the function is
located.

This approach causes an incorrect attachment address to be computed if
the function to attach to is shadowed by a function of the same name
located earlier in kallsyms.

Since the attachment must contain the BTF of the program to attach to,
we may extract the module name from it (if the attach target is a
module) and search for the function address in the correct module.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
---
 include/linux/btf.h   | 1 +
 kernel/bpf/btf.c      | 5 +++++
 kernel/bpf/verifier.c | 9 ++++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)

Comments

Jiri Olsa Nov. 28, 2022, 9:07 a.m. UTC | #1
On Mon, Nov 28, 2022 at 08:26:29AM +0100, Viktor Malik wrote:
> When attaching fentry/fexit/fmod_ret/lsm to a function located in a
> module without specifying the target program, the verifier tries to find
> the address to attach to in kallsyms. This is always done by searching
> the entire kallsyms, not respecting the module in which the function is
> located.
> 
> This approach causes an incorrect attachment address to be computed if
> the function to attach to is shadowed by a function of the same name
> located earlier in kallsyms.
> 
> Since the attachment must contain the BTF of the program to attach to,
> we may extract the module name from it (if the attach target is a
> module) and search for the function address in the correct module.
> 
> Signed-off-by: Viktor Malik <vmalik@redhat.com>
> ---
>  include/linux/btf.h   | 1 +
>  kernel/bpf/btf.c      | 5 +++++
>  kernel/bpf/verifier.c | 9 ++++++++-
>  3 files changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 9ed00077db6e..bdbf3eb7083d 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -187,6 +187,7 @@ u32 btf_obj_id(const struct btf *btf);
>  bool btf_is_kernel(const struct btf *btf);
>  bool btf_is_module(const struct btf *btf);
>  struct module *btf_try_get_module(const struct btf *btf);
> +const char *btf_module_name(const struct btf *btf);
>  u32 btf_nr_types(const struct btf *btf);
>  bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
>  			   const struct btf_member *m,
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 1a59cc7ad730..211fcbb7649d 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -7192,6 +7192,11 @@ bool btf_is_module(const struct btf *btf)
>  	return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
>  }
>  
> +const char *btf_module_name(const struct btf *btf)
> +{
> +	return btf->name;
> +}
> +
>  enum {
>  	BTF_MODULE_F_LIVE = (1 << 0),
>  };
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 9528a066cfa5..acbe62a73559 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -16343,7 +16343,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
>  			else
>  				addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
>  		} else {
> -			addr = kallsyms_lookup_name(tname);
> +			if (btf_is_module(btf)) {
> +				char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];

looks good.. would be nice to have module_kallsyms lookup function that
takes module name and symbol separately so we won't waste stack on that..

especially when module_kallsyms_lookup_name just separates it back again
and does module lookup.. but not sure how much pain it'd be

jirka

> +				snprintf(tmodname, sizeof(tmodname), "%s:%s",
> +					 btf_module_name(btf), tname);
> +				addr = module_kallsyms_lookup_name(tmodname);
> +			}
> +			else
> +				addr = kallsyms_lookup_name(tname);
>  			if (!addr) {
>  				bpf_log(log,
>  					"The address of function %s cannot be found\n",
> -- 
> 2.38.1
>
Hao Luo Nov. 28, 2022, 8:06 p.m. UTC | #2
On Mon, Nov 28, 2022 at 1:07 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Mon, Nov 28, 2022 at 08:26:29AM +0100, Viktor Malik wrote:
> > When attaching fentry/fexit/fmod_ret/lsm to a function located in a
> > module without specifying the target program, the verifier tries to find
> > the address to attach to in kallsyms. This is always done by searching
> > the entire kallsyms, not respecting the module in which the function is
> > located.
> >
> > This approach causes an incorrect attachment address to be computed if
> > the function to attach to is shadowed by a function of the same name
> > located earlier in kallsyms.
> >
> > Since the attachment must contain the BTF of the program to attach to,
> > we may extract the module name from it (if the attach target is a
> > module) and search for the function address in the correct module.
> >
> > Signed-off-by: Viktor Malik <vmalik@redhat.com>
> > ---
> >  include/linux/btf.h   | 1 +
> >  kernel/bpf/btf.c      | 5 +++++
> >  kernel/bpf/verifier.c | 9 ++++++++-
> >  3 files changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > index 9ed00077db6e..bdbf3eb7083d 100644
> > --- a/include/linux/btf.h
> > +++ b/include/linux/btf.h
> > @@ -187,6 +187,7 @@ u32 btf_obj_id(const struct btf *btf);
> >  bool btf_is_kernel(const struct btf *btf);
> >  bool btf_is_module(const struct btf *btf);
> >  struct module *btf_try_get_module(const struct btf *btf);
> > +const char *btf_module_name(const struct btf *btf);
> >  u32 btf_nr_types(const struct btf *btf);
> >  bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
> >                          const struct btf_member *m,
> > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > index 1a59cc7ad730..211fcbb7649d 100644
> > --- a/kernel/bpf/btf.c
> > +++ b/kernel/bpf/btf.c
> > @@ -7192,6 +7192,11 @@ bool btf_is_module(const struct btf *btf)
> >       return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
> >  }
> >
> > +const char *btf_module_name(const struct btf *btf)
> > +{
> > +     return btf->name;
> > +}
> > +
> >  enum {
> >       BTF_MODULE_F_LIVE = (1 << 0),
> >  };
> > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > index 9528a066cfa5..acbe62a73559 100644
> > --- a/kernel/bpf/verifier.c
> > +++ b/kernel/bpf/verifier.c
> > @@ -16343,7 +16343,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
> >                       else
> >                               addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
> >               } else {
> > -                     addr = kallsyms_lookup_name(tname);
> > +                     if (btf_is_module(btf)) {
> > +                             char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];
>
> looks good.. would be nice to have module_kallsyms lookup function that
> takes module name and symbol separately so we won't waste stack on that..
>
> especially when module_kallsyms_lookup_name just separates it back again
> and does module lookup.. but not sure how much pain it'd be
>
> jirka
>
> > +                             snprintf(tmodname, sizeof(tmodname), "%s:%s",
> > +                                      btf_module_name(btf), tname);
> > +                             addr = module_kallsyms_lookup_name(tmodname);
> > +                     }
> > +                     else
> > +                             addr = kallsyms_lookup_name(tname);

In addition to what Jiri suggested, we should also have brackets in
the 'else' branch.

if (...) {
  ...
} else {
  ...
}

> >                       if (!addr) {
> >                               bpf_log(log,
> >                                       "The address of function %s cannot be found\n",
> > --
> > 2.38.1
> >
diff mbox series

Patch

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 9ed00077db6e..bdbf3eb7083d 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -187,6 +187,7 @@  u32 btf_obj_id(const struct btf *btf);
 bool btf_is_kernel(const struct btf *btf);
 bool btf_is_module(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
+const char *btf_module_name(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   const struct btf_member *m,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 1a59cc7ad730..211fcbb7649d 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7192,6 +7192,11 @@  bool btf_is_module(const struct btf *btf)
 	return btf->kernel_btf && strcmp(btf->name, "vmlinux") != 0;
 }
 
+const char *btf_module_name(const struct btf *btf)
+{
+	return btf->name;
+}
+
 enum {
 	BTF_MODULE_F_LIVE = (1 << 0),
 };
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 9528a066cfa5..acbe62a73559 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -16343,7 +16343,14 @@  int bpf_check_attach_target(struct bpf_verifier_log *log,
 			else
 				addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
 		} else {
-			addr = kallsyms_lookup_name(tname);
+			if (btf_is_module(btf)) {
+				char tmodname[MODULE_NAME_LEN + KSYM_NAME_LEN + 1];
+				snprintf(tmodname, sizeof(tmodname), "%s:%s",
+					 btf_module_name(btf), tname);
+				addr = module_kallsyms_lookup_name(tmodname);
+			}
+			else
+				addr = kallsyms_lookup_name(tname);
 			if (!addr) {
 				bpf_log(log,
 					"The address of function %s cannot be found\n",