BPF List
 help / color / mirror / Atom feed
* [PATCH v2 bpf] bpf: Fix remap of arena.
@ 2024-06-17 17:18 Alexei Starovoitov
  2024-06-17 17:52 ` Barret Rhoden
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2024-06-17 17:18 UTC (permalink / raw)
  To: bpf
  Cc: daniel, andrii, martin.lau, memxor, eddyz87, pengfei.xu, brho,
	kernel-team

From: Alexei Starovoitov <ast@kernel.org>

The bpf arena logic didn't account for mremap operation. Add a refcnt for
multiple mmap events to prevent use-after-free in arena_vm_close.

Reported-by: Pengfei Xu <pengfei.xu@intel.com>
Closes: https://lore.kernel.org/bpf/Zmuw29IhgyPNKnIM@xpf.sh.intel.com/
Fixes: 317460317a02 ("bpf: Introduce bpf_arena.")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/arena.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 583ee4fe48ef..fe35b45cf6f8 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -212,6 +212,7 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
 struct vma_list {
 	struct vm_area_struct *vma;
 	struct list_head head;
+	atomic_t mmap_count;
 };
 
 static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
@@ -221,20 +222,32 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
 	vml = kmalloc(sizeof(*vml), GFP_KERNEL);
 	if (!vml)
 		return -ENOMEM;
+	atomic_set(&vml->mmap_count, 1);
 	vma->vm_private_data = vml;
 	vml->vma = vma;
 	list_add(&vml->head, &arena->vma_list);
 	return 0;
 }
 
+static void arena_vm_open(struct vm_area_struct *vma)
+{
+	struct bpf_map *map = vma->vm_file->private_data;
+	struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
+	struct vma_list *vml = vma->vm_private_data;
+
+	atomic_inc(&vml->mmap_count);
+}
+
 static void arena_vm_close(struct vm_area_struct *vma)
 {
 	struct bpf_map *map = vma->vm_file->private_data;
 	struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
-	struct vma_list *vml;
+	struct vma_list *vml = vma->vm_private_data;
 
+	if (!atomic_dec_and_test(&vml->mmap_count))
+		return;
 	guard(mutex)(&arena->lock);
-	vml = vma->vm_private_data;
+	/* update link list under lock */
 	list_del(&vml->head);
 	vma->vm_private_data = NULL;
 	kfree(vml);
@@ -287,6 +300,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
 }
 
 static const struct vm_operations_struct arena_vm_ops = {
+	.open		= arena_vm_open,
 	.close		= arena_vm_close,
 	.fault          = arena_vm_fault,
 };
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-17 17:18 [PATCH v2 bpf] bpf: Fix remap of arena Alexei Starovoitov
@ 2024-06-17 17:52 ` Barret Rhoden
  2024-06-18  2:02 ` Pengfei Xu
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Barret Rhoden @ 2024-06-17 17:52 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, daniel, andrii, martin.lau, memxor, eddyz87, pengfei.xu,
	kernel-team

On 6/17/24 13:18, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> The bpf arena logic didn't account for mremap operation. Add a refcnt for
> multiple mmap events to prevent use-after-free in arena_vm_close.
> 
> Reported-by: Pengfei Xu <pengfei.xu@intel.com>
> Closes: https://lore.kernel.org/bpf/Zmuw29IhgyPNKnIM@xpf.sh.intel.com/
> Fixes: 317460317a02 ("bpf: Introduce bpf_arena.")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Reviewed-by: Barret Rhoden <brho@google.com>




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-17 17:18 [PATCH v2 bpf] bpf: Fix remap of arena Alexei Starovoitov
  2024-06-17 17:52 ` Barret Rhoden
@ 2024-06-18  2:02 ` Pengfei Xu
  2024-06-18  4:43 ` kernel test robot
  2024-06-18 15:30 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 7+ messages in thread
From: Pengfei Xu @ 2024-06-18  2:02 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, daniel, andrii, martin.lau, memxor, eddyz87, brho,
	kernel-team


The issue cannot be reproduced in the new patch below.

Best Regards,
Thanks!

