mmap.c 5.4 KB

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