mmap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/elf-randomize.h>
  25. #include <linux/personality.h>
  26. #include <linux/mm.h>
  27. #include <linux/mman.h>
  28. #include <linux/module.h>
  29. #include <linux/random.h>
  30. #include <linux/compat.h>
  31. #include <linux/security.h>
  32. #include <asm/pgalloc.h>
  33. static unsigned long stack_maxrandom_size(void)
  34. {
  35. if (!(current->flags & PF_RANDOMIZE))
  36. return 0;
  37. if (current->personality & ADDR_NO_RANDOMIZE)
  38. return 0;
  39. return STACK_RND_MASK << PAGE_SHIFT;
  40. }
  41. /*
  42. * Top of mmap area (just below the process stack).
  43. *
  44. * Leave at least a ~32 MB hole.
  45. */
  46. #define MIN_GAP (32*1024*1024)
  47. #define MAX_GAP (STACK_TOP/6*5)
  48. static inline int mmap_is_legacy(void)
  49. {
  50. if (current->personality & ADDR_COMPAT_LAYOUT)
  51. return 1;
  52. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
  53. return 1;
  54. return sysctl_legacy_va_layout;
  55. }
  56. unsigned long arch_mmap_rnd(void)
  57. {
  58. return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
  59. }
  60. static unsigned long mmap_base_legacy(unsigned long rnd)
  61. {
  62. return TASK_UNMAPPED_BASE + rnd;
  63. }
  64. static inline unsigned long mmap_base(unsigned long rnd)
  65. {
  66. unsigned long gap = rlimit(RLIMIT_STACK);
  67. if (gap < MIN_GAP)
  68. gap = MIN_GAP;
  69. else if (gap > MAX_GAP)
  70. gap = MAX_GAP;
  71. gap &= PAGE_MASK;
  72. return STACK_TOP - stack_maxrandom_size() - rnd - gap;
  73. }
  74. unsigned long
  75. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  76. unsigned long len, unsigned long pgoff, unsigned long flags)
  77. {
  78. struct mm_struct *mm = current->mm;
  79. struct vm_area_struct *vma;
  80. struct vm_unmapped_area_info info;
  81. if (len > TASK_SIZE - mmap_min_addr)
  82. return -ENOMEM;
  83. if (flags & MAP_FIXED)
  84. return addr;
  85. if (addr) {
  86. addr = PAGE_ALIGN(addr);
  87. vma = find_vma(mm, addr);
  88. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  89. (!vma || addr + len <= vm_start_gap(vma)))
  90. return addr;
  91. }
  92. info.flags = 0;
  93. info.length = len;
  94. info.low_limit = mm->mmap_base;
  95. info.high_limit = TASK_SIZE;
  96. if (filp || (flags & MAP_SHARED))
  97. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  98. else
  99. info.align_mask = 0;
  100. info.align_offset = pgoff << PAGE_SHIFT;
  101. return vm_unmapped_area(&info);
  102. }
  103. unsigned long
  104. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  105. const unsigned long len, const unsigned long pgoff,
  106. const unsigned long flags)
  107. {
  108. struct vm_area_struct *vma;
  109. struct mm_struct *mm = current->mm;
  110. unsigned long addr = addr0;
  111. struct vm_unmapped_area_info info;
  112. /* requested length too big for entire address space */
  113. if (len > TASK_SIZE - mmap_min_addr)
  114. return -ENOMEM;
  115. if (flags & MAP_FIXED)
  116. return addr;
  117. /* requesting a specific address */
  118. if (addr) {
  119. addr = PAGE_ALIGN(addr);
  120. vma = find_vma(mm, addr);
  121. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  122. (!vma || addr + len <= vm_start_gap(vma)))
  123. return addr;
  124. }
  125. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  126. info.length = len;
  127. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  128. info.high_limit = mm->mmap_base;
  129. if (filp || (flags & MAP_SHARED))
  130. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  131. else
  132. info.align_mask = 0;
  133. info.align_offset = pgoff << PAGE_SHIFT;
  134. addr = vm_unmapped_area(&info);
  135. /*
  136. * A failed mmap() very likely causes application failure,
  137. * so fall back to the bottom-up function here. This scenario
  138. * can happen with large stack limits and large mmap()
  139. * allocations.
  140. */
  141. if (addr & ~PAGE_MASK) {
  142. VM_BUG_ON(addr != -ENOMEM);
  143. info.flags = 0;
  144. info.low_limit = TASK_UNMAPPED_BASE;
  145. info.high_limit = TASK_SIZE;
  146. addr = vm_unmapped_area(&info);
  147. }
  148. return addr;
  149. }
  150. int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
  151. {
  152. if (is_compat_task() || TASK_SIZE >= TASK_MAX_SIZE)
  153. return 0;
  154. if (!(flags & MAP_FIXED))
  155. addr = 0;
  156. if ((addr + len) >= TASK_SIZE)
  157. return crst_table_upgrade(current->mm);
  158. return 0;
  159. }
  160. static unsigned long
  161. s390_get_unmapped_area(struct file *filp, unsigned long addr,
  162. unsigned long len, unsigned long pgoff, unsigned long flags)
  163. {
  164. struct mm_struct *mm = current->mm;
  165. unsigned long area;
  166. int rc;
  167. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  168. if (!(area & ~PAGE_MASK))
  169. return area;
  170. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < TASK_MAX_SIZE) {
  171. /* Upgrade the page table to 4 levels and retry. */
  172. rc = crst_table_upgrade(mm);
  173. if (rc)
  174. return (unsigned long) rc;
  175. area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
  176. }
  177. return area;
  178. }
  179. static unsigned long
  180. s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
  181. const unsigned long len, const unsigned long pgoff,
  182. const unsigned long flags)
  183. {
  184. struct mm_struct *mm = current->mm;
  185. unsigned long area;
  186. int rc;
  187. area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
  188. if (!(area & ~PAGE_MASK))
  189. return area;
  190. if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < TASK_MAX_SIZE) {
  191. /* Upgrade the page table to 4 levels and retry. */
  192. rc = crst_table_upgrade(mm);
  193. if (rc)
  194. return (unsigned long) rc;
  195. area = arch_get_unmapped_area_topdown(filp, addr, len,
  196. pgoff, flags);
  197. }
  198. return area;
  199. }
  200. /*
  201. * This function, called very early during the creation of a new
  202. * process VM image, sets up which VM layout function to use:
  203. */
  204. void arch_pick_mmap_layout(struct mm_struct *mm)
  205. {
  206. unsigned long random_factor = 0UL;
  207. if (current->flags & PF_RANDOMIZE)
  208. random_factor = arch_mmap_rnd();
  209. /*
  210. * Fall back to the standard layout if the personality
  211. * bit is set, or if the expected stack growth is unlimited:
  212. */
  213. if (mmap_is_legacy()) {
  214. mm->mmap_base = mmap_base_legacy(random_factor);
  215. mm->get_unmapped_area = s390_get_unmapped_area;
  216. } else {
  217. mm->mmap_base = mmap_base(random_factor);
  218. mm->get_unmapped_area = s390_get_unmapped_area_topdown;
  219. }
  220. }