diff mbox series

[bpf] bpf: fix a potential use-after-free in bpf_link_free()

Message ID 20240602182703.207276-1-xiyou.wangcong@gmail.com (mailing list archive)
State Accepted
Commit 2884dc7d08d98a89d8d65121524bb7533183a63a
Delegated to: BPF
Headers show
Series [bpf] bpf: fix a potential use-after-free in bpf_link_free() | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR success PR summary
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 45 this patch: 45
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers fail 2 blamed authors not CCed: jolsa@kernel.org ast@kernel.org; 11 maintainers not CCed: song@kernel.org john.fastabend@gmail.com martin.lau@linux.dev kpsingh@kernel.org yonghong.song@linux.dev sdf@google.com haoluo@google.com ast@kernel.org daniel@iogearbox.net eddyz87@gmail.com jolsa@kernel.org
netdev/build_clang fail Errors and warnings before: 36 this patch: 36
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 Fixes tag looks correct
netdev/build_allmodconfig_warn fail Errors and warnings before: 45 this patch: 45
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18

Commit Message

Cong Wang June 2, 2024, 6:27 p.m. UTC
From: Cong Wang <cong.wang@bytedance.com>

After commit 1a80dbcb2dba, bpf_link can be freed by
link->ops->dealloc_deferred, but the code still tests and uses
link->ops->dealloc afterward, which leads to a use-after-free as
reported by syzbot. Actually, one of them should be sufficient, so
just call one of them instead of both. Also add a WARN_ON() in case
of any problematic implementation.

Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
Cc: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
---
 kernel/bpf/syscall.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jiri Olsa June 2, 2024, 7:32 p.m. UTC | #1
On Sun, Jun 02, 2024 at 11:27:03AM -0700, Cong Wang wrote:
> From: Cong Wang <cong.wang@bytedance.com>
> 
> After commit 1a80dbcb2dba, bpf_link can be freed by
> link->ops->dealloc_deferred, but the code still tests and uses
> link->ops->dealloc afterward, which leads to a use-after-free as
> reported by syzbot. Actually, one of them should be sufficient, so
> just call one of them instead of both. Also add a WARN_ON() in case
> of any problematic implementation.
> 
> Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
> Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Cong Wang <cong.wang@bytedance.com>
> ---
>  kernel/bpf/syscall.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 2222c3ff88e7..d8f244069495 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -2998,6 +2998,7 @@ static int bpf_obj_get(const union bpf_attr *attr)
>  void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
>  		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
>  {
> +	WARN_ON(ops->dealloc && ops->dealloc_deferred);
>  	atomic64_set(&link->refcnt, 1);
>  	link->type = type;
>  	link->id = 0;
> @@ -3074,8 +3075,7 @@ static void bpf_link_free(struct bpf_link *link)
>  			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
>  		else
>  			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
> -	}
> -	if (link->ops->dealloc)
> +	} else if (link->ops->dealloc)
>  		link->ops->dealloc(link);

nice catch

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka
Daniel Borkmann June 3, 2024, 4:15 p.m. UTC | #2
On 6/2/24 9:32 PM, Jiri Olsa wrote:
> On Sun, Jun 02, 2024 at 11:27:03AM -0700, Cong Wang wrote:
>> From: Cong Wang <cong.wang@bytedance.com>
>>
>> After commit 1a80dbcb2dba, bpf_link can be freed by
>> link->ops->dealloc_deferred, but the code still tests and uses
>> link->ops->dealloc afterward, which leads to a use-after-free as
>> reported by syzbot. Actually, one of them should be sufficient, so
>> just call one of them instead of both. Also add a WARN_ON() in case
>> of any problematic implementation.
>>
>> Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
>> Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
>> Cc: Andrii Nakryiko <andrii@kernel.org>
>> Signed-off-by: Cong Wang <cong.wang@bytedance.com>
>> ---
>>   kernel/bpf/syscall.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index 2222c3ff88e7..d8f244069495 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -2998,6 +2998,7 @@ static int bpf_obj_get(const union bpf_attr *attr)
>>   void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
>>   		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
>>   {
>> +	WARN_ON(ops->dealloc && ops->dealloc_deferred);
>>   	atomic64_set(&link->refcnt, 1);
>>   	link->type = type;
>>   	link->id = 0;
>> @@ -3074,8 +3075,7 @@ static void bpf_link_free(struct bpf_link *link)
>>   			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
>>   		else
>>   			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
>> -	}
>> -	if (link->ops->dealloc)
>> +	} else if (link->ops->dealloc)
>>   		link->ops->dealloc(link);
> 
> nice catch

+1, thanks Cong !

> Acked-by: Jiri Olsa <jolsa@kernel.org>

I think it would also be slightly nicer to just fetch the ops once, which
wouldn't have caused the issue if it was done back then in the first place.
Do you mind if I squash this in and then apply it to bpf tree? Looks as
follows :

 From 220fb40d88c7b5d1f3234a5f1b807f6067450832 Mon Sep 17 00:00:00 2001
Message-Id: <220fb40d88c7b5d1f3234a5f1b807f6067450832.1717431085.git.daniel@iogearbox.net>
From: Cong Wang <cong.wang@bytedance.com>
Date: Sun, 2 Jun 2024 11:27:03 -0700
Subject: [PATCH bpf] bpf: Fix a potential use-after-free in bpf_link_free()

