Skip to content

Commit 55309dd

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: bpf: arm: address randomize and write protect JIT code
This is the ARM variant for 314beb9 ("x86: bpf_jit_comp: secure bpf jit against spraying attacks"). It is now possible to implement it due to commits 75374ad ("ARM: mm: Define set_memory_* functions for ARM") and dca9aa9 ("ARM: add DEBUG_SET_MODULE_RONX option to Kconfig") which added infrastructure for this facility. Thus, this patch makes sure the BPF generated JIT code is marked RO, as other kernel text sections, and also lets the generated JIT code start at a pseudo random offset instead on a page boundary. The holes are filled with illegal instructions. JIT tested on armv7hl with BPF test suite. Reference: http://mainisusuallyafunction.blogspot.com/2012/11/attacking-hardened-linux-systems-with.html Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Acked-by: Mircea Gherzan <mgherzan@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 738cbe7 commit 55309dd

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

‎arch/arm/net/bpf_jit_32.c‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <linux/compiler.h>
1313
#include <linux/errno.h>
1414
#include <linux/filter.h>
15-
#include <linux/moduleloader.h>
1615
#include <linux/netdevice.h>
1716
#include <linux/string.h>
1817
#include <linux/slab.h>
@@ -174,6 +173,15 @@ static inline bool is_load_to_a(u16 inst)
174173
}
175174
}
176175

176+
static void jit_fill_hole(void *area, unsigned int size)
177+
{
178+
/* Insert illegal UND instructions. */
179+
u32 *ptr, fill_ins = 0xe7ffffff;
180+
/* We are guaranteed to have aligned memory. */
181+
for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
182+
*ptr++ = fill_ins;
183+
}
184+
177185
static void build_prologue(struct jit_ctx *ctx)
178186
{
179187
u16 reg_set = saved_regs(ctx);
@@ -859,9 +867,11 @@ static int build_body(struct jit_ctx *ctx)
859867

860868
void bpf_jit_compile(struct bpf_prog *fp)
861869
{
870+
struct bpf_binary_header *header;
862871
struct jit_ctx ctx;
863872
unsigned tmp_idx;
864873
unsigned alloc_size;
874+
u8 *target_ptr;
865875

866876
if (!bpf_jit_enable)
867877
return;
@@ -897,13 +907,15 @@ void bpf_jit_compile(struct bpf_prog *fp)
897907
/* there's nothing after the epilogue on ARMv7 */
898908
build_epilogue(&ctx);
899909
#endif
900-
901910
alloc_size = 4 * ctx.idx;
902-
ctx.target = module_alloc(alloc_size);
903-
if (unlikely(ctx.target == NULL))
911+
header = bpf_jit_binary_alloc(alloc_size, &target_ptr,
912+
4, jit_fill_hole);
913+
if (header == NULL)
904914
goto out;
905915

916+
ctx.target = (u32 *) target_ptr;
906917
ctx.idx = 0;
918+
907919
build_prologue(&ctx);
908920
build_body(&ctx);
909921
build_epilogue(&ctx);
@@ -919,6 +931,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
919931
/* there are 2 passes here */
920932
bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
921933

934+
set_memory_ro((unsigned long)header, header->pages);
922935
fp->bpf_func = (void *)ctx.target;
923936
fp->jited = 1;
924937
out:
@@ -928,8 +941,15 @@ void bpf_jit_compile(struct bpf_prog *fp)
928941

929942
void bpf_jit_free(struct bpf_prog *fp)
930943
{
931-
if (fp->jited)
932-
module_free(NULL, fp->bpf_func);
944+
unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
945+
struct bpf_binary_header *header = (void *)addr;
946+
947+
if (!fp->jited)
948+
goto free_filter;
949+
950+
set_memory_rw(addr, header->pages);
951+
bpf_jit_binary_free(header);
933952

953+
free_filter:
934954
bpf_prog_unlock_free(fp);
935955
}

0 commit comments

Comments
 (0)