Skip to content

Commit 4dc6e55

Browse files
borkmanngregkh
authored andcommitted
bpf: Move sanitize_val_alu out of op switch
commit f528819 upstream. 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 <daniel@iogearbox.net> Reviewed-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> [fllinden@amazon.com: backported to 5.4] Signed-off-by: Frank van der Linden <fllinden@amazon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 876d1ce commit 4dc6e55

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4352,6 +4352,11 @@ static int sanitize_val_alu(struct bpf_verifier_env *env,
43524352
return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
43534353
}
43544354

4355+
static bool sanitize_needed(u8 opcode)
4356+
{
4357+
return opcode == BPF_ADD || opcode == BPF_SUB;
4358+
}
4359+
43554360
static int sanitize_ptr_alu(struct bpf_verifier_env *env,
43564361
struct bpf_insn *insn,
43574362
const struct bpf_reg_state *ptr_reg,
@@ -4753,11 +4758,14 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
47534758
return 0;
47544759
}
47554760

4756-
switch (opcode) {
4757-
case BPF_ADD:
4761+
if (sanitize_needed(opcode)) {
47584762
ret = sanitize_val_alu(env, insn);
47594763
if (ret < 0)
47604764
return sanitize_err(env, insn, ret, NULL, NULL);
4765+
}
4766+
4767+
switch (opcode) {
4768+
case BPF_ADD:
47614769
if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
47624770
signed_add_overflows(dst_reg->smax_value, smax_val)) {
47634771
dst_reg->smin_value = S64_MIN;
@@ -4777,9 +4785,6 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
47774785
dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
47784786
break;
47794787
case BPF_SUB:
4780-
ret = sanitize_val_alu(env, insn);
4781-
if (ret < 0)
4782-
return sanitize_err(env, insn, ret, NULL, NULL);
47834788
if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
47844789
signed_sub_overflows(dst_reg->smax_value, smin_val)) {
47854790
/* Overflow possible, we know nothing */

0 commit comments

Comments
 (0)