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+
117126static 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