Skip to content

Commit b03c9f9

Browse files
ecree-solarflaredavem330
authored andcommitted
bpf/verifier: track signed and unsigned min/max values
Allows us to, sometimes, combine information from a signed check of one bound and an unsigned check of the other. We now track the full range of possible values, rather than restricting ourselves to [0, 1<<30) and considering anything beyond that as unknown. While this is probably not necessary, it makes the code more straightforward and symmetrical between signed and unsigned bounds. Signed-off-by: Edward Cree <ecree@solarflare.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f1174f7 commit b03c9f9

File tree

4 files changed

+461
-317
lines changed

4 files changed

+461
-317
lines changed

include/linux/bpf_verifier.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
#include <linux/filter.h> /* for MAX_BPF_STACK */
1212
#include <linux/tnum.h>
1313

14-
/* Just some arbitrary values so we can safely do math without overflowing and
15-
* are obviously wrong for any sort of memory access.
16-
*/
17-
#define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024)
18-
#define BPF_REGISTER_MIN_RANGE -1
14+
/* Maximum variable offset umax_value permitted when resolving memory accesses.
15+
* In practice this is far bigger than any realistic pointer offset; this limit
16+
* ensures that umax_value + (int)off + (int)size cannot overflow a u64.
17+
*/
18+
#define BPF_MAX_VAR_OFF (1ULL << 31)
19+
/* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures
20+
* that converting umax_value to int cannot overflow.
21+
*/
22+
#define BPF_MAX_VAR_SIZ INT_MAX
1923

2024
struct bpf_reg_state {
2125
enum bpf_reg_type type;
@@ -36,7 +40,7 @@ struct bpf_reg_state {
3640
* came from, when one is tested for != NULL.
3741
*/
3842
u32 id;
39-
/* These three fields must be last. See states_equal() */
43+
/* These five fields must be last. See states_equal() */
4044
/* For scalar types (SCALAR_VALUE), this represents our knowledge of
4145
* the actual value.
4246
* For pointer types, this represents the variable part of the offset
@@ -49,9 +53,10 @@ struct bpf_reg_state {
4953
* These refer to the same value as var_off, not necessarily the actual
5054
* contents of the register.
5155
*/
52-
s64 min_value;
53-
u64 max_value;
54-
bool value_from_signed;
56+
s64 smin_value; /* minimum possible (s64)value */
57+
s64 smax_value; /* maximum possible (s64)value */
58+
u64 umin_value; /* minimum possible (u64)value */
59+
u64 umax_value; /* maximum possible (u64)value */
5560
};
5661

5762
enum bpf_stack_slot_type {

include/linux/tnum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct tnum {
1717
struct tnum tnum_const(u64 value);
1818
/* A completely unknown value */
1919
extern const struct tnum tnum_unknown;
20+
/* A value that's unknown except that @min <= value <= @max */
21+
struct tnum tnum_range(u64 min, u64 max);
2022

2123
/* Arithmetic and logical ops */
2224
/* Shift a tnum left (by a fixed shift) */

kernel/bpf/tnum.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ struct tnum tnum_const(u64 value)
1717
return TNUM(value, 0);
1818
}
1919

20+
struct tnum tnum_range(u64 min, u64 max)
21+
{
22+
u64 chi = min ^ max, delta;
23+
u8 bits = fls64(chi);
24+
25+
/* special case, needed because 1ULL << 64 is undefined */
26+
if (bits > 63)
27+
return tnum_unknown;
28+
/* e.g. if chi = 4, bits = 3, delta = (1<<3) - 1 = 7.
29+
* if chi = 0, bits = 0, delta = (1<<0) - 1 = 0, so we return
30+
* constant min (since min == max).
31+
*/
32+
delta = (1ULL << bits) - 1;
33+
return TNUM(min & ~delta, delta);
34+
}
35+
2036
struct tnum tnum_lshift(struct tnum a, u8 shift)
2137
{
2238
return TNUM(a.value << shift, a.mask << shift);

0 commit comments

Comments
 (0)