From: Liu Shixin <liushixin2@huawei.com>
To: Matthew Wilcox <willy@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andrew Morton <akpm@linux-foundation.org>,
Jan Kara <jack@suse.cz>,
William Kucharski <william.kucharski@oracle.com>,
Christoph Hellwig <hch@lst.de>, <linux-kernel@vger.kernel.org>,
<stable@vger.kernel.org>
Subject: Re: [PATCH 5.15] mm/filemap: fix UAF in find_lock_entries
Date: Wed, 6 Jul 2022 14:47:52 +0800 [thread overview]
Message-ID: <9aa8aa19-b3ba-ab7e-abcb-78b8a65577ce@huawei.com> (raw)
In-Reply-To: <YsT/qAPruIimH3+R@casper.infradead.org>
On 2022/7/6 11:21, Matthew Wilcox wrote:
> On Wed, Jul 06, 2022 at 11:24:34AM +0800, Liu Shixin wrote:
>> Release refcount after xas_set to fix UAF which may cause panic like this:
> I think we can do better. How about this?
>
> diff --git a/mm/filemap.c b/mm/filemap.c
> index 00e391e75880..11ae38cc4fd3 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -2090,7 +2090,9 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
>
> rcu_read_lock();
> while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
> + unsigned long next_idx = xas.xa_index + 1;
> if (!xa_is_value(page)) {
> + next_idx = page->index + thp_nr_pages(page);
I noticed that there was a VM_BUG_ON_PAGE before which was deleted by patch 6560e8cd869b ("mm/filemap.c: remove bogus VM_BUG_ON")
It seems that page->index and xas.xa_index are not guaranteed to be equal. Therefore, I conservatively retained
the PageTransHuge to keep consistent with the original logic :)
@@ -2090,7 +2090,11 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
rcu_read_lock();
while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
+ unsigned long next_idx = xas.xa_index;
+
if (!xa_is_value(page)) {
+ if (PageTransHuge(page))
+ next_idx = page->index + thp_nr_pages(page);
if (page->index < start)
goto put;
if (page->index + thp_nr_pages(page) - 1 > end)
> if (page->index < start)
> goto put;
> if (page->index + thp_nr_pages(page) - 1 > end)
> @@ -2111,14 +2113,11 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
> put:
> put_page(page);
> next:
> - if (!xa_is_value(page) && PageTransHuge(page)) {
> - unsigned int nr_pages = thp_nr_pages(page);
> -
> - /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */
> - xas_set(&xas, page->index + nr_pages);
> - if (xas.xa_index < nr_pages)
> - break;
> - }
> + /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */
> + if (next_idx < xas.xa_index)
> + break;
> + if (next_idx != xas.xa_index + 1)
> + xas_set(&xas, next_idx);
> }
> rcu_read_unlock();
>
>
> .
>
prev parent reply other threads:[~2022-07-06 6:47 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-06 3:24 Liu Shixin
2022-07-06 3:21 ` Matthew Wilcox
2022-07-06 6:13 ` Liu Shixin
2022-07-06 6:47 ` Liu Shixin [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=9aa8aa19-b3ba-ab7e-abcb-78b8a65577ce@huawei.com \
--to=liushixin2@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=gregkh@linuxfoundation.org \
--cc=hch@lst.de \
--cc=jack@suse.cz \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=william.kucharski@oracle.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
all inboxes | Powered by JetHome®