BACKPORT: FROMGIT: kasan, arm64: don't tag executable vmalloc allocations

(Backport: resolve conflict in bpf_jit_alloc_exec.)

Besides asking vmalloc memory to be executable via the prot argument of
__vmalloc_node_range() (see the previous patch), the kernel can skip that
bit and instead mark memory as executable via set_memory_x().

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.

Generic kernel code typically allocates memory via module_alloc() if it
intends to mark memory as executable.  (On arm64 module_alloc() uses
__vmalloc_node_range() without setting the executable bit).

Thus, reset pointer tags of pointers returned from module_alloc().

However, on arm64 there's an exception: the eBPF subsystem.  Instead of
using module_alloc(), it uses vmalloc() (via bpf_jit_alloc_exec()) to
allocate its JIT region.

Thus, reset pointer tags of pointers returned from bpf_jit_alloc_exec().

Resetting tags for these pointers results in untagged pointers being
passed to set_memory_x().  This causes conflicts in arithmetic checks in
change_memory_common(), as vm_struct->addr pointer returned by
find_vm_area() is tagged.

Reset pointer tag of find_vm_area(addr)->addr in change_memory_common().

Link: https://lkml.kernel.org/r/b7b2595423340cd7d76b770e5d519acf3b72f0ab.1643047180.git.andreyknvl@google.com
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Acked-by: Marco Elver <elver@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.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 c9944678742d7377382423d84b7883cc163e663f
 git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git akpm)
Bug: 217222520
Change-Id: I19bea2781827cffe5d4a2a0a0a7c36c4e51776f3
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
This commit is contained in:
Andrey Konovalov
2022-01-27 16:25:53 +11:00
parent 460aa619e3
commit d3578ba7a5
3 changed files with 9 additions and 6 deletions

View File

@@ -63,7 +63,8 @@ void *module_alloc(unsigned long size)
return NULL; return NULL;
} }
return p; /* Memory is intended to be executable, reset the pointer tag. */
return kasan_reset_tag(p);
} }
enum aarch64_reloc_op { enum aarch64_reloc_op {

View File

@@ -80,7 +80,7 @@ static int change_memory_common(unsigned long addr, int numpages,
*/ */
area = find_vm_area((void *)addr); area = find_vm_area((void *)addr);
if (!area || if (!area ||
end > (unsigned long)area->addr + area->size || end > (unsigned long)kasan_reset_tag(area->addr) + area->size ||
!(area->flags & VM_ALLOC)) !(area->flags & VM_ALLOC))
return -EINVAL; return -EINVAL;

View File

@@ -1144,10 +1144,12 @@ u64 bpf_jit_alloc_exec_limit(void)
void *bpf_jit_alloc_exec(unsigned long size) void *bpf_jit_alloc_exec(unsigned long size)
{ {
return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START, void *p = __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
BPF_JIT_REGION_END, GFP_KERNEL, BPF_JIT_REGION_END, GFP_KERNEL,
PAGE_KERNEL, 0, NUMA_NO_NODE, PAGE_KERNEL, 0, NUMA_NO_NODE,
__builtin_return_address(0)); __builtin_return_address(0));
/* Memory is intended to be executable, reset the pointer tag. */
return kasan_reset_tag(p);
} }
void bpf_jit_free_exec(void *addr) void bpf_jit_free_exec(void *addr)