Skip to content

Commit 2b12d06

Browse files
Pu Lehuiakpm00
authored andcommitted
mm: fix uprobe pte be overwritten when expanding vma
Patch series "Fix uprobe pte be overwritten when expanding vma". This patch (of 4): We encountered a BUG alert triggered by Syzkaller as follows: BUG: Bad rss-counter state mm:00000000b4a60fca type:MM_ANONPAGES val:1 And we can reproduce it with the following steps: 1. register uprobe on file at zero offset 2. mmap the file at zero offset: addr1 = mmap(NULL, 2 * 4096, PROT_NONE, MAP_PRIVATE, fd, 0); 3. mremap part of vma1 to new vma2: addr2 = mremap(addr1, 4096, 2 * 4096, MREMAP_MAYMOVE); 4. mremap back to orig addr1: mremap(addr2, 4096, 4096, MREMAP_MAYMOVE | MREMAP_FIXED, addr1); In step 3, the vma1 range [addr1, addr1 + 4096] will be remap to new vma2 with range [addr2, addr2 + 8192], and remap uprobe anon page from the vma1 to vma2, then unmap the vma1 range [addr1, addr1 + 4096]. In step 4, the vma2 range [addr2, addr2 + 4096] will be remap back to the addr range [addr1, addr1 + 4096]. Since the addr range [addr1 + 4096, addr1 + 8192] still maps the file, it will take vma_merge_new_range to expand the range, and then do uprobe_mmap in vma_complete. Since the merged vma pgoff is also zero offset, it will install uprobe anon page to the merged vma. However, the upcomming move_page_tables step, which use set_pte_at to remap the vma2 uprobe pte to the merged vma, will overwrite the newly uprobe pte in the merged vma, and lead that pte to be orphan. Since the uprobe pte will be remapped to the merged vma, we can remove the unnecessary uprobe_mmap upon merged vma. This problem was first found in linux-6.6.y and also exists in the community syzkaller: https://lore.kernel.org/all/000000000000ada39605a5e71711@google.com/T/ Link: https://lkml.kernel.org/r/20250529155650.4017699-1-pulehui@huaweicloud.com Link: https://lkml.kernel.org/r/20250529155650.4017699-2-pulehui@huaweicloud.com Fixes: 2b14449 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints") Signed-off-by: Pu Lehui <pulehui@huawei.com> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ea68ea9 commit 2b12d06

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

mm/vma.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ static void init_multi_vma_prep(struct vma_prepare *vp,
169169
vp->file = vma->vm_file;
170170
if (vp->file)
171171
vp->mapping = vma->vm_file->f_mapping;
172+
173+
if (vmg && vmg->skip_vma_uprobe)
174+
vp->skip_vma_uprobe = true;
172175
}
173176

174177
/*
@@ -358,10 +361,13 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,
358361

359362
if (vp->file) {
360363
i_mmap_unlock_write(vp->mapping);
361-
uprobe_mmap(vp->vma);
362364

363-
if (vp->adj_next)
364-
uprobe_mmap(vp->adj_next);
365+
if (!vp->skip_vma_uprobe) {
366+
uprobe_mmap(vp->vma);
367+
368+
if (vp->adj_next)
369+
uprobe_mmap(vp->adj_next);
370+
}
365371
}
366372

367373
if (vp->remove) {
@@ -1823,6 +1829,14 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
18231829
faulted_in_anon_vma = false;
18241830
}
18251831

1832+
/*
1833+
* If the VMA we are copying might contain a uprobe PTE, ensure
1834+
* that we do not establish one upon merge. Otherwise, when mremap()
1835+
* moves page tables, it will orphan the newly created PTE.
1836+
*/
1837+
if (vma->vm_file)
1838+
vmg.skip_vma_uprobe = true;
1839+
18261840
new_vma = find_vma_prev(mm, addr, &vmg.prev);
18271841
if (new_vma && new_vma->vm_start < addr + len)
18281842
return NULL; /* should never get here */

mm/vma.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct vma_prepare {
1919
struct vm_area_struct *insert;
2020
struct vm_area_struct *remove;
2121
struct vm_area_struct *remove2;
22+
23+
bool skip_vma_uprobe :1;
2224
};
2325

2426
struct unlink_vma_file_batch {
@@ -120,6 +122,11 @@ struct vma_merge_struct {
120122
*/
121123
bool give_up_on_oom :1;
122124

125+
/*
126+
* If set, skip uprobe_mmap upon merged vma.
127+
*/
128+
bool skip_vma_uprobe :1;
129+
123130
/* Internal flags set during merge process: */
124131

125132
/*

0 commit comments

Comments
 (0)