From: Tianci Cao Extend ALU variant validation to accept off == 1 and off == 2 as UHMUL and SHMUL, respectively, for BPF_ALU64 MUL instructions. Both offsets remain reserved for BPF_ALU MUL, since the high-half variants are only defined for 64-bit operations. All other MUL offsets remain invalid. For scalar tracking, compute the exact high-half result when both operands are constants. Mark the destination unknown when either operand is variable until range analysis for these variants is added. Keep the existing scalar analysis for ordinary low-half MUL unchanged. Reject UHMUL and SHMUL when either operand is PTR_TO_ARENA. Arena arithmetic can be rewritten from BPF_ALU64 to BPF_ALU to enforce its 32-bit pointer representation, but such a rewrite cannot preserve the semantics of variants that are only defined for BPF_ALU64. Link: https://lore.kernel.org/bpf/CAADnVQ+2O2zgdy11+vxE6dOp+3Br4zZBysHF-Hne7cP+eeucnQ@mail.gmail.com/ Suggested-by: Alexei Starovoitov Signed-off-by: Tianci Cao Co-developed-by: Yazhou Tang Signed-off-by: Yazhou Tang Co-developed-by: Shenghao Yuan Signed-off-by: Shenghao Yuan --- kernel/bpf/verifier.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 92ab55ba349d..82a45550d2ee 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15056,6 +15056,24 @@ static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, cnum64_from_srange(smin, smax)); } +static void scalar_min_max_uhmul(struct bpf_reg_state *dst_reg, + struct bpf_reg_state *src_reg) +{ + u64 dst = reg_const_value(dst_reg, false); + u64 src = reg_const_value(src_reg, false); + + ___mark_reg_known(dst_reg, bpf_uhmul64(dst, src)); +} + +static void scalar_min_max_shmul(struct bpf_reg_state *dst_reg, + struct bpf_reg_state *src_reg) +{ + s64 dst = reg_const_value(dst_reg, false); + s64 src = reg_const_value(src_reg, false); + + ___mark_reg_known(dst_reg, bpf_shmul64(dst, src)); +} + static void scalar32_min_max_udiv(struct bpf_reg_state *dst_reg, struct bpf_reg_state *src_reg) { @@ -15752,6 +15770,25 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, dst_reg->var_off = tnum_neg(env->fake_reg[0].var_off); break; case BPF_MUL: + /* + * Range analysis for UHMUL/SHMUL is not implemented yet. + * Compute an exact result only when both operands are constants. + * Ordinary low-half MUL continues to use the analysis below. + */ + if (bpf_insn_is_hmul(insn) && + (!is_reg_const(dst_reg, false) || + !is_reg_const(&src_reg, false))) { + __mark_reg_unknown(env, dst_reg); + break; + } + if (!alu32 && insn->off == BPF_MUL_VARIANT_UHMUL) { + scalar_min_max_uhmul(dst_reg, &src_reg); + break; + } + if (!alu32 && insn->off == BPF_MUL_VARIANT_SHMUL) { + scalar_min_max_shmul(dst_reg, &src_reg); + break; + } dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); scalar32_min_max_mul(dst_reg, &src_reg); scalar_min_max_mul(dst_reg, &src_reg); @@ -15877,6 +15914,11 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, if (dst_reg->type == PTR_TO_ARENA || (src_reg && src_reg->type == PTR_TO_ARENA)) { struct bpf_insn_aux_data *aux = cur_aux(env); + if (bpf_insn_is_hmul(insn)) { + verbose(env, "UHMUL/SHMUL with arena pointers are not supported\n"); + return -EOPNOTSUPP; + } + if (dst_reg->type != PTR_TO_ARENA) *dst_reg = *src_reg; @@ -19111,6 +19153,9 @@ static int fd_array_get_map_idx(struct bpf_verifier_env *env, u32 idx) static bool is_valid_alu_variant(const struct bpf_insn *insn) { switch (BPF_OP(insn->code)) { + case BPF_MUL: + return insn->off == BPF_MUL_VARIANT_LO || + bpf_insn_is_hmul(insn); case BPF_DIV: case BPF_MOD: /* off == 1 selects SDIV/SMOD. */ -- 2.43.0