1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * flexible mmap layout support
5 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
8 * Started by Ingo Molnar <mingo@elte.hu>
11 #include <linux/personality.h>
13 #include <linux/random.h>
14 #include <linux/sched/signal.h>
15 #include <linux/sched/mm.h>
16 #include <linux/elf-randomize.h>
17 #include <linux/security.h>
18 #include <linux/mman.h>
21 * Top of mmap area (just below the process stack).
23 * Leave at least a ~128 MB hole.
25 #define MIN_GAP (128*1024*1024)
26 #define MAX_GAP (TASK_SIZE/6*5)
28 static inline int mmap_is_legacy(struct rlimit
*rlim_stack
)
30 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
33 if (rlim_stack
->rlim_cur
== RLIM_INFINITY
)
36 return sysctl_legacy_va_layout
;
39 unsigned long arch_mmap_rnd(void)
41 unsigned long shift
, rnd
;
43 shift
= mmap_rnd_bits
;
46 shift
= mmap_rnd_compat_bits
;
48 rnd
= get_random_long() % (1ul << shift
);
50 return rnd
<< PAGE_SHIFT
;
53 static inline unsigned long stack_maxrandom_size(void)
55 if (!(current
->flags
& PF_RANDOMIZE
))
58 /* 8MB for 32bit, 1GB for 64bit */
65 static inline unsigned long mmap_base(unsigned long rnd
,
66 struct rlimit
*rlim_stack
)
68 unsigned long gap
= rlim_stack
->rlim_cur
;
69 unsigned long pad
= stack_maxrandom_size() + stack_guard_gap
;
71 /* Values close to RLIM_INFINITY can overflow. */
77 else if (gap
> MAX_GAP
)
80 return PAGE_ALIGN(DEFAULT_MAP_WINDOW
- gap
- rnd
);
83 #ifdef CONFIG_PPC_RADIX_MMU
85 * Same function as generic code used only for radix, because we don't need to overload
86 * the generic one. But we will have to duplicate, because hash select
87 * HAVE_ARCH_UNMAPPED_AREA
90 radix__arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
91 unsigned long len
, unsigned long pgoff
,
94 struct mm_struct
*mm
= current
->mm
;
95 struct vm_area_struct
*vma
;
96 int fixed
= (flags
& MAP_FIXED
);
97 unsigned long high_limit
;
98 struct vm_unmapped_area_info info
;
100 high_limit
= DEFAULT_MAP_WINDOW
;
101 if (addr
>= high_limit
|| (fixed
&& (addr
+ len
> high_limit
)))
102 high_limit
= TASK_SIZE
;
104 if (len
> high_limit
)
108 if (addr
> high_limit
- len
)
114 addr
= PAGE_ALIGN(addr
);
115 vma
= find_vma(mm
, addr
);
116 if (high_limit
- len
>= addr
&& addr
>= mmap_min_addr
&&
117 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
123 info
.low_limit
= mm
->mmap_base
;
124 info
.high_limit
= high_limit
;
127 return vm_unmapped_area(&info
);
131 radix__arch_get_unmapped_area_topdown(struct file
*filp
,
132 const unsigned long addr0
,
133 const unsigned long len
,
134 const unsigned long pgoff
,
135 const unsigned long flags
)
137 struct vm_area_struct
*vma
;
138 struct mm_struct
*mm
= current
->mm
;
139 unsigned long addr
= addr0
;
140 int fixed
= (flags
& MAP_FIXED
);
141 unsigned long high_limit
;
142 struct vm_unmapped_area_info info
;
144 high_limit
= DEFAULT_MAP_WINDOW
;
145 if (addr
>= high_limit
|| (fixed
&& (addr
+ len
> high_limit
)))
146 high_limit
= TASK_SIZE
;
148 if (len
> high_limit
)
152 if (addr
> high_limit
- len
)
158 addr
= PAGE_ALIGN(addr
);
159 vma
= find_vma(mm
, addr
);
160 if (high_limit
- len
>= addr
&& addr
>= mmap_min_addr
&&
161 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
165 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
167 info
.low_limit
= max(PAGE_SIZE
, mmap_min_addr
);
168 info
.high_limit
= mm
->mmap_base
+ (high_limit
- DEFAULT_MAP_WINDOW
);
171 addr
= vm_unmapped_area(&info
);
172 if (!(addr
& ~PAGE_MASK
))
174 VM_BUG_ON(addr
!= -ENOMEM
);
177 * A failed mmap() very likely causes application failure,
178 * so fall back to the bottom-up function here. This scenario
179 * can happen with large stack limits and large mmap()
182 return radix__arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
185 static void radix__arch_pick_mmap_layout(struct mm_struct
*mm
,
186 unsigned long random_factor
,
187 struct rlimit
*rlim_stack
)
189 if (mmap_is_legacy(rlim_stack
)) {
190 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
191 mm
->get_unmapped_area
= radix__arch_get_unmapped_area
;
193 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
194 mm
->get_unmapped_area
= radix__arch_get_unmapped_area_topdown
;
199 extern void radix__arch_pick_mmap_layout(struct mm_struct
*mm
,
200 unsigned long random_factor
,
201 struct rlimit
*rlim_stack
);
204 * This function, called very early during the creation of a new
205 * process VM image, sets up which VM layout function to use:
207 void arch_pick_mmap_layout(struct mm_struct
*mm
, struct rlimit
*rlim_stack
)
209 unsigned long random_factor
= 0UL;
211 if (current
->flags
& PF_RANDOMIZE
)
212 random_factor
= arch_mmap_rnd();
215 return radix__arch_pick_mmap_layout(mm
, random_factor
,
218 * Fall back to the standard layout if the personality
219 * bit is set, or if the expected stack growth is unlimited:
221 if (mmap_is_legacy(rlim_stack
)) {
222 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
223 mm
->get_unmapped_area
= arch_get_unmapped_area
;
225 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
226 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;