Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/vma: do not leak memory when .mmap_prepare swaps the file

The current implementation of mmap() is set up such that a struct file
object is obtained for the input fd in ksys_mmap_pgoff() via fget(), and
its reference count decremented at the end of the function via. fput().

If a merge can be achieved, we are fine to simply decrement the refcount
on the file. Otherwise, in __mmap_new_file_vma(), we increment the
reference count on the file via get_file() such that the fput() in
ksys_mmap_pgoff() does not free the now-referenced file object.

The introduction of the f_op->mmap_prepare hook changes things, as it
becomes possible for a driver to replace the file object right at the
beginning of the mmap operation.

The current implementation is buggy if this happens because it
unconditionally calls get_file() on the mapping's file whether or not it
was replaced (and thus whether or not its reference count will be
decremented at the end of ksys_mmap_pgoff()).

This results in a memory leak, and was exposed in commit ab04945f91bc
("mm: update mem char driver to use mmap_prepare").

This patch solves the problem by explicitly tracking whether we actually
need to call get_file() on the file or not, and only doing so if required.

Link: https://lkml.kernel.org/r/20260112155143.661284-1-lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Fixes: ab04945f91bc ("mm: update mem char driver to use mmap_prepare")
Reported-by: syzbot+bf5de69ebb4bdf86f59f@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6964a92b.050a0220.eaf7.008a.GAE@google.com/
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
605f6586 b7880cb1

+9 -2
+9 -2
mm/vma.c
··· 37 37 bool check_ksm_early :1; 38 38 /* If we map new, hold the file rmap lock on mapping. */ 39 39 bool hold_file_rmap_lock :1; 40 + /* If .mmap_prepare changed the file, we don't need to pin. */ 41 + bool file_doesnt_need_get :1; 40 42 }; 41 43 42 44 #define MMAP_STATE(name, mm_, vmi_, addr_, len_, pgoff_, vm_flags_, file_) \ ··· 2452 2450 struct vma_iterator *vmi = map->vmi; 2453 2451 int error; 2454 2452 2455 - vma->vm_file = get_file(map->file); 2453 + vma->vm_file = map->file; 2454 + if (!map->file_doesnt_need_get) 2455 + get_file(map->file); 2456 2456 2457 2457 if (!map->file->f_op->mmap) 2458 2458 return 0; ··· 2642 2638 2643 2639 /* Update fields permitted to be changed. */ 2644 2640 map->pgoff = desc->pgoff; 2645 - map->file = desc->vm_file; 2641 + if (desc->vm_file != map->file) { 2642 + map->file_doesnt_need_get = true; 2643 + map->file = desc->vm_file; 2644 + } 2646 2645 map->vm_flags = desc->vm_flags; 2647 2646 map->page_prot = desc->page_prot; 2648 2647 /* User-defined fields. */