Commit f6e39794 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds

kasan, vmalloc: only tag normal vmalloc allocations

The kernel can use to allocate executable memory.  The only supported
way to do that is via __vmalloc_node_range() with the executable bit set
in the prot argument.  (vmap() resets the bit via pgprot_nx()).

Once tag-based KASAN modes start tagging vmalloc allocations, executing
code from such allocations will lead to the PC register getting a tag,
which is not tolerated by the kernel.

Only tag the allocations for normal kernel pages.

[andreyknvl@google.com: pass KASAN_VMALLOC_PROT_NORMAL to kasan_unpoison_vmalloc()]
  Link: https://lkml.kernel.org/r/9230ca3d3e40ffca041c133a524191fd71969a8d.1646233925.git.andreyknvl@google.com
[andreyknvl@google.com: support tagged vmalloc mappings]
  Link: https://lkml.kernel.org/r/2f6605e3a358cf64d73a05710cb3da356886ad29.1646233925.git.andreyknvl@google.com
[andreyknvl@google.com: don't unintentionally disabled poisoning]
  Link: https://lkml.kernel.org/r/de4587d6a719232e83c760113e46ed2d4d8da61e.1646757322.git.andreyknvl@google.com

Link: https://lkml.kernel.org/r/fbfd9939a4dc375923c9a5c6b9e7ab05c26b8c6b.1643047180.git.andreyknvl@google.comSigned-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Acked-by: default avatarMarco Elver <elver@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Collingbourne <pcc@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 23689e91
...@@ -31,6 +31,7 @@ typedef unsigned int __bitwise kasan_vmalloc_flags_t; ...@@ -31,6 +31,7 @@ typedef unsigned int __bitwise kasan_vmalloc_flags_t;
#define KASAN_VMALLOC_NONE 0x00u #define KASAN_VMALLOC_NONE 0x00u
#define KASAN_VMALLOC_INIT 0x01u #define KASAN_VMALLOC_INIT 0x01u
#define KASAN_VMALLOC_VM_ALLOC 0x02u #define KASAN_VMALLOC_VM_ALLOC 0x02u
#define KASAN_VMALLOC_PROT_NORMAL 0x04u
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
......
...@@ -32,15 +32,19 @@ static void *__scs_alloc(int node) ...@@ -32,15 +32,19 @@ static void *__scs_alloc(int node)
for (i = 0; i < NR_CACHED_SCS; i++) { for (i = 0; i < NR_CACHED_SCS; i++) {
s = this_cpu_xchg(scs_cache[i], NULL); s = this_cpu_xchg(scs_cache[i], NULL);
if (s) { if (s) {
kasan_unpoison_vmalloc(s, SCS_SIZE, KASAN_VMALLOC_NONE); s = kasan_unpoison_vmalloc(s, SCS_SIZE,
KASAN_VMALLOC_PROT_NORMAL);
memset(s, 0, SCS_SIZE); memset(s, 0, SCS_SIZE);
return s; goto out;
} }
} }
return __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END, s = __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END,
GFP_SCS, PAGE_KERNEL, 0, node, GFP_SCS, PAGE_KERNEL, 0, node,
__builtin_return_address(0)); __builtin_return_address(0));
out:
return kasan_reset_tag(s);
} }
void *scs_alloc(int node) void *scs_alloc(int node)
...@@ -78,7 +82,7 @@ void scs_free(void *s) ...@@ -78,7 +82,7 @@ void scs_free(void *s)
if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL) if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL)
return; return;
kasan_unpoison_vmalloc(s, SCS_SIZE, KASAN_VMALLOC_NONE); kasan_unpoison_vmalloc(s, SCS_SIZE, KASAN_VMALLOC_PROT_NORMAL);
vfree_atomic(s); vfree_atomic(s);
} }
......
...@@ -247,6 +247,13 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, ...@@ -247,6 +247,13 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
if (!(flags & KASAN_VMALLOC_VM_ALLOC)) if (!(flags & KASAN_VMALLOC_VM_ALLOC))
return (void *)start; return (void *)start;
/*
* Don't tag executable memory.
* The kernel doesn't tolerate having the PC register tagged.
*/
if (!(flags & KASAN_VMALLOC_PROT_NORMAL))
return (void *)start;
tag = kasan_random_tag(); tag = kasan_random_tag();
start = set_tag(start, tag); start = set_tag(start, tag);
......
...@@ -488,6 +488,14 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, ...@@ -488,6 +488,14 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
if (!is_vmalloc_or_module_addr(start)) if (!is_vmalloc_or_module_addr(start))
return (void *)start; return (void *)start;
/*
* Don't tag executable memory with the tag-based mode.
* The kernel doesn't tolerate having the PC register tagged.
*/
if (IS_ENABLED(CONFIG_KASAN_SW_TAGS) &&
!(flags & KASAN_VMALLOC_PROT_NORMAL))
return (void *)start;
start = set_tag(start, kasan_random_tag()); start = set_tag(start, kasan_random_tag());
kasan_unpoison(start, size, false); kasan_unpoison(start, size, false);
return (void *)start; return (void *)start;
......
...@@ -2242,7 +2242,7 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node) ...@@ -2242,7 +2242,7 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node)
* With hardware tag-based KASAN, marking is skipped for * With hardware tag-based KASAN, marking is skipped for
* non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc(). * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
*/ */
mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_NONE); mem = kasan_unpoison_vmalloc(mem, size, KASAN_VMALLOC_PROT_NORMAL);
return mem; return mem;
} }
...@@ -2481,7 +2481,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size, ...@@ -2481,7 +2481,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
*/ */
if (!(flags & VM_ALLOC)) if (!(flags & VM_ALLOC))
area->addr = kasan_unpoison_vmalloc(area->addr, requested_size, area->addr = kasan_unpoison_vmalloc(area->addr, requested_size,
KASAN_VMALLOC_NONE); KASAN_VMALLOC_PROT_NORMAL);
return area; return area;
} }
...@@ -3091,7 +3091,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, ...@@ -3091,7 +3091,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
{ {
struct vm_struct *area; struct vm_struct *area;
void *ret; void *ret;
kasan_vmalloc_flags_t kasan_flags; kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE;
unsigned long real_size = size; unsigned long real_size = size;
unsigned long real_align = align; unsigned long real_align = align;
unsigned int shift = PAGE_SHIFT; unsigned int shift = PAGE_SHIFT;
...@@ -3144,23 +3144,30 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, ...@@ -3144,23 +3144,30 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
goto fail; goto fail;
} }
/* Prepare arguments for __vmalloc_area_node(). */ /*
if (kasan_hw_tags_enabled() && * Prepare arguments for __vmalloc_area_node() and
pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) { * kasan_unpoison_vmalloc().
*/
if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
if (kasan_hw_tags_enabled()) {
/* /*
* Modify protection bits to allow tagging. * Modify protection bits to allow tagging.
* This must be done before mapping in __vmalloc_area_node(). * This must be done before mapping.
*/ */
prot = arch_vmap_pgprot_tagged(prot); prot = arch_vmap_pgprot_tagged(prot);
/* /*
* Skip page_alloc poisoning and zeroing for physical pages * Skip page_alloc poisoning and zeroing for physical
* backing VM_ALLOC mapping. Memory is instead poisoned and * pages backing VM_ALLOC mapping. Memory is instead
* zeroed by kasan_unpoison_vmalloc(). * poisoned and zeroed by kasan_unpoison_vmalloc().
*/ */
gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO; gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO;
} }
/* Take note that the mapping is PAGE_KERNEL. */
kasan_flags |= KASAN_VMALLOC_PROT_NORMAL;
}
/* Allocate physical pages and map them into vmalloc space. */ /* Allocate physical pages and map them into vmalloc space. */
ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node);
if (!ret) if (!ret)
...@@ -3172,10 +3179,13 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, ...@@ -3172,10 +3179,13 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
* (except for the should_skip_init() check) to make sure that memory * (except for the should_skip_init() check) to make sure that memory
* is initialized under the same conditions regardless of the enabled * is initialized under the same conditions regardless of the enabled
* KASAN mode. * KASAN mode.
* Tag-based KASAN modes only assign tags to normal non-executable
* allocations, see __kasan_unpoison_vmalloc().
*/ */
kasan_flags = KASAN_VMALLOC_VM_ALLOC; kasan_flags |= KASAN_VMALLOC_VM_ALLOC;
if (!want_init_on_free() && want_init_on_alloc(gfp_mask)) if (!want_init_on_free() && want_init_on_alloc(gfp_mask))
kasan_flags |= KASAN_VMALLOC_INIT; kasan_flags |= KASAN_VMALLOC_INIT;
/* KASAN_VMALLOC_PROT_NORMAL already set if required. */
area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags); area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags);
/* /*
...@@ -3881,8 +3891,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, ...@@ -3881,8 +3891,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
*/ */
for (area = 0; area < nr_vms; area++) for (area = 0; area < nr_vms; area++)
vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
vms[area]->size, vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);
KASAN_VMALLOC_NONE);
kfree(vas); kfree(vas);
return vms; return vms;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment