diff mbox series

[bpf-next,1/2] bpf: Fix memleak due to fentry attach failure

Message ID 20230509151511.3937-2-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: bpf trampoline improvements | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 10 this patch: 10
netdev/cc_maintainers warning 2 maintainers not CCed: martin.lau@linux.dev song@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
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: 10 this patch: 10
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 41 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-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-36 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_verifier on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on s390x with gcc

Commit Message

Yafang Shao May 9, 2023, 3:15 p.m. UTC
If it fails to attach fentry, the allocated bpf trampoline image will be
left in the system. That can be verified by checking /proc/kallsyms.

This meamleak can be verified by a simple bpf program as follows,

  SEC("fentry/trap_init")
  int fentry_run()
  {
      return 0;
  }

It will fail to attach trap_init because this function is freed after
kernel init, and then we can find the trampoline image is left in the
system by checking /proc/kallsyms.
  $ tail /proc/kallsyms
  ffffffffc0613000 t bpf_trampoline_6442453466_1  [bpf]
  ffffffffc06c3000 t bpf_trampoline_6442453466_1  [bpf]

  $ bpftool btf dump file /sys/kernel/btf/vmlinux | grep "FUNC 'trap_init'"
  [2522] FUNC 'trap_init' type_id=119 linkage=static

  $ echo $((6442453466 & 0x7fffffff))
  2522

Note that there are two left bpf trampoline images, that is because the
libbpf will fallback to raw tracepoint if -EINVAL is returned.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/bpf/trampoline.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

Comments

Song Liu May 9, 2023, 5:40 p.m. UTC | #1
On Tue, May 9, 2023 at 8:15 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> If it fails to attach fentry, the allocated bpf trampoline image will be
> left in the system. That can be verified by checking /proc/kallsyms.
>
> This meamleak can be verified by a simple bpf program as follows,
>
>   SEC("fentry/trap_init")
>   int fentry_run()
>   {
>       return 0;
>   }

Nice trick! We can build some interesting tests with trap_init.

>
> It will fail to attach trap_init because this function is freed after
> kernel init, and then we can find the trampoline image is left in the
> system by checking /proc/kallsyms.
>   $ tail /proc/kallsyms
>   ffffffffc0613000 t bpf_trampoline_6442453466_1  [bpf]
>   ffffffffc06c3000 t bpf_trampoline_6442453466_1  [bpf]
>
>   $ bpftool btf dump file /sys/kernel/btf/vmlinux | grep "FUNC 'trap_init'"
>   [2522] FUNC 'trap_init' type_id=119 linkage=static
>
>   $ echo $((6442453466 & 0x7fffffff))
>   2522
>
> Note that there are two left bpf trampoline images, that is because the
> libbpf will fallback to raw tracepoint if -EINVAL is returned.
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>

I guess we need:

 Fixes: e21aa341785c ("bpf: Fix fexit trampoline.")

> ---
>  kernel/bpf/trampoline.c | 17 +++++++++++++++--
>  1 file changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> index ac021bc..7067cdf 100644
> --- a/kernel/bpf/trampoline.c
> +++ b/kernel/bpf/trampoline.c
> @@ -251,6 +251,15 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
>         return tlinks;
>  }
>
> +static void bpf_tramp_image_free(struct bpf_tramp_image *im)
> +{
> +       bpf_image_ksym_del(&im->ksym);
> +       bpf_jit_free_exec(im->image);
> +       bpf_jit_uncharge_modmem(PAGE_SIZE);
> +       percpu_ref_exit(&im->pcref);
> +       kfree(im);
> +}

Can we share some of this function with __bpf_tramp_image_put_deferred?

Thanks,
Song
[...]
Yafang Shao May 10, 2023, 2:38 a.m. UTC | #2
On Wed, May 10, 2023 at 1:41 AM Song Liu <song@kernel.org> wrote:
>
> On Tue, May 9, 2023 at 8:15 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > If it fails to attach fentry, the allocated bpf trampoline image will be
> > left in the system. That can be verified by checking /proc/kallsyms.
> >
> > This meamleak can be verified by a simple bpf program as follows,
> >
> >   SEC("fentry/trap_init")
> >   int fentry_run()
> >   {
> >       return 0;
> >   }
>
> Nice trick! We can build some interesting tests with trap_init.
>

Good suggestion. I will think about it.

> >
> > It will fail to attach trap_init because this function is freed after
> > kernel init, and then we can find the trampoline image is left in the
> > system by checking /proc/kallsyms.
> >   $ tail /proc/kallsyms
> >   ffffffffc0613000 t bpf_trampoline_6442453466_1  [bpf]
> >   ffffffffc06c3000 t bpf_trampoline_6442453466_1  [bpf]
> >
> >   $ bpftool btf dump file /sys/kernel/btf/vmlinux | grep "FUNC 'trap_init'"
> >   [2522] FUNC 'trap_init' type_id=119 linkage=static
> >
> >   $ echo $((6442453466 & 0x7fffffff))
> >   2522
> >
> > Note that there are two left bpf trampoline images, that is because the
> > libbpf will fallback to raw tracepoint if -EINVAL is returned.
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
>
> I guess we need:
>
>  Fixes: e21aa341785c ("bpf: Fix fexit trampoline.")
>

Thanks for pointing it out. I will add it.

