Skip to content

Commit

Permalink
arm: enable KASAN
Browse files Browse the repository at this point in the history
  • Loading branch information
aryabinin committed Jul 22, 2015
1 parent 4bb5604 commit 0b54f17
Show file tree
Hide file tree
Showing 25 changed files with 412 additions and 12 deletions.
7 changes: 7 additions & 0 deletions arch/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ config ARM
select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32
select HAVE_ARCH_KASAN
select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32
select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
select HAVE_ARCH_TRACEHOOK
Expand Down Expand Up @@ -1480,6 +1481,12 @@ config PAGE_OFFSET
default 0x80000000 if VMSPLIT_2G
default 0xC0000000

config KASAN_SHADOW_OFFSET
hex
default 0x1f000000 if VMSPLIT_1G
default 0x5f000000 if VMSPLIT_2G
default 0x9f000000

config NR_CPUS
int "Maximum number of CPUs (2-32)"
range 2 32
Expand Down
1 change: 1 addition & 0 deletions arch/arm/boot/compressed/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ OBJS += hyp-stub.o
endif

GCOV_PROFILE := n
KASAN_SANITIZE := n

#
# Architecture dependencies
Expand Down
2 changes: 2 additions & 0 deletions arch/arm/boot/compressed/libfdt_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ typedef __be64 fdt64_t;
#define fdt64_to_cpu(x) be64_to_cpu(x)
#define cpu_to_fdt64(x) cpu_to_be64(x)

#undef memset

#endif
23 changes: 23 additions & 0 deletions arch/arm/include/asm/kasan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __ASM_KASAN_H
#define __ASM_KASAN_H

#ifdef CONFIG_KASAN

#include <asm/memory.h>
/*
* Compiler uses shadow offset assuming that addresses start
* from 0. Kernel addresses don't start from 0, so shadow
* for kernel really starts from 'compiler's shadow offset' +
* ('kernel address space start' >> KASAN_SHADOW_SCALE_SHIFT)
*/
#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET)

#define KASAN_SHADOW_END (KASAN_SHADOW_START + (1ULL << 29))

void kasan_init(void);

#else
static inline void kasan_init(void) { }
#endif

#endif
4 changes: 4 additions & 0 deletions arch/arm/include/asm/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
* TASK_SIZE - the maximum size of a user space task.
* TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area
*/
#ifndef CONFIG_KASAN
#define TASK_SIZE (UL(CONFIG_PAGE_OFFSET) - UL(SZ_16M))
#else
#define TASK_SIZE (UL(CONFIG_PAGE_OFFSET) - UL(SZ_16M) - UL(SZ_512M))
#endif
#define TASK_UNMAPPED_BASE ALIGN(TASK_SIZE / 3, SZ_16M)

/*
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/include/asm/pgalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
*/
#define pmd_alloc_one(mm,addr) ({ BUG(); ((pmd_t *)2); })
#define pmd_free(mm, pmd) do { } while (0)
#define pud_populate(mm,pmd,pte) BUG()
#define pud_populate(mm,pmd,pte) do { } while (0)

#endif /* CONFIG_ARM_LPAE */

Expand Down
22 changes: 22 additions & 0 deletions arch/arm/include/asm/proc-fns.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,29 @@ extern void cpu_resume(void);
pg &= ~(PTRS_PER_PGD*sizeof(pgd_t)-1); \
(pgd_t *)phys_to_virt(pg); \
})
#define cpu_set_ttbr0(val) \
do { \
u64 ttbr = val; \
__asm__("mcrr p15, 0, %0, %1, c2" \
: : "r" (ttbr), "r" (ttbr >> 32)); \
} while (0)

#else
#define cpu_get_ttbr(nr) \
({ \
unsigned long ttbr; \
__asm__("mrc p15, 0, %0, c2, c0, 0" \
: "=r" (ttbr)); \
ttbr; \
})

#define cpu_set_ttbr0(val) \
do { \
u64 ttbr = val; \
__asm__("mcr p15, 0, %0, c2, c0, 0" \
: : "r" (ttbr)); \
} while (0)

