mmap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011 Wind River Systems,
  7. * written by Ralf Baechle <ralf@linux-mips.org>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/elf-randomize.h>
  11. #include <linux/errno.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/export.h>
  15. #include <linux/personality.h>
  16. #include <linux/random.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/sched/mm.h>
  19. unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
  20. EXPORT_SYMBOL(shm_align_mask);
  21. /* gap between mmap and stack */
  22. #define MIN_GAP (128*1024*1024UL)
  23. #define MAX_GAP ((TASK_SIZE)/6*5)
  24. #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12))
  25. static int mmap_is_legacy(struct rlimit *rlim_stack)
  26. {
  27. if (current->personality & ADDR_COMPAT_LAYOUT)
  28. return 1;
  29. if (rlim_stack->rlim_cur == RLIM_INFINITY)
  30. return 1;
  31. return sysctl_legacy_va_layout;
  32. }
  33. static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
  34. {
  35. unsigned long gap = rlim_stack->rlim_cur;
  36. unsigned long pad = stack_guard_gap;
  37. /* Account for stack randomization if necessary */
  38. if (current->flags & PF_RANDOMIZE)
  39. pad += (STACK_RND_MASK << PAGE_SHIFT);
  40. /* Values close to RLIM_INFINITY can overflow. */
  41. if (gap + pad > gap)
  42. gap += pad;
  43. if (gap < MIN_GAP)
  44. gap = MIN_GAP;
  45. else if (gap > MAX_GAP)
  46. gap = MAX_GAP;
  47. return PAGE_ALIGN(TASK_SIZE - gap - rnd);
  48. }
  49. #define COLOUR_ALIGN(addr, pgoff) \
  50. ((((addr) + shm_align_mask) & ~shm_align_mask) + \
  51. (((pgoff) << PAGE_SHIFT) & shm_align_mask))
  52. enum mmap_allocation_direction {UP, DOWN};
  53. static unsigned long arch_get_unmapped_area_common(struct file *filp,
  54. unsigned long addr0, unsigned long len, unsigned long pgoff,
  55. unsigned long flags, enum mmap_allocation_direction dir)
  56. {
  57. struct mm_struct *mm = current->mm;
  58. struct vm_area_struct *vma;
  59. unsigned long addr = addr0;
  60. int do_color_align;
  61. struct vm_unmapped_area_info info;
  62. if (unlikely(len > TASK_SIZE))
  63. return -ENOMEM;
  64. if (flags & MAP_FIXED) {
  65. /* Even MAP_FIXED mappings must reside within TASK_SIZE */
  66. if (TASK_SIZE - len < addr)
  67. return -EINVAL;
  68. /*
  69. * We do not accept a shared mapping if it would violate
  70. * cache aliasing constraints.
  71. */
  72. if ((flags & MAP_SHARED) &&
  73. ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
  74. return -EINVAL;
  75. return addr;
  76. }
  77. do_color_align = 0;
  78. if (filp || (flags & MAP_SHARED))
  79. do_color_align = 1;
  80. /* requesting a specific address */
  81. if (addr) {
  82. if (do_color_align)
  83. addr = COLOUR_ALIGN(addr, pgoff);
  84. else
  85. addr = PAGE_ALIGN(addr);
  86. vma = find_vma(mm, addr);
  87. if (TASK_SIZE - len >= addr &&
  88. (!vma || addr + len <= vm_start_gap(vma)))
  89. return addr;
  90. }
  91. info.length = len;
  92. info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
  93. info.align_offset = pgoff << PAGE_SHIFT;
  94. if (dir == DOWN) {
  95. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  96. info.low_limit = PAGE_SIZE;
  97. info.high_limit = mm->mmap_base;
  98. addr = vm_unmapped_area(&info);
  99. if (!(addr & ~PAGE_MASK))
  100. return addr;
  101. /*
  102. * A failed mmap() very likely causes application failure,
  103. * so fall back to the bottom-up function here. This scenario
  104. * can happen with large stack limits and large mmap()
  105. * allocations.
  106. */
  107. }
  108. info.flags = 0;
  109. info.low_limit = mm->mmap_base;
  110. info.high_limit = TASK_SIZE;
  111. return vm_unmapped_area(&info);
  112. }
  113. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
  114. unsigned long len, unsigned long pgoff, unsigned long flags)
  115. {
  116. return arch_get_unmapped_area_common(filp,
  117. addr0, len, pgoff, flags, UP);
  118. }
  119. /*
  120. * There is no need to export this but sched.h declares the function as
  121. * extern so making it static here results in an error.
  122. */
  123. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  124. unsigned long addr0, unsigned long len, unsigned long pgoff,
  125. unsigned long flags)
  126. {
  127. return arch_get_unmapped_area_common(filp,
  128. addr0, len, pgoff, flags, DOWN);
  129. }
  130. unsigned long arch_mmap_rnd(void)
  131. {
  132. unsigned long rnd;
  133. #ifdef CONFIG_COMPAT
  134. if (TASK_IS_32BIT_ADDR)
  135. rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
  136. else
  137. #endif /* CONFIG_COMPAT */
  138. rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
  139. return rnd << PAGE_SHIFT;
  140. }
  141. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  142. {
  143. unsigned long random_factor = 0UL;
  144. if (current->flags & PF_RANDOMIZE)
  145. random_factor = arch_mmap_rnd();
  146. if (mmap_is_legacy(rlim_stack)) {
  147. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  148. mm->get_unmapped_area = arch_get_unmapped_area;
  149. } else {
  150. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  151. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  152. }
  153. }
  154. static inline unsigned long brk_rnd(void)
  155. {
  156. unsigned long rnd = get_random_long();
  157. rnd = rnd << PAGE_SHIFT;
  158. /* 8MB for 32bit, 256MB for 64bit */
  159. if (TASK_IS_32BIT_ADDR)
  160. rnd = rnd & 0x7ffffful;
  161. else
  162. rnd = rnd & 0xffffffful;
  163. return rnd;
  164. }
  165. unsigned long arch_randomize_brk(struct mm_struct *mm)
  166. {
  167. unsigned long base = mm->brk;
  168. unsigned long ret;
  169. ret = PAGE_ALIGN(base + brk_rnd());
  170. if (ret < mm->brk)
  171. return mm->brk;
  172. return ret;
  173. }
  174. int __virt_addr_valid(const volatile void *kaddr)
  175. {
  176. unsigned long vaddr = (unsigned long)kaddr;
  177. if ((vaddr < PAGE_OFFSET) || (vaddr >= MAP_BASE))
  178. return 0;
  179. return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
  180. }
  181. EXPORT_SYMBOL_GPL(__virt_addr_valid);