1 /* 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/signal.h> 26 #include <linux/sched/mm.h> 27 #include <linux/io.h> 28 #include <linux/personality.h> 29 #include <linux/random.h> 30 31 #include <asm/cputype.h> 32 33 /* 34 * Leave enough space between the mmap area and the stack to honour ulimit in 35 * the face of randomisation. 36 */ 37 #define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1)) 38 #define MAX_GAP (STACK_TOP/6*5) 39 40 static int mmap_is_legacy(void) 41 { 42 if (current->personality & ADDR_COMPAT_LAYOUT) 43 return 1; 44 45 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) 46 return 1; 47 48 return sysctl_legacy_va_layout; 49 } 50 51 unsigned long arch_mmap_rnd(void) 52 { 53 unsigned long rnd; 54 55 #ifdef CONFIG_COMPAT 56 if (test_thread_flag(TIF_32BIT)) 57 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); 58 else 59 #endif 60 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); 61 return rnd << PAGE_SHIFT; 62 } 63 64 static unsigned long mmap_base(unsigned long rnd) 65 { 66 unsigned long gap = rlimit(RLIMIT_STACK); 67 68 if (gap < MIN_GAP) 69 gap = MIN_GAP; 70 else if (gap > MAX_GAP) 71 gap = MAX_GAP; 72 73 return PAGE_ALIGN(STACK_TOP - gap - rnd); 74 } 75 76 /* 77 * This function, called very early during the creation of a new process VM 78 * image, sets up which VM layout function to use: 79 */ 80 void arch_pick_mmap_layout(struct mm_struct *mm) 81 { 82 unsigned long random_factor = 0UL; 83 84 if (current->flags & PF_RANDOMIZE) 85 random_factor = arch_mmap_rnd(); 86 87 /* 88 * Fall back to the standard layout if the personality bit is set, or 89 * if the expected stack growth is unlimited: 90 */ 91 if (mmap_is_legacy()) { 92 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; 93 mm->get_unmapped_area = arch_get_unmapped_area; 94 } else { 95 mm->mmap_base = mmap_base(random_factor); 96 mm->get_unmapped_area = arch_get_unmapped_area_topdown; 97 } 98 } 99 100 /* 101 * You really shouldn't be using read() or write() on /dev/mem. This might go 102 * away in the future. 103 */ 104 int valid_phys_addr_range(phys_addr_t addr, size_t size) 105 { 106 if (addr < PHYS_OFFSET) 107 return 0; 108 if (addr + size > __pa(high_memory - 1) + 1) 109 return 0; 110 111 return 1; 112 } 113 114 /* 115 * Do not allow /dev/mem mappings beyond the supported physical range. 116 */ 117 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 118 { 119 return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK); 120 } 121 122 #ifdef CONFIG_STRICT_DEVMEM 123 124 #include <linux/ioport.h> 125 126 /* 127 * devmem_is_allowed() checks to see if /dev/mem access to a certain address 128 * is valid. The argument is a physical page number. We mimic x86 here by 129 * disallowing access to system RAM as well as device-exclusive MMIO regions. 130 * This effectively disable read()/write() on /dev/mem. 131 */ 132 int devmem_is_allowed(unsigned long pfn) 133 { 134 if (iomem_is_exclusive(pfn << PAGE_SHIFT)) 135 return 0; 136 if (!page_is_ram(pfn)) 137 return 1; 138 return 0; 139 } 140 141 #endif 142