Skip to content

Commit e72d88d

Browse files
Peter ZijlstraAlexei Starovoitov
authored andcommitted
x86/cfi,bpf: Fix bpf_callback_t CFI
Where the main BPF program is expected to match bpf_func_t, sub-programs are expected to match bpf_callback_t. This fixes things like: tools/testing/selftests/bpf/progs/bloom_filter_bench.c: bpf_for_each_map_elem(&array_map, bloom_callback, &data, 0); Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 4f9087f commit e72d88d

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

arch/x86/include/asm/cfi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct pt_regs;
106106
enum bug_trap_type handle_cfi_failure(struct pt_regs *regs);
107107
#define __bpfcall
108108
extern u32 cfi_bpf_hash;
109+
extern u32 cfi_bpf_subprog_hash;
109110

110111
static inline int cfi_get_offset(void)
111112
{
@@ -128,6 +129,7 @@ static inline enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)
128129
return BUG_TRAP_TYPE_NONE;
129130
}
130131
#define cfi_bpf_hash 0U
132+
#define cfi_bpf_subprog_hash 0U
131133
#endif /* CONFIG_CFI_CLANG */
132134

133135
#endif /* _ASM_X86_CFI_H */

arch/x86/kernel/alternative.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,23 @@ asm (
866866
" .size cfi_bpf_hash, 4 \n"
867867
" .popsection \n"
868868
);
869+
870+
/* Must match bpf_callback_t */
871+
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
872+
873+
__ADDRESSABLE(__bpf_callback_fn);
874+
875+
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
876+
asm (
877+
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
878+
" .type cfi_bpf_subprog_hash,@object \n"
879+
" .globl cfi_bpf_subprog_hash \n"
880+
" .p2align 2, 0x0 \n"
881+
"cfi_bpf_subprog_hash: \n"
882+
" .long __kcfi_typeid___bpf_callback_fn \n"
883+
" .size cfi_bpf_subprog_hash, 4 \n"
884+
" .popsection \n"
885+
);
869886
#endif
870887

871888
#ifdef CONFIG_FINEIBT
@@ -1181,6 +1198,7 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
11811198
if (builtin) {
11821199
cfi_seed = get_random_u32();
11831200
cfi_bpf_hash = cfi_rehash(cfi_bpf_hash);
1201+
cfi_bpf_subprog_hash = cfi_rehash(cfi_bpf_subprog_hash);
11841202
}
11851203

11861204
ret = cfi_rand_preamble(start_cfi, end_cfi);

arch/x86/net/bpf_jit_comp.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,13 @@ static void pop_callee_regs(u8 **pprog, bool *callee_regs_used)
312312
* in arch/x86/kernel/alternative.c
313313
*/
314314

315-
static void emit_fineibt(u8 **pprog)
315+
static void emit_fineibt(u8 **pprog, bool is_subprog)
316316
{
317+
u32 hash = is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash;
317318
u8 *prog = *pprog;
318319

319320
EMIT_ENDBR();
320-
EMIT3_off32(0x41, 0x81, 0xea, cfi_bpf_hash); /* subl $hash, %r10d */
321+
EMIT3_off32(0x41, 0x81, 0xea, hash); /* subl $hash, %r10d */
321322
EMIT2(0x74, 0x07); /* jz.d8 +7 */
322323
EMIT2(0x0f, 0x0b); /* ud2 */
323324
EMIT1(0x90); /* nop */
@@ -326,11 +327,12 @@ static void emit_fineibt(u8 **pprog)
326327
*pprog = prog;
327328
}
328329

329-
static void emit_kcfi(u8 **pprog)
330+
static void emit_kcfi(u8 **pprog, bool is_subprog)
330331
{
332+
u32 hash = is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash;
331333
u8 *prog = *pprog;
332334

333-
EMIT1_off32(0xb8, cfi_bpf_hash); /* movl $hash, %eax */
335+
EMIT1_off32(0xb8, hash); /* movl $hash, %eax */
334336
#ifdef CONFIG_CALL_PADDING
335337
EMIT1(0x90);
336338
EMIT1(0x90);
@@ -349,17 +351,17 @@ static void emit_kcfi(u8 **pprog)
349351
*pprog = prog;
350352
}
351353

352-
static void emit_cfi(u8 **pprog)
354+
static void emit_cfi(u8 **pprog, bool is_subprog)
353355
{
354356
u8 *prog = *pprog;
355357

356358
switch (cfi_mode) {
357359
case CFI_FINEIBT:
358-
emit_fineibt(&prog);
360+
emit_fineibt(&prog, is_subprog);
359361
break;
360362

361363
case CFI_KCFI:
362-
emit_kcfi(&prog);
364+
emit_kcfi(&prog, is_subprog);
363365
break;
364366

365367
default:
@@ -381,7 +383,7 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
381383
{
382384
u8 *prog = *pprog;
383385

384-
emit_cfi(&prog);
386+
emit_cfi(&prog, is_subprog);
385387
/* BPF trampoline can be made to work without these nops,
386388
* but let's waste 5 bytes for now and optimize later
387389
*/

0 commit comments

Comments
 (0)