LoongArch: BPF: Prevent out-of-bounds memory access
[platform/kernel/linux-starfive.git] / arch / loongarch / net / bpf_jit.c
index 00915fb..9eb7753 100644 (file)
@@ -461,7 +461,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
        const u8 dst = regmap[insn->dst_reg];
        const s16 off = insn->off;
        const s32 imm = insn->imm;
-       const u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm;
        const bool is32 = BPF_CLASS(insn->code) == BPF_ALU || BPF_CLASS(insn->code) == BPF_JMP32;
 
        switch (code) {
@@ -865,8 +864,12 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
 
        /* dst = imm64 */
        case BPF_LD | BPF_IMM | BPF_DW:
+       {
+               const u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm;
+
                move_imm(ctx, dst, imm64, is32);
                return 1;
+       }
 
        /* dst = *(size *)(src + off) */
        case BPF_LDX | BPF_MEM | BPF_B: