mmap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * flexible mmap layout support
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * Started by Ingo Molnar <mingo@elte.hu>
  23. */
  24. #include <linux/personality.h>
  25. #include <linux/mm.h>
  26. #include <linux/random.h>
  27. #include <linux/sched/signal.h>
  28. #include <linux/sched/mm.h>
  29. #include <linux/elf-randomize.h>
  30. #include <linux/security.h>
  31. #include <linux/mman.h>
  32. /*
  33. * Top of mmap area (just below the process stack).
  34. *
  35. * Leave at least a ~128 MB hole.
  36. */
  37. #define MIN_GAP (128*1024*1024)
  38. #define MAX_GAP (TASK_SIZE/6*5)
  39. static inline int mmap_is_legacy(struct rlimit *rlim_stack)
  40. {
  41. if (current->personality & ADDR_COMPAT_LAYOUT)
  42. return 1;
  43. if (rlim_stack->rlim_cur == RLIM_INFINITY)
  44. return 1;
  45. return sysctl_legacy_va_layout;
  46. }
  47. unsigned long arch_mmap_rnd(void)
  48. {
  49. unsigned long shift, rnd;
  50. shift = mmap_rnd_bits;
  51. #ifdef CONFIG_COMPAT
  52. if (is_32bit_task())
  53. shift = mmap_rnd_compat_bits;
  54. #endif
  55. rnd = get_random_long() % (1ul << shift);
  56. return rnd << PAGE_SHIFT;
  57. }
  58. static inline unsigned long stack_maxrandom_size(void)
  59. {
  60. if (!(current->flags & PF_RANDOMIZE))
  61. return 0;
  62. /* 8MB for 32bit, 1GB for 64bit */
  63. if (is_32bit_task())
  64. return (1<<23);
  65. else
  66. return (1<<30);
  67. }
  68. static inline unsigned long mmap_base(unsigned long rnd,
  69. struct rlimit *rlim_stack)
  70. {
  71. unsigned long gap = rlim_stack->rlim_cur;
  72. unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
  73. /* Values close to RLIM_INFINITY can overflow. */
  74. if (gap + pad > gap)
  75. gap += pad;
  76. if (gap < MIN_GAP)
  77. gap = MIN_GAP;
  78. else if (gap > MAX_GAP)
  79. gap = MAX_GAP;
  80. return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
  81. }
  82. #ifdef CONFIG_PPC_RADIX_MMU
  83. /*
  84. * Same function as generic code used only for radix, because we don't need to overload
  85. * the generic one. But we will have to duplicate, because hash select
  86. * HAVE_ARCH_UNMAPPED_AREA
  87. */
  88. static unsigned long
  89. radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
  90. unsigned long len, unsigned long pgoff,
  91. unsigned long flags)
  92. {
  93. struct mm_struct *mm = current->mm;
  94. struct vm_area_struct *vma;
  95. int fixed = (flags & MAP_FIXED);
  96. unsigned long high_limit;
  97. struct vm_unmapped_area_info info;
  98. high_limit = DEFAULT_MAP_WINDOW;
  99. if (addr >= high_limit || (fixed && (addr + len > high_limit)))
  100. high_limit = TASK_SIZE;
  101. if (len > high_limit)
  102. return -ENOMEM;
  103. if (fixed) {
  104. if (addr > high_limit - len)
  105. return -ENOMEM;
  106. return addr;
  107. }
  108. if (addr) {
  109. addr = PAGE_ALIGN(addr);
  110. vma = find_vma(mm, addr);
  111. if (high_limit - len >= addr && addr >= mmap_min_addr &&
  112. (!vma || addr + len <= vm_start_gap(vma)))
  113. return addr;
  114. }
  115. info.flags = 0;
  116. info.length = len;
  117. info.low_limit = mm->mmap_base;
  118. info.high_limit = high_limit;
  119. info.align_mask = 0;
  120. return vm_unmapped_area(&info);
  121. }
  122. static unsigned long
  123. radix__arch_get_unmapped_area_topdown(struct file *filp,
  124. const unsigned long addr0,
  125. const unsigned long len,
  126. const unsigned long pgoff,
  127. const unsigned long flags)
  128. {
  129. struct vm_area_struct *vma;
  130. struct mm_struct *mm = current->mm;
  131. unsigned long addr = addr0;
  132. int fixed = (flags & MAP_FIXED);
  133. unsigned long high_limit;
  134. struct vm_unmapped_area_info info;
  135. high_limit = DEFAULT_MAP_WINDOW;
  136. if (addr >= high_limit || (fixed && (addr + len > high_limit)))
  137. high_limit = TASK_SIZE;
  138. if (len > high_limit)
  139. return -ENOMEM;
  140. if (fixed) {
  141. if (addr > high_limit - len)
  142. return -ENOMEM;
  143. return addr;
  144. }
  145. if (addr) {
  146. addr = PAGE_ALIGN(addr);
  147. vma = find_vma(mm, addr);
  148. if (high_limit - len >= addr && addr >= mmap_min_addr &&
  149. (!vma || addr + len <= vm_start_gap(vma)))
  150. return addr;
  151. }
  152. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  153. info.length = len;
  154. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  155. info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
  156. info.align_mask = 0;
  157. addr = vm_unmapped_area(&info);
  158. if (!(addr & ~PAGE_MASK))
  159. return addr;
  160. VM_BUG_ON(addr != -ENOMEM);
  161. /*
  162. * A failed mmap() very likely causes application failure,
  163. * so fall back to the bottom-up function here. This scenario
  164. * can happen with large stack limits and large mmap()
  165. * allocations.
  166. */
  167. return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  168. }
  169. static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
  170. unsigned long random_factor,
  171. struct rlimit *rlim_stack)
  172. {
  173. if (mmap_is_legacy(rlim_stack)) {
  174. mm->mmap_base = TASK_UNMAPPED_BASE;
  175. mm->get_unmapped_area = radix__arch_get_unmapped_area;
  176. } else {
  177. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  178. mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
  179. }
  180. }
  181. #else
  182. /* dummy */
  183. extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
  184. unsigned long random_factor,
  185. struct rlimit *rlim_stack);
  186. #endif
  187. /*
  188. * This function, called very early during the creation of a new
  189. * process VM image, sets up which VM layout function to use:
  190. */
  191. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  192. {
  193. unsigned long random_factor = 0UL;
  194. if (current->flags & PF_RANDOMIZE)
  195. random_factor = arch_mmap_rnd();
  196. if (radix_enabled())
  197. return radix__arch_pick_mmap_layout(mm, random_factor,
  198. rlim_stack);
  199. /*
  200. * Fall back to the standard layout if the personality
  201. * bit is set, or if the expected stack growth is unlimited:
  202. */
  203. if (mmap_is_legacy(rlim_stack)) {
  204. mm->mmap_base = TASK_UNMAPPED_BASE;
  205. mm->get_unmapped_area = arch_get_unmapped_area;
  206. } else {
  207. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  208. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  209. }
  210. }