After commit 1a80dbcb2dba, bpf_link can be freed by
link->ops->dealloc_deferred, but the code still tests and uses
link->ops->dealloc afterward, which leads to a use-after-free as
reported by syzbot. Actually, one of them should be sufficient, so
just call one of them instead of both. Also add a WARN_ON() in case
of any problematic implementation.

Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20240602182703.207276-1-xiyou.wangcong@gmail.com
---
  kernel/bpf/syscall.c | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 5070fa20d05c..869265852d51 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2998,6 +2998,7 @@ static int bpf_obj_get(const union bpf_attr *attr)
  void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
  		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
  {
+	WARN_ON(ops->dealloc && ops->dealloc_deferred);
  	atomic64_set(&link->refcnt, 1);
  	link->type = type;
  	link->id = 0;
@@ -3056,16 +3057,17 @@ static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu)
  /* bpf_link_free is guaranteed to be called from process context */
  static void bpf_link_free(struct bpf_link *link)
  {
+	const struct bpf_link_ops *ops = link->ops;
  	bool sleepable = false;

  	bpf_link_free_id(link->id);
  	if (link->prog) {
  		sleepable = link->prog->sleepable;
  		/* detach BPF program, clean up used resources */
-		link->ops->release(link);
+		ops->release(link);
  		bpf_prog_put(link->prog);
  	}
-	if (link->ops->dealloc_deferred) {
+	if (ops->dealloc_deferred) {
  		/* schedule BPF link deallocation; if underlying BPF program
  		 * is sleepable, we need to first wait for RCU tasks trace
  		 * sync, then go through "classic" RCU grace period
@@ -3074,9 +3076,8 @@ static void bpf_link_free(struct bpf_link *link)
  			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
  		else
  			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
-	}
-	if (link->ops->dealloc)
-		link->ops->dealloc(link);
+	} else if (ops->dealloc)
+		ops->dealloc(link);
  }

  static void bpf_link_put_deferred(struct work_struct *work)
Cong Wang June 4, 2024, 3:22 p.m. UTC | #3
On Mon, Jun 03, 2024 at 06:15:55PM +0200, Daniel Borkmann wrote:
> On 6/2/24 9:32 PM, Jiri Olsa wrote:
> > On Sun, Jun 02, 2024 at 11:27:03AM -0700, Cong Wang wrote:
> > > From: Cong Wang <cong.wang@bytedance.com>
> > > 
> > > After commit 1a80dbcb2dba, bpf_link can be freed by
> > > link->ops->dealloc_deferred, but the code still tests and uses
> > > link->ops->dealloc afterward, which leads to a use-after-free as
> > > reported by syzbot. Actually, one of them should be sufficient, so
> > > just call one of them instead of both. Also add a WARN_ON() in case
> > > of any problematic implementation.
> > > 
> > > Reported-by: syzbot+1989ee16d94720836244@syzkaller.appspotmail.com
> > > Fixes: 1a80dbcb2dba ("bpf: support deferring bpf_link dealloc to after RCU grace period")
> > > Cc: Andrii Nakryiko <andrii@kernel.org>
> > > Signed-off-by: Cong Wang <cong.wang@bytedance.com>
> > > ---
> > >   kernel/bpf/syscall.c | 4 ++--
> > >   1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > > index 2222c3ff88e7..d8f244069495 100644
> > > --- a/kernel/bpf/syscall.c
> > > +++ b/kernel/bpf/syscall.c
> > > @@ -2998,6 +2998,7 @@ static int bpf_obj_get(const union bpf_attr *attr)
> > >   void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
> > >   		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
> > >   {
> > > +	WARN_ON(ops->dealloc && ops->dealloc_deferred);
> > >   	atomic64_set(&link->refcnt, 1);
> > >   	link->type = type;
> > >   	link->id = 0;
> > > @@ -3074,8 +3075,7 @@ static void bpf_link_free(struct bpf_link *link)
> > >   			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
> > >   		else
> > >   			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
> > > -	}
> > > -	if (link->ops->dealloc)
> > > +	} else if (link->ops->dealloc)
> > >   		link->ops->dealloc(link);
> > 
> > nice catch
> 
> +1, thanks Cong !
> 
> > Acked-by: Jiri Olsa <jolsa@kernel.org>
> 
> I think it would also be slightly nicer to just fetch the ops once, which
> wouldn't have caused the issue if it was done back then in the first place.
> Do you mind if I squash this in and then apply it to bpf tree? Looks as
> follows :
> 

Sounds good to me.

Thanks.
patchwork-bot+netdevbpf@kernel.org June 4, 2024, 3:50 p.m. UTC | #4
Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Sun,  2 Jun 2024 11:27:03 -0700 you wrote:
> From: Cong Wang <cong.wang@bytedance.com>
> 
> After commit 1a80dbcb2dba, bpf_link can be freed by
> link->ops->dealloc_deferred, but the code still tests and uses
> link->ops->dealloc afterward, which leads to a use-after-free as
> reported by syzbot. Actually, one of them should be sufficient, so
> just call one of them instead of both. Also add a WARN_ON() in case
> of any problematic implementation.
> 
> [...]

Here is the summary with links:
  - [bpf] bpf: fix a potential use-after-free in bpf_link_free()
    https://git.kernel.org/bpf/bpf/c/2884dc7d08d9

You are awesome, thank you!
diff mbox series

Patch

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 2222c3ff88e7..d8f244069495 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2998,6 +2998,7 @@  static int bpf_obj_get(const union bpf_attr *attr)
 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
 		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
 {
+	WARN_ON(ops->dealloc && ops->dealloc_deferred);
 	atomic64_set(&link->refcnt, 1);
 	link->type = type;
 	link->id = 0;
@@ -3074,8 +3075,7 @@  static void bpf_link_free(struct bpf_link *link)
 			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
 		else
 			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
-	}
-	if (link->ops->dealloc)
+	} else if (link->ops->dealloc)
 		link->ops->dealloc(link);
 }