Skip to content

Commit 9e80f36

Browse files
pccgregkh
authored andcommitted
bpf, arm64: Fix address emission with tag-based KASAN enabled
commit a552e2ef5fd1a6c78267cd4ec5a9b49aa11bbb1c upstream. 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: 19d3c179a377 ("bpf, arm64: Fix trampoline for BPF_TRAMP_F_CALL_ORIG") Signed-off-by: Peter Collingbourne <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Xu Kuohai <[email protected]> Link: https://linux-review.googlesource.com/id/I1496f2bc24fba7a1d492e16e2b94cf43714f2d3c Link: https://lore.kernel.org/bpf/[email protected] [Minor context change fixed.] Signed-off-by: Bin Lan <[email protected]> Signed-off-by: He Zhe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0771494 commit 9e80f36

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

arch/arm64/net/bpf_jit_comp.c

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

19441944
if (flags & BPF_TRAMP_F_CALL_ORIG) {
1945-
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
1945+
/* for the first pass, assume the worst case */
1946+
if (!ctx->image)
1947+
ctx->idx += 4;
1948+
else
1949+
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
19461950
emit_call((const u64)__bpf_tramp_enter, ctx);
19471951
}
19481952

@@ -1986,7 +1990,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
19861990

19871991
if (flags & BPF_TRAMP_F_CALL_ORIG) {
19881992
im->ip_epilogue = ctx->image + ctx->idx;
1989-
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
1993+
/* for the first pass, assume the worst case */
1994+
if (!ctx->image)
1995+
ctx->idx += 4;
1996+
else
1997+
emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
19901998
emit_call((const u64)__bpf_tramp_exit, ctx);
19911999
}
19922000

0 commit comments

Comments
 (0)