Skip to content

Commit 22fbe89

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
bpf: Introduce CONFIG_BPF_ORACLE
This patch puts all BPF oracle logic behind a new BPF_ORACLE kernel config. At the moment, this config requires CONFIG_BPF_JIT_ALWAYS_ON to be disabled as the oracle only runs in the interpreter. Signed-off-by: Paul Chaignon <[email protected]>
1 parent ac1680e commit 22fbe89

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

kernel/bpf/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,18 @@ config BPF_LSM
101101

102102
If you are unsure how to answer this question, answer N.
103103

104+
config BPF_ORACLE
105+
bool "Enable BPF test oracle"
106+
depends on BPF_SYSCALL
107+
depends on DEBUG_KERNEL
108+
depends on !BPF_JIT_ALWAYS_ON
109+
default n
110+
help
111+
Enable the BPF test oracle to compare concrete runtime values of
112+
registers with their verification-time bounds. This will throw a kernel
113+
warning if the runtime values don't match the expected bounds from the
114+
verifier.
115+
116+
If you are unsure how to answer this question, answer N.
117+
104118
endmenu # "BPF subsystem"

kernel/bpf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse
66
endif
77
CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy)
88

9-
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o oracle.o log.o token.o liveness.o
9+
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o liveness.o
1010
obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o
1111
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
1212
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
@@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
5656
ifeq ($(CONFIG_DMA_SHARED_BUFFER),y)
5757
obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
5858
endif
59+
obj-$(CONFIG_BPF_ORACLE) += oracle.o
5960

6061
CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
6162
CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)

kernel/bpf/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,11 +1851,13 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
18511851
LD_IMM_DW: {
18521852
u64 address = (u64)(u32)insn[0].imm | ((u64)(u32)insn[1].imm) << 32;
18531853

1854+
#ifdef CONFIG_BPF_ORACLE
18541855
if (insn[0].src_reg == BPF_PSEUDO_MAP_ORACLE) {
18551856
oracle_test((struct bpf_map *)address, regs);
18561857
insn++;
18571858
CONT;
18581859
}
1860+
#endif
18591861
DST = address;
18601862
insn++;
18611863
CONT;

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20505,9 +20505,11 @@ static int do_check(struct bpf_verifier_env *env)
2050520505
state->insn_idx = env->insn_idx;
2050620506

2050720507
if (is_prune_point(env, env->insn_idx)) {
20508+
#ifdef CONFIG_BPF_ORACLE
2050820509
err = save_state_in_oracle(env, env->insn_idx);
2050920510
if (err < 0)
2051020511
return err;
20512+
#endif
2051120513

2051220514
err = is_state_visited(env, env->insn_idx);
2051320515
if (err < 0)
@@ -22641,6 +22643,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2264122643
}
2264222644

2264322645
for (i = 0; i < insn_cnt;) {
22646+
#ifdef CONFIG_BPF_ORACLE
2264422647
if (is_prune_point(env, i + delta)) {
2264522648
new_prog = patch_oracle_check_insn(env, insn, i + delta, &cnt);
2264622649
if (IS_ERR(new_prog))
@@ -22650,6 +22653,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2265022653
env->prog = prog = new_prog;
2265122654
insn = new_prog->insnsi + i + delta;
2265222655
}
22656+
#endif
2265322657

2265422658
if (insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && insn->imm) {
2265522659
if ((insn->off == BPF_ADDR_SPACE_CAST && insn->imm == 1) ||
@@ -25303,8 +25307,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
2530325307
if (ret == 0)
2530425308
ret = do_misc_fixups(env);
2530525309

25310+
#ifdef CONFIG_BPF_ORACLE
2530625311
if (ret == 0)
2530725312
ret = create_and_populate_oracle_map(env);
25313+
#endif
2530825314

2530925315
/* do 32-bit optimization after insn patching has done so those patched
2531025316
* insns could be handled correctly.

0 commit comments

Comments
 (0)