Skip to content

Commit 581fc0a

Browse files
thejhgregkh
authored andcommitted
bpf: Fix tnum constraints for 32-bit comparisons
[ Upstream commit 604dca5 ] 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 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 71bab39 commit 581fc0a

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
@@ -5550,6 +5550,70 @@ static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
55505550
reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
55515551
}
55525552

5553+
/* Constrain the possible values of @reg with unsigned upper bound @bound.
5554+
* If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive.
5555+
* If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits
5556+
* of @reg.
5557+
*/
5558+
static void set_upper_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5559+
bool is_exclusive)
5560+
{
5561+
if (is_exclusive) {
5562+
/* There are no values for `reg` that make `reg<0` true. */
5563+
if (bound == 0)
5564+
return;
5565+
bound--;
5566+
}
5567+
if (is_jmp32) {
5568+
/* Constrain the register's value in the tnum representation.
5569+
* For 64-bit comparisons this happens later in
5570+
* __reg_bound_offset(), but for 32-bit comparisons, we can be
5571+
* more precise than what can be derived from the updated
5572+
* numeric bounds.
5573+
*/
5574+
struct tnum t = tnum_range(0, bound);
5575+
5576+
t.mask |= ~0xffffffffULL; /* upper half is unknown */
5577+
reg->var_off = tnum_intersect(reg->var_off, t);
5578+
5579+
/* Compute the 64-bit bound from the 32-bit bound. */
5580+
bound += gen_hi_max(reg->var_off);
5581+
}
5582+
reg->umax_value = min(reg->umax_value, bound);
5583+
}
5584+
5585+
/* Constrain the possible values of @reg with unsigned lower bound @bound.
5586+
* If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive.
5587+
* If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits
5588+
* of @reg.
5589+
*/
5590+
static void set_lower_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5591+
bool is_exclusive)
5592+
{
5593+
if (is_exclusive) {
5594+
/* There are no values for `reg` that make `reg>MAX` true. */
5595+
if (bound == (is_jmp32 ? U32_MAX : U64_MAX))
5596+
return;
5597+
bound++;
5598+
}
5599+
if (is_jmp32) {
5600+
/* Constrain the register's value in the tnum representation.
5601+
* For 64-bit comparisons this happens later in
5602+
* __reg_bound_offset(), but for 32-bit comparisons, we can be
5603+
* more precise than what can be derived from the updated
5604+
* numeric bounds.
5605+
*/
5606+
struct tnum t = tnum_range(bound, U32_MAX);
5607+
5608+
t.mask |= ~0xffffffffULL; /* upper half is unknown */
5609+
reg->var_off = tnum_intersect(reg->var_off, t);
5610+
5611+
/* Compute the 64-bit bound from the 32-bit bound. */
5612+
bound += gen_hi_min(reg->var_off);
5613+
}
5614+
reg->umin_value = max(reg->umin_value, bound);
5615+
}
5616+
55535617
/* Adjusts the register min/max values in the case that the dst_reg is the
55545618
* variable register that we are working on, and src_reg is a constant or we're
55555619
* simply doing a BPF_K check.
@@ -5605,15 +5669,8 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg,
56055669
case BPF_JGE:
56065670
case BPF_JGT:
56075671
{
5608-
u64 false_umax = opcode == BPF_JGT ? val : val - 1;
5609-
u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
5610-
5611-
if (is_jmp32) {
5612-
false_umax += gen_hi_max(false_reg->var_off);
5613-
true_umin += gen_hi_min(true_reg->var_off);
5614-
}
5615-
false_reg->umax_value = min(false_reg->umax_value, false_umax);
5616-
true_reg->umin_value = max(true_reg->umin_value, true_umin);
5672+
set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5673+
set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
56175674
break;
56185675
}
56195676
case BPF_JSGE:
@@ -5634,15 +5691,8 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg,
56345691
case BPF_JLE:
56355692
case BPF_JLT:
56365693
{
5637-
u64 false_umin = opcode == BPF_JLT ? val : val + 1;
5638-
u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
5639-
5640-
if (is_jmp32) {
5641-
false_umin += gen_hi_min(false_reg->var_off);
5642-
true_umax += gen_hi_max(true_reg->var_off);
5643-
}
5644-
false_reg->umin_value = max(false_reg->umin_value, false_umin);
5645-
true_reg->umax_value = min(true_reg->umax_value, true_umax);
5694+
set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5695+
set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
56465696
break;
56475697
}
56485698
case BPF_JSLE:
@@ -5717,15 +5767,8 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
57175767
case BPF_JGE:
57185768
case BPF_JGT:
57195769
{
5720-
u64 false_umin = opcode == BPF_JGT ? val : val + 1;
5721-
u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
5722-
5723-
if (is_jmp32) {
5724-
false_umin += gen_hi_min(false_reg->var_off);
5725-
true_umax += gen_hi_max(true_reg->var_off);
5726-
}
5727-
false_reg->umin_value = max(false_reg->umin_value, false_umin);
5728-
true_reg->umax_value = min(true_reg->umax_value, true_umax);
5770+
set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5771+
set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
57295772
break;
57305773
}
57315774
case BPF_JSGE:
@@ -5743,15 +5786,8 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
57435786
case BPF_JLE:
57445787
case BPF_JLT:
57455788
{
5746-
u64 false_umax = opcode == BPF_JLT ? val : val - 1;
5747-
u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
5748-
5749-
if (is_jmp32) {
5750-
false_umax += gen_hi_max(false_reg->var_off);
5751-
true_umin += gen_hi_min(true_reg->var_off);
5752-
}
5753-
false_reg->umax_value = min(false_reg->umax_value, false_umax);
5754-
true_reg->umin_value = max(true_reg->umin_value, true_umin);
5789+
set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5790+
set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
57555791
break;
57565792
}
57575793
case BPF_JSLE:

0 commit comments

Comments
 (0)