mmap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Based on arch/arm/mm/mmap.c
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/elf.h>
  19. #include <linux/fs.h>
  20. #include <linux/memblock.h>
  21. #include <linux/mm.h>
  22. #include <linux/mman.h>
  23. #include <linux/export.h>
  24. #include <linux/shm.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/sched/mm.h>
  27. #include <linux/io.h>
  28. #include <linux/personality.h>
  29. #include <linux/random.h>
  30. #include <asm/cputype.h>
  31. /*
  32. * Leave enough space between the mmap area and the stack to honour ulimit in
  33. * the face of randomisation.
  34. */
  35. #define MIN_GAP (SZ_128M)
  36. #define MAX_GAP (STACK_TOP/6*5)
  37. static int mmap_is_legacy(struct rlimit *rlim_stack)
  38. {
  39. if (current->personality & ADDR_COMPAT_LAYOUT)
  40. return 1;
  41. if (rlim_stack->rlim_cur == RLIM_INFINITY)
  42. return 1;
  43. return sysctl_legacy_va_layout;
  44. }
  45. unsigned long arch_mmap_rnd(void)
  46. {
  47. unsigned long rnd;
  48. #ifdef CONFIG_COMPAT
  49. if (test_thread_flag(TIF_32BIT))
  50. rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
  51. else
  52. #endif
  53. rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
  54. return rnd << PAGE_SHIFT;
  55. }
  56. static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
  57. {
  58. unsigned long gap = rlim_stack->rlim_cur;
  59. unsigned long pad = stack_guard_gap;
  60. /* Account for stack randomization if necessary */
  61. if (current->flags & PF_RANDOMIZE)
  62. pad += (STACK_RND_MASK << PAGE_SHIFT);
  63. /* Values close to RLIM_INFINITY can overflow. */
  64. if (gap + pad > gap)
  65. gap += pad;
  66. if (gap < MIN_GAP)
  67. gap = MIN_GAP;
  68. else if (gap > MAX_GAP)
  69. gap = MAX_GAP;
  70. return PAGE_ALIGN(STACK_TOP - gap - rnd);
  71. }
  72. /*
  73. * This function, called very early during the creation of a new process VM
  74. * image, sets up which VM layout function to use:
  75. */
  76. void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
  77. {
  78. unsigned long random_factor = 0UL;
  79. if (current->flags & PF_RANDOMIZE)
  80. random_factor = arch_mmap_rnd();
  81. /*
  82. * Fall back to the standard layout if the personality bit is set, or
  83. * if the expected stack growth is unlimited:
  84. */
  85. if (mmap_is_legacy(rlim_stack)) {
  86. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  87. mm->get_unmapped_area = arch_get_unmapped_area;
  88. } else {
  89. mm->mmap_base = mmap_base(random_factor, rlim_stack);
  90. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  91. }
  92. }
  93. /*
  94. * You really shouldn't be using read() or write() on /dev/mem. This might go
  95. * away in the future.
  96. */
  97. int valid_phys_addr_range(phys_addr_t addr, size_t size)
  98. {
  99. /*
  100. * Check whether addr is covered by a memory region without the
  101. * MEMBLOCK_NOMAP attribute, and whether that region covers the
  102. * entire range. In theory, this could lead to false negatives
  103. * if the range is covered by distinct but adjacent memory regions
  104. * that only differ in other attributes. However, few of such
  105. * attributes have been defined, and it is debatable whether it
  106. * follows that /dev/mem read() calls should be able traverse
  107. * such boundaries.
  108. */
  109. return memblock_is_region_memory(addr, size) &&
  110. memblock_is_map_memory(addr);
  111. }
  112. /*
  113. * Do not allow /dev/mem mappings beyond the supported physical range.
  114. */
  115. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  116. {
  117. return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
  118. }
  119. #ifdef CONFIG_STRICT_DEVMEM
  120. #include <linux/ioport.h>
  121. /*
  122. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  123. * is valid. The argument is a physical page number. We mimic x86 here by
  124. * disallowing access to system RAM as well as device-exclusive MMIO regions.
  125. * This effectively disable read()/write() on /dev/mem.
  126. */
  127. int devmem_is_allowed(unsigned long pfn)
  128. {
  129. if (iomem_is_exclusive(pfn << PAGE_SHIFT))
  130. return 0;
  131. if (!page_is_ram(pfn))
  132. return 1;
  133. return 0;
  134. }
  135. #endif