Skip to content

Commit 8a0fe56

Browse files
rpedgecoakpm00
authored andcommitted
mm: use get_unmapped_area_vmflags()
When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn't placed in an any existing mappings guard gaps. 2. That the new mapping isn't placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Use mm_get_unmapped_area_vmflags() in the do_mmap() so future changes can cause shadow stack mappings to be placed with a guard gap. Also use the THP variant that takes vm_flags, such that THP shadow stack can get the same treatment. Adjust the vm_flags calculation to happen earlier so that the vm_flags can be passed into __get_unmapped_area(). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Rick Edgecombe <[email protected]> Reviewed-by: Christophe Leroy <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: Borislav Petkov (AMD) <[email protected]> Cc: Dan Williams <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Deepak Gupta <[email protected]> Cc: Guo Ren <[email protected]> Cc: Helge Deller <[email protected]> Cc: H. Peter Anvin (Intel) <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: "James E.J. Bottomley" <[email protected]> Cc: Kees Cook <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Mark Brown <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Naveen N. Rao <[email protected]> Cc: Nicholas Piggin <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 529781b commit 8a0fe56

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

include/linux/mm.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3413,7 +3413,16 @@ extern int install_special_mapping(struct mm_struct *mm,
34133413
unsigned long randomize_stack_top(unsigned long stack_top);
34143414
unsigned long randomize_page(unsigned long start, unsigned long range);
34153415

3416-
extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
3416+
unsigned long
3417+
__get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
3418+
unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags);
3419+
3420+
static inline unsigned long
3421+
get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
3422+
unsigned long pgoff, unsigned long flags)
3423+
{
3424+
return __get_unmapped_area(file, addr, len, pgoff, flags, 0);
3425+
}
34173426

34183427
extern unsigned long mmap_region(struct file *file, unsigned long addr,
34193428
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,

mm/mmap.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,18 +1255,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
12551255
if (mm->map_count > sysctl_max_map_count)
12561256
return -ENOMEM;
12571257

1258-
/* Obtain the address to map to. we verify (or select) it and ensure
1259-
* that it represents a valid section of the address space.
1260-
*/
1261-
addr = get_unmapped_area(file, addr, len, pgoff, flags);
1262-
if (IS_ERR_VALUE(addr))
1263-
return addr;
1264-
1265-
if (flags & MAP_FIXED_NOREPLACE) {
1266-
if (find_vma_intersection(mm, addr, addr + len))
1267-
return -EEXIST;
1268-
}
1269-
12701258
if (prot == PROT_EXEC) {
12711259
pkey = execute_only_pkey(mm);
12721260
if (pkey < 0)
@@ -1280,6 +1268,18 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
12801268
vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
12811269
mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
12821270

1271+
/* Obtain the address to map to. we verify (or select) it and ensure
1272+
* that it represents a valid section of the address space.
1273+
*/
1274+
addr = __get_unmapped_area(file, addr, len, pgoff, flags, vm_flags);
1275+
if (IS_ERR_VALUE(addr))
1276+
return addr;
1277+
1278+
if (flags & MAP_FIXED_NOREPLACE) {
1279+
if (find_vma_intersection(mm, addr, addr + len))
1280+
return -EEXIST;
1281+
}
1282+
12831283
if (flags & MAP_LOCKED)
12841284
if (!can_do_mlock())
12851285
return -EPERM;
@@ -1836,8 +1836,8 @@ unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *fi
18361836
}
18371837

18381838
unsigned long
1839-
get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1840-
unsigned long pgoff, unsigned long flags)
1839+
__get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1840+
unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags)
18411841
{
18421842
unsigned long (*get_area)(struct file *, unsigned long,
18431843
unsigned long, unsigned long, unsigned long)
@@ -1872,8 +1872,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
18721872
if (get_area)
18731873
addr = get_area(file, addr, len, pgoff, flags);
18741874
else
1875-
addr = mm_get_unmapped_area(current->mm, file, addr, len,
1876-
pgoff, flags);
1875+
addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len,
1876+
pgoff, flags, vm_flags);
18771877
if (IS_ERR_VALUE(addr))
18781878
return addr;
18791879

0 commit comments

Comments
 (0)