]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
MIPS: BPF: Quit clobbering callee saved registers in JIT code.
authorDavid Daney <david.daney@cavium.com>
Tue, 14 Mar 2017 21:21:43 +0000 (14:21 -0700)
committerSasha Levin <alexander.levin@microsoft.com>
Wed, 23 May 2018 01:33:45 +0000 (21:33 -0400)
[ Upstream commit 1ef0910cfd681f0bd0b81f8809935b2006e9cfb9 ]

If bpf_needs_clear_a() returns true, only actually clear it if it is
ever used.  If it is not used, we don't save and restore it, so the
clearing has the nasty side effect of clobbering caller state.

Also, don't emit stack pointer adjustment instructions if the
adjustment amount is zero.

Signed-off-by: David Daney <david.daney@cavium.com>
Cc: James Hogan <james.hogan@imgtec.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Steven J. Hill <steven.hill@cavium.com>
Cc: linux-mips@linux-mips.org
Cc: netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/15745/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
arch/mips/net/bpf_jit.c

index d6d27d51d1310601348b3a22410b64e8ac401f96..5cf32b77f128c0c067ac2b167fb6ac2e6380a8ec 100644 (file)
@@ -562,7 +562,8 @@ static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
        u32 sflags, tmp_flags;
 
        /* Adjust the stack pointer */
-       emit_stack_offset(-align_sp(offset), ctx);
+       if (offset)
+               emit_stack_offset(-align_sp(offset), ctx);
 
        if (ctx->flags & SEEN_CALL) {
                /* Argument save area */
@@ -641,7 +642,8 @@ static void restore_bpf_jit_regs(struct jit_ctx *ctx,
                emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
 
        /* Restore the sp and discard the scrach memory */
-       emit_stack_offset(align_sp(offset), ctx);
+       if (offset)
+               emit_stack_offset(align_sp(offset), ctx);
 }
 
 static unsigned int get_stack_depth(struct jit_ctx *ctx)
@@ -685,8 +687,14 @@ static void build_prologue(struct jit_ctx *ctx)
        if (ctx->flags & SEEN_X)
                emit_jit_reg_move(r_X, r_zero, ctx);
 
-       /* Do not leak kernel data to userspace */
-       if (bpf_needs_clear_a(&ctx->skf->insns[0]))
+       /*
+        * Do not leak kernel data to userspace, we only need to clear
+        * r_A if it is ever used.  In fact if it is never used, we
+        * will not save/restore it, so clearing it in this case would
+        * corrupt the state of the caller.
+        */
+       if (bpf_needs_clear_a(&ctx->skf->insns[0]) &&
+           (ctx->flags & SEEN_A))
                emit_jit_reg_move(r_A, r_zero, ctx);
 }