net: bpf: arm64: address randomize and write protect JIT code
Subsystems (3)
PATCH v2 1/1 net: bpf: arm64: address randomize and write protect JIT code
This is the ARM64 variant for 314beb9bcab ("x86: bpf_jit_comp: secure bpf jit against spraying attacks").
Thanks to commit 11d91a770f1f ("arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support") which added necessary infrastructure, we can now implement RO marking of eBPF generated JIT image pages and randomize start offset for the JIT code, so that it does not reside directly on a page boundary anymore. Likewise, the holes are filled with illegal instructions.
This is basically the ARM64 variant of what we already have in ARM via commit 55309dd3d4cd ("net: bpf: arm: address randomize and write protect JIT code"). Moreover, this commit also presents a merge resolution due to conflicts with commit 60a3b2253c41 ("net: bpf: make eBPF interpreter images read-only") as we don't use kfree() in bpf_jit_free() anymore to release the locked bpf_prog structure, but instead bpf_prog_unlock_free() through a different allocator.
JIT tested on aarch64 with BPF test suite.
Reference: http://mainisusuallyafunction.blogspot.com/2012/11/attacking-hardened-linux-systems-with.html
---
v1 -> v2:
- Use brk insn as suggested by Catalin, thanks a lot for your feedback! Rest unchanged.
Note:
- This patch depends on net-next being merged to mainline due to the mentioned merge conflict.
arch/arm64/net/bpf_jit_comp.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-)
arch/arm64/net/bpf_jit_comp.c +29 -9
@@ -19,7 +19,6 @@#define pr_fmt (fmt) "bpf_jit: " fmt#include <linux/filter.h> -#include <linux/moduleloader.h> #include <linux/printk.h> #include <linux/skbuff.h> #include <linux/slab.h>
@@ -119,6 +118,15 @@ static inline int bpf2a64_offset(int bpf_to, int bpf_from,return to - from; } +static void jit_fill_hole (void *area,unsigned int size) +{ +/* We use brk #0x100 to trigger a fault. */ +u32 *ptr, fill_ins =0xd4202000 ; +/* We are guaranteed to have aligned memory. */ +for (ptr = area; size >=sizeof (u32); size -=sizeof (u32)) + *ptr++ = fill_ins; +} +static inline int epilogue_offset (const struct jit_ctx *ctx) {int to = ctx->offset[ctx->prog->len -1 ];
@@ -613,8 +621,10 @@ void bpf_jit_compile(struct bpf_prog *prog)void bpf_int_jit_compile (struct bpf_prog *prog) { +struct bpf_binary_header *header;struct jit_ctx ctx;int image_size; +u8 *image_ptr;if (!bpf_jit_enable)return ;
@@ -636,23 +646,25 @@ void bpf_int_jit_compile(struct bpf_prog *prog) goto out;build_prologue (&ctx); -build_epilogue (&ctx);/* Now we know the actual image size. */ image_size =sizeof (u32) * ctx.idx; - ctx.image =module_alloc (image_size); -if (unlikely (ctx.image ==NULL )) + header =bpf_jit_binary_alloc (image_size, &image_ptr, +sizeof (u32), jit_fill_hole); +if (header ==NULL ) goto out;/* 2. Now, the actual pass. */ + ctx.image = (u32 *)image_ptr; ctx.idx =0 ; +build_prologue (&ctx); ctx.body_offset = ctx.idx;if (build_body (&ctx)) { -module_free (NULL , ctx.image); +bpf_jit_binary_free (header); goto out; }
@@ -663,17 +675,25 @@ void bpf_int_jit_compile(struct bpf_prog *prog)bpf_jit_dump (prog->len, image_size,2 , ctx.image);bpf_flush_icache (ctx.image, ctx.image + ctx.idx); + +set_memory_ro ((unsigned long )header, header->pages); prog->bpf_func = (void *)ctx.image; prog->jited =1 ; - out:kfree (ctx.offset); }void bpf_jit_free (struct bpf_prog *prog) { -if (prog->jited) -module_free (NULL , prog->bpf_func); +unsigned long addr = (unsigned long )prog->bpf_func &PAGE_MASK ; +struct bpf_binary_header *header = (void *)addr; + +if (!prog->jited) + goto free_filter; + +set_memory_rw (addr, header->pages); +bpf_jit_binary_free (header); -kfree (prog); +free_filter: +bpf_prog_unlock_free (prog); }
Signature · 1 line
1.9.3
Quoted 50 lines · Daniel Borkmann
This is the ARM64 variant for
314beb9bcab("x86:bpf_jit_comp: secure bpf jit against spraying attacks").Thanks to commit
11d91a770f1f("arm64: AddCONFIG_DEBUG_SET_MODULE_RONXsupport") which added necessary infrastructure, we can now implement RO marking of eBPF generated JIT image pages and randomize start offset for the JIT code, so that it does not reside directly on a page boundary anymore. Likewise, the holes are filled with illegal instructions.This is basically the ARM64 variant of what we already have in ARM via commit
55309dd3d4cd("net: bpf: arm: address randomize and write protect JIT code"). Moreover, this commit also presents a merge resolution due to conflicts with commit60a3b2253c41("net: bpf: make eBPF interpreter images read-only") as we don't usekfree()inbpf_jit_free()anymore to release the lockedbpf_progstructure, but insteadbpf_prog_unlock_free()through a different allocator.JIT tested on aarch64 with BPF test suite.
Reference: http://mainisusuallyafunction.blogspot.com/2012/11/attacking-hardened-linux-systems-with.html
Signed-off-by Daniel Borkmann <[email protected]>Cc Zi Shen Lim <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc David S. Miller <[email protected]>Cc Alexei Starovoitov <[email protected]>---
v1 -> v2:
- Use brk insn as suggested by Catalin, thanks a lot for your feedback! Rest unchanged.
Note:
- This patch depends on net-next being merged to mainline due to the mentioned merge conflict.arch/arm64/net/bpf_jit_comp.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-)arch/arm64/net/bpf_jit_comp.c +0
-1@@ -19,7 +19,6 @@#define pr_fmt (fmt) "bpf_jit: " fmt#include <linux/filter.h> -#include <linux/moduleloader.h> #include <linux/printk.h> #include <linux/skbuff.h> #include <linux/slab.h>
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 124
Quoted 7 lines
arch/arm64/net/bpf_jit_comp.c +4
-0@@ -119,6 +118,15 @@ static inline int bpf2a64_offset(int bpf_to, int bpf_from,return to - from; } +static void jit_fill_hole (void *area,unsigned int size) +{ +/* We use brk #0x100 to trigger a fault. */ +u32 *ptr, fill_ins =0xd4202000 ;
Missed this on first round of review, I think we also need cpu_to_le32(...) here.
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 129
Quoted 5 lines
arch/arm64/net/bpf_jit_comp.c +5
-0+/* We are guaranteed to have aligned memory. */ +for (ptr = area; size >=sizeof (u32); size -=sizeof (u32)) + *ptr++ = fill_ins; +} +
[...]
Thanks Daniel.
Quoted 54 lines · Z Lim
Quoted 53 lines · Daniel Borkmann
This is the ARM64 variant for
314beb9bcab("x86:bpf_jit_comp: secure bpf jit against spraying attacks").Thanks to commit
11d91a770f1f("arm64: AddCONFIG_DEBUG_SET_MODULE_RONXsupport") which added necessary infrastructure, we can now implement RO marking of eBPF generated JIT image pages and randomize start offset for the JIT code, so that it does not reside directly on a page boundary anymore. Likewise, the holes are filled with illegal instructions.This is basically the ARM64 variant of what we already have in ARM via commit
55309dd3d4cd("net: bpf: arm: address randomize and write protect JIT code"). Moreover, this commit also presents a merge resolution due to conflicts with commit60a3b2253c41("net: bpf: make eBPF interpreter images read-only") as we don't usekfree()inbpf_jit_free()anymore to release the lockedbpf_progstructure, but insteadbpf_prog_unlock_free()through a different allocator.JIT tested on aarch64 with BPF test suite.
Reference: http://mainisusuallyafunction.blogspot.com/2012/11/attacking-hardened-linux-systems-with.html
Signed-off-by Daniel Borkmann <[email protected]>Cc Zi Shen Lim <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc David S. Miller <[email protected]>Cc Alexei Starovoitov <[email protected]>---
v1 -> v2:
- Use brk insn as suggested by Catalin, thanks a lot for your feedback! Rest unchanged.
Note:
- This patch depends on net-next being merged to mainline due to the mentioned merge conflict.arch/arm64/net/bpf_jit_comp.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-)arch/arm64/net/bpf_jit_comp.c +0
-1@@ -19,7 +19,6 @@#define pr_fmt (fmt) "bpf_jit: " fmt#include <linux/filter.h> -#include <linux/moduleloader.h> #include <linux/printk.h> #include <linux/skbuff.h> #include <linux/slab.h> @@ -119,6 +118,15 @@ static inline int bpf2a64_offset(int bpf_to, int bpf_from,return to - from; }
Quoted 7 lines
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 124
Quoted 4 lines
arch/arm64/net/bpf_jit_comp.c +4
-0+static void jit_fill_hole (void *area,unsigned int size) +{ +/* We use brk #0x100 to trigger a fault. */ +u32 *ptr, fill_ins =0xd4202000 ;Missed this on first round of review, I think we also need
cpu_to_le32(...)here.
Just wondering ... so that would also hold true in case I build/run my kernel in big-endian (CPU_BIG_ENDIAN)?
Quoted 8 lines
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 129
Quoted 5 lines
arch/arm64/net/bpf_jit_comp.c +5
-0+/* We are guaranteed to have aligned memory. */ +for (ptr = area; size >=sizeof (u32); size -=sizeof (u32)) + *ptr++ = fill_ins; +} +[...]
Thanks Daniel.
Quoted 63 lines · Daniel Borkmann
Quoted 54 lines · Z Lim
Quoted 53 lines · Daniel Borkmann
This is the ARM64 variant for
314beb9bcab("x86:bpf_jit_comp: secure bpf jit against spraying attacks").Thanks to commit
11d91a770f1f("arm64: AddCONFIG_DEBUG_SET_MODULE_RONXsupport") which added necessary infrastructure, we can now implement RO marking of eBPF generated JIT image pages and randomize start offset for the JIT code, so that it does not reside directly on a page boundary anymore. Likewise, the holes are filled with illegal instructions.This is basically the ARM64 variant of what we already have in ARM via commit
55309dd3d4cd("net: bpf: arm: address randomize and write protect JIT code"). Moreover, this commit also presents a merge resolution due to conflicts with commit60a3b2253c41("net: bpf: make eBPF interpreter images read-only") as we don't usekfree()inbpf_jit_free()anymore to release the lockedbpf_progstructure, but insteadbpf_prog_unlock_free()through a different allocator.JIT tested on aarch64 with BPF test suite.
Reference: http://mainisusuallyafunction.blogspot.com/2012/11/attacking-hardened-linux-systems-with.html
Signed-off-by Daniel Borkmann <[email protected]>Cc Zi Shen Lim <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>Cc David S. Miller <[email protected]>Cc Alexei Starovoitov <[email protected]>---
v1 -> v2:
- Use brk insn as suggested by Catalin, thanks a lot for your feedback! Rest unchanged.
Note:
- This patch depends on net-next being merged to mainline due to the mentioned merge conflict.arch/arm64/net/bpf_jit_comp.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-)arch/arm64/net/bpf_jit_comp.c +0
-1@@ -19,7 +19,6 @@#define pr_fmt (fmt) "bpf_jit: " fmt#include <linux/filter.h> -#include <linux/moduleloader.h> #include <linux/printk.h> #include <linux/skbuff.h> #include <linux/slab.h> @@ -119,6 +118,15 @@ static inline int bpf2a64_offset(int bpf_to, int bpf_from,return to - from; }Quoted 7 lines
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 124
Quoted 4 lines
arch/arm64/net/bpf_jit_comp.c +4
-0+static void jit_fill_hole (void *area,unsigned int size) +{ +/* We use brk #0x100 to trigger a fault. */ +u32 *ptr, fill_ins =0xd4202000 ;Missed this on first round of review, I think we also need
cpu_to_le32(...)here.
Quoted 2 lines
Just wondering ... so that would also hold true in case I build/run my kernel in big-endian (
CPU_BIG_ENDIAN)?
Yes, the instruction stream is always little endian so the conversion is required.
Will
Quoted 9 lines
Quoted 8 lines
arch/arm64/net/bpf_jit_comp.c static inline int bpf2a64_offset(int bpf_to, int bpf_from, · line 129
Quoted 5 lines
arch/arm64/net/bpf_jit_comp.c +5
-0+/* We are guaranteed to have aligned memory. */ +for (ptr = area; size >=sizeof (u32); size -=sizeof (u32)) + *ptr++ = fill_ins; +} +[...]
Thanks Daniel.