All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] MIPS: eBPF: Fix icache flush end address
@ 2019-03-01 22:58 Paul Burton
  2019-03-01 23:07 ` Daniel Borkmann
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Burton @ 2019-03-01 22:58 UTC (permalink / raw)
  To: linux-mips, bpf, netdev
  Cc: linux-kernel, Paul Burton, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Yonghong Song, stable

The MIPS eBPF JIT calls flush_icache_range() in order to ensure the
icache observes the code that we just wrote. Unfortunately it gets the
end address calculation wrong due to some bad pointer arithmetic.

The struct jit_ctx target field is of type pointer to u32, and as such
adding one to it will increment the address being pointed to by 4 bytes.
Therefore in order to find the address of the end of the code we simply
need to add the number of 4 byte instructions emitted, but we mistakenly
add the number of instructions multiplied by 4. This results in the call
to flush_icache_range() operating on a memory region 4x larger than
intended, which is always wasteful and can cause crashes if we overrun
into an unmapped page.

Fix this by correcting the pointer arithmetic to remove the bogus
multiplication, and use braces to remove the need for a set of brackets
whilst also making it obvious that the target field is a pointer.

Signed-off-by: Paul Burton <paul.burton@mips.com>
Fixes: b6bd53f9c4e8 ("MIPS: Add missing file for eBPF JIT.")
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Cc: netdev@vger.kernel.org
Cc: bpf@vger.kernel.org
Cc: linux-mips@vger.kernel.org
Cc: stable@vger.kernel.org # v4.13+
---
 arch/mips/net/ebpf_jit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/net/ebpf_jit.c b/arch/mips/net/ebpf_jit.c
index 76e9bf88d3b9..0effd3cba9a7 100644
--- a/arch/mips/net/ebpf_jit.c
+++ b/arch/mips/net/ebpf_jit.c
@@ -1819,7 +1819,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 
 	/* Update the icache */
 	flush_icache_range((unsigned long)ctx.target,
-			   (unsigned long)(ctx.target + ctx.idx * sizeof(u32)));
+			   (unsigned long)&ctx.target[ctx.idx]);
 
 	if (bpf_jit_enable > 1)
 		/* Dump JIT code */
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] MIPS: eBPF: Fix icache flush end address
  2019-03-01 22:58 [PATCH] MIPS: eBPF: Fix icache flush end address Paul Burton
@ 2019-03-01 23:07 ` Daniel Borkmann
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Borkmann @ 2019-03-01 23:07 UTC (permalink / raw)
  To: Paul Burton, linux-mips, bpf, netdev
  Cc: linux-kernel, Paul Burton, Alexei Starovoitov, Martin KaFai Lau,
	Song Liu, Yonghong Song, stable

On 03/01/2019 11:58 PM, Paul Burton wrote:
> The MIPS eBPF JIT calls flush_icache_range() in order to ensure the
> icache observes the code that we just wrote. Unfortunately it gets the
> end address calculation wrong due to some bad pointer arithmetic.
> 
> The struct jit_ctx target field is of type pointer to u32, and as such
> adding one to it will increment the address being pointed to by 4 bytes.
> Therefore in order to find the address of the end of the code we simply
> need to add the number of 4 byte instructions emitted, but we mistakenly
> add the number of instructions multiplied by 4. This results in the call
> to flush_icache_range() operating on a memory region 4x larger than
> intended, which is always wasteful and can cause crashes if we overrun
> into an unmapped page.
> 
> Fix this by correcting the pointer arithmetic to remove the bogus
> multiplication, and use braces to remove the need for a set of brackets
> whilst also making it obvious that the target field is a pointer.
> 
> Signed-off-by: Paul Burton <paul.burton@mips.com>
> Fixes: b6bd53f9c4e8 ("MIPS: Add missing file for eBPF JIT.")
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Martin KaFai Lau <kafai@fb.com>
> Cc: Song Liu <songliubraving@fb.com>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: netdev@vger.kernel.org
> Cc: bpf@vger.kernel.org
> Cc: linux-mips@vger.kernel.org
> Cc: stable@vger.kernel.org # v4.13+

Good catch, applied to bpf, thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-01 23:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-01 22:58 [PATCH] MIPS: eBPF: Fix icache flush end address Paul Burton
2019-03-01 23:07 ` Daniel Borkmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.