Skip to contentSkip to conversation

mm: mmap: Fix general protection fault in unlink_file_vma()

PATCH linmiaohe 2 participants 2 messages Oct 8, 2020 – Oct 9, 2020 Memory management

lore
Share & tools
lore

linmiaohe

author
#0 lore raw
Quoted 52 lines · Andrew Morton
Quoted 36 lines · On Wed, 16 Sep 2020 05:07:33 -0400 Miaohe Lin <[email protected]> wrote:

The syzbot reported the below general protection fault:

general protection fault, probably for non-canonical address
0xe00eeaee0000003b: 0000 [#1] PREEMPT SMP KASAN
KASAN: maybe wild-memory-access in range 
[0x00777770000001d8-0x00777770000001df]
CPU: 1 PID: 10488 Comm: syz-executor721 Not tainted 
5.9.0-rc3-syzkaller #0
Trace:
 free_pgtables+0x1b3/0x2f0 mm/memory.c:415
 exit_mmap+0x2c0/0x530 mm/mmap.c:3184
 __mmput+0x122/0x470 kernel/fork.c:1076

It's because the ->mmap() callback can change vma->vm_file and fput the original file. But the commit d70cec898324 ("mm: mmap: merge vma after
call_mmap() if possible") failed to catch this case and always fput() the original file, hence add an extra fput().

...

mm/mmap.c +5 -1
@@ -1815,7 +1815,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 			merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags,
 				NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX);
 			if (merge) {
-				fput(file);
+				/* ->mmap() can change vma->vm_file and fput the original file. So
+				 * fput the vma->vm_file here or we would add an extra fput for file
+				 * and cause general protection fault ultimately.
+				 */
+				fput(vma->vm_file);
 				vm_area_free(vma);
 				vma = merge;
 				/* Update vm_flags and possible addr to pick up the change. We 
Quoted 1 line

don't

What about the case where this code block does its `goto unmap_writable'?

	/* Once vma denies write, undo our temporary denial count */
	if (file) {
unmap_writable:
		if (vm_flags & VM_SHARED)
			mapping_unmap_writable(file->f_mapping);
		if (vm_flags & VM_DENYWRITE)
			allow_write_access(file);
	}
	file = vma->vm_file;
Quoted 2 lines

is this using the correct file? I think it is, but please do check.

Many thanks for your reply.

Yes, I think so too. We do deny_write_access and mapping_map_writable on @file, so we should undo all of this on @file. Since @file is unchanged over the second vma_merge() time, we'are using the correct @file to undo our temporary denial count.

But how should I check this explicitly ? I can't find out a way to do this. Could you please figure it out for me? Thanks again.

maintainer
#1 lore raw
Quoted 29 lines · On Thu, 8 Oct 2020 07:17:18 +0000 linmiaohe <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Wed, 16 Sep 2020 05:07:33 -0400 Miaohe Lin <[email protected]> wrote:

The syzbot reported the below general protection fault:

general protection fault, probably for non-canonical address
0xe00eeaee0000003b: 0000 [#1] PREEMPT SMP KASAN
KASAN: maybe wild-memory-access in range 
[0x00777770000001d8-0x00777770000001df]
CPU: 1 PID: 10488 Comm: syz-executor721 Not tainted 
5.9.0-rc3-syzkaller #0
Trace:
 free_pgtables+0x1b3/0x2f0 mm/memory.c:415
 exit_mmap+0x2c0/0x530 mm/mmap.c:3184
 __mmput+0x122/0x470 kernel/fork.c:1076

It's because the ->mmap() callback can change vma->vm_file and fput the original file. But the commit d70cec898324 ("mm: mmap: merge vma after
call_mmap() if possible") failed to catch this case and always fput() the original file, hence add an extra fput().

...

Quoted 3 lines

is this using the correct file? I think it is, but please do check.

Quoted 6 lines

Many thanks for your reply.

Yes, I think so too. We do deny_write_access and mapping_map_writable on @file, so we should undo all of this on @file. Since @file is unchanged over the second vma_merge() time, we'are using the correct @file to undo our temporary denial count.

But how should I check this explicitly ? I can't find out a way to do this. Could you please figure it out for me?

I meant "please check (review) the code as it now is", not "please add a check" ;)