Skip to content

Commit

Permalink
FROMGIT: kasan, vmalloc: only tag normal vmalloc allocations
Browse files Browse the repository at this point in the history
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.

Link: https://lkml.kernel.org/r/fbfd9939a4dc375923c9a5c6b9e7ab05c26b8c6b.1643047180.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Acked-by: Marco 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: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
(cherry picked from commit 831af5e
 git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git akpm)
Bug: 217222520
Change-Id: I77c52e16d63f23ed84a6eb488996b1822eeb09e9
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
  • Loading branch information
xairy committed Feb 15, 2022
1 parent 01047c8 commit 460aa61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
7 changes: 4 additions & 3 deletions include/linux/kasan.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ struct kunit_kasan_expectation {

typedef unsigned int __bitwise kasan_vmalloc_flags_t;

#define KASAN_VMALLOC_NONE 0x00u
#define KASAN_VMALLOC_INIT 0x01u
#define KASAN_VMALLOC_VM_ALLOC 0x02u
#define KASAN_VMALLOC_NONE 0x00u
#define KASAN_VMALLOC_INIT 0x01u
#define KASAN_VMALLOC_VM_ALLOC 0x02u
#define KASAN_VMALLOC_PROT_NORMAL 0x04u

#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)

Expand Down
7 changes: 7 additions & 0 deletions mm/kasan/hw_tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
if (!(flags & KASAN_VMALLOC_VM_ALLOC))
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();
start = set_tag(start, tag);

Expand Down
7 changes: 7 additions & 0 deletions mm/kasan/shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
if (!is_vmalloc_or_module_addr(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;

start = set_tag(start, kasan_random_tag());
kasan_unpoison(start, size, false);
return (void *)start;
Expand Down
49 changes: 29 additions & 20 deletions mm/vmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node)
* With hardware tag-based KASAN, marking is skipped for
* 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;
}
Expand Down Expand Up @@ -2108,7 +2108,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
*/
if (!(flags & VM_ALLOC))
area->addr = kasan_unpoison_vmalloc(area->addr, requested_size,
KASAN_VMALLOC_NONE);
KASAN_VMALLOC_PROT_NORMAL);

return area;
}
Expand Down Expand Up @@ -2576,7 +2576,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
{
struct vm_struct *area;
void *ret;
kasan_vmalloc_flags_t kasan_flags;
kasan_vmalloc_flags_t kasan_flags = KASAN_VMALLOC_NONE;
unsigned long real_size = size;
unsigned int shift = PAGE_SHIFT;

Expand All @@ -2589,21 +2589,28 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
if (!area)
goto fail;

/* Prepare arguments for __vmalloc_area_node(). */
if (kasan_hw_tags_enabled() &&
pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
/*
* Modify protection bits to allow tagging.
* This must be done before mapping in __vmalloc_area_node().
*/
prot = arch_vmap_pgprot_tagged(prot);
/*
* Prepare arguments for __vmalloc_area_node() and
* kasan_unpoison_vmalloc().
*/
if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) {
if (kasan_hw_tags_enabled()) {
/*
* Modify protection bits to allow tagging.
* This must be done before mapping.
*/
prot = arch_vmap_pgprot_tagged(prot);

/*
* Skip page_alloc poisoning and zeroing for physical pages
* backing VM_ALLOC mapping. Memory is instead poisoned and
* zeroed by kasan_unpoison_vmalloc().
*/
gfp_mask |= __GFP_SKIP_KASAN_UNPOISON | __GFP_SKIP_ZERO;
/*
* Skip page_alloc poisoning and zeroing for physical
* pages backing VM_ALLOC mapping. Memory is instead
* poisoned and zeroed by kasan_unpoison_vmalloc().
*/
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. */
Expand All @@ -2617,10 +2624,13 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
* (except for the should_skip_init() check) to make sure that memory
* is initialized under the same conditions regardless of the enabled
* 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))
kasan_flags |= KASAN_VMALLOC_INIT;
/* KASAN_VMALLOC_PROT_NORMAL already set if required. */
area->addr = kasan_unpoison_vmalloc(area->addr, real_size, kasan_flags);

/*
Expand Down Expand Up @@ -3410,8 +3420,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
*/
for (area = 0; area < nr_vms; area++)
vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr,
vms[area]->size,
KASAN_VMALLOC_NONE);
vms[area]->size, KASAN_VMALLOC_PROT_NORMAL);

kfree(vas);
return vms;
Expand Down

0 comments on commit 460aa61

Please sign in to comment.