From: Pengfei Xu <pengfei.xu@intel.com>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
<linux-fsdevel@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-mm@kvack.org>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
"Vlastimil Babka" <vbabka@suse.cz>,
Matthew Wilcox <willy@infradead.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Eric Biederman <ebiederm@xmission.com>,
Kees Cook <kees@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
SeongJae Park <sj@kernel.org>, Shuah Khan <shuah@kernel.org>,
Brendan Higgins <brendanhiggins@google.com>,
David Gow <davidgow@google.com>, Rae Moar <rmoar@google.com>,
<syzkaller-bugs@googlegroups.com>
Subject: Re: [PATCH v4 1/7] userfaultfd: move core VMA manipulation logic to mm/userfaultfd.c
Date: Thu, 8 Aug 2024 13:24:32 +0800 [thread overview]
Message-ID: <ZrRWkINGB9jC1nQR@xpf.sh.intel.com> (raw)
In-Reply-To: <3c947ddc-b804-49b7-8fe9-3ea3ca13def5@lucifer.local>
Hi Lorenzo Stoakes,
On 2024-08-07 at 13:03:52 +0100, Lorenzo Stoakes wrote:
> On Wed, Aug 07, 2024 at 11:45:56AM GMT, Pengfei Xu wrote:
> > Hi Lorenzo Stoakes,
> >
> > Greetings!
> >
> > I used syzkaller and found
> > KASAN: slab-use-after-free Read in userfaultfd_set_ctx in next-20240805.
> >
> > Bisected the first bad commit:
> > 4651ba8201cf userfaultfd: move core VMA manipulation logic to mm/userfaultfd.c
> >
> > All detailed info: https://github.com/xupengfe/syzkaller_logs/tree/main/240806_122723_userfaultfd_set_ct
>
> [snip]
>
> Andrew - As this is so small, could you take this as a fix-patch? The fix
> is enclosed below.
>
>
> Pengfei - Sorry for the delay on getting this resolved, I was struggling to
> repro with my usual dev setup, after trying a lot of things I ended up
> using the supplied repro env and was able to do so there.
>
> (I suspect that VMAs are laid out slightly differently in my usual arch base
> image perhaps based on tunables, and this was the delta on that!)
>
> Regardless, I was able to identify the cause - we incorrectly pass a stale
> pointer to userfaultfd_reset_ctx() if a merge is performed in
> userfaultfd_clear_vma().
>
> This was a subtle mistake on my part, I don't see any other instances like
> this in the patch.
>
> Syzkaller managed to get this merge to happen and kasan picked up on it, so
> thank you very much for supplying the infra!
>
> The fix itself is very simple, a one-liner, enclosed below.
>
Thanks for your patch!
I tested the below patch on top of next-20240805, this issue could not
be reproduced. So it's fixed.
Best Regrads,
Thanks!
> ----8<----
> From 193abd1c3a51e6bf1d85ddfe01845e9713336970 Mon Sep 17 00:00:00 2001
> From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Date: Wed, 7 Aug 2024 12:44:27 +0100
> Subject: [PATCH] mm: userfaultfd: fix user-after-free in
> userfaultfd_clear_vma()
>
> After invoking vma_modify_flags_uffd() in userfaultfd_clear_vma(), we may
> have merged the vma, and depending on the kind of merge, deleted the vma,
> rendering the vma pointer invalid.
>
> The code incorrectly referenced this now possibly invalid vma pointer when
> invoking userfaultfd_reset_ctx().
>
> If no merge is possible, vma_modify_flags_uffd() performs a split and
> returns the original vma. Therefore the correct approach is to simply pass
> the ret pointer to userfaultfd_ret_ctx().
>
> Reported-by: Pengfei Xu <pengfei.xu@intel.com>
> Fixes: e310f2b78a77 ("userfaultfd: move core VMA manipulation logic to mm/userfaultfd.c")
> Closes: https://lore.kernel.org/all/ZrLt9HIxV9QiZotn@xpf.sh.intel.com/
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> ---
> mm/userfaultfd.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index 3b7715ecf292..966e6c81a685 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -1813,7 +1813,7 @@ struct vm_area_struct *userfaultfd_clear_vma(struct vma_iterator *vmi,
> * the current one has not been updated yet.
> */
> if (!IS_ERR(ret))
> - userfaultfd_reset_ctx(vma);
> + userfaultfd_reset_ctx(ret);
>
> return ret;
> }
> --
> 2.45.2
next prev parent reply other threads:[~2024-08-08 5:23 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-29 11:50 [PATCH v4 0/7] Make core VMA operations internal and testable Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 1/7] userfaultfd: move core VMA manipulation logic to mm/userfaultfd.c Lorenzo Stoakes
2024-08-07 3:45 ` Pengfei Xu
2024-08-07 8:07 ` Lorenzo Stoakes
2024-08-07 12:03 ` Lorenzo Stoakes
2024-08-08 5:24 ` Pengfei Xu [this message]
2024-08-08 8:10 ` Vlastimil Babka
2024-08-08 13:39 ` Pengfei Xu
2024-07-29 11:50 ` [PATCH v4 2/7] mm: move vma_modify() and helpers to internal header Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 3/7] mm: move vma_shrink(), vma_expand() " Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 4/7] mm: move internal core VMA manipulation functions to own file Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 5/7] MAINTAINERS: Add entry for new VMA files Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 6/7] tools: separate out shared radix-tree components Lorenzo Stoakes
2024-07-29 11:50 ` [PATCH v4 7/7] tools: add skeleton code for userland testing of VMA logic Lorenzo Stoakes
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=ZrRWkINGB9jC1nQR@xpf.sh.intel.com \
--to=pengfei.xu@intel.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=brendanhiggins@google.com \
--cc=davidgow@google.com \
--cc=ebiederm@xmission.com \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=rmoar@google.com \
--cc=shuah@kernel.org \
--cc=sj@kernel.org \
--cc=surenb@google.com \
--cc=syzkaller-bugs@googlegroups.com \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--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;
as well as URLs for NNTP newsgroup(s).