// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Flexible mmap layout support3*4* Based on code by Ingo Molnar and Andi Kleen, copyrighted5* as follows:6*7* Copyright 2003-2009 Red Hat Inc.8* All Rights Reserved.9* Copyright 2005 Andi Kleen, SUSE Labs.10* Copyright 2007 Jiri Kosina, SUSE Labs.11*/1213#include <linux/personality.h>14#include <linux/mm.h>15#include <linux/random.h>16#include <linux/limits.h>17#include <linux/sched/signal.h>18#include <linux/sched/mm.h>19#include <linux/compat.h>20#include <linux/elf-randomize.h>21#include <asm/elf.h>22#include <asm/io.h>2324#include "physaddr.h"2526struct va_alignment __read_mostly va_align = {27.flags = -1,28};2930unsigned long task_size_32bit(void)31{32return IA32_PAGE_OFFSET;33}3435unsigned long task_size_64bit(int full_addr_space)36{37return full_addr_space ? TASK_SIZE_MAX : DEFAULT_MAP_WINDOW;38}3940static unsigned long stack_maxrandom_size(unsigned long task_size)41{42unsigned long max = 0;43if (current->flags & PF_RANDOMIZE) {44max = (-1UL) & __STACK_RND_MASK(task_size == task_size_32bit());45max <<= PAGE_SHIFT;46}4748return max;49}5051#ifdef CONFIG_COMPAT52# define mmap32_rnd_bits mmap_rnd_compat_bits53# define mmap64_rnd_bits mmap_rnd_bits54#else55# define mmap32_rnd_bits mmap_rnd_bits56# define mmap64_rnd_bits mmap_rnd_bits57#endif5859#define SIZE_128M (128 * 1024 * 1024UL)6061static int mmap_is_legacy(void)62{63if (current->personality & ADDR_COMPAT_LAYOUT)64return 1;6566return sysctl_legacy_va_layout;67}6869static unsigned long arch_rnd(unsigned int rndbits)70{71if (!(current->flags & PF_RANDOMIZE))72return 0;73return (get_random_long() & ((1UL << rndbits) - 1)) << PAGE_SHIFT;74}7576unsigned long arch_mmap_rnd(void)77{78return arch_rnd(mmap_is_ia32() ? mmap32_rnd_bits : mmap64_rnd_bits);79}8081static unsigned long mmap_base(unsigned long rnd, unsigned long task_size,82struct rlimit *rlim_stack)83{84unsigned long gap = rlim_stack->rlim_cur;85unsigned long pad = stack_maxrandom_size(task_size) + stack_guard_gap;8687/* Values close to RLIM_INFINITY can overflow. */88if (gap + pad > gap)89gap += pad;9091/*92* Top of mmap area (just below the process stack).93* Leave an at least ~128 MB hole with possible stack randomization.94*/95gap = clamp(gap, SIZE_128M, (task_size / 6) * 5);9697return PAGE_ALIGN(task_size - gap - rnd);98}99100static unsigned long mmap_legacy_base(unsigned long rnd,101unsigned long task_size)102{103return __TASK_UNMAPPED_BASE(task_size) + rnd;104}105106/*107* This function, called very early during the creation of a new108* process VM image, sets up which VM layout function to use:109*/110static void arch_pick_mmap_base(unsigned long *base, unsigned long *legacy_base,111unsigned long random_factor, unsigned long task_size,112struct rlimit *rlim_stack)113{114*legacy_base = mmap_legacy_base(random_factor, task_size);115if (mmap_is_legacy())116*base = *legacy_base;117else118*base = mmap_base(random_factor, task_size, rlim_stack);119}120121void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)122{123if (mmap_is_legacy())124clear_bit(MMF_TOPDOWN, &mm->flags);125else126set_bit(MMF_TOPDOWN, &mm->flags);127128arch_pick_mmap_base(&mm->mmap_base, &mm->mmap_legacy_base,129arch_rnd(mmap64_rnd_bits), task_size_64bit(0),130rlim_stack);131132#ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES133/*134* The mmap syscall mapping base decision depends solely on the135* syscall type (64-bit or compat). This applies for 64bit136* applications and 32bit applications. The 64bit syscall uses137* mmap_base, the compat syscall uses mmap_compat_base.138*/139arch_pick_mmap_base(&mm->mmap_compat_base, &mm->mmap_compat_legacy_base,140arch_rnd(mmap32_rnd_bits), task_size_32bit(),141rlim_stack);142#endif143}144145unsigned long get_mmap_base(int is_legacy)146{147struct mm_struct *mm = current->mm;148149#ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES150if (in_32bit_syscall()) {151return is_legacy ? mm->mmap_compat_legacy_base152: mm->mmap_compat_base;153}154#endif155return is_legacy ? mm->mmap_legacy_base : mm->mmap_base;156}157158/**159* mmap_address_hint_valid - Validate the address hint of mmap160* @addr: Address hint161* @len: Mapping length162*163* Check whether @addr and @addr + @len result in a valid mapping.164*165* On 32bit this only checks whether @addr + @len is <= TASK_SIZE.166*167* On 64bit with 5-level page tables another sanity check is required168* because mappings requested by mmap(@addr, 0) which cross the 47-bit169* virtual address boundary can cause the following theoretical issue:170*171* An application calls mmap(addr, 0), i.e. without MAP_FIXED, where @addr172* is below the border of the 47-bit address space and @addr + @len is173* above the border.174*175* With 4-level paging this request succeeds, but the resulting mapping176* address will always be within the 47-bit virtual address space, because177* the hint address does not result in a valid mapping and is178* ignored. Hence applications which are not prepared to handle virtual179* addresses above 47-bit work correctly.180*181* With 5-level paging this request would be granted and result in a182* mapping which crosses the border of the 47-bit virtual address183* space. If the application cannot handle addresses above 47-bit this184* will lead to misbehaviour and hard to diagnose failures.185*186* Therefore ignore address hints which would result in a mapping crossing187* the 47-bit virtual address boundary.188*189* Note, that in the same scenario with MAP_FIXED the behaviour is190* different. The request with @addr < 47-bit and @addr + @len > 47-bit191* fails on a 4-level paging machine but succeeds on a 5-level paging192* machine. It is reasonable to expect that an application does not rely on193* the failure of such a fixed mapping request, so the restriction is not194* applied.195*/196bool mmap_address_hint_valid(unsigned long addr, unsigned long len)197{198if (TASK_SIZE - len < addr)199return false;200201return (addr > DEFAULT_MAP_WINDOW) == (addr + len > DEFAULT_MAP_WINDOW);202}203204/* Can we access it for direct reading/writing? Must be RAM: */205int valid_phys_addr_range(phys_addr_t addr, size_t count)206{207return addr + count - 1 <= __pa(high_memory - 1);208}209210/* Can we access it through mmap? Must be a valid physical address: */211int valid_mmap_phys_addr_range(unsigned long pfn, size_t count)212{213phys_addr_t addr = (phys_addr_t)pfn << PAGE_SHIFT;214215return phys_addr_valid(addr + count - 1);216}217218/*219* Only allow root to set high MMIO mappings to PROT_NONE.220* This prevents an unpriv. user to set them to PROT_NONE and invert221* them, then pointing to valid memory for L1TF speculation.222*223* Note: for locked down kernels may want to disable the root override.224*/225bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot)226{227if (!boot_cpu_has_bug(X86_BUG_L1TF))228return true;229if (!__pte_needs_invert(pgprot_val(prot)))230return true;231/* If it's real memory always allow */232if (pfn_valid(pfn))233return true;234if (pfn >= l1tf_pfn_limit() && !capable(CAP_SYS_ADMIN))235return false;236return true;237}238239240