Skip to content

Commit aa2d2c7

Browse files
heicarstMartin Schwidefsky
authored andcommitted
s390/bpf,jit: address randomize and write protect jit code
This is the s390 variant of 314beb9 "x86: bpf_jit_comp: secure bpf jit against spraying attacks". With this change the whole jit code and literal pool will be write protected after creation. In addition the start address of the jit code won't be always on a page boundary anymore. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent fee1b54 commit aa2d2c7

1 file changed

Lines changed: 46 additions & 5 deletions

File tree

‎arch/s390/net/bpf_jit_comp.c‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/netdevice.h>
1010
#include <linux/if_vlan.h>
1111
#include <linux/filter.h>
12+
#include <linux/random.h>
1213
#include <asm/cacheflush.h>
1314
#include <asm/processor.h>
1415
#include <asm/facility.h>
@@ -738,8 +739,41 @@ load_abs: if ((int) K < 0)
738739
return -1;
739740
}
740741

742+
/*
743+
* Note: for security reasons, bpf code will follow a randomly
744+
* sized amount of illegal instructions.
745+
*/
746+
struct bpf_binary_header {
747+
unsigned int pages;
748+
u8 image[];
749+
};
750+
751+
static struct bpf_binary_header *bpf_alloc_binary(unsigned int bpfsize,
752+
u8 **image_ptr)
753+
{
754+
struct bpf_binary_header *header;
755+
unsigned int sz, hole;
756+
757+
/* Most BPF filters are really small, but if some of them fill a page,
758+
* allow at least 128 extra bytes for illegal instructions.
759+
*/
760+
sz = round_up(bpfsize + sizeof(*header) + 128, PAGE_SIZE);
761+
header = module_alloc(sz);
762+
if (!header)
763+
return NULL;
764+
memset(header, 0, sz);
765+
header->pages = sz / PAGE_SIZE;
766+
hole = sz - bpfsize + sizeof(*header);
767+
/* Insert random number of illegal instructions before BPF code
768+
* and make sure the first instruction starts at an even address.
769+
*/
770+
*image_ptr = &header->image[(prandom_u32() % hole) & -2];
771+
return header;
772+
}
773+
741774
void bpf_jit_compile(struct sk_filter *fp)
742775
{
776+
struct bpf_binary_header *header = NULL;
743777
unsigned long size, prg_len, lit_len;
744778
struct bpf_jit jit, cjit;
745779
unsigned int *addrs;
@@ -775,8 +809,8 @@ void bpf_jit_compile(struct sk_filter *fp)
775809
size = prg_len + lit_len;
776810
if (size >= BPF_SIZE_MAX)
777811
goto out;
778-
jit.start = module_alloc(size);
779-
if (!jit.start)
812+
header = bpf_alloc_binary(size, &jit.start);
813+
if (!header)
780814
goto out;
781815
jit.prg = jit.mid = jit.start + prg_len;
782816
jit.lit = jit.end = jit.start + prg_len + lit_len;
@@ -791,14 +825,21 @@ void bpf_jit_compile(struct sk_filter *fp)
791825
if (jit.start)
792826
print_fn_code(jit.start, jit.mid - jit.start);
793827
}
794-
if (jit.start)
828+
if (jit.start) {
829+
set_memory_ro((unsigned long)header, header->pages);
795830
fp->bpf_func = (void *) jit.start;
831+
}
796832
out:
797833
kfree(addrs);
798834
}
799835

800836
void bpf_jit_free(struct sk_filter *fp)
801837
{
802-
if (fp->bpf_func != sk_run_filter)
803-
module_free(NULL, fp->bpf_func);
838+
unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
839+
struct bpf_binary_header *header = (void *)addr;
840+
841+
if (fp->bpf_func == sk_run_filter)
842+
return;
843+
set_memory_rw(addr, header->pages);
844+
module_free(NULL, header);
804845
}

0 commit comments

Comments
 (0)