On 2024-06-17 at 10:18:12 -0700, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> The bpf arena logic didn't account for mremap operation. Add a refcnt for
> multiple mmap events to prevent use-after-free in arena_vm_close.
> 
> Reported-by: Pengfei Xu <pengfei.xu@intel.com>
> Closes: https://lore.kernel.org/bpf/Zmuw29IhgyPNKnIM@xpf.sh.intel.com/
> Fixes: 317460317a02 ("bpf: Introduce bpf_arena.")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
>  kernel/bpf/arena.c | 18 ++++++++++++++++--
>  1 file changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> index 583ee4fe48ef..fe35b45cf6f8 100644
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
> @@ -212,6 +212,7 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
>  struct vma_list {
>  	struct vm_area_struct *vma;
>  	struct list_head head;
> +	atomic_t mmap_count;
>  };
>  
>  static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
> @@ -221,20 +222,32 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
>  	vml = kmalloc(sizeof(*vml), GFP_KERNEL);
>  	if (!vml)
>  		return -ENOMEM;
> +	atomic_set(&vml->mmap_count, 1);
>  	vma->vm_private_data = vml;
>  	vml->vma = vma;
>  	list_add(&vml->head, &arena->vma_list);
>  	return 0;
>  }
>  
> +static void arena_vm_open(struct vm_area_struct *vma)
> +{
> +	struct bpf_map *map = vma->vm_file->private_data;
> +	struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
> +	struct vma_list *vml = vma->vm_private_data;
> +
> +	atomic_inc(&vml->mmap_count);
> +}
> +
>  static void arena_vm_close(struct vm_area_struct *vma)
>  {
>  	struct bpf_map *map = vma->vm_file->private_data;
>  	struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
> -	struct vma_list *vml;
> +	struct vma_list *vml = vma->vm_private_data;
>  
> +	if (!atomic_dec_and_test(&vml->mmap_count))
> +		return;
>  	guard(mutex)(&arena->lock);
> -	vml = vma->vm_private_data;
> +	/* update link list under lock */
>  	list_del(&vml->head);
>  	vma->vm_private_data = NULL;
>  	kfree(vml);
> @@ -287,6 +300,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
>  }
>  
>  static const struct vm_operations_struct arena_vm_ops = {
> +	.open		= arena_vm_open,
>  	.close		= arena_vm_close,
>  	.fault          = arena_vm_fault,
>  };
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-17 17:18 [PATCH v2 bpf] bpf: Fix remap of arena Alexei Starovoitov
  2024-06-17 17:52 ` Barret Rhoden
  2024-06-18  2:02 ` Pengfei Xu
@ 2024-06-18  4:43 ` kernel test robot
  2024-06-18 14:49   ` Alexei Starovoitov
  2024-06-18 15:30 ` patchwork-bot+netdevbpf
  3 siblings, 1 reply; 7+ messages in thread
From: kernel test robot @ 2024-06-18  4:43 UTC (permalink / raw)
  To: Alexei Starovoitov, bpf
  Cc: oe-kbuild-all, daniel, andrii, martin.lau, memxor, eddyz87,
	pengfei.xu, brho, kernel-team

Hi Alexei,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexei-Starovoitov/bpf-Fix-remap-of-arena/20240618-012054
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
patch link:    https://lore.kernel.org/r/20240617171812.76634-1-alexei.starovoitov%40gmail.com
patch subject: [PATCH v2 bpf] bpf: Fix remap of arena.
config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/config)
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202406181248.u80sRLXy-lkp@intel.com/

All warnings (new ones prefixed by >>):

   kernel/bpf/arena.c: In function 'arena_vm_open':
>> kernel/bpf/arena.c:235:27: warning: unused variable 'arena' [-Wunused-variable]
     235 |         struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
         |                           ^~~~~


vim +/arena +235 kernel/bpf/arena.c

   231	
   232	static void arena_vm_open(struct vm_area_struct *vma)
   233	{
   234		struct bpf_map *map = vma->vm_file->private_data;
 > 235		struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
   236		struct vma_list *vml = vma->vm_private_data;
   237	
   238		atomic_inc(&vml->mmap_count);
   239	}
   240	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-18  4:43 ` kernel test robot
@ 2024-06-18 14:49   ` Alexei Starovoitov
  2024-06-18 15:28     ` Daniel Borkmann
  0 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2024-06-18 14:49 UTC (permalink / raw)
  To: kernel test robot
  Cc: bpf, oe-kbuild-all, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Kumar Kartikeya Dwivedi, Eddy Z, Pengfei Xu,
	Barret Rhoden, Kernel Team

