From e1b89754b66a17286389bf46195f3b6eca185a97 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Wed, 29 Jan 2020 17:32:26 -0800 Subject: [PATCH] ANDROID: Revert "ANDROID: bpf: validate bpf_func when BPF_JIT is enabled with CFI" This reverts commit 788bbf4f261fc558b714bdedd4122d7115efc940. Reason for revert: fixes a conflict with upcoming upstream BPF changes. Bug: 145210207 Change-Id: I3bbc1279fc613be0d2e833008413ad3561b851df Signed-off-by: Sami Tolvanen --- include/linux/filter.h | 64 ++---------------------------------------- kernel/bpf/core.c | 9 ------ 2 files changed, 2 insertions(+), 71 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index db11f04b8341..345f3748e0fb 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -518,12 +518,7 @@ struct sock_fprog_kern { /* Some arches need doubleword alignment for their instructions and/or data */ #define BPF_IMAGE_ALIGNMENT 8 -#define BPF_BINARY_HEADER_MAGIC 0x05de0e82 - struct bpf_binary_header { -#ifdef CONFIG_CFI_CLANG - u32 magic; -#endif u32 pages; u8 image[] __aligned(BPF_IMAGE_ALIGNMENT); }; @@ -564,75 +559,20 @@ struct sk_filter { DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key); -#if IS_ENABLED(CONFIG_BPF_JIT) && IS_ENABLED(CONFIG_CFI_CLANG) -/* - * With JIT, the kernel makes an indirect call to dynamically generated - * code. Use bpf_call_func to perform additional validation of the call - * target to narrow down attack surface. Architectures implementing BPF - * JIT can override arch_bpf_jit_check_func for arch-specific checking. - */ -extern bool arch_bpf_jit_check_func(const struct bpf_prog *prog); - -static inline unsigned int __bpf_call_func(const struct bpf_prog *prog, - const void *ctx) -{ - /* Call interpreter with CFI checking. */ - return prog->bpf_func(ctx, prog->insnsi); -} - -static inline struct bpf_binary_header * -bpf_jit_binary_hdr(const struct bpf_prog *fp); - -static inline unsigned int __nocfi bpf_call_func(const struct bpf_prog *prog, - const void *ctx) -{ - const struct bpf_binary_header *hdr = bpf_jit_binary_hdr(prog); - - if (!IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) && !prog->jited) - return __bpf_call_func(prog, ctx); - - /* - * We are about to call dynamically generated code. Check that the - * page has bpf_binary_header with a valid magic to limit possible - * call targets. - */ - BUG_ON(hdr->magic != BPF_BINARY_HEADER_MAGIC || - !arch_bpf_jit_check_func(prog)); - - /* Call jited function without CFI checking. */ - return prog->bpf_func(ctx, prog->insnsi); -} - -static inline void bpf_jit_set_header_magic(struct bpf_binary_header *hdr) -{ - hdr->magic = BPF_BINARY_HEADER_MAGIC; -} -#else -static inline unsigned int bpf_call_func(const struct bpf_prog *prog, - const void *ctx) -{ - return prog->bpf_func(ctx, prog->insnsi); -} - -static inline void bpf_jit_set_header_magic(struct bpf_binary_header *hdr) -{ -} -#endif - #define BPF_PROG_RUN(prog, ctx) ({ \ u32 ret; \ cant_sleep(); \ if (static_branch_unlikely(&bpf_stats_enabled_key)) { \ struct bpf_prog_stats *stats; \ u64 start = sched_clock(); \ - ret = bpf_call_func(prog, ctx); \ + ret = (*(prog)->bpf_func)(ctx, (prog)->insnsi); \ stats = this_cpu_ptr(prog->aux->stats); \ u64_stats_update_begin(&stats->syncp); \ stats->cnt++; \ stats->nsecs += sched_clock() - start; \ u64_stats_update_end(&stats->syncp); \ } else { \ - ret = bpf_call_func(prog, ctx); \ + ret = (*(prog)->bpf_func)(ctx, (prog)->insnsi); \ } \ ret; }) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index e11fbb13cd82..af6b738cf435 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -842,14 +842,6 @@ void __weak bpf_jit_free_exec(void *addr) module_memfree(addr); } -#if IS_ENABLED(CONFIG_BPF_JIT) && IS_ENABLED(CONFIG_CFI_CLANG) -bool __weak arch_bpf_jit_check_func(const struct bpf_prog *prog) -{ - return true; -} -EXPORT_SYMBOL(arch_bpf_jit_check_func); -#endif - struct bpf_binary_header * bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, unsigned int alignment, @@ -879,7 +871,6 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, /* Fill space with illegal/arch-dep instructions. */ bpf_fill_ill_insns(hdr, size); - bpf_jit_set_header_magic(hdr); hdr->pages = pages; hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), PAGE_SIZE - sizeof(*hdr));