Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Uladzislau Rezki <urezki@gmail.com>
To: Barry Song <21cnbao@gmail.com>
Cc: Uladzislau Rezki <urezki@gmail.com>, Dev Jain <dev.jain@arm.com>,
	Matthew Wilcox <willy@infradead.org>,
	kees@kernel.org, akpm@linux-foundation.org,
	gustavoars@kernel.org, linux-hardening@vger.kernel.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	ryan.roberts@arm.com, anshuman.khandual@arm.com,
	david@kernel.org
Subject: Re: [PATCH] mm/usercopy: harden bounds checking for vmalloc allocations
Date: Thu, 30 Jul 2026 16:14:35 +0200	[thread overview]
Message-ID: <amtcS9NiXJj_eIl5@milan> (raw)
In-Reply-To: <CAGsJ_4wGGSf2mhwykH137aA5enhgLGj=-asKe0B09tAOVP8qvg@mail.gmail.com>

On Tue, Jul 28, 2026 at 06:22:57AM +0800, Barry Song wrote:
> On Mon, Jul 27, 2026 at 5:32 PM Uladzislau Rezki <urezki@gmail.com> wrote:
> >
> > On Mon, Jul 27, 2026 at 12:33:49PM +0530, Dev Jain wrote:
> > >
> > >
> > > On 26/07/26 4:16 pm, Matthew Wilcox wrote:
> > > > On Sun, Jul 26, 2026 at 02:23:20PM +0530, Dev Jain wrote:
> > > >> On 22/07/26 7:59 pm, Dev Jain wrote:
> > > >>> The vmalloc allocator stores the actual allocation size inside the
> > > >>> vm_struct structure. We can use this bound in usercopy instead of the
> > > >>> page-aligned va_end to catch usercopy beyond the actual allocation size.
> > > >>>
> > > >>> For vmap, the requested_size field is always page-aligned since it maps a
> > > >>> certain number of pages. Same for vm_map_ram (alongwith, not even having
> > > >>> a vm_struct). So the check is only relevant for vmalloc mappings.
> > > >>>
> > > >>> Because there are early vm areas registered even before vmalloc_init,
> > > >>> requested_size may be zero. So also check whether the requested_size
> > > >>> is set.
> > > >>>
> > > >>> Signed-off-by: Dev Jain <dev.jain@arm.com>
> > > >>> ---
> > > >>
> > > >> Sashiko:
> > > >>
> > > >> 1. "Does this locklessly access area->vm after find_vmap_area() has dropped
> > > >> the busy tree lock?
> > > >>
> > > >> If an out-of-bounds pointer falls into an adjacent vmap_area, and that
> > > >> adjacent area is concurrently freed by another thread, its vm_struct
> > > >> is freed. Additionally, when the vmap_area is moved to the free tree,
> > > >> area->vm (which shares a union with subtree_max_size) is overwritten
> > > >> with an integer size.
> > > >>
> > > >> Would dereferencing vm->flags later in this function cause a use-after-free
> > > >> or a wild pointer dereference?"
> > > >>
> > > >>
> > > >> I don't get it. So usercopy is checking OOB for an object but shouldn't
> > > >> assume the existence of that object while using it?
> > > >>
> > > >> It is a bug in the caller if someone does vfree() while usercopy is operating
> > > >> on the vmalloc object. I don't think usercopy should handle it. For example
> > > >> we don't handle it for slabs currently.
> > > >
> > > > I think the question Sashiko is getting to is how we handle:
> > > >
> > > >     char *p = vmalloc();
> > > >     copy_to_user(p - 4);
> > >
> > > Thanks Willy, I completely misread Sashiko's point.
> > >
> > > I think this is an existing problem? We are using area->va_end currently,
> > > so we can access freed memory.
> > >
> > It is a bit mess here since now we need also take into account a
> > requested_size due to vrealloc. We have requested_size, nr_pages
> > and size in the vm_struct.
> 
> 
> I see that vrealloc() always updates vm->requested_size to the new size.
> So we could always use requested_size? no?
> 
Probably :) But i am not sure what the patch fixes. There is a race anyway?

You check VA on CPU_1 successfully, after that CPU_2 unmaps the pages by
calling vrealloc() and CPU_1 is about to read unmapped pages.

--
Uladzislau Rezki


      reply	other threads:[~2026-07-30 14:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 14:29 [PATCH] mm/usercopy: harden bounds checking for vmalloc allocations Dev Jain
2026-07-23  4:03 ` Kees Cook
2026-07-23  5:57   ` Dev Jain
2026-07-25  5:44 ` Barry Song
2026-07-26  8:53 ` Dev Jain
2026-07-26  8:55   ` Dev Jain
2026-07-26 10:46   ` Matthew Wilcox
2026-07-27  7:03     ` Dev Jain
2026-07-27  9:26       ` Uladzislau Rezki
2026-07-27 22:22         ` Barry Song
2026-07-30 14:14           ` Uladzislau Rezki [this message]

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=amtcS9NiXJj_eIl5@milan \
    --to=urezki@gmail.com \
    --cc=21cnbao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=anshuman.khandual@arm.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryan.roberts@arm.com \
    --cc=willy@infradead.org \
    /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