2015-10-12 23:52:58 +08:00
|
|
|
/*
|
|
|
|
* This file contains kasan initialization code for ARM64.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
|
|
|
|
* Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "kasan: " fmt
|
|
|
|
#include <linux/kasan.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/memblock.h>
|
|
|
|
#include <linux/start_kernel.h>
|
|
|
|
|
2016-01-25 19:45:02 +08:00
|
|
|
#include <asm/mmu_context.h>
|
2016-02-16 20:52:40 +08:00
|
|
|
#include <asm/kernel-pgtable.h>
|
2015-10-12 23:52:58 +08:00
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/pgtable.h>
|
2016-02-16 20:52:40 +08:00
|
|
|
#include <asm/sections.h>
|
2015-10-12 23:52:58 +08:00
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
|
|
|
|
static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
|
|
|
|
|
|
|
|
static void __init kasan_early_pte_populate(pmd_t *pmd, unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pte_t *pte;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (pmd_none(*pmd))
|
|
|
|
pmd_populate_kernel(&init_mm, pmd, kasan_zero_pte);
|
|
|
|
|
2016-02-16 20:52:40 +08:00
|
|
|
pte = pte_offset_kimg(pmd, addr);
|
2015-10-12 23:52:58 +08:00
|
|
|
do {
|
|
|
|
next = addr + PAGE_SIZE;
|
|
|
|
set_pte(pte, pfn_pte(virt_to_pfn(kasan_zero_page),
|
|
|
|
PAGE_KERNEL));
|
|
|
|
} while (pte++, addr = next, addr != end && pte_none(*pte));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_pmd_populate(pud_t *pud,
|
|
|
|
unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pmd_t *pmd;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (pud_none(*pud))
|
|
|
|
pud_populate(&init_mm, pud, kasan_zero_pmd);
|
|
|
|
|
2016-02-16 20:52:40 +08:00
|
|
|
pmd = pmd_offset_kimg(pud, addr);
|
2015-10-12 23:52:58 +08:00
|
|
|
do {
|
|
|
|
next = pmd_addr_end(addr, end);
|
|
|
|
kasan_early_pte_populate(pmd, addr, next);
|
|
|
|
} while (pmd++, addr = next, addr != end && pmd_none(*pmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_early_pud_populate(pgd_t *pgd,
|
|
|
|
unsigned long addr,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
pud_t *pud;
|
|
|
|
unsigned long next;
|
|
|
|
|
|
|
|
if (pgd_none(*pgd))
|
|
|
|
pgd_populate(&init_mm, pgd, kasan_zero_pud);
|
|
|
|
|
2016-02-16 20:52:40 +08:00
|
|
|
pud = pud_offset_kimg(pgd, addr);
|
2015-10-12 23:52:58 +08:00
|
|
|
do {
|
|
|
|
next = pud_addr_end(addr, end);
|
|
|
|
kasan_early_pmd_populate(pud, addr, next);
|
|
|
|
} while (pud++, addr = next, addr != end && pud_none(*pud));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kasan_map_early_shadow(void)
|
|
|
|
{
|
|
|
|
unsigned long addr = KASAN_SHADOW_START;
|
|
|
|
unsigned long end = KASAN_SHADOW_END;
|
|
|
|
unsigned long next;
|
|
|
|
pgd_t *pgd;
|
|
|
|
|
|
|
|
pgd = pgd_offset_k(addr);
|
|
|
|
do {
|
|
|
|
next = pgd_addr_end(addr, end);
|
|
|
|
kasan_early_pud_populate(pgd, addr, next);
|
|
|
|
} while (pgd++, addr = next, addr != end);
|
|
|
|
}
|
|
|
|
|
2015-10-13 21:01:06 +08:00
|
|
|
asmlinkage void __init kasan_early_init(void)
|
2015-10-12 23:52:58 +08:00
|
|
|
{
|
|
|
|
BUILD_BUG_ON(KASAN_SHADOW_OFFSET != KASAN_SHADOW_END - (1UL << 61));
|
|
|
|
BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
|
|
|
|
BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
|
|
|
|
kasan_map_early_shadow();
|
|
|
|
}
|
|
|
|
|
arm64: mm: create new fine-grained mappings at boot
At boot we may change the granularity of the tables mapping the kernel
(by splitting or making sections). This may happen when we create the
linear mapping (in __map_memblock), or at any point we try to apply
fine-grained permissions to the kernel (e.g. fixup_executable,
mark_rodata_ro, fixup_init).
Changing the active page tables in this manner may result in multiple
entries for the same address being allocated into TLBs, risking problems
such as TLB conflict aborts or issues derived from the amalgamation of
TLB entries. Generally, a break-before-make (BBM) approach is necessary
to avoid conflicts, but we cannot do this for the kernel tables as it
risks unmapping text or data being used to do so.
Instead, we can create a new set of tables from scratch in the safety of
the existing mappings, and subsequently migrate over to these using the
new cpu_replace_ttbr1 helper, which avoids the two sets of tables being
active simultaneously.
To avoid issues when we later modify permissions of the page tables
(e.g. in fixup_init), we must create the page tables at a granularity
such that later modification does not result in splitting of tables.
This patch applies this strategy, creating a new set of fine-grained
page tables from scratch, and safely migrating to them. The existing
fixmap and kasan shadow page tables are reused in the new fine-grained
tables.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Tested-by: Jeremy Linton <jeremy.linton@arm.com>
Cc: Laura Abbott <labbott@fedoraproject.org>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-01-25 19:45:12 +08:00
|
|
|
/*
|
|
|
|
* Copy the current shadow region into a new pgdir.
|
|
|
|
*/
|
|
|
|
void __init kasan_copy_shadow(pgd_t *pgdir)
|
|
|
|
{
|
|
|
|
pgd_t *pgd, *pgd_new, *pgd_end;
|
|
|
|
|
|
|
|
pgd = pgd_offset_k(KASAN_SHADOW_START);
|
|
|
|
pgd_end = pgd_offset_k(KASAN_SHADOW_END);
|
|
|
|
pgd_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
|
|
|
|
do {
|
|
|
|
set_pgd(pgd_new, *pgd);
|
|
|
|
} while (pgd++, pgd_new++, pgd != pgd_end);
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:52:58 +08:00
|
|
|
static void __init clear_pgds(unsigned long start,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Remove references to kasan page tables from
|
|
|
|
* swapper_pg_dir. pgd_clear() can't be used
|
|
|
|
* here because it's nop on 2,3-level pagetable setups
|
|
|
|
*/
|
|
|
|
for (; start < end; start += PGDIR_SIZE)
|
|
|
|
set_pgd(pgd_offset_k(start), __pgd(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init kasan_init(void)
|
|
|
|
{
|
2016-02-16 20:52:40 +08:00
|
|
|
u64 kimg_shadow_start, kimg_shadow_end;
|
2015-10-12 23:52:58 +08:00
|
|
|
struct memblock_region *reg;
|
2016-01-11 21:50:21 +08:00
|
|
|
int i;
|
2015-10-12 23:52:58 +08:00
|
|
|
|
2016-02-16 20:52:40 +08:00
|
|
|
kimg_shadow_start = (u64)kasan_mem_to_shadow(_text);
|
|
|
|
kimg_shadow_end = (u64)kasan_mem_to_shadow(_end);
|
|
|
|
|
2015-10-12 23:52:58 +08:00
|
|
|
/*
|
|
|
|
* We are going to perform proper setup of shadow memory.
|
|
|
|
* At first we should unmap early shadow (clear_pgds() call bellow).
|
|
|
|
* However, instrumented code couldn't execute without shadow memory.
|
|
|
|
* tmp_pg_dir used to keep early shadow mapped until full shadow
|
|
|
|
* setup will be finished.
|
|
|
|
*/
|
|
|
|
memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
|
2016-01-25 19:45:02 +08:00
|
|
|
dsb(ishst);
|
|
|
|
cpu_replace_ttbr1(tmp_pg_dir);
|
2015-10-12 23:52:58 +08:00
|
|
|
|
|
|
|
clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
|
|
|
|
|
2016-02-16 20:52:40 +08:00
|
|
|
vmemmap_populate(kimg_shadow_start, kimg_shadow_end, NUMA_NO_NODE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmemmap_populate() has populated the shadow region that covers the
|
|
|
|
* kernel image with SWAPPER_BLOCK_SIZE mappings, so we have to round
|
|
|
|
* the start and end addresses to SWAPPER_BLOCK_SIZE as well, to prevent
|
|
|
|
* kasan_populate_zero_shadow() from replacing the PMD block mappings
|
|
|
|
* with PMD table mappings at the edges of the shadow region for the
|
|
|
|
* kernel image.
|
|
|
|
*/
|
|
|
|
if (ARM64_SWAPPER_USES_SECTION_MAPS)
|
|
|
|
kimg_shadow_end = round_up(kimg_shadow_end, SWAPPER_BLOCK_SIZE);
|
|
|
|
|
2015-10-12 23:52:58 +08:00
|
|
|
kasan_populate_zero_shadow((void *)KASAN_SHADOW_START,
|
|
|
|
kasan_mem_to_shadow((void *)MODULES_VADDR));
|
2016-02-16 20:52:40 +08:00
|
|
|
kasan_populate_zero_shadow((void *)kimg_shadow_end,
|
|
|
|
kasan_mem_to_shadow((void *)PAGE_OFFSET));
|
2015-10-12 23:52:58 +08:00
|
|
|
|
|
|
|
for_each_memblock(memory, reg) {
|
|
|
|
void *start = (void *)__phys_to_virt(reg->base);
|
|
|
|
void *end = (void *)__phys_to_virt(reg->base + reg->size);
|
|
|
|
|
|
|
|
if (start >= end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* end + 1 here is intentional. We check several shadow bytes in
|
|
|
|
* advance to slightly speed up fastpath. In some rare cases
|
|
|
|
* we could cross boundary of mapped shadow, so we just map
|
|
|
|
* some more here.
|
|
|
|
*/
|
|
|
|
vmemmap_populate((unsigned long)kasan_mem_to_shadow(start),
|
|
|
|
(unsigned long)kasan_mem_to_shadow(end) + 1,
|
|
|
|
pfn_to_nid(virt_to_pfn(start)));
|
|
|
|
}
|
|
|
|
|
2016-01-11 21:50:21 +08:00
|
|
|
/*
|
|
|
|
* KAsan may reuse the contents of kasan_zero_pte directly, so we
|
|
|
|
* should make sure that it maps the zero page read-only.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < PTRS_PER_PTE; i++)
|
|
|
|
set_pte(&kasan_zero_pte[i],
|
|
|
|
pfn_pte(virt_to_pfn(kasan_zero_page), PAGE_KERNEL_RO));
|
|
|
|
|
2015-10-12 23:52:58 +08:00
|
|
|
memset(kasan_zero_page, 0, PAGE_SIZE);
|
2016-01-25 19:45:02 +08:00
|
|
|
cpu_replace_ttbr1(swapper_pg_dir);
|
2015-10-12 23:52:58 +08:00
|
|
|
|
|
|
|
/* At this point kasan is fully initialized. Enable error messages */
|
|
|
|
init_task.kasan_depth = 0;
|
|
|
|
pr_info("KernelAddressSanitizer initialized\n");
|
|
|
|
}
|