Skip to content

Commit f528819

Browse files
committed
bpf: Move sanitize_val_alu out of op switch
Add a small sanitize_needed() helper function and move sanitize_val_alu() out of the main opcode switch. In upcoming work, we'll move sanitize_ptr_alu() as well out of its opcode switch so this helps to streamline both. Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent 073815b commit f528819

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

kernel/bpf/verifier.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5945,6 +5945,11 @@ static int sanitize_val_alu(struct bpf_verifier_env *env,
59455945
return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
59465946
}
59475947

5948+
static bool sanitize_needed(u8 opcode)
5949+
{
5950+
return opcode == BPF_ADD || opcode == BPF_SUB;
5951+
}
5952+
59485953
static int sanitize_ptr_alu(struct bpf_verifier_env *env,
59495954
struct bpf_insn *insn,
59505955
const struct bpf_reg_state *ptr_reg,
@@ -6968,6 +6973,12 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
69686973
return 0;
69696974
}
69706975

6976+
if (sanitize_needed(opcode)) {
6977+
ret = sanitize_val_alu(env, insn);
6978+
if (ret < 0)
6979+
return sanitize_err(env, insn, ret, NULL, NULL);
6980+
}
6981+
69716982
/* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops.
69726983
* There are two classes of instructions: The first class we track both
69736984
* alu32 and alu64 sign/unsigned bounds independently this provides the
@@ -6984,17 +6995,11 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
69846995
*/
69856996
switch (opcode) {
69866997
case BPF_ADD:
6987-
ret = sanitize_val_alu(env, insn);
6988-
if (ret < 0)
6989-
return sanitize_err(env, insn, ret, NULL, NULL);
69906998
scalar32_min_max_add(dst_reg, &src_reg);
69916999
scalar_min_max_add(dst_reg, &src_reg);
69927000
dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
69937001
break;
69947002
case BPF_SUB:
6995-
ret = sanitize_val_alu(env, insn);
6996-
if (ret < 0)
6997-
return sanitize_err(env, insn, ret, NULL, NULL);
69987003
scalar32_min_max_sub(dst_reg, &src_reg);
69997004
scalar_min_max_sub(dst_reg, &src_reg);
70007005
dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);

0 commit comments

Comments
 (0)