Skip to content

Commit 066a5b6

Browse files
Liu Shixingregkh
Liu Shixin
authored andcommitted
mm/filemap: fix UAF in find_lock_entries
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 6b24ca4 ("mm: Use multi-index entries in the page cache") since it deletes the related code. Fixes: 5c211ba ("mm: add and use find_lock_entries") Signed-off-by: Liu Shixin <[email protected]> Acked-by: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0515cc9 commit 066a5b6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

mm/filemap.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,11 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
20902090

20912091
rcu_read_lock();
20922092
while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
2093+
unsigned long next_idx = xas.xa_index + 1;
2094+
20932095
if (!xa_is_value(page)) {
2096+
if (PageTransHuge(page))
2097+
next_idx = page->index + thp_nr_pages(page);
20942098
if (page->index < start)
20952099
goto put;
20962100
if (page->index + thp_nr_pages(page) - 1 > end)
@@ -2111,13 +2115,11 @@ unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
21112115
put:
21122116
put_page(page);
21132117
next:
2114-
if (!xa_is_value(page) && PageTransHuge(page)) {
2115-
unsigned int nr_pages = thp_nr_pages(page);
2116-
2118+
if (next_idx != xas.xa_index + 1) {
21172119
/* Final THP may cross MAX_LFS_FILESIZE on 32-bit */
2118-
xas_set(&xas, page->index + nr_pages);
2119-
if (xas.xa_index < nr_pages)
2120+
if (next_idx < xas.xa_index)
21202121
break;
2122+
xas_set(&xas, next_idx);
21212123
}
21222124
}
21232125
rcu_read_unlock();

0 commit comments

Comments
 (0)