.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Based on arch/arm/mm/mmap.c |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2012 ARM Ltd. |
---|
5 | | - * |
---|
6 | | - * This program is free software; you can redistribute it and/or modify |
---|
7 | | - * it under the terms of the GNU General Public License version 2 as |
---|
8 | | - * published by the Free Software Foundation. |
---|
9 | | - * |
---|
10 | | - * This program is distributed in the hope that it will be useful, |
---|
11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | | - * GNU General Public License for more details. |
---|
14 | | - * |
---|
15 | | - * You should have received a copy of the GNU General Public License |
---|
16 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | 6 | */ |
---|
18 | 7 | |
---|
19 | 8 | #include <linux/elf.h> |
---|
.. | .. |
---|
30 | 19 | #include <linux/random.h> |
---|
31 | 20 | |
---|
32 | 21 | #include <asm/cputype.h> |
---|
33 | | - |
---|
34 | | -/* |
---|
35 | | - * Leave enough space between the mmap area and the stack to honour ulimit in |
---|
36 | | - * the face of randomisation. |
---|
37 | | - */ |
---|
38 | | -#define MIN_GAP (SZ_128M) |
---|
39 | | -#define MAX_GAP (STACK_TOP/6*5) |
---|
40 | | - |
---|
41 | | -static int mmap_is_legacy(struct rlimit *rlim_stack) |
---|
42 | | -{ |
---|
43 | | - if (current->personality & ADDR_COMPAT_LAYOUT) |
---|
44 | | - return 1; |
---|
45 | | - |
---|
46 | | - if (rlim_stack->rlim_cur == RLIM_INFINITY) |
---|
47 | | - return 1; |
---|
48 | | - |
---|
49 | | - return sysctl_legacy_va_layout; |
---|
50 | | -} |
---|
51 | | - |
---|
52 | | -unsigned long arch_mmap_rnd(void) |
---|
53 | | -{ |
---|
54 | | - unsigned long rnd; |
---|
55 | | - |
---|
56 | | -#ifdef CONFIG_COMPAT |
---|
57 | | - if (test_thread_flag(TIF_32BIT)) |
---|
58 | | - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); |
---|
59 | | - else |
---|
60 | | -#endif |
---|
61 | | - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); |
---|
62 | | - return rnd << PAGE_SHIFT; |
---|
63 | | -} |
---|
64 | | - |
---|
65 | | -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) |
---|
66 | | -{ |
---|
67 | | - unsigned long gap = rlim_stack->rlim_cur; |
---|
68 | | - unsigned long pad = stack_guard_gap; |
---|
69 | | - |
---|
70 | | - /* Account for stack randomization if necessary */ |
---|
71 | | - if (current->flags & PF_RANDOMIZE) |
---|
72 | | - pad += (STACK_RND_MASK << PAGE_SHIFT); |
---|
73 | | - |
---|
74 | | - /* Values close to RLIM_INFINITY can overflow. */ |
---|
75 | | - if (gap + pad > gap) |
---|
76 | | - gap += pad; |
---|
77 | | - |
---|
78 | | - if (gap < MIN_GAP) |
---|
79 | | - gap = MIN_GAP; |
---|
80 | | - else if (gap > MAX_GAP) |
---|
81 | | - gap = MAX_GAP; |
---|
82 | | - |
---|
83 | | - return PAGE_ALIGN(STACK_TOP - gap - rnd); |
---|
84 | | -} |
---|
85 | | - |
---|
86 | | -/* |
---|
87 | | - * This function, called very early during the creation of a new process VM |
---|
88 | | - * image, sets up which VM layout function to use: |
---|
89 | | - */ |
---|
90 | | -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) |
---|
91 | | -{ |
---|
92 | | - unsigned long random_factor = 0UL; |
---|
93 | | - |
---|
94 | | - if (current->flags & PF_RANDOMIZE) |
---|
95 | | - random_factor = arch_mmap_rnd(); |
---|
96 | | - |
---|
97 | | - /* |
---|
98 | | - * Fall back to the standard layout if the personality bit is set, or |
---|
99 | | - * if the expected stack growth is unlimited: |
---|
100 | | - */ |
---|
101 | | - if (mmap_is_legacy(rlim_stack)) { |
---|
102 | | - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; |
---|
103 | | - mm->get_unmapped_area = arch_get_unmapped_area; |
---|
104 | | - } else { |
---|
105 | | - mm->mmap_base = mmap_base(random_factor, rlim_stack); |
---|
106 | | - mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
---|
107 | | - } |
---|
108 | | -} |
---|
109 | 22 | |
---|
110 | 23 | /* |
---|
111 | 24 | * You really shouldn't be using read() or write() on /dev/mem. This might go |
---|