Skip to content

Commit 30a59cc

Browse files
Pu LehuiAlexei Starovoitov
authored andcommitted
riscv, bpf: Fix possible infinite tailcall when CONFIG_CFI_CLANG is enabled
When CONFIG_CFI_CLANG is enabled, the number of prologue instructions skipped by tailcall needs to include the kcfi instruction, otherwise the TCC will be initialized every tailcall is called, which may result in infinite tailcalls. Fixes: e63985e ("bpf, riscv64/cfi: Support kCFI + BPF on riscv64") Signed-off-by: Pu Lehui <pulehui@huawei.com> Acked-by: Björn Töpel <bjorn@kernel.org> Link: https://lore.kernel.org/r/20241008124544.171161-1-pulehui@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 4538a38 commit 30a59cc

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define RV_MAX_REG_ARGS 8
1919
#define RV_FENTRY_NINSNS 2
2020
#define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
21+
#define RV_KCFI_NINSNS (IS_ENABLED(CONFIG_CFI_CLANG) ? 1 : 0)
2122
/* imm that allows emit_imm to emit max count insns */
2223
#define RV_MAX_COUNT_IMM 0x7FFF7FF7FF7FF7FF
2324

@@ -271,7 +272,8 @@ static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
271272
if (!is_tail_call)
272273
emit_addiw(RV_REG_A0, RV_REG_A5, 0, ctx);
273274
emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
274-
is_tail_call ? (RV_FENTRY_NINSNS + 1) * 4 : 0, /* skip reserved nops and TCC init */
275+
/* kcfi, fentry and TCC init insns will be skipped on tailcall */
276+
is_tail_call ? (RV_KCFI_NINSNS + RV_FENTRY_NINSNS + 1) * 4 : 0,
275277
ctx);
276278
}
277279

0 commit comments

Comments
 (0)