Skip to content

Commit 604dca5

Browse files
thejhAlexei Starovoitov
authored andcommitted
bpf: Fix tnum constraints for 32-bit comparisons
The BPF verifier tried to track values based on 32-bit comparisons by (ab)using the tnum state via 581738a ("bpf: Provide better register bounds after jmp32 instructions"). The idea is that after a check like this: if ((u32)r0 > 3) exit We can't meaningfully constrain the arithmetic-range-based tracking, but we can update the tnum state to (value=0,mask=0xffff'ffff'0000'0003). However, the implementation from 581738a didn't compute the tnum constraint based on the fixed operand, but instead derives it from the arithmetic-range-based tracking. This means that after the following sequence of operations: if (r0 >= 0x1'0000'0001) exit if ((u32)r0 > 7) exit The verifier assumed that the lower half of r0 is in the range (0, 0) and apply the tnum constraint (value=0,mask=0xffff'ffff'0000'0000) thus causing the overall tnum to be (value=0,mask=0x1'0000'0000), which was incorrect. Provide a fixed implementation. Fixes: 581738a ("bpf: Provide better register bounds after jmp32 instructions") Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200330160324.15259-3-daniel@iogearbox.net
1 parent f2d67fe commit 604dca5

1 file changed

Lines changed: 72 additions & 36 deletions

File tree

‎kernel/bpf/verifier.c‎

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5678,6 +5678,70 @@ static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
56785678
reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
56795679
}
56805680

5681+
/* Constrain the possible values of @reg with unsigned upper bound @bound.
5682+
* If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive.
5683+
* If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits
5684+
* of @reg.
5685+
*/
5686+
static void set_upper_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5687+
bool is_exclusive)
5688+
{
5689+
if (is_exclusive) {
5690+
/* There are no values for `reg` that make `reg<0` true. */
5691+
if (bound == 0)
5692+
return;
5693+
bound--;
5694+
}
5695+
if (is_jmp32) {
5696+
/* Constrain the register's value in the tnum representation.
5697+
* For 64-bit comparisons this happens later in
5698+
* __reg_bound_offset(), but for 32-bit comparisons, we can be
5699+
* more precise than what can be derived from the updated
5700+
* numeric bounds.
5701+
*/
5702+
struct tnum t = tnum_range(0, bound);
5703+
5704+
t.mask |= ~0xffffffffULL; /* upper half is unknown */
5705+
reg->var_off = tnum_intersect(reg->var_off, t);
5706+
5707+
/* Compute the 64-bit bound from the 32-bit bound. */
5708+
bound += gen_hi_max(reg->var_off);
5709+
}
5710+
reg->umax_value = min(reg->umax_value, bound);
5711+
}
5712+
5713+
/* Constrain the possible values of @reg with unsigned lower bound @bound.
5714+
* If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive.
5715+
* If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits
5716+
* of @reg.
5717+
*/
5718+
static void set_lower_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5719+
bool is_exclusive)
5720+
{
5721+
if (is_exclusive) {
5722+
/* There are no values for `reg` that make `reg>MAX` true. */
5723+
if (bound == (is_jmp32 ? U32_MAX : U64_MAX))
5724+
return;
5725+
bound++;
5726+
}
5727+
if (is_jmp32) {
5728+
/* Constrain the register's value in the tnum representation.
5729+
* For 64-bit comparisons this happens later in
5730+
* __reg_bound_offset(), but for 32-bit comparisons, we can be
5731+
* more precise than what can be derived from the updated
5732+
* numeric bounds.
5733+
*/
5734+
struct tnum t = tnum_range(bound, U32_MAX);
5735+
5736+
t.mask |= ~0xffffffffULL; /* upper half is unknown */
5737+
reg->var_off = tnum_intersect(reg->var_off, t);
5738+
5739+
/* Compute the 64-bit bound from the 32-bit bound. */
5740+
bound += gen_hi_min(reg->var_off);
5741+
}
5742+
reg->umin_value = max(reg->umin_value, bound);
5743+
}
5744+
56815745
/* Adjusts the register min/max values in the case that the dst_reg is the
56825746
* variable register that we are working on, and src_reg is a constant or we're
56835747
* simply doing a BPF_K check.
@@ -5733,15 +5797,8 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg,
57335797
case BPF_JGE:
57345798
case BPF_JGT:
57355799
{
5736-
u64 false_umax = opcode == BPF_JGT ? val : val - 1;
5737-
u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
5738-
5739-
if (is_jmp32) {
5740-
false_umax += gen_hi_max(false_reg->var_off);
5741-
true_umin += gen_hi_min(true_reg->var_off);
5742-
}
5743-
false_reg->umax_value = min(false_reg->umax_value, false_umax);
5744-
true_reg->umin_value = max(true_reg->umin_value, true_umin);
5800+
set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5801+
set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
57455802
break;
57465803
}
57475804
case BPF_JSGE:
@@ -5762,15 +5819,8 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg,
57625819
case BPF_JLE:
57635820
case BPF_JLT:
57645821
{
5765-
u64 false_umin = opcode == BPF_JLT ? val : val + 1;
5766-
u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
5767-
5768-
if (is_jmp32) {
5769-
false_umin += gen_hi_min(false_reg->var_off);
5770-
true_umax += gen_hi_max(true_reg->var_off);
5771-
}
5772-
false_reg->umin_value = max(false_reg->umin_value, false_umin);
5773-
true_reg->umax_value = min(true_reg->umax_value, true_umax);
5822+
set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5823+
set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
57745824
break;
57755825
}
57765826
case BPF_JSLE:
@@ -5845,15 +5895,8 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
58455895
case BPF_JGE:
58465896
case BPF_JGT:
58475897
{
5848-
u64 false_umin = opcode == BPF_JGT ? val : val + 1;
5849-
u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
5850-
5851-
if (is_jmp32) {
5852-
false_umin += gen_hi_min(false_reg->var_off);
5853-
true_umax += gen_hi_max(true_reg->var_off);
5854-
}
5855-
false_reg->umin_value = max(false_reg->umin_value, false_umin);
5856-
true_reg->umax_value = min(true_reg->umax_value, true_umax);
5898+
set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5899+
set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
58575900
break;
58585901
}
58595902
case BPF_JSGE:
@@ -5871,15 +5914,8 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
58715914
case BPF_JLE:
58725915
case BPF_JLT:
58735916
{
5874-
u64 false_umax = opcode == BPF_JLT ? val : val - 1;
5875-
u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
5876-
5877-
if (is_jmp32) {
5878-
false_umax += gen_hi_max(false_reg->var_off);
5879-
true_umin += gen_hi_min(true_reg->var_off);
5880-
}
5881-
false_reg->umax_value = min(false_reg->umax_value, false_umax);
5882-
true_reg->umin_value = max(true_reg->umin_value, true_umin);
5917+
set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5918+
set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
58835919
break;
58845920
}
58855921
case BPF_JSLE:

0 commit comments

Comments
 (0)