Skip to content

Commit c462ac2

Browse files
committed
mm: Introduce arch_validate_flags()
Similarly to arch_validate_prot() called from do_mprotect_pkey(), an architecture may need to sanity-check the new vm_flags. Define a dummy function always returning true. In addition to do_mprotect_pkey(), also invoke it from mmap_region() prior to updating vma->vm_page_prot to allow the architecture code to veto potentially inconsistent vm_flags. Signed-off-by: Catalin Marinas <[email protected]> Acked-by: Andrew Morton <[email protected]>
1 parent 9f34193 commit c462ac2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

include/linux/mman.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
108108
#define arch_validate_prot arch_validate_prot
109109
#endif
110110

111+
#ifndef arch_validate_flags
112+
/*
113+
* This is called from mmap() and mprotect() with the updated vma->vm_flags.
114+
*
115+
* Returns true if the VM_* flags are valid.
116+
*/
117+
static inline bool arch_validate_flags(unsigned long flags)
118+
{
119+
return true;
120+
}
121+
#define arch_validate_flags arch_validate_flags
122+
#endif
123+
111124
/*
112125
* Optimisation macro. It is equivalent to:
113126
* (x & bit1) ? bit2 : 0

mm/mmap.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
18121812
vma_set_anonymous(vma);
18131813
}
18141814

1815+
/* Allow architectures to sanity-check the vm_flags */
1816+
if (!arch_validate_flags(vma->vm_flags)) {
1817+
error = -EINVAL;
1818+
if (file)
1819+
goto unmap_and_free_vma;
1820+
else
1821+
goto free_vma;
1822+
}
1823+
18151824
vma_link(mm, vma, prev, rb_link, rb_parent);
18161825
/* Once vma denies write, undo our temporary denial count */
18171826
if (file) {

mm/mprotect.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
603603
goto out;
604604
}
605605

606+
/* Allow architectures to sanity-check the new flags */
607+
if (!arch_validate_flags(newflags)) {
608+
error = -EINVAL;
609+
goto out;
610+
}
611+
606612
error = security_file_mprotect(vma, reqprot, prot);
607613
if (error)
608614
goto out;

0 commit comments

Comments
 (0)