[PATCH 5.15] mm/filemap: fix UAF in find_lock_entries

Liu Shixin posted 1 patch 3 years, 9 months ago
There is a newer version of this series
mm/filemap.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
[PATCH 5.15] mm/filemap: fix UAF in find_lock_entries
Posted by Liu Shixin 3 years, 9 months ago
Release refcount after xas_set to fix UAF which may cause panic like this:

 page:ffffea000491fa40 refcount:1 mapcount:0 mapping:0000000000000000 index:0x1 pfn:0x1247e9
 head:ffffea000491fa00 order:3 compound_mapcount:0 compound_pincount:0
 memcg:ffff888104f91091
 flags: 0x2fffff80010200(slab|head|node=0|zone=2|lastcpupid=0x1fffff)
...
page dumped because: VM_BUG_ON_PAGE(PageTail(page))
 ------------[ cut here ]------------
 kernel BUG at include/linux/page-flags.h:632!
 invalid opcode: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN
 CPU: 1 PID: 7642 Comm: sh Not tainted 5.15.51-dirty #26
...
 Call Trace:
  <TASK>
  __invalidate_mapping_pages+0xe7/0x540
  drop_pagecache_sb+0x159/0x320
  iterate_supers+0x120/0x240
  drop_caches_sysctl_handler+0xaa/0xe0
  proc_sys_call_handler+0x2b4/0x480
  new_sync_write+0x3d6/0x5c0
  vfs_write+0x446/0x7a0
  ksys_write+0x105/0x210
  do_syscall_64+0x35/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xae
 RIP: 0033:0x7f52b5733130
...

This problem has been fixed on mainline by patch 6b24ca4a1a8d ("mm: Use
multi-index entries in the page cache") since it deletes the related code.

Fixes: 5c211ba29deb ("mm: add and use find_lock_entries")
Signed-off-by: Liu Shixin <liushixin2@huawei.com>
---
 mm/filemap.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 00e391e75880..24b5d7ebdc29 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2087,16 +2087,18 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
 {
 	XA_STATE(xas, &mapping->i_pages, start);
 	struct page *page;
+	bool putpage;
 
 	rcu_read_lock();
 	while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
+		putpage = true;
 		if (!xa_is_value(page)) {
 			if (page->index < start)
-				goto put;
+				goto next;
 			if (page->index + thp_nr_pages(page) - 1 > end)
-				goto put;
+				goto next;
 			if (!trylock_page(page))
-				goto put;
+				goto next;
 			if (page->mapping != mapping || PageWriteback(page))
 				goto unlock;
 			VM_BUG_ON_PAGE(!thp_contains(page, xas.xa_index),
@@ -2105,20 +2107,24 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
 		indices[pvec->nr] = xas.xa_index;
 		if (!pagevec_add(pvec, page))
 			break;
+		putpage = false;
 		goto next;
 unlock:
 		unlock_page(page);
-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)
+			if (xas.xa_index < nr_pages) {
+				if (putpage)
+					put_page(page);
 				break;
+			}
 		}
+		if (putpage)
+			put_page(page);
 	}
 	rcu_read_unlock();
 
-- 
2.25.1
Re: [PATCH 5.15] mm/filemap: fix UAF in find_lock_entries
Posted by Matthew Wilcox 3 years, 9 months ago
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);
 			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();
Re: [PATCH 5.15] mm/filemap: fix UAF in find_lock_entries
Posted by Liu Shixin 3 years, 9 months ago

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();
>  
>
> .
>
Re: [PATCH 5.15] mm/filemap: fix UAF in find_lock_entries
Posted by Liu Shixin 3 years, 9 months ago
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?
Thank you for your idea, it looks more concise.
>
> 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);
>  			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();
>  
>
> .
>