Skip to content

Commit fe73656

Browse files
liu-song-6borkmann
authored andcommitted
bpf: Introduce bpf_arch_text_invalidate for bpf_prog_pack
Introduce bpf_arch_text_invalidate and use it to fill unused part of the bpf_prog_pack with illegal instructions when a BPF program is freed. 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 aadd1b6 commit fe73656

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ static void jit_fill_hole(void *area, unsigned int size)
228228
memset(area, 0xcc, size);
229229
}
230230

231+
int bpf_arch_text_invalidate(void *dst, size_t len)
232+
{
233+
return IS_ERR_OR_NULL(text_poke_set(dst, 0xcc, len));
234+
}
235+
231236
struct jit_context {
232237
int cleanup_addr; /* Epilogue code offset */
233238

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
23652365
void *addr1, void *addr2);
23662366

23672367
void *bpf_arch_text_copy(void *dst, void *src, size_t len);
2368+
int bpf_arch_text_invalidate(void *dst, size_t len);
23682369

23692370
struct btf_id_set;
23702371
bool btf_id_set_contains(const struct btf_id_set *set, u32 id);

kernel/bpf/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,9 @@ static void bpf_prog_pack_free(struct bpf_binary_header *hdr)
968968
nbits = BPF_PROG_SIZE_TO_NBITS(hdr->size);
969969
pos = ((unsigned long)hdr - (unsigned long)pack_ptr) >> BPF_PROG_CHUNK_SHIFT;
970970

971+
WARN_ONCE(bpf_arch_text_invalidate(hdr, hdr->size),
972+
"bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
973+
971974
bitmap_clear(pack->bitmap, pos, nbits);
972975
if (bitmap_find_next_zero_area(pack->bitmap, bpf_prog_chunk_count(), 0,
973976
bpf_prog_chunk_count(), 0) == 0) {
@@ -2740,6 +2743,11 @@ void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len)
27402743
return ERR_PTR(-ENOTSUPP);
27412744
}
27422745

2746+
int __weak bpf_arch_text_invalidate(void *dst, size_t len)
2747+
{
2748+
return -ENOTSUPP;
2749+
}
2750+
27432751
DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
27442752
EXPORT_SYMBOL(bpf_stats_enabled_key);
27452753

0 commit comments

Comments
 (0)