Skip to content

Commit a552e2e

Browse files
pccborkmann
authored andcommitted
bpf, arm64: Fix address emission with tag-based KASAN enabled
When BPF_TRAMP_F_CALL_ORIG is enabled, the address of a bpf_tramp_image struct on the stack is passed during the size calculation pass and an address on the heap is passed during code generation. This may cause a heap buffer overflow if the heap address is tagged because emit_a64_mov_i64() will emit longer code than it did during the size calculation pass. The same problem could occur without tag-based KASAN if one of the 16-bit words of the stack address happened to be all-ones during the size calculation pass. Fix the problem by assuming the worst case (4 instructions) when calculating the size of the bpf_tramp_image address emission. Fixes: 19d3c17 ("bpf, arm64: Fix trampoline for BPF_TRAMP_F_CALL_ORIG") Signed-off-by: Peter Collingbourne <pcc@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Xu Kuohai <xukuohai@huawei.com> Link: https://linux-review.googlesource.com/id/I1496f2bc24fba7a1d492e16e2b94cf43714f2d3c Link: https://lore.kernel.org/bpf/20241018221644.3240898-1-pcc@google.com
1 parent 42f7652 commit a552e2e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

arch/arm64/net/bpf_jit_comp.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
22202220
emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
22212221

22222222
if (flags & BPF_TRAMP_F_CALL_ORIG) {
2223-
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
2223+
/* for the first pass, assume the worst case */
2224+
if (!ctx->image)
2225+
ctx->idx += 4;
2226+
else
2227+
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
22242228
emit_call((const u64)__bpf_tramp_enter, ctx);
22252229
}
22262230

@@ -2264,7 +2268,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
22642268

22652269
if (flags & BPF_TRAMP_F_CALL_ORIG) {
22662270
im->ip_epilogue = ctx->ro_image + ctx->idx;
2267-
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
2271+
/* for the first pass, assume the worst case */
2272+
if (!ctx->image)
2273+
ctx->idx += 4;
2274+
else
2275+
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
22682276
emit_call((const u64)__bpf_tramp_exit, ctx);
22692277
}
22702278

0 commit comments

Comments
 (0)