blob: 232f787a088ae8e992c52cba450159a43a900174 [file] [log] [blame]
Catalin Marinas1d18c472012-03-05 11:49:27 +00001/*
2 * Based on arch/arm/mm/mmap.c
3 *
4 * 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 */
18
19#include <linux/elf.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
22#include <linux/mman.h>
23#include <linux/export.h>
24#include <linux/shm.h>
25#include <linux/sched.h>
26#include <linux/io.h>
27#include <linux/personality.h>
28#include <linux/random.h>
29
30#include <asm/cputype.h>
31
32/*
33 * Leave enough space between the mmap area and the stack to honour ulimit in
34 * the face of randomisation.
35 */
36#define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1))
37#define MAX_GAP (STACK_TOP/6*5)
38
39static int mmap_is_legacy(void)
40{
41 if (current->personality & ADDR_COMPAT_LAYOUT)
42 return 1;
43
44 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
45 return 1;
46
47 return sysctl_legacy_va_layout;
48}
49
Kees Cook2b68f6c2015-04-14 15:48:00 -070050unsigned long arch_mmap_rnd(void)
Catalin Marinas1d18c472012-03-05 11:49:27 +000051{
Kees Cookdd04cff2015-04-14 15:47:48 -070052 unsigned long rnd;
Catalin Marinas1d18c472012-03-05 11:49:27 +000053
Daniel Cashman8f0d3aa2016-01-14 15:20:01 -080054#ifdef CONFIG_COMPAT
55 if (test_thread_flag(TIF_32BIT))
Daniel Cashman5ef11c32016-02-26 15:19:37 -080056 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
Daniel Cashman8f0d3aa2016-01-14 15:20:01 -080057 else
58#endif
Daniel Cashman5ef11c32016-02-26 15:19:37 -080059 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Yann Droneaudd6c763a2014-11-17 23:02:19 +000060 return rnd << PAGE_SHIFT;
Catalin Marinas1d18c472012-03-05 11:49:27 +000061}
62
Kees Cookdd04cff2015-04-14 15:47:48 -070063static unsigned long mmap_base(unsigned long rnd)
Catalin Marinas1d18c472012-03-05 11:49:27 +000064{
65 unsigned long gap = rlimit(RLIMIT_STACK);
66
67 if (gap < MIN_GAP)
68 gap = MIN_GAP;
69 else if (gap > MAX_GAP)
70 gap = MAX_GAP;
71
Kees Cookdd04cff2015-04-14 15:47:48 -070072 return PAGE_ALIGN(STACK_TOP - gap - rnd);
Catalin Marinas1d18c472012-03-05 11:49:27 +000073}
74
75/*
76 * This function, called very early during the creation of a new process VM
77 * image, sets up which VM layout function to use:
78 */
79void arch_pick_mmap_layout(struct mm_struct *mm)
80{
Kees Cookdd04cff2015-04-14 15:47:48 -070081 unsigned long random_factor = 0UL;
82
83 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -070084 random_factor = arch_mmap_rnd();
Kees Cookdd04cff2015-04-14 15:47:48 -070085
Catalin Marinas1d18c472012-03-05 11:49:27 +000086 /*
87 * Fall back to the standard layout if the personality bit is set, or
88 * if the expected stack growth is unlimited:
89 */
90 if (mmap_is_legacy()) {
Kees Cookdd04cff2015-04-14 15:47:48 -070091 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
Catalin Marinas1d18c472012-03-05 11:49:27 +000092 mm->get_unmapped_area = arch_get_unmapped_area;
Catalin Marinas1d18c472012-03-05 11:49:27 +000093 } else {
Kees Cookdd04cff2015-04-14 15:47:48 -070094 mm->mmap_base = mmap_base(random_factor);
Catalin Marinas1d18c472012-03-05 11:49:27 +000095 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Catalin Marinas1d18c472012-03-05 11:49:27 +000096 }
97}
98EXPORT_SYMBOL_GPL(arch_pick_mmap_layout);
99
100
101/*
102 * You really shouldn't be using read() or write() on /dev/mem. This might go
103 * away in the future.
104 */
Min-Hua Chen097cbd82014-10-02 15:56:59 +0100105int valid_phys_addr_range(phys_addr_t addr, size_t size)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000106{
107 if (addr < PHYS_OFFSET)
108 return 0;
109 if (addr + size > __pa(high_memory - 1) + 1)
110 return 0;
111
112 return 1;
113}
114
115/*
116 * Do not allow /dev/mem mappings beyond the supported physical range.
117 */
118int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
119{
120 return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
121}
122
123#ifdef CONFIG_STRICT_DEVMEM
124
125#include <linux/ioport.h>
126
127/*
128 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
129 * is valid. The argument is a physical page number. We mimic x86 here by
130 * disallowing access to system RAM as well as device-exclusive MMIO regions.
131 * This effectively disable read()/write() on /dev/mem.
132 */
133int devmem_is_allowed(unsigned long pfn)
134{
135 if (iomem_is_exclusive(pfn << PAGE_SHIFT))
136 return 0;
137 if (!page_is_ram(pfn))
138 return 1;
139 return 0;
140}
141
142#endif