* [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline
@ 2023-10-10 20:20 Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 1/2] s390/bpf: Fix clobbering the caller's backchain " Ilya Leoshkevich
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2023-10-10 20:20 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: bpf, Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Song Liu,
Ilya Leoshkevich
Hi,
Song reported that a patch he wrote was causing kernel panics on s390.
The disassembly printed by the kernel indicated that the stored
backchain was not a valid pointer; setting a watchpoint in GDB has
shown the culprit: the trampoline.
Currently it's implemented without regard for backchain: it clobbers
the caller's backchain and causes the issue reported by Song, and also
doesn't store its own, making it impossible to unwind past itself.
This series fixes both problems.
Best regards,
Ilya
[1] https://lore.kernel.org/bpf/20231004004350.533234-1-song@kernel.org/
Ilya Leoshkevich (2):
s390/bpf: Fix clobbering the caller's backchain in the trampoline
s390/bpf: Fix unwinding past the trampoline
arch/s390/net/bpf_jit_comp.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)
--
2.41.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf 1/2] s390/bpf: Fix clobbering the caller's backchain in the trampoline
2023-10-10 20:20 [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline Ilya Leoshkevich
@ 2023-10-10 20:20 ` Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 2/2] s390/bpf: Fix unwinding past " Ilya Leoshkevich
2023-10-10 22:10 ` [PATCH bpf 0/2] s390/bpf: Fix backchain issues in " patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2023-10-10 20:20 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: bpf, Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Song Liu,
Ilya Leoshkevich
One of the first things that s390x kernel functions do is storing the
the caller's frame address (backchain) on stack. This makes unwinding
possible. The backchain is always stored at frame offset 152, which is
inside the 160-byte stack area, that the functions allocate for their
callees. The callees must preserve the backchain; the remaining 152
bytes they may use as they please.
Currently the trampoline uses all 160 bytes, clobbering the backchain.
This causes kernel panics when using __builtin_return_address() in
functions called by the trampoline.
Fix by reducing the usage of the caller-reserved stack area by 8 bytes
in the trampoline.
Fixes: 528eb2cb87bc ("s390/bpf: Implement arch_prepare_bpf_trampoline()")
Reported-by: Song Liu <song@kernel.org>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
arch/s390/net/bpf_jit_comp.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index 9ed0a13865ca..8955bc80270a 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -2399,8 +2399,12 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
tjit->run_ctx_off = alloc_stack(tjit,
sizeof(struct bpf_tramp_run_ctx));
tjit->tccnt_off = alloc_stack(tjit, sizeof(u64));
- /* The caller has already reserved STACK_FRAME_OVERHEAD bytes. */
- tjit->stack_size -= STACK_FRAME_OVERHEAD;
+ /*
+ * In accordance with the s390x ABI, the caller has allocated
+ * STACK_FRAME_OVERHEAD bytes for us. 8 of them contain the caller's
+ * backchain, and the rest we can use.
+ */
+ tjit->stack_size -= STACK_FRAME_OVERHEAD - sizeof(u64);
tjit->orig_stack_args_off = tjit->stack_size + STACK_FRAME_OVERHEAD;
/* aghi %r15,-stack_size */
--
2.41.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH bpf 2/2] s390/bpf: Fix unwinding past the trampoline
2023-10-10 20:20 [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 1/2] s390/bpf: Fix clobbering the caller's backchain " Ilya Leoshkevich
@ 2023-10-10 20:20 ` Ilya Leoshkevich
2023-10-10 22:10 ` [PATCH bpf 0/2] s390/bpf: Fix backchain issues in " patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Ilya Leoshkevich @ 2023-10-10 20:20 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: bpf, Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Song Liu,
Ilya Leoshkevich
When functions called by the trampoline panic, the backtrace that is
printed stops at the trampoline, because the trampoline does not store
its caller's frame address (backchain) on stack; it also stores the
return address at a wrong location.
Store both the same way as is already done for the regular eBPF
programs.
Fixes: 528eb2cb87bc ("s390/bpf: Implement arch_prepare_bpf_trampoline()")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
arch/s390/net/bpf_jit_comp.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index 8955bc80270a..082b913f214e 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -2205,6 +2205,7 @@ struct bpf_tramp_jit {
* func_addr's original caller
*/
int stack_size; /* Trampoline stack size */
+ int backchain_off; /* Offset of backchain */
int stack_args_off; /* Offset of stack arguments for calling
* func_addr, has to be at the top
*/
@@ -2225,9 +2226,10 @@ struct bpf_tramp_jit {
* for __bpf_prog_enter() return value and
* func_addr respectively
*/
- int r14_off; /* Offset of saved %r14 */
int run_ctx_off; /* Offset of struct bpf_tramp_run_ctx */
int tccnt_off; /* Offset of saved tailcall counter */
+ int r14_off; /* Offset of saved %r14, has to be at the
+ * bottom */
int do_fexit; /* do_fexit: label */
};
@@ -2386,8 +2388,12 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
* Calculate the stack layout.
*/
- /* Reserve STACK_FRAME_OVERHEAD bytes for the callees. */
+ /*
+ * Allocate STACK_FRAME_OVERHEAD bytes for the callees. As the s390x
+ * ABI requires, put our backchain at the end of the allocated memory.
+ */
tjit->stack_size = STACK_FRAME_OVERHEAD;
+ tjit->backchain_off = tjit->stack_size - sizeof(u64);
tjit->stack_args_off = alloc_stack(tjit, nr_stack_args * sizeof(u64));
tjit->reg_args_off = alloc_stack(tjit, nr_reg_args * sizeof(u64));
tjit->ip_off = alloc_stack(tjit, sizeof(u64));
@@ -2395,10 +2401,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
tjit->bpf_args_off = alloc_stack(tjit, nr_bpf_args * sizeof(u64));
tjit->retval_off = alloc_stack(tjit, sizeof(u64));
tjit->r7_r8_off = alloc_stack(tjit, 2 * sizeof(u64));
- tjit->r14_off = alloc_stack(tjit, sizeof(u64));
tjit->run_ctx_off = alloc_stack(tjit,
sizeof(struct bpf_tramp_run_ctx));
tjit->tccnt_off = alloc_stack(tjit, sizeof(u64));
+ tjit->r14_off = alloc_stack(tjit, sizeof(u64) * 2);
/*
* In accordance with the s390x ABI, the caller has allocated
* STACK_FRAME_OVERHEAD bytes for us. 8 of them contain the caller's
@@ -2407,8 +2413,13 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
tjit->stack_size -= STACK_FRAME_OVERHEAD - sizeof(u64);
tjit->orig_stack_args_off = tjit->stack_size + STACK_FRAME_OVERHEAD;
+ /* lgr %r1,%r15 */
+ EMIT4(0xb9040000, REG_1, REG_15);
/* aghi %r15,-stack_size */
EMIT4_IMM(0xa70b0000, REG_15, -tjit->stack_size);
+ /* stg %r1,backchain_off(%r15) */
+ EMIT6_DISP_LH(0xe3000000, 0x0024, REG_1, REG_0, REG_15,
+ tjit->backchain_off);
/* mvc tccnt_off(4,%r15),stack_size+STK_OFF_TCCNT(%r15) */
_EMIT6(0xd203f000 | tjit->tccnt_off,
0xf000 | (tjit->stack_size + STK_OFF_TCCNT));
--
2.41.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline
2023-10-10 20:20 [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 1/2] s390/bpf: Fix clobbering the caller's backchain " Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 2/2] s390/bpf: Fix unwinding past " Ilya Leoshkevich
@ 2023-10-10 22:10 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-10-10 22:10 UTC (permalink / raw)
To: Ilya Leoshkevich; +Cc: ast, daniel, andrii, bpf, hca, gor, agordeev, song
Hello:
This series was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:
On Tue, 10 Oct 2023 22:20:08 +0200 you wrote:
> Hi,
>
> Song reported that a patch he wrote was causing kernel panics on s390.
> The disassembly printed by the kernel indicated that the stored
> backchain was not a valid pointer; setting a watchpoint in GDB has
> shown the culprit: the trampoline.
>
> [...]
Here is the summary with links:
- [bpf,1/2] s390/bpf: Fix clobbering the caller's backchain in the trampoline
https://git.kernel.org/bpf/bpf/c/ce10fc0604bc
- [bpf,2/2] s390/bpf: Fix unwinding past the trampoline
https://git.kernel.org/bpf/bpf/c/5356ba1ff4f2
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-10-10 22:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-10 20:20 [PATCH bpf 0/2] s390/bpf: Fix backchain issues in the trampoline Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 1/2] s390/bpf: Fix clobbering the caller's backchain " Ilya Leoshkevich
2023-10-10 20:20 ` [PATCH bpf 2/2] s390/bpf: Fix unwinding past " Ilya Leoshkevich
2023-10-10 22:10 ` [PATCH bpf 0/2] s390/bpf: Fix backchain issues in " patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox