History

mm, fs: move randomize_stack_top from fs to mm

Patch series "Provide generic top-down mmap layout functions", v6. This series introduces generic functions to make top-down mmap layout easily accessible to architectures, in particular riscv which was the initial goal of this series. The generic implementation was taken from arm64 and used successively by arm, mips and finally riscv. Note that in addition the series fixes 2 issues: - stack randomization was taken into account even if not necessary. - [1] fixed an issue with mmap base which did not take into account randomization but did not report it to arm and mips, so by moving arm64 into a generic library, this problem is now fixed for both architectures. This work is an effort to factorize architecture functions to avoid code duplication and oversights as in [1]. [1]: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html This patch (of 14): This preparatory commit moves this function so that further introduction of generic topdown mmap layout is contained only in mm/util.c.

Alexandre Ghiti committed 7 years ago

Commit 649775b1 parent f385cb83 files · +24 −20Browse files

Unified · Split

3 files
  1. fs/binfmt_elf.c +0 −20
  2. include/linux/mm.h +2 −0
  3. mm/util.c +22 −0
fs/binfmt_elf.c+0−20
@@ -670,26 +670,6 @@ out: · 669 unmodified lines
670670 * libraries. There is no binary dependent code anywhere else.
671671 */
672672
673−#ifndef STACK_RND_MASK
674−#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
675−#endif
676−
677−static unsigned long randomize_stack_top(unsigned long stack_top)
678−{
679− unsigned long random_variable = 0;
680−
681− if (current->flags & PF_RANDOMIZE) {
682− random_variable = get_random_long();
683− random_variable &= STACK_RND_MASK;
684− random_variable <<= PAGE_SHIFT;
685− }
686−#ifdef CONFIG_STACK_GROWSUP
687− return PAGE_ALIGN(stack_top) + random_variable;
688−#else
689− return PAGE_ALIGN(stack_top) - random_variable;
690−#endif
691−}
692−
693673 static int load_elf_binary(struct linux_binprm *bprm)
694674 {
695675 struct file *interpreter = NULL; /* to shut gcc up */
include/linux/mm.h+2−0
@@ -2328,6 +2328,8 @@ extern int install_special_mapping(struct mm_struct *mm, · 2327 unmodified lines
23282328 unsigned long addr, unsigned long len,
23292329 unsigned long flags, struct page **pages);
23302330
2331+unsigned long randomize_stack_top(unsigned long stack_top);
2332+
23312333 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
23322334
23332335 extern unsigned long mmap_region(struct file *file, unsigned long addr,
mm/util.c+22−0
@@ -16,6 +16,8 @@ · 15 unmodified lines
1616 #include <linux/hugetlb.h>
1717 #include <linux/vmalloc.h>
1818 #include <linux/userfaultfd_k.h>
19+#include <linux/elf.h>
20+#include <linux/random.h>
1921
2022 #include <linux/uaccess.h>
2123
@@ -293,6 +295,26 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) · 271 unmodified lines
293295 return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
294296 }
295297
298+#ifndef STACK_RND_MASK
299+#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
300+#endif
301+
302+unsigned long randomize_stack_top(unsigned long stack_top)
303+{
304+ unsigned long random_variable = 0;
305+
306+ if (current->flags & PF_RANDOMIZE) {
307+ random_variable = get_random_long();
308+ random_variable &= STACK_RND_MASK;
309+ random_variable <<= PAGE_SHIFT;
310+ }
311+#ifdef CONFIG_STACK_GROWSUP
312+ return PAGE_ALIGN(stack_top) + random_variable;
313+#else
314+ return PAGE_ALIGN(stack_top) - random_variable;
315+#endif
316+}
317+
296318 #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
297319 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
298320 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t