Skip to content

Commit dcfa24b

Browse files
author
Matthew Wilcox (Oracle)
committed
filemap: Cache the value of vm_flags
After we have unlocked the mmap_lock for I/O, the file is pinned, but the VMA is not. Checking this flag after that can be a use-after-free. It's not a terribly interesting use-after-free as it can only read one bit, and it's used to decide whether to read 2MB or 4MB. But it upsets the automated tools and it's generally bad practice anyway, so let's fix it. Reported-by: [email protected] Fixes: 4687fdb ("mm/filemap: Support VM_HUGEPAGE for file mappings") Cc: [email protected] Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>
1 parent 6bf74cd commit dcfa24b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

mm/filemap.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,19 +2991,20 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
29912991
struct address_space *mapping = file->f_mapping;
29922992
DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
29932993
struct file *fpin = NULL;
2994+
unsigned long vm_flags = vmf->vma->vm_flags;
29942995
unsigned int mmap_miss;
29952996

29962997
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
29972998
/* Use the readahead code, even if readahead is disabled */
2998-
if (vmf->vma->vm_flags & VM_HUGEPAGE) {
2999+
if (vm_flags & VM_HUGEPAGE) {
29993000
fpin = maybe_unlock_mmap_for_io(vmf, fpin);
30003001
ractl._index &= ~((unsigned long)HPAGE_PMD_NR - 1);
30013002
ra->size = HPAGE_PMD_NR;
30023003
/*
30033004
* Fetch two PMD folios, so we get the chance to actually
30043005
* readahead, unless we've been told not to.
30053006
*/
3006-
if (!(vmf->vma->vm_flags & VM_RAND_READ))
3007+
if (!(vm_flags & VM_RAND_READ))
30073008
ra->size *= 2;
30083009
ra->async_size = HPAGE_PMD_NR;
30093010
page_cache_ra_order(&ractl, ra, HPAGE_PMD_ORDER);
@@ -3012,12 +3013,12 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
30123013
#endif
30133014

30143015
/* If we don't want any read-ahead, don't bother */
3015-
if (vmf->vma->vm_flags & VM_RAND_READ)
3016+
if (vm_flags & VM_RAND_READ)
30163017
return fpin;
30173018
if (!ra->ra_pages)
30183019
return fpin;
30193020

3020-
if (vmf->vma->vm_flags & VM_SEQ_READ) {
3021+
if (vm_flags & VM_SEQ_READ) {
30213022
fpin = maybe_unlock_mmap_for_io(vmf, fpin);
30223023
page_cache_sync_ra(&ractl, ra->ra_pages);
30233024
return fpin;

0 commit comments

Comments
 (0)