Skip to content

Commit d88bb5e

Browse files
liu-song-6borkmann
authored andcommitted
bpf: Fill new bpf_prog_pack with illegal instructions
bpf_prog_pack enables sharing huge pages among multiple BPF programs. These pages are marked as executable before the JIT engine fill it with BPF programs. To make these pages safe, fill the hole bpf_prog_pack with illegal instructions before making it executable. Fixes: 5763105 ("bpf: Introduce bpf_prog_pack allocator") Fixes: 33c9805 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]") Reported-by: Linus Torvalds <[email protected]> Signed-off-by: Song Liu <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f9a3eca commit d88bb5e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

kernel/bpf/core.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ static size_t select_bpf_prog_pack_size(void)
873873
return size;
874874
}
875875

876-
static struct bpf_prog_pack *alloc_new_pack(void)
876+
static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns)
877877
{
878878
struct bpf_prog_pack *pack;
879879

@@ -886,6 +886,7 @@ static struct bpf_prog_pack *alloc_new_pack(void)
886886
kfree(pack);
887887
return NULL;
888888
}
889+
bpf_fill_ill_insns(pack->ptr, bpf_prog_pack_size);
889890
bitmap_zero(pack->bitmap, bpf_prog_pack_size / BPF_PROG_CHUNK_SIZE);
890891
list_add_tail(&pack->list, &pack_list);
891892

@@ -895,7 +896,7 @@ static struct bpf_prog_pack *alloc_new_pack(void)
895896
return pack;
896897
}
897898

898-
static void *bpf_prog_pack_alloc(u32 size)
899+
static void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
899900
{
900901
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
901902
struct bpf_prog_pack *pack;
@@ -910,6 +911,7 @@ static void *bpf_prog_pack_alloc(u32 size)
910911
size = round_up(size, PAGE_SIZE);
911912
ptr = module_alloc(size);
912913
if (ptr) {
914+
bpf_fill_ill_insns(ptr, size);
913915
set_vm_flush_reset_perms(ptr);
914916
set_memory_ro((unsigned long)ptr, size / PAGE_SIZE);
915917
set_memory_x((unsigned long)ptr, size / PAGE_SIZE);
@@ -923,7 +925,7 @@ static void *bpf_prog_pack_alloc(u32 size)
923925
goto found_free_area;
924926
}
925927

926-
pack = alloc_new_pack();
928+
pack = alloc_new_pack(bpf_fill_ill_insns);
927929
if (!pack)
928930
goto out;
929931

@@ -1102,7 +1104,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr,
11021104

11031105
if (bpf_jit_charge_modmem(size))
11041106
return NULL;
1105-
ro_header = bpf_prog_pack_alloc(size);
1107+
ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
11061108
if (!ro_header) {
11071109
bpf_jit_uncharge_modmem(size);
11081110
return NULL;

0 commit comments

Comments
 (0)