#define cpu_get_pgd() \
({ \
unsigned long pg; \
Expand Down
17 changes: 17 additions & 0 deletions arch/arm/include/asm/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ extern char * strchr(const char * s, int c);

#define __HAVE_ARCH_MEMCPY
extern void * memcpy(void *, const void *, __kernel_size_t);
extern void * __memcpy(void *, const void *, __kernel_size_t);

#define __HAVE_ARCH_MEMMOVE
extern void * memmove(void *, const void *, __kernel_size_t);
extern void * __memmove(void *, const void *, __kernel_size_t);

#define __HAVE_ARCH_MEMCHR
extern void * memchr(const void *, int, __kernel_size_t);

#define __HAVE_ARCH_MEMSET
extern void * memset(void *, int, __kernel_size_t);
extern void * __memset(void *, int, __kernel_size_t);

extern void __memzero(void *ptr, __kernel_size_t n);

#ifndef CONFIG_KASAN
#define memset(p,v,n) \
({ \
void *__p = (p); size_t __n = n; \
Expand All @@ -37,5 +41,18 @@ extern void __memzero(void *ptr, __kernel_size_t n);
} \
(__p); \
})
#endif
#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)

/*
* For files that not instrumented (e.g. mm/slub.c) we
* should use not instrumented version of mem* functions.
*/

#define memcpy(dst, src, len) __memcpy(dst, src, len)
#define memmove(dst, src, len) __memmove(dst, src, len)
#define memset(s, c, n) __memset(s, c, n)
#endif


#endif
4 changes: 4 additions & 0 deletions arch/arm/include/asm/thread_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include <asm/fpstate.h>
#include <asm/page.h>

#ifndef CONFIG_KASAN
#define THREAD_SIZE_ORDER 1
#else
#define THREAD_SIZE_ORDER 2
#endif
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
#define THREAD_START_SP (THREAD_SIZE - 8)

Expand Down
2 changes: 1 addition & 1 deletion arch/arm/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CFLAGS_REMOVE_patch.o = -pg
endif

CFLAGS_REMOVE_return_address.o = -pg

KASAN_SANITIZE_stacktrace.o := n
# Object file lists.

obj-y := elf.o entry-common.o irq.o opcodes.o \
Expand Down
3 changes: 3 additions & 0 deletions arch/arm/kernel/head-common.S
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ __mmap_switched:
str r2, [r6] @ Save atags pointer
cmp r7, #0
strne r0, [r7] @ Save control register values
#ifdef CONFIG_KASAN
b kasan_early_init
#endif
b start_kernel
ENDPROC(__mmap_switched)

Expand Down
8 changes: 7 additions & 1 deletion arch/arm/kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
#include <linux/module.h>
#include <linux/moduleloader.h>
#include <linux/kasan.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/elf.h>
Expand Down Expand Up @@ -40,9 +41,14 @@
#ifdef CONFIG_MMU
void *module_alloc(unsigned long size)
{
void *p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
void *p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
__builtin_return_address(0));
if (p && (kasan_module_alloc(p, size) < 0)) {
vfree(p);
return NULL;
}

if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p)
return p;
return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
Expand Down
4 changes: 3 additions & 1 deletion arch/arm/lib/memcpy.S
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@
.text

/* Prototype: void *memcpy(void *dest, const void *src, size_t n); */

ENTRY(mmiocpy)
.weak memcpy
ENTRY(__memcpy)
ENTRY(memcpy)

#include "copy_template.S"

ENDPROC(memcpy)
ENDPROC(mmiocpy)
ENDPROC(__memcpy)
8 changes: 5 additions & 3 deletions arch/arm/lib/memmove.S
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
* normally a bit faster. Otherwise the copy is done going downwards. This
* is a transposition of the code from copy_template.S but with the copy
* occurring in the opposite direction.
*/

*/
.weak memmove
ENTRY(__memmove)
ENTRY(memmove)
UNWIND( .fnstart )

subs ip, r0, r1
cmphi r2, ip
bls memcpy
bls __memcpy

stmfd sp!, {r0, r4, lr}
UNWIND( .fnend )
Expand Down Expand Up @@ -225,3 +226,4 @@ ENTRY(memmove)
18: backward_copy_shift push=24 pull=8

ENDPROC(memmove)
ENDPROC(__memmove)
3 changes: 3 additions & 0 deletions arch/arm/lib/memset.S
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
.align 5

ENTRY(mmioset)
.weak memset
ENTRY(__memset)
ENTRY(memset)
UNWIND( .fnstart )
ands r3, r0, #3 @ 1 unaligned?
Expand Down Expand Up @@ -135,3 +137,4 @@ UNWIND( .fnstart )
UNWIND( .fnend )
ENDPROC(memset)
ENDPROC(mmioset)
ENDPROC(__memset)
3 changes: 3 additions & 0 deletions arch/arm/mm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ obj-$(CONFIG_CACHE_FEROCEON_L2) += cache-feroceon-l2.o
obj-$(CONFIG_CACHE_L2X0) += cache-l2x0.o l2c-l2x0-resume.o
obj-$(CONFIG_CACHE_XSC3L2) += cache-xsc3l2.o
obj-$(CONFIG_CACHE_TAUROS2) += cache-tauros2.o

KASAN_SANITIZE_kasan_init.o := n
obj-$(CONFIG_KASAN) += kasan_init.o
Loading

0 comments on commit 0b54f17

Please sign in to comment.