Skip to content

Commit cb1a393

Browse files
ardbiesheuvelctmarinas
authored andcommitted
mm: add arch hook to validate mmap() prot flags
Add a hook to permit architectures to perform validation on the prot flags passed to mmap(), like arch_validate_prot() does for mprotect(). This will be used by arm64 to reject PROT_WRITE+PROT_EXEC mappings on configurations that run with WXN enabled. Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 5d10165 commit cb1a393

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/linux/mman.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ static inline bool arch_validate_flags(unsigned long flags)
124124
#define arch_validate_flags arch_validate_flags
125125
#endif
126126

127+
#ifndef arch_validate_mmap_prot
128+
/*
129+
* This is called from mmap(), which ignores unknown prot bits so the default
130+
* is to accept anything.
131+
*
132+
* Returns true if the prot flags are valid
133+
*/
134+
static inline bool arch_validate_mmap_prot(unsigned long prot,
135+
unsigned long addr)
136+
{
137+
return true;
138+
}
139+
#define arch_validate_mmap_prot arch_validate_mmap_prot
140+
#endif
141+
127142
/*
128143
* Optimisation macro. It is equivalent to:
129144
* (x & bit1) ? bit2 : 0

mm/mmap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
12291229
if (!(file && path_noexec(&file->f_path)))
12301230
prot |= PROT_EXEC;
12311231

1232+
if (!arch_validate_mmap_prot(prot, addr))
1233+
return -EACCES;
1234+
12321235
/* force arch specific MAP_FIXED handling in get_unmapped_area */
12331236
if (flags & MAP_FIXED_NOREPLACE)
12341237
flags |= MAP_FIXED;

0 commit comments

Comments
 (0)