Skip to content

Commit 710618c

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
arm64/cfi,bpf: Support kCFI + BPF on arm64
Currently, bpf_dispatcher_*_func() is marked with `__nocfi` therefore calling BPF programs from this interface doesn't cause CFI warnings. When BPF programs are called directly from C: from BPF helpers or struct_ops, CFI warnings are generated. Implement proper CFI prologues for the BPF programs and callbacks and drop __nocfi for arm64. Fix the trampoline generation code to emit kCFI prologue when a struct_ops trampoline is being prepared. Signed-off-by: Puranjay Mohan <[email protected]> Co-developed-by: Maxwell Bland <[email protected]> Signed-off-by: Maxwell Bland <[email protected]> Co-developed-by: Sami Tolvanen <[email protected]> Signed-off-by: Sami Tolvanen <[email protected]> Tested-by: Dao Huang <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f1befc8 commit 710618c

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

arch/arm64/include/asm/cfi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_ARM64_CFI_H
3+
#define _ASM_ARM64_CFI_H
4+
5+
#define __bpfcall
6+
7+
#endif /* _ASM_ARM64_CFI_H */

arch/arm64/net/bpf_jit_comp.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/arm-smccc.h>
1111
#include <linux/bitfield.h>
1212
#include <linux/bpf.h>
13+
#include <linux/cfi.h>
1314
#include <linux/filter.h>
1415
#include <linux/memory.h>
1516
#include <linux/printk.h>
@@ -114,6 +115,14 @@ static inline void emit(const u32 insn, struct jit_ctx *ctx)
114115
ctx->idx++;
115116
}
116117

118+
static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx)
119+
{
120+
if (ctx->image != NULL && ctx->write)
121+
ctx->image[ctx->idx] = data;
122+
123+
ctx->idx++;
124+
}
125+
117126
static inline void emit_a64_mov_i(const int is64, const int reg,
118127
const s32 val, struct jit_ctx *ctx)
119128
{
@@ -174,6 +183,12 @@ static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
174183
emit(insn, ctx);
175184
}
176185

186+
static inline void emit_kcfi(u32 hash, struct jit_ctx *ctx)
187+
{
188+
if (IS_ENABLED(CONFIG_CFI_CLANG))
189+
emit_u32_data(hash, ctx);
190+
}
191+
177192
/*
178193
* Kernel addresses in the vmalloc space use at most 48 bits, and the
179194
* remaining bits are guaranteed to be 0x1. So we can compose the address
@@ -503,7 +518,6 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
503518
const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
504519
const u8 priv_sp = bpf2a64[PRIVATE_SP];
505520
void __percpu *priv_stack_ptr;
506-
const int idx0 = ctx->idx;
507521
int cur_offset;
508522

509523
/*
@@ -529,6 +543,9 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
529543
*
530544
*/
531545

546+
emit_kcfi(is_main_prog ? cfi_bpf_hash : cfi_bpf_subprog_hash, ctx);
547+
const int idx0 = ctx->idx;
548+
532549
/* bpf function may be invoked by 3 instruction types:
533550
* 1. bl, attached via freplace to bpf prog via short jump
534551
* 2. br, attached via freplace to bpf prog via long jump
@@ -2146,9 +2163,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21462163
jit_data->ro_header = ro_header;
21472164
}
21482165

2149-
prog->bpf_func = (void *)ctx.ro_image;
2166+
prog->bpf_func = (void *)ctx.ro_image + cfi_get_offset();
21502167
prog->jited = 1;
2151-
prog->jited_len = prog_size;
2168+
prog->jited_len = prog_size - cfi_get_offset();
21522169

21532170
if (!prog->is_func || extra_pass) {
21542171
int i;
@@ -2527,6 +2544,12 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
25272544
/* return address locates above FP */
25282545
retaddr_off = stack_size + 8;
25292546

2547+
if (flags & BPF_TRAMP_F_INDIRECT) {
2548+
/*
2549+
* Indirect call for bpf_struct_ops
2550+
*/
2551+
emit_kcfi(cfi_get_func_hash(func_addr), ctx);
2552+
}
25302553
/* bpf trampoline may be invoked by 3 instruction types:
25312554
* 1. bl, attached to bpf prog or kernel function via short jump
25322555
* 2. br, attached to bpf prog or kernel function via long jump
@@ -3045,6 +3068,7 @@ void bpf_jit_free(struct bpf_prog *prog)
30453068
sizeof(jit_data->header->size));
30463069
kfree(jit_data);
30473070
}
3071+
prog->bpf_func -= cfi_get_offset();
30483072
hdr = bpf_jit_binary_pack_hdr(prog);
30493073
bpf_jit_binary_pack_free(hdr, NULL);
30503074
priv_stack_ptr = prog->aux->priv_stack_ptr;

0 commit comments

Comments
 (0)