util.c (649775be63c8b2e0b56ecc5bbc96d38205ec5259) util.c (67f3977f805b34cf0e41090679800d2091d41d49)
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/mm.h>
3#include <linux/slab.h>
4#include <linux/string.h>
5#include <linux/compiler.h>
6#include <linux/export.h>
7#include <linux/err.h>
8#include <linux/sched.h>
9#include <linux/sched/mm.h>
10#include <linux/sched/signal.h>
11#include <linux/sched/task_stack.h>
12#include <linux/security.h>
13#include <linux/swap.h>
14#include <linux/swapops.h>
15#include <linux/mman.h>
16#include <linux/hugetlb.h>
17#include <linux/vmalloc.h>
18#include <linux/userfaultfd_k.h>
19#include <linux/elf.h>
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/mm.h>
3#include <linux/slab.h>
4#include <linux/string.h>
5#include <linux/compiler.h>
6#include <linux/export.h>
7#include <linux/err.h>
8#include <linux/sched.h>
9#include <linux/sched/mm.h>
10#include <linux/sched/signal.h>
11#include <linux/sched/task_stack.h>
12#include <linux/security.h>
13#include <linux/swap.h>
14#include <linux/swapops.h>
15#include <linux/mman.h>
16#include <linux/hugetlb.h>
17#include <linux/vmalloc.h>
18#include <linux/userfaultfd_k.h>
19#include <linux/elf.h>
20#include <linux/elf-randomize.h>
21#include <linux/personality.h>
20#include <linux/random.h>
22#include <linux/random.h>
23#include <linux/processor.h>
24#include <linux/sizes.h>
25#include <linux/compat.h>
21
22#include <linux/uaccess.h>
23
24#include "internal.h"
25
26/**
27 * kfree_const - conditionally free memory
28 * @x: pointer to the memory

--- 281 unchanged lines hidden (view full) ---

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
26
27#include <linux/uaccess.h>
28
29#include "internal.h"
30
31/**
32 * kfree_const - conditionally free memory
33 * @x: pointer to the memory

--- 281 unchanged lines hidden (view full) ---

315 }
316#ifdef CONFIG_STACK_GROWSUP
317 return PAGE_ALIGN(stack_top) + random_variable;
318#else
319 return PAGE_ALIGN(stack_top) - random_variable;
320#endif
321}
322
318#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
323#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
324#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE
325unsigned long arch_mmap_rnd(void)
326{
327 unsigned long rnd;
328
329#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
330 if (is_compat_task())
331 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
332 else
333#endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */
334 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
335
336 return rnd << PAGE_SHIFT;
337}
338#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */
339
340static int mmap_is_legacy(struct rlimit *rlim_stack)
341{
342 if (current->personality & ADDR_COMPAT_LAYOUT)
343 return 1;
344
345 if (rlim_stack->rlim_cur == RLIM_INFINITY)
346 return 1;
347
348 return sysctl_legacy_va_layout;
349}
350
351/*
352 * Leave enough space between the mmap area and the stack to honour ulimit in
353 * the face of randomisation.
354 */
355#define MIN_GAP (SZ_128M)
356#define MAX_GAP (STACK_TOP / 6 * 5)
357
358static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
359{
360 unsigned long gap = rlim_stack->rlim_cur;
361 unsigned long pad = stack_guard_gap;
362
363 /* Account for stack randomization if necessary */
364 if (current->flags & PF_RANDOMIZE)
365 pad += (STACK_RND_MASK << PAGE_SHIFT);
366
367 /* Values close to RLIM_INFINITY can overflow. */
368 if (gap + pad > gap)
369 gap += pad;
370
371 if (gap < MIN_GAP)
372 gap = MIN_GAP;
373 else if (gap > MAX_GAP)
374 gap = MAX_GAP;
375
376 return PAGE_ALIGN(STACK_TOP - gap - rnd);
377}
378
319void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
320{
379void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
380{
381 unsigned long random_factor = 0UL;
382
383 if (current->flags & PF_RANDOMIZE)
384 random_factor = arch_mmap_rnd();
385
386 if (mmap_is_legacy(rlim_stack)) {
387 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
388 mm->get_unmapped_area = arch_get_unmapped_area;
389 } else {
390 mm->mmap_base = mmap_base(random_factor, rlim_stack);
391 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
392 }
393}
394#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
395void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
396{
321 mm->mmap_base = TASK_UNMAPPED_BASE;
322 mm->get_unmapped_area = arch_get_unmapped_area;
323}
324#endif
325
326/**
327 * __account_locked_vm - account locked pages to an mm's locked_vm
328 * @mm: mm to account against

--- 492 unchanged lines hidden ---
397 mm->mmap_base = TASK_UNMAPPED_BASE;
398 mm->get_unmapped_area = arch_get_unmapped_area;
399}
400#endif
401
402/**
403 * __account_locked_vm - account locked pages to an mm's locked_vm
404 * @mm: mm to account against

--- 492 unchanged lines hidden ---