On Mon, Jun 17, 2024 at 9:43 PM kernel test robot <lkp@intel.com> wrote:
>
> Hi Alexei,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on bpf/master]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Alexei-Starovoitov/bpf-Fix-remap-of-arena/20240618-012054
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
> patch link:    https://lore.kernel.org/r/20240617171812.76634-1-alexei.starovoitov%40gmail.com
> patch subject: [PATCH v2 bpf] bpf: Fix remap of arena.
> config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/config)
> compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202406181248.u80sRLXy-lkp@intel.com/
>
> All warnings (new ones prefixed by >>):
>
>    kernel/bpf/arena.c: In function 'arena_vm_open':
> >> kernel/bpf/arena.c:235:27: warning: unused variable 'arena' [-Wunused-variable]
>      235 |         struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
>          |                           ^~~~~

Daniel or Andrii,
could you please remove this line while applying?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-18 14:49   ` Alexei Starovoitov
@ 2024-06-18 15:28     ` Daniel Borkmann
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Borkmann @ 2024-06-18 15:28 UTC (permalink / raw)
  To: Alexei Starovoitov, kernel test robot
  Cc: bpf, oe-kbuild-all, Andrii Nakryiko, Martin KaFai Lau,
	Kumar Kartikeya Dwivedi, Eddy Z, Pengfei Xu, Barret Rhoden,
	Kernel Team

On 6/18/24 4:49 PM, Alexei Starovoitov wrote:
> On Mon, Jun 17, 2024 at 9:43 PM kernel test robot <lkp@intel.com> wrote:
>>
>> Hi Alexei,
>>
>> kernel test robot noticed the following build warnings:
>>
>> [auto build test WARNING on bpf/master]
>>
>> url:    https://github.com/intel-lab-lkp/linux/commits/Alexei-Starovoitov/bpf-Fix-remap-of-arena/20240618-012054
>> base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
>> patch link:    https://lore.kernel.org/r/20240617171812.76634-1-alexei.starovoitov%40gmail.com
>> patch subject: [PATCH v2 bpf] bpf: Fix remap of arena.
>> config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/config)
>> compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
>> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240618/202406181248.u80sRLXy-lkp@intel.com/reproduce)
>>
>> If you fix the issue in a separate patch/commit (i.e. not just a new version of
>> the same patch/commit), kindly add following tags
>> | Reported-by: kernel test robot <lkp@intel.com>
>> | Closes: https://lore.kernel.org/oe-kbuild-all/202406181248.u80sRLXy-lkp@intel.com/
>>
>> All warnings (new ones prefixed by >>):
>>
>>     kernel/bpf/arena.c: In function 'arena_vm_open':
>>>> kernel/bpf/arena.c:235:27: warning: unused variable 'arena' [-Wunused-variable]
>>       235 |         struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
>>           |                           ^~~~~
> 
> Daniel or Andrii,
> could you please remove this line while applying?

Ok, done, both lines actually.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 bpf] bpf: Fix remap of arena.
  2024-06-17 17:18 [PATCH v2 bpf] bpf: Fix remap of arena Alexei Starovoitov
                   ` (2 preceding siblings ...)
  2024-06-18  4:43 ` kernel test robot
@ 2024-06-18 15:30 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-06-18 15:30 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, daniel, andrii, martin.lau, memxor, eddyz87, pengfei.xu,
	brho, kernel-team

Hello:

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

On Mon, 17 Jun 2024 10:18:12 -0700 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> The bpf arena logic didn't account for mremap operation. Add a refcnt for
> multiple mmap events to prevent use-after-free in arena_vm_close.
> 
> Reported-by: Pengfei Xu <pengfei.xu@intel.com>
> Closes: https://lore.kernel.org/bpf/Zmuw29IhgyPNKnIM@xpf.sh.intel.com/
> Fixes: 317460317a02 ("bpf: Introduce bpf_arena.")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> 
> [...]

Here is the summary with links:
  - [v2,bpf] bpf: Fix remap of arena.
    https://git.kernel.org/bpf/bpf/c/b90d77e5fd78

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-06-18 15:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17 17:18 [PATCH v2 bpf] bpf: Fix remap of arena Alexei Starovoitov
2024-06-17 17:52 ` Barret Rhoden
2024-06-18  2:02 ` Pengfei Xu
2024-06-18  4:43 ` kernel test robot
2024-06-18 14:49   ` Alexei Starovoitov
2024-06-18 15:28     ` Daniel Borkmann
2024-06-18 15:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox