[PATCH RESEND bpf 1/2] x86/bpf: Fix IP after emitting call depth accounting

Uros Bizjak posted 2 patches 1 year, 10 months ago
There is a newer version of this series
[PATCH RESEND bpf 1/2] x86/bpf: Fix IP after emitting call depth accounting
Posted by Uros Bizjak 1 year, 10 months ago
From: Joan Bruguera Micó <joanbrugueram@gmail.com>

Adjust the IP passed to `emit_patch` so it calculates the correct offset
for the CALL instruction if `x86_call_depth_emit_accounting` emits code.
Otherwise we will skip some instructions and most likely crash.

Fixes: b2e9dfe54be4 ("x86/bpf: Emit call depth accounting if required")
Link: https://lore.kernel.org/lkml/20230105214922.250473-1-joanbrugueram@gmail.com/
Signed-off-by: Joan Bruguera Micó <joanbrugueram@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
---
 arch/x86/net/bpf_jit_comp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index a7ba8e178645..09f7dc9d4d65 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -479,9 +479,10 @@ static int emit_call(u8 **pprog, void *func, void *ip)
 
 static int emit_rsb_call(u8 **pprog, void *func, void *ip)
 {
+	void *adjusted_ip;
 	OPTIMIZER_HIDE_VAR(func);
-	x86_call_depth_emit_accounting(pprog, func);
-	return emit_patch(pprog, func, ip, 0xE8);
+	adjusted_ip = ip + x86_call_depth_emit_accounting(pprog, func);
+	return emit_patch(pprog, func, adjusted_ip, 0xE8);
 }
 
 static int emit_jump(u8 **pprog, void *func, void *ip)
-- 
2.44.0

Re: [PATCH RESEND bpf 1/2] x86/bpf: Fix IP after emitting call depth accounting
Posted by Alexei Starovoitov 1 year, 10 months ago
On Fri, Mar 29, 2024 at 2:49 AM Uros Bizjak <ubizjak@gmail.com> wrote:
>
> From: Joan Bruguera Micó <joanbrugueram@gmail.com>
>
> Adjust the IP passed to `emit_patch` so it calculates the correct offset
> for the CALL instruction if `x86_call_depth_emit_accounting` emits code.
> Otherwise we will skip some instructions and most likely crash.
>
> Fixes: b2e9dfe54be4 ("x86/bpf: Emit call depth accounting if required")
> Link: https://lore.kernel.org/lkml/20230105214922.250473-1-joanbrugueram@gmail.com/
> Signed-off-by: Joan Bruguera Micó <joanbrugueram@gmail.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> ---
>  arch/x86/net/bpf_jit_comp.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index a7ba8e178645..09f7dc9d4d65 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -479,9 +479,10 @@ static int emit_call(u8 **pprog, void *func, void *ip)
>
>  static int emit_rsb_call(u8 **pprog, void *func, void *ip)
>  {
> +       void *adjusted_ip;
>         OPTIMIZER_HIDE_VAR(func);
> -       x86_call_depth_emit_accounting(pprog, func);
> -       return emit_patch(pprog, func, ip, 0xE8);
> +       adjusted_ip = ip + x86_call_depth_emit_accounting(pprog, func);

Why not just
ip += x86_call_depth_emit_accounting(pprog, func);

?

> +       return emit_patch(pprog, func, adjusted_ip, 0xE8);
>  }
>
>  static int emit_jump(u8 **pprog, void *func, void *ip)
> --
> 2.44.0
>