Skip to content

Commit 9d876e7

Browse files
borkmanndavem330
authored andcommitted
bpf: fix unlocking of jited image when module ronx not set
Eric and Willem reported that they recently saw random crashes when JIT was in use and bisected this to 74451e6 ("bpf: make jited programs visible in traces"). Issue was that the consolidation part added bpf_jit_binary_unlock_ro() that would unlock previously made read-only memory back to read-write. However, DEBUG_SET_MODULE_RONX cannot be used for this to test for presence of set_memory_*() functions. We need to use ARCH_HAS_SET_MEMORY instead to fix this; also add the corresponding bpf_jit_binary_lock_ro() to filter.h. Fixes: 74451e6 ("bpf: make jited programs visible in traces") Reported-by: Eric Dumazet <[email protected]> Reported-by: Willem de Bruijn <[email protected]> Bisected-by: Eric Dumazet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d2852a2 commit 9d876e7

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
898898

899899
bpf_flush_icache(header, ctx.image + ctx.idx);
900900

901-
set_memory_ro((unsigned long)header, header->pages);
901+
bpf_jit_binary_lock_ro(header);
902902
prog->bpf_func = (void *)ctx.image;
903903
prog->jited = 1;
904904

arch/s390/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
13271327
print_fn_code(jit.prg_buf, jit.size_prg);
13281328
}
13291329
if (jit.prg_buf) {
1330-
set_memory_ro((unsigned long)header, header->pages);
1330+
bpf_jit_binary_lock_ro(header);
13311331
fp->bpf_func = (void *) jit.prg_buf;
13321332
fp->jited = 1;
13331333
}

arch/x86/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
11651165

11661166
if (image) {
11671167
bpf_flush_icache(header, image + proglen);
1168-
set_memory_ro((unsigned long)header, header->pages);
1168+
bpf_jit_binary_lock_ro(header);
11691169
prog->bpf_func = (void *)image;
11701170
prog->jited = 1;
11711171
} else {

include/linux/filter.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static inline bool bpf_prog_was_classic(const struct bpf_prog *prog)
551551

552552
#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
553553

554-
#ifdef CONFIG_DEBUG_SET_MODULE_RONX
554+
#ifdef CONFIG_ARCH_HAS_SET_MEMORY
555555
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
556556
{
557557
set_memory_ro((unsigned long)fp, fp->pages);
@@ -562,6 +562,11 @@ static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
562562
set_memory_rw((unsigned long)fp, fp->pages);
563563
}
564564

565+
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
566+
{
567+
set_memory_ro((unsigned long)hdr, hdr->pages);
568+
}
569+
565570
static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
566571
{
567572
set_memory_rw((unsigned long)hdr, hdr->pages);
@@ -575,10 +580,14 @@ static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
575580
{
576581
}
577582

583+
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
584+
{
585+
}
586+
578587
static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
579588
{
580589
}
581-
#endif /* CONFIG_DEBUG_SET_MODULE_RONX */
590+
#endif /* CONFIG_ARCH_HAS_SET_MEMORY */
582591

583592
static inline struct bpf_binary_header *
584593
bpf_jit_binary_hdr(const struct bpf_prog *fp)

0 commit comments

Comments
 (0)