mmap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(void)
  40. {
  41. if (current->personality & ADDR_COMPAT_LAYOUT)
  42. return 1;
  43. if (rlimit(RLIMIT_STACK) == 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. {
  70. unsigned long gap = rlimit(RLIMIT_STACK);
  71. unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
  72. /* Values close to RLIM_INFINITY can overflow. */
  73. if (gap + pad > gap)
  74. gap += pad;
  75. if (gap < MIN_GAP)
  76. gap = MIN_GAP;
  77. else if (gap > MAX_GAP)
  78. gap = MAX_GAP;
  79. return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
  80. }
  81. #ifdef CONFIG_PPC_RADIX_MMU
  82. /*
  83. * Same function as generic code used only for radix, because we don't need to overload
  84. * the generic one. But we will have to duplicate, because hash select
  85. * HAVE_ARCH_UNMAPPED_AREA
  86. */
  87. static unsigned long
  88. radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
  89. unsigned long len, unsigned long pgoff,
  90. unsigned long flags)
  91. {
  92. struct mm_struct *mm = current->mm;
  93. struct vm_area_struct *vma;
  94. int fixed = (flags & MAP_FIXED);
  95. unsigned long high_limit;
  96. struct vm_unmapped_area_info info;
  97. high_limit = DEFAULT_MAP_WINDOW;
  98. if (addr >= high_limit || (fixed && (addr + len > high_limit)))
  99. high_limit = TASK_SIZE;
  100. if (len > high_limit)
  101. return -ENOMEM;
  102. if (fixed) {
  103. if (addr > high_limit - len)
  104. return -ENOMEM;
  105. return addr;
  106. }
  107. if (addr) {
  108. addr = PAGE_ALIGN(addr);
  109. vma = find_vma(mm, addr);
  110. if (high_limit - len >= addr && addr >= mmap_min_addr &&
  111. (!vma || addr + len <= vm_start_gap(vma)))
  112. return addr;
  113. }
  114. info.flags = 0;
  115. info.length = len;
  116. info.low_limit = mm->mmap_base;
  117. info.high_limit = high_limit;
  118. info.align_mask = 0;
  119. return vm_unmapped_area(&info);
  120. }
  121. static unsigned long
  122. radix__arch_get_unmapped_area_topdown(struct file *filp,
  123. const unsigned long addr0,
  124. const unsigned long len,
  125. const unsigned long pgoff,
  126. const unsigned long flags)
  127. {
  128. struct vm_area_struct *vma;
  129. struct mm_struct *mm = current->mm;
  130. unsigned long addr = addr0;
  131. int fixed = (flags & MAP_FIXED);
  132. unsigned long high_limit;
  133. struct vm_unmapped_area_info info;
  134. high_limit = DEFAULT_MAP_WINDOW;
  135. if (addr >= high_limit || (fixed && (addr + len > high_limit)))
  136. high_limit = TASK_SIZE;
  137. if (len > high_limit)
  138. return -ENOMEM;
  139. if (fixed) {
  140. if (addr > high_limit - len)
  141. return -ENOMEM;
  142. return addr;
  143. }
  144. if (addr) {
  145. addr = PAGE_ALIGN(addr);
  146. vma = find_vma(mm, addr);
  147. if (high_limit - len >= addr && addr >= mmap_min_addr &&
  148. (!vma || addr + len <= vm_start_gap(vma)))
  149. return addr;
  150. }
  151. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  152. info.length = len;
  153. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  154. info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
  155. info.align_mask = 0;
  156. addr = vm_unmapped_area(&info);
  157. if (!(addr & ~PAGE_MASK))
  158. return addr;
  159. VM_BUG_ON(addr != -ENOMEM);
  160. /*
  161. * A failed mmap() very likely causes application failure,
  162. * so fall back to the bottom-up function here. This scenario
  163. * can happen with large stack limits and large mmap()
  164. * allocations.
  165. */
  166. return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  167. }
  168. static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
  169. unsigned long random_factor)
  170. {
  171. if (mmap_is_legacy()) {
  172. mm->mmap_base = TASK_UNMAPPED_BASE;
  173. mm->get_unmapped_area = radix__arch_get_unmapped_area;
  174. } else {
  175. mm->mmap_base = mmap_base(random_factor);
  176. mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
  177. }
  178. }
  179. #else
  180. /* dummy */
  181. extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
  182. unsigned long random_factor);
  183. #endif
  184. /*
  185. * This function, called very early during the creation of a new
  186. * process VM image, sets up which VM layout function to use:
  187. */
  188. void arch_pick_mmap_layout(struct mm_struct *mm)
  189. {
  190. unsigned long random_factor = 0UL;
  191. if (current->flags & PF_RANDOMIZE)
  192. random_factor = arch_mmap_rnd();
  193. if (radix_enabled())
  194. return radix__arch_pick_mmap_layout(mm, random_factor);
  195. /*
  196. * Fall back to the standard layout if the personality
  197. * bit is set, or if the expected stack growth is unlimited:
  198. */
  199. if (mmap_is_legacy()) {
  200. mm->mmap_base = TASK_UNMAPPED_BASE;
  201. mm->get_unmapped_area = arch_get_unmapped_area;
  202. } else {
  203. mm->mmap_base = mmap_base(random_factor);
  204. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  205. }
  206. }