BPF List
 help / color / mirror / Atom feed
From: Barret Rhoden <brho@google.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com,
	pengfei.xu@intel.com, kernel-team@fb.com
Subject: Re: [PATCH bpf] bpf: Fix remap of arena.
Date: Mon, 17 Jun 2024 11:34:07 -0400	[thread overview]
Message-ID: <90d6740a-6b7e-474d-a218-50f4e0de343c@google.com> (raw)
In-Reply-To: <20240615181935.76049-1-alexei.starovoitov@gmail.com>

On 6/15/24 14:19, 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 | 13 +++++++++++++
>   1 file changed, 13 insertions(+)
> 
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> index 583ee4fe48ef..f31fcaf7ee8e 100644
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
> @@ -48,6 +48,7 @@ struct bpf_arena {
>   	struct maple_tree mt;
>   	struct list_head vma_list;
>   	struct mutex lock;
> +	atomic_t mmap_count;
>   };
>   
>   u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena)
> @@ -227,12 +228,22 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
>   	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);
> +
> +	atomic_inc(&arena->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;
>   
> +	if (!atomic_dec_and_test(&arena->mmap_count))
> +		return;
>   	guard(mutex)(&arena->lock);
>   	vml = vma->vm_private_data;
>   	list_del(&vml->head);
> @@ -287,6 +298,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,
>   };
> @@ -361,6 +373,7 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
>   	 */
>   	vm_flags_set(vma, VM_DONTEXPAND);
>   	vma->vm_ops = &arena_vm_ops;
> +	atomic_set(&arena->mmap_count, 1);

i'm not sure, but i have the feeling that this refcnt should be on the 
struct vma_list or something.

what happens if two different processes mmap the same arena?  will the 
second one come in and set the mmap_count = 1, clobbering whatever the 
first process had already done?

what are the rules for a vma's vm_ops?  something like: "there will be a 
close() for the initial mmap and for every open()"?

if that's what it's doing, then this initial refcnt = 1 corresponds to 
the remember_vma() call.  in which case, vm_ops->open ought to lookup 
the remembered vma (struct vma_list) and do the incref there.

barret


  parent reply	other threads:[~2024-06-17 15:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-15 18:19 [PATCH bpf] bpf: Fix remap of arena Alexei Starovoitov
2024-06-16  3:07 ` Pengfei Xu
2024-06-17 15:34 ` Barret Rhoden [this message]
2024-06-17 16:17   ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=90d6740a-6b7e-474d-a218-50f4e0de343c@google.com \
    --to=brho@google.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=memxor@gmail.com \
    --cc=pengfei.xu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox