If a BPF program jumps to an ALU instruction that was combined with
a preceding MOV, it will incorrectly execute both the MOV and the
ALU operation.

Therefore, avoid 3-op folding if the second instruction is a jump
target.

Fixes: 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations")
Signed-off-by: Lukas Stockner <[email protected]>
---
 arch/powerpc/net/bpf_jit.h        | 4 ++--
 arch/powerpc/net/bpf_jit_comp.c   | 8 ++++----
 arch/powerpc/net/bpf_jit_comp32.c | 6 +++---
 arch/powerpc/net/bpf_jit_comp64.c | 5 +++--
 include/linux/bpf.h               | 2 ++
 kernel/bpf/core.c                 | 9 +++++++++
 6 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h
index 4da8bde92e1e..fea87cc88b85 100644
--- a/arch/powerpc/net/bpf_jit.h
+++ b/arch/powerpc/net/bpf_jit.h
@@ -224,8 +224,8 @@ static inline void bpf_clear_seen_register(struct 
codegen_context *ctx, int i)
 
 void bpf_jit_init_reg_mapping(struct codegen_context *ctx);
 int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context 
*ctx, u64 func);
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct 
codegen_context *ctx,
-                      u32 *addrs, int pass, bool extra_pass);
+int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 
*image, u32 *fimage,
+                      struct codegen_context *ctx, u32 *addrs, int pass, bool 
extra_pass);
 void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx);
 void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context 
*ctx);
 void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct 
codegen_context *ctx);
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index ce2966c52ac5..01151a78f2b2 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -289,7 +289,7 @@ struct bpf_prog *bpf_int_jit_compile(struct 
bpf_verifier_env *env, struct bpf_pr
        }
 
        /* Scouting faux-generate pass 0 */
-       if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false))
+       if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, false))
                /* We hit something illegal or unsupported. */
                goto out_err;
 
@@ -303,7 +303,7 @@ struct bpf_prog *bpf_int_jit_compile(struct 
bpf_verifier_env *env, struct bpf_pr
        if (cgctx.seen & SEEN_TAILCALL || 
!is_offset_in_branch_range((long)cgctx.idx * 4)) {
                cgctx.idx = 0;
                cgctx.alt_exit_addr = 0;
-               if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false))
+               if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, 
false))
                        goto out_err;
        }
 
@@ -344,8 +344,8 @@ struct bpf_prog *bpf_int_jit_compile(struct 
bpf_verifier_env *env, struct bpf_pr
                cgctx.exentry_idx = 0;
                cgctx.alt_exit_addr = 0;
                bpf_jit_build_prologue(code_base, &cgctx);
-               if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, 
addrs, pass,
-                                      extra_pass)) {
+               if (bpf_jit_build_body(env, fp, code_base, fcode_base, &cgctx, 
addrs,
+                                      pass, extra_pass)) {
                        bpf_arch_text_copy(&fhdr->size, &hdr->size, 
sizeof(hdr->size));
                        bpf_jit_binary_pack_free(fhdr, hdr);
                        goto out_err;
diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
b/arch/powerpc/net/bpf_jit_comp32.c
index 83457b21d0a8..ec2d5ce2f70a 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -334,8 +334,8 @@ static int bpf_jit_emit_tail_call(u32 *image, struct 
codegen_context *ctx, u32 o
 }
 
 /* Assemble the body code between the prologue & epilogue */
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct 
codegen_context *ctx,
-                      u32 *addrs, int pass, bool extra_pass)
+int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 
*image, u32 *fimage,
+                      struct codegen_context *ctx, u32 *addrs, int pass, bool 
extra_pass)
 {
        const struct bpf_insn *insn = fp->insnsi;
        int flen = fp->len;
@@ -368,7 +368,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 
*fimage, struct code
                    (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == 
BPF_ALU) &&
                    BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X &&
                    insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm 
!= 1 &&
-                   insn[i - 1].off == 0) {
+                   insn[i - 1].off == 0 && !bpf_insn_is_jump_target(env, fp, 
i)) {
                        src2_reg = bpf_to_ppc(insn[i - 1].src_reg);
                        src2_reg_h = src2_reg - 1;
                        if (BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i 
- 1].dst_reg) {
diff --git a/arch/powerpc/net/bpf_jit_comp64.c 
b/arch/powerpc/net/bpf_jit_comp64.c
index c1e86fe0bd79..d7edb2723f42 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -964,8 +964,9 @@ static int emit_atomic_ld_st(const struct bpf_insn insn, 
struct codegen_context
 }
 
 /* Assemble the body code between the prologue & epilogue */
-int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct 
codegen_context *ctx,
-                      u32 *addrs, int pass, bool extra_pass)
+int bpf_jit_build_body(struct bpf_verifier_env *env __always_unused, struct 
bpf_prog *fp,
+                      u32 *image, u32 *fimage, struct codegen_context *ctx, 
u32 *addrs, int pass,
+                      bool extra_pass)
 {
        enum stf_barrier_type stf_barrier = stf_barrier_type_get();
        bool sync_emitted, ori31_emitted;
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 1dd9db16f324..c1a7a0be3a9c 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1697,6 +1697,8 @@ void bpf_jit_uncharge_modmem(u32 size);
 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
 bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const 
struct bpf_prog *prog,
                                 int insn_idx);
+bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct 
bpf_prog *prog,
+                                int insn_idx);
 u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct 
bpf_prog *prog);
 #else
 static inline int bpf_trampoline_link_prog(struct bpf_tramp_node *node,
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 0bfd23a41401..27c200f43dd5 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -1662,6 +1662,15 @@ bool bpf_insn_is_indirect_target(const struct 
bpf_verifier_env *env, const struc
        return env->insn_aux_data[insn_idx].indirect_target;
 }
 
+bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct 
bpf_prog *prog,
+                                int insn_idx)
+{
+       if (!env)
+               return false;
+       insn_idx += prog->aux->subprog_start;
+       return env->insn_aux_data[insn_idx].jump_target;
+}
+
 u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct 
bpf_prog *prog)
 {
        const struct bpf_subprog_info *sub;
-- 
2.56.0


Reply via email to