mmap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * flexible mmap layout support
  4. *
  5. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  6. * All Rights Reserved.
  7. *
  8. * Started by Ingo Molnar <mingo@elte.hu>
  9. */
  10. #include <linux/elf-randomize.h>
  11. #include <linux/personality.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/random.h>
  17. #include <linux/compat.h>
  18. #include <linux/security.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/elf.h>
  21. static unsigned long stack_maxrandom_size(void)
  22. {
  23. if (!(current->flags & PF_RANDOMIZE))
  24. return 0;
  25. if (current->personality & ADDR_NO_RANDOMIZE)
  26. return 0;
  27. return STACK_RND_MASK << PAGE_SHIFT;
  28. }
  29. /*
  30. * Top of mmap area (just below the process stack).
  31. *
  32. * Leave at least a ~32 MB hole.
  33. */
  34. #define MIN_GAP (32*1024*1024)
  35. #define MAX_GAP (STACK_TOP/6*5)
  36. static inline int mmap_is_legacy(struct rlimit *rlim_stack)
  37. {
  38. if (current->personality & ADDR_COMPAT_LAYOUT)
  39. return 1;
  40. if (rlim_stack->rlim_cur == RLIM_INFINITY)
  41. return 1;
  42. return sysctl_legacy_va_layout;
  43. }
  44. unsigned long arch_mmap_rnd(void)
  45. {
  46. return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
  47. }
  48. static unsigned long mmap_base_legacy(unsigned long rnd)
  49. {
  50. return TASK_UNMAPPED_BASE + rnd;
  51. }
  52. static inline unsigned long mmap_base(unsigned long rnd,
  53. struct rlimit *rlim_stack)
  54. {
  55. unsigned long gap = rlim_stack->rlim_cur;
  56. if (gap < MIN_GAP)
  57. gap = MIN_GAP;
  58. else if (gap > MAX_GAP)
  59. gap = MAX_GAP;
  60. gap &= PAGE_MASK;
  61. return STACK_TOP - stack_maxrandom_size() - rnd - gap;
  62. }
  63. unsigned long
  64. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  65. unsigned long len, unsigned long pgoff, unsigned long flags)
  66. {
  67. struct mm_struct *mm = current->mm;
  68. struct vm_area_struct *vma;
  69. struct vm_unmapped_area_info info;
  70. int rc;
  71. if (len > TASK_SIZE - mmap_min_addr)
  72. return -ENOMEM;
  73. if (flags & MAP_FIXED)
  74. goto check_asce_limit;
  75. if (addr) {
  76. addr = PAGE_ALIGN(addr);
  77. vma = find_vma(mm, addr);
  78. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  79. (!vma || addr + len <= vm_start_gap(vma)))
  80. goto check_asce_limit;
  81. }
  82. info.flags = 0;
  83. info.length = len;
  84. info.low_limit = mm->mmap_base;
  85. info.high_limit = TASK_SIZE;
  86. if (filp || (flags & MAP_SHARED))
  87. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  88. else
  89. info.align_mask = 0;
  90. info.align_offset = pgoff << PAGE_SHIFT;
  91. addr = vm_unmapped_area(&info);
  92. if (addr & ~PAGE_MASK)
  93. return addr;
  94. check_asce_limit:
  95. if (addr + len > current->mm->context.asce_limit &&
  96. addr + len <= TASK_SIZE) {
  97. rc = crst_table_upgrade(mm, addr + len);
  98. if (rc)
  99. return (unsigned long) rc;
  100. }
  101. return addr;
  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. int rc;
  113. /* requested length too big for entire address space */
  114. if (len > TASK_SIZE - mmap_min_addr)
  115. return -ENOMEM;
  116. if (flags & MAP_FIXED)
  117. goto check_asce_limit;
  118. /* requesting a specific address */
  119. if (addr) {
  120. addr = PAGE_ALIGN(addr);
  121. vma = find_vma(mm, addr);
  122. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  123. (!vma || addr + len <= vm_start_gap(vma)))
  124. goto check_asce_limit;
  125. }
  126. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  127. info.length = len;
  128. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  129. info.high_limit = mm->mmap_base;
  130. if (filp || (flags & MAP_SHARED))
  131. info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
  132. else
  133. info.align_mask = 0;
  134. info.align_offset = pgoff << PAGE_SHIFT;
  135. addr = vm_unmapped_area(&info);
  136. /*
  137. * A failed mmap() very likely causes application failure,
  138. * so fall back to the bottom-up function here. This scenario
  139. * can happen with large stack limits and large mmap()
  140. * allocations.
  141. */
  142. if (addr & ~PAGE_MASK) {
  143. VM_BUG_ON(addr != -ENOMEM);
  144. info.flags = 0;
  145. info.low_limit = TASK_UNMAPPED_BASE;
  146. info.high_limit = TASK_SIZE;
  147. addr = vm_unmapped_area(&info);
  148. if (addr & ~PAGE_MASK)
  149. return addr;
  150. }
  151. check_asce_limit:
  152. if (addr + len > current->mm->context.asce_limit &&
  153. addr + len <= TASK_SIZE) {
  154. rc = crst_table_upgrade(mm, addr + len);
  155. if (rc)
  156. return (unsigned long) rc;
  157. }
  158. return addr;
  159. }
  160. /*
  161. * This function, called very early during the creation of a new
  162. * process VM image, sets up which VM layout function to use:
  163. */
  164. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  165. {
  166. unsigned long random_factor = 0UL;
  167. if (current->flags & PF_RANDOMIZE)
  168. random_factor = arch_mmap_rnd();
  169. /*
  170. * Fall back to the standard layout if the personality
  171. * bit is set, or if the expected stack growth is unlimited:
  172. */
  173. if (mmap_is_legacy(rlim_stack)) {
  174. mm->mmap_base = mmap_base_legacy(random_factor);
  175. mm->get_unmapped_area = arch_get_unmapped_area;
  176. } else {
  177. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  178. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  179. }
  180. }