> > ---
> >  kernel/bpf/trampoline.c | 17 +++++++++++++++--
> >  1 file changed, 15 insertions(+), 2 deletions(-)
> >
> > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> > index ac021bc..7067cdf 100644
> > --- a/kernel/bpf/trampoline.c
> > +++ b/kernel/bpf/trampoline.c
> > @@ -251,6 +251,15 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
> >         return tlinks;
> >  }
> >
> > +static void bpf_tramp_image_free(struct bpf_tramp_image *im)
> > +{
> > +       bpf_image_ksym_del(&im->ksym);
> > +       bpf_jit_free_exec(im->image);
> > +       bpf_jit_uncharge_modmem(PAGE_SIZE);
> > +       percpu_ref_exit(&im->pcref);
> > +       kfree(im);
> > +}
>
> Can we share some of this function with __bpf_tramp_image_put_deferred?
>

It seems we can introduce a generic helper as follows,
  static void __bpf_tramp_image_free(struct bpf_tramp_image *im)
  {
      bpf_image_ksym_del(&im->ksym);
      bpf_jit_free_exec(im->image);
      bpf_jit_uncharge_modmem(PAGE_SIZE);
      percpu_ref_exit(&im->pcref);
  }

And then use it in both bpf_tramp_image_free() and
__bpf_tramp_image_put_deferred().
WDYT?
Song Liu May 10, 2023, 6:23 a.m. UTC | #3
On Tue, May 9, 2023 at 7:39 PM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> On Wed, May 10, 2023 at 1:41 AM Song Liu <song@kernel.org> wrote:
[...]
> > > +static void bpf_tramp_image_free(struct bpf_tramp_image *im)
> > > +{
> > > +       bpf_image_ksym_del(&im->ksym);
> > > +       bpf_jit_free_exec(im->image);
> > > +       bpf_jit_uncharge_modmem(PAGE_SIZE);
> > > +       percpu_ref_exit(&im->pcref);
> > > +       kfree(im);
> > > +}
> >
> > Can we share some of this function with __bpf_tramp_image_put_deferred?
> >
>
> It seems we can introduce a generic helper as follows,
>   static void __bpf_tramp_image_free(struct bpf_tramp_image *im)
>   {
>       bpf_image_ksym_del(&im->ksym);
>       bpf_jit_free_exec(im->image);
>       bpf_jit_uncharge_modmem(PAGE_SIZE);
>       percpu_ref_exit(&im->pcref);
>   }
>
> And then use it in both bpf_tramp_image_free() and
> __bpf_tramp_image_put_deferred().
> WDYT?

How about we also use kfree_rcu() in bpf_tramp_image_free()?

Thanks,
Song
Yafang Shao May 10, 2023, 3:30 p.m. UTC | #4
On Wed, May 10, 2023 at 2:24 PM Song Liu <song@kernel.org> wrote:
>
> On Tue, May 9, 2023 at 7:39 PM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > On Wed, May 10, 2023 at 1:41 AM Song Liu <song@kernel.org> wrote:
> [...]
> > > > +static void bpf_tramp_image_free(struct bpf_tramp_image *im)
> > > > +{
> > > > +       bpf_image_ksym_del(&im->ksym);
> > > > +       bpf_jit_free_exec(im->image);
> > > > +       bpf_jit_uncharge_modmem(PAGE_SIZE);
> > > > +       percpu_ref_exit(&im->pcref);
> > > > +       kfree(im);
> > > > +}
> > >
> > > Can we share some of this function with __bpf_tramp_image_put_deferred?
> > >
> >
> > It seems we can introduce a generic helper as follows,
> >   static void __bpf_tramp_image_free(struct bpf_tramp_image *im)
> >   {
> >       bpf_image_ksym_del(&im->ksym);
> >       bpf_jit_free_exec(im->image);
> >       bpf_jit_uncharge_modmem(PAGE_SIZE);
> >       percpu_ref_exit(&im->pcref);
> >   }
> >
> > And then use it in both bpf_tramp_image_free() and
> > __bpf_tramp_image_put_deferred().
> > WDYT?
>
> How about we also use kfree_rcu() in bpf_tramp_image_free()?
>

Looks good. I will change it.
diff mbox series

Patch

diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index ac021bc..7067cdf 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -251,6 +251,15 @@  static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
 	return tlinks;
 }
 
+static void bpf_tramp_image_free(struct bpf_tramp_image *im)
+{
+	bpf_image_ksym_del(&im->ksym);
+	bpf_jit_free_exec(im->image);
+	bpf_jit_uncharge_modmem(PAGE_SIZE);
+	percpu_ref_exit(&im->pcref);
+	kfree(im);
+}
+
 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
 {
 	struct bpf_tramp_image *im;
@@ -438,7 +447,7 @@  static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 					  &tr->func.model, tr->flags, tlinks,
 					  tr->func.addr);
 	if (err < 0)
-		goto out;
+		goto out_free;
 
 	set_memory_rox((long)im->image, 1);
 
@@ -468,7 +477,7 @@  static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 	}
 #endif
 	if (err)
-		goto out;
+		goto out_free;
 
 	if (tr->cur_image)
 		bpf_tramp_image_put(tr->cur_image);
@@ -480,6 +489,10 @@  static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
 		tr->flags = orig_flags;
 	kfree(tlinks);
 	return err;
+
+out_free:
+	bpf_tramp_image_free(im);
+	goto out;
 }
 
 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)