bpf, verifier: Improve precision of BPF_MUL
Subsystems (4)
v3 cover bpf, verifier: Improve precision of BPF_MUL
Hi All,
This patch-set aims to improve precision of BPF_MUL and add testcases to illustrate precision gains using signed and unsigned bounds.
Thanks for taking the time to review and specifically for Eduard's feedback!
Best,
Matan
Changes from v1:
- Fixed typo made in patch
Changes from v2:
- Added signed multiplication to BPF_MUL
- Added test cases to exercise BPF_MUL
- Reordered patches in the series.
Matan Shachnai (2):
bpf, verifier: Improve precision of BPF_MUL
selftests/bpf: Add testcases for BPF_MUL
kernel/bpf/verifier.c | 72 +++++----- .../selftests/bpf/progs/verifier_bounds.c | 134 ++++++++++++++++++ 2 files changed, 166 insertions(+), 40 deletions(-)
Signature · 1 line
2.25.1
PATCH v3 1/2 bpf, verifier: Improve precision of BPF_MUL
This patch improves (or maintains) the precision of register value tracking in BPF_MUL across all possible inputs. It also simplifies scalar32_min_max_mul() and scalar_min_max_mul().
As it stands, BPF_MUL is composed of three functions:
case BPF_MUL:
tnum_mul();
scalar32_min_max_mul();
scalar_min_max_mul();
The current implementation of scalar_min_max_mul() restricts the u64 input ranges of dst_reg and src_reg to be within [0, U32_MAX]:
/* Both values are positive, so we can work with unsigned and
* copy the result to signed (unless it exceeds S64_MAX).
*/
if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
/* Potential overflow, we know nothing */
__mark_reg64_unbounded(dst_reg);
return;
}This restriction is done to avoid unsigned overflow, which could otherwise wrap the result around 0, and leave an unsound output where umin > umax. We also observe that limiting these u64 input ranges to [0, U32_MAX] leads to a loss of precision. Consider the case where the u64 bounds of dst_reg are [0, 2^34] and the u64 bounds of src_reg are [0, 2^2]. While the multiplication of these two bounds doesn't overflow and is sound [0, 2^36], the current scalar_min_max_mul() would set the entire register state to unbounded.
Importantly, we update BPF_MUL to allow signed bound multiplication (i.e. multiplying negative bounds) as well as allow u64 inputs to take on values from [0, U64_MAX]. We perform signed multiplication on two bounds [a,b] and [c,d] by multiplying every combination of the bounds (i.e. a*c, a*d, b*c, and b*d) and checking for overflow of each product. If there is an overflow, we mark the signed bounds unbounded [S64_MIN, S64_MAX]. In the case of no overflow, we take the minimum of these products to be the resulting smin, and the maximum to be the resulting smax.
The key idea here is that if there’s no possibility of overflow, either when multiplying signed bounds or unsigned bounds, we can safely multiply the respective bounds; otherwise, we set the bounds that exhibit overflow (during multiplication) to unbounded.
if (check_mul_overflow(*dst_umax, src_reg->umax_value, dst_umax) ||
(check_mul_overflow(*dst_umin, src_reg->umin_value, dst_umin))) {
/* Overflow possible, we know nothing */
dst_reg->umin_value = 0;
dst_reg->umax_value = U64_MAX;
}...
Below, we provide an example BPF program (below) that exhibits the imprecision in the current BPF_MUL, where the outputs are all unbounded. In contrast, the updated BPF_MUL produces a bounded register state:
BPF_LD_IMM64(BPF_REG_1, 11),BPF_LD_IMM64(BPF_REG_2, 4503599627370624),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_REG(BPF_AND, BPF_REG_1, BPF_REG_2),BPF_LD_IMM64(BPF_REG_3, 809591906117232263),BPF_ALU64_REG(BPF_MUL, BPF_REG_3, BPF_REG_1),BPF_MOV64_IMM(BPF_REG_0, 1),BPF_EXIT_INSN(),
Verifier log using the old BPF_MUL:
func#0 @0
0: R1=ctx() R10=fp0
0: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar()
...
Verifier using the new updated BPF_MUL (more precise bounds at label 9)
func#0 @0
0: R1=ctx() R10=fp0
0: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar(smin=0,smax=umax=0x7b96bb0a94a3a7cd,var_off=(0x0; 0x7fffffffffffffff))
...
Finally, we proved the soundness of the new scalar_min_max_mul() and scalar32_min_max_mul() functions. Typically, multiplication operations are expensive to check with bitvector-based solvers. We were able to prove the soundness of these functions using Non-Linear Integer Arithmetic (NIA) theory. Additionally, using Agni [2,3], we obtained the encodings for scalar32_min_max_mul() and scalar_min_max_mul() in bitvector theory, and were able to prove their soundness using 8-bit bitvectors (instead of 64-bit bitvectors that the functions actually use).
In conclusion, with this patch,
1. We were able to show that we can improve the overall precision of BPF_MUL. We proved (using an SMT solver) that this new version of BPF_MUL is at least as precise as the current version for all inputs and more precise for some inputs.
2. We are able to prove the soundness of the new scalar_min_max_mul() and scalar32_min_max_mul(). By leveraging the existing proof of tnum_mul [1], we can say that the composition of these three functions within BPF_MUL is sound.
[1] https://ieeexplore.ieee.org/abstract/document/9741267
[2] https://link.springer.com/chapter/10.1007/978-3-031-37709-9_12
[3] https://people.cs.rutgers.edu/~sn349/papers/sas24-preprint.pdf
--- kernel/bpf/verifier.c | 72 +++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 40 deletions(-)
kernel/bpf/verifier.c +32 -40
@@ -14118,64 +14118,56 @@ static void scalar_min_max_sub(struct bpf_reg_state *dst_reg,static void scalar32_min_max_mul (struct bpf_reg_state *dst_reg,struct bpf_reg_state *src_reg) { -s32 smin_val = src_reg->s32_min_value; -u32 umin_val = src_reg->u32_min_value; -u32 umax_val = src_reg->u32_max_value; +s32 *dst_smin = &dst_reg->s32_min_value; +s32 *dst_smax = &dst_reg->s32_max_value; +u32 *dst_umin = &dst_reg->u32_min_value; +u32 *dst_umax = &dst_reg->u32_max_value; +s32 tmp_prod[4 ]; -if (smin_val <0 || dst_reg->s32_min_value <0 ) { -/* Ain't nobody got time to multiply that sign */ -__mark_reg32_unbounded (dst_reg); -return ; - } -/* Both values are positive, so we can work with unsigned and -* copy the result to signed (unless it exceeds S32_MAX). -*/ -if (umax_val >U16_MAX || dst_reg->u32_max_value >U16_MAX ) { -/* Potential overflow, we know nothing */ -__mark_reg32_unbounded (dst_reg); -return ; +if (check_mul_overflow (*dst_umax, src_reg->u32_max_value, dst_umax) || +check_mul_overflow (*dst_umin, src_reg->u32_min_value, dst_umin)) { +/* Overflow possible, we know nothing */ + dst_reg->u32_min_value =0 ; + dst_reg->u32_max_value =U32_MAX ; } - dst_reg->u32_min_value *= umin_val; - dst_reg->u32_max_value *= umax_val; -if (dst_reg->u32_max_value >S32_MAX ) { +if (check_mul_overflow (*dst_smin, src_reg->s32_min_value, &tmp_prod[0 ]) || +check_mul_overflow (*dst_smin, src_reg->s32_max_value, &tmp_prod[1 ]) || +check_mul_overflow (*dst_smax, src_reg->s32_min_value, &tmp_prod[2 ]) || +check_mul_overflow (*dst_smax, src_reg->s32_max_value, &tmp_prod[3 ])) {/* Overflow possible, we know nothing */ dst_reg->s32_min_value =S32_MIN ; dst_reg->s32_max_value =S32_MAX ; }else { - dst_reg->s32_min_value = dst_reg->u32_min_value; - dst_reg->s32_max_value = dst_reg->u32_max_value; + dst_reg->s32_min_value =min_array (tmp_prod,4 ); + dst_reg->s32_max_value =max_array (tmp_prod,4 ); } }static void scalar_min_max_mul (struct bpf_reg_state *dst_reg,struct bpf_reg_state *src_reg) { -s64 smin_val = src_reg->smin_value; -u64 umin_val = src_reg->umin_value; -u64 umax_val = src_reg->umax_value; +s64 *dst_smin = &dst_reg->smin_value; +s64 *dst_smax = &dst_reg->smax_value; +u64 *dst_umin = &dst_reg->umin_value; +u64 *dst_umax = &dst_reg->umax_value; +s64 tmp_prod[4 ]; -if (smin_val <0 || dst_reg->smin_value <0 ) { -/* Ain't nobody got time to multiply that sign */ -__mark_reg64_unbounded (dst_reg); -return ; - } -/* Both values are positive, so we can work with unsigned and -* copy the result to signed (unless it exceeds S64_MAX). -*/ -if (umax_val >U32_MAX || dst_reg->umax_value >U32_MAX ) { -/* Potential overflow, we know nothing */ -__mark_reg64_unbounded (dst_reg); -return ; +if (check_mul_overflow (*dst_umax, src_reg->umax_value, dst_umax) || +check_mul_overflow (*dst_umin, src_reg->umin_value, dst_umin)) { +/* Overflow possible, we know nothing */ + dst_reg->umin_value =0 ; + dst_reg->umax_value =U64_MAX ; } - dst_reg->umin_value *= umin_val; - dst_reg->umax_value *= umax_val; -if (dst_reg->umax_value >S64_MAX ) { +if (check_mul_overflow (*dst_smin, src_reg->smin_value, &tmp_prod[0 ]) || +check_mul_overflow (*dst_smin, src_reg->smax_value, &tmp_prod[1 ]) || +check_mul_overflow (*dst_smax, src_reg->smin_value, &tmp_prod[2 ]) || +check_mul_overflow (*dst_smax, src_reg->smax_value, &tmp_prod[3 ])) {/* Overflow possible, we know nothing */ dst_reg->smin_value =S64_MIN ; dst_reg->smax_value =S64_MAX ; }else { - dst_reg->smin_value = dst_reg->umin_value; - dst_reg->smax_value = dst_reg->umax_value; + dst_reg->smin_value =min_array (tmp_prod,4 ); + dst_reg->smax_value =max_array (tmp_prod,4 ); } }
Signature · 1 line
2.25.1
Quoted 155 lines · Matan Shachnai
This patch improves (or maintains) the precision of register value tracking in
BPF_MULacross all possible inputs. It also simplifiesscalar32_min_max_mul()andscalar_min_max_mul().As it stands,
BPF_MULis composed of three functions:case
BPF_MUL:
tnum_mul();
scalar32_min_max_mul();
scalar_min_max_mul();The current implementation of
scalar_min_max_mul()restricts the u64 input ranges ofdst_regandsrc_regto be within [0,U32_MAX]:/* Both values are positive, so we can work with unsigned and * copy the result to signed (unless it exceeds S64_MAX). */ if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { /* Potential overflow, we know nothing */ __mark_reg64_unbounded(dst_reg); return; }This restriction is done to avoid unsigned overflow, which could otherwise wrap the result around 0, and leave an unsound output where umin > umax. We also observe that limiting these u64 input ranges to [0,
U32_MAX] leads to a loss of precision. Consider the case where the u64 bounds ofdst_regare [0, 2^34] and the u64 bounds ofsrc_regare [0, 2^2]. While the multiplication of these two bounds doesn't overflow and is sound [0, 2^36], the currentscalar_min_max_mul()would set the entire register state to unbounded.Importantly, we update
BPF_MULto allow signed bound multiplication (i.e. multiplying negative bounds) as well as allow u64 inputs to take on values from [0,U64_MAX]. We perform signed multiplication on two bounds [a,b] and [c,d] by multiplying every combination of the bounds (i.e. a*c, a*d, b*c, and b*d) and checking for overflow of each product. If there is an overflow, we mark the signed bounds unbounded [S64_MIN,S64_MAX]. In the case of no overflow, we take the minimum of these products to be the resulting smin, and the maximum to be the resulting smax.The key idea here is that if there’s no possibility of overflow, either when multiplying signed bounds or unsigned bounds, we can safely multiply the respective bounds; otherwise, we set the bounds that exhibit overflow (during multiplication) to unbounded.
if (
check_mul_overflow(*dst_umax, src_reg->umax_value, dst_umax)||(check_mul_overflow(*dst_umin, src_reg->umin_value, dst_umin))) { /* Overflow possible, we know nothing */ dst_reg->umin_value = 0; dst_reg->umax_value = U64_MAX; }...
Below, we provide an example BPF program (below) that exhibits the imprecision in the current
BPF_MUL, where the outputs are all unbounded. In contrast, the updatedBPF_MULproduces a bounded register state:
BPF_LD_IMM64(BPF_REG_1, 11),BPF_LD_IMM64(BPF_REG_2, 4503599627370624),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_REG(BPF_AND, BPF_REG_1, BPF_REG_2),BPF_LD_IMM64(BPF_REG_3, 809591906117232263),BPF_ALU64_REG(BPF_MUL, BPF_REG_3, BPF_REG_1),BPF_MOV64_IMM(BPF_REG_0, 1),BPF_EXIT_INSN(),Verifier log using the old
BPF_MUL:func#0 @0
0: R1=ctx() R10=fp00: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar()...
Verifier using the new updated
BPF_MUL(more precise bounds at label 9)func#0 @0
0: R1=ctx() R10=fp00: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar(smin=0,smax=umax=0x7b96bb0a94a3a7cd,var_off=(0x0; 0x7fffffffffffffff))...
Finally, we proved the soundness of the new
scalar_min_max_mul()andscalar32_min_max_mul()functions. Typically, multiplication operations are expensive to check with bitvector-based solvers. We were able to prove the soundness of these functions using Non-Linear Integer Arithmetic (NIA) theory. Additionally, using Agni [2,3], we obtained the encodings forscalar32_min_max_mul()andscalar_min_max_mul()in bitvector theory, and were able to prove their soundness using 8-bit bitvectors (instead of 64-bit bitvectors that the functions actually use).In conclusion, with this patch,
1. We were able to show that we can improve the overall precision of
BPF_MUL. We proved (using an SMT solver) that this new version ofBPF_MULis at least as precise as the current version for all inputs and more precise for some inputs.2. We are able to prove the soundness of the new
scalar_min_max_mul()andscalar32_min_max_mul(). By leveraging the existing proof oftnum_mul[1], we can say that the composition of these three functions withinBPF_MULis sound.[1] https://ieeexplore.ieee.org/abstract/document/9741267
[2] https://link.springer.com/chapter/10.1007/978-3-031-37709-9_12
[3] https://people.cs.rutgers.edu/~sn349/papers/sas24-preprint.pdfCo-developed-by Harishankar Vishwanathan <[email protected]>Signed-off-by Harishankar Vishwanathan <[email protected]>Co-developed-by Srinivas Narayana <[email protected]>Signed-off-by Srinivas Narayana <[email protected]>Co-developed-by Santosh Nagarakatte <[email protected]>Signed-off-by Santosh Nagarakatte <[email protected]>Signed-off-by Matan Shachnai <[email protected]>--- kernel/bpf/verifier.c | 72 +++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 40 deletions(-)kernel/bpf/verifier.c +5
-10@@ -14118,64 +14118,56 @@ static void scalar_min_max_sub(struct bpf_reg_state *dst_reg,static void scalar32_min_max_mul (struct bpf_reg_state *dst_reg,struct bpf_reg_state *src_reg) { -s32 smin_val = src_reg->s32_min_value; -u32 umin_val = src_reg->u32_min_value; -u32 umax_val = src_reg->u32_max_value; +s32 *dst_smin = &dst_reg->s32_min_value; +s32 *dst_smax = &dst_reg->s32_max_value; +u32 *dst_umin = &dst_reg->u32_min_value; +u32 *dst_umax = &dst_reg->u32_max_value; +s32 tmp_prod[4 ]; -if (smin_val <0 || dst_reg->s32_min_value <0 ) { -/* Ain't nobody got time to multiply that sign */ -__mark_reg32_unbounded (dst_reg); -return ; - } -/* Both values are positive, so we can work with unsigned and -* copy the result to signed (unless it exceeds S32_MAX).
kernel/bpf/verifier.c static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, · line 14131
Quoted 10 lines
kernel/bpf/verifier.c +5
-5-*/ -if (umax_val >U16_MAX || dst_reg->u32_max_value >U16_MAX ) { -/* Potential overflow, we know nothing */ -__mark_reg32_unbounded (dst_reg); -return ; +if (check_mul_overflow (*dst_umax, src_reg->u32_max_value, dst_umax) || +check_mul_overflow (*dst_umin, src_reg->u32_min_value, dst_umin)) { +/* Overflow possible, we know nothing */ + dst_reg->u32_min_value =0 ; + dst_reg->u32_max_value =U32_MAX ;
It would be cleaner to use:*dst_umin = 0;*dst_umax = U32_MAX;
to make it obvious that though check_mul_overflow()-s wrote something into dst_umax and dst_umin,
we clean them up with these two assignments.
Just like scalar32_min_max_add() does already.
Quoted 6 lines
kernel/bpf/verifier.c +2
-3} - dst_reg->u32_min_value *= umin_val; - dst_reg->u32_max_value *= umax_val; -if (dst_reg->u32_max_value >S32_MAX ) { + if (check_mul_overflow(*dst_smin, src_reg->s32_min_value, &tmp_prod[0]) || + check_mul_overflow(*dst_smin, src_reg->s32_max_value, &tmp_prod[1]) ||
kernel/bpf/verifier.c static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, · line 14142
Quoted 10 lines
kernel/bpf/verifier.c +4
-2+ check_mul_overflow(*dst_smax, src_reg->s32_min_value, &tmp_prod[2]) || + check_mul_overflow(*dst_smax, src_reg->s32_max_value, &tmp_prod[3])) { /* Overflow possible, we know nothing */ dst_reg->s32_min_value = S32_MIN; dst_reg->s32_max_value = S32_MAX; } else { - dst_reg->s32_min_value = dst_reg->u32_min_value; - dst_reg->s32_max_value = dst_reg->u32_max_value; + dst_reg->s32_min_value =min_array (tmp_prod,4 ); + dst_reg->s32_max_value =max_array (tmp_prod,4 );
dst_smin/smax here too.
Quoted 172 lines · Alexei Starovoitov
Quoted 155 lines · Matan Shachnai
This patch improves (or maintains) the precision of register value tracking in
BPF_MULacross all possible inputs. It also simplifiesscalar32_min_max_mul()andscalar_min_max_mul().As it stands,
BPF_MULis composed of three functions:case
BPF_MUL:
tnum_mul();
scalar32_min_max_mul();
scalar_min_max_mul();The current implementation of
scalar_min_max_mul()restricts the u64 input ranges ofdst_regandsrc_regto be within [0,U32_MAX]:/* Both values are positive, so we can work with unsigned and * copy the result to signed (unless it exceeds S64_MAX). */ if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { /* Potential overflow, we know nothing */ __mark_reg64_unbounded(dst_reg); return; }This restriction is done to avoid unsigned overflow, which could otherwise wrap the result around 0, and leave an unsound output where umin > umax. We also observe that limiting these u64 input ranges to [0,
U32_MAX] leads to a loss of precision. Consider the case where the u64 bounds ofdst_regare [0, 2^34] and the u64 bounds ofsrc_regare [0, 2^2]. While the multiplication of these two bounds doesn't overflow and is sound [0, 2^36], the currentscalar_min_max_mul()would set the entire register state to unbounded.Importantly, we update
BPF_MULto allow signed bound multiplication (i.e. multiplying negative bounds) as well as allow u64 inputs to take on values from [0,U64_MAX]. We perform signed multiplication on two bounds [a,b] and [c,d] by multiplying every combination of the bounds (i.e. a*c, a*d, b*c, and b*d) and checking for overflow of each product. If there is an overflow, we mark the signed bounds unbounded [S64_MIN,S64_MAX]. In the case of no overflow, we take the minimum of these products to be the resulting smin, and the maximum to be the resulting smax.The key idea here is that if there’s no possibility of overflow, either when multiplying signed bounds or unsigned bounds, we can safely multiply the respective bounds; otherwise, we set the bounds that exhibit overflow (during multiplication) to unbounded.
if (
check_mul_overflow(*dst_umax, src_reg->umax_value, dst_umax)||(check_mul_overflow(*dst_umin, src_reg->umin_value, dst_umin))) { /* Overflow possible, we know nothing */ dst_reg->umin_value = 0; dst_reg->umax_value = U64_MAX; }...
Below, we provide an example BPF program (below) that exhibits the imprecision in the current
BPF_MUL, where the outputs are all unbounded. In contrast, the updatedBPF_MULproduces a bounded register state:
BPF_LD_IMM64(BPF_REG_1, 11),BPF_LD_IMM64(BPF_REG_2, 4503599627370624),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_IMM(BPF_NEG, BPF_REG_2, 0),BPF_ALU64_REG(BPF_AND, BPF_REG_1, BPF_REG_2),BPF_LD_IMM64(BPF_REG_3, 809591906117232263),BPF_ALU64_REG(BPF_MUL, BPF_REG_3, BPF_REG_1),BPF_MOV64_IMM(BPF_REG_0, 1),BPF_EXIT_INSN(),Verifier log using the old
BPF_MUL:func#0 @0
0: R1=ctx() R10=fp00: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar()...
Verifier using the new updated
BPF_MUL(more precise bounds at label 9)func#0 @0
0: R1=ctx() R10=fp00: (18) r1 = 0xb ; R1_w=11 2: (18) r2 = 0x10000000000080 ; R2_w=0x10000000000080 4: (87) r2 = -r2 ; R2_w=scalar() 5: (87) r2 = -r2 ; R2_w=scalar() 6: (5f) r1 &= r2 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R2_w=scalar() 7: (18) r3 = 0xb3c3f8c99262687 ; R3_w=0xb3c3f8c99262687 9: (2f) r3 *= r1 ; R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R3_w=scalar(smin=0,smax=umax=0x7b96bb0a94a3a7cd,var_off=(0x0; 0x7fffffffffffffff))...
Finally, we proved the soundness of the new
scalar_min_max_mul()andscalar32_min_max_mul()functions. Typically, multiplication operations are expensive to check with bitvector-based solvers. We were able to prove the soundness of these functions using Non-Linear Integer Arithmetic (NIA) theory. Additionally, using Agni [2,3], we obtained the encodings forscalar32_min_max_mul()andscalar_min_max_mul()in bitvector theory, and were able to prove their soundness using 8-bit bitvectors (instead of 64-bit bitvectors that the functions actually use).In conclusion, with this patch,
1. We were able to show that we can improve the overall precision of
BPF_MUL. We proved (using an SMT solver) that this new version ofBPF_MULis at least as precise as the current version for all inputs and more precise for some inputs.2. We are able to prove the soundness of the new
scalar_min_max_mul()andscalar32_min_max_mul(). By leveraging the existing proof oftnum_mul[1], we can say that the composition of these three functions withinBPF_MULis sound.[1] https://ieeexplore.ieee.org/abstract/document/9741267
[2] https://link.springer.com/chapter/10.1007/978-3-031-37709-9_12
[3] https://people.cs.rutgers.edu/~sn349/papers/sas24-preprint.pdfCo-developed-by Harishankar Vishwanathan <[email protected]>Signed-off-by Harishankar Vishwanathan <[email protected]>Co-developed-by Srinivas Narayana <[email protected]>Signed-off-by Srinivas Narayana <[email protected]>Co-developed-by Santosh Nagarakatte <[email protected]>Signed-off-by Santosh Nagarakatte <[email protected]>Signed-off-by Matan Shachnai <[email protected]>--- kernel/bpf/verifier.c | 72 +++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 40 deletions(-)kernel/bpf/verifier.c +5
-10@@ -14118,64 +14118,56 @@ static void scalar_min_max_sub(struct bpf_reg_state *dst_reg,static void scalar32_min_max_mul (struct bpf_reg_state *dst_reg,struct bpf_reg_state *src_reg) { -s32 smin_val = src_reg->s32_min_value; -u32 umin_val = src_reg->u32_min_value; -u32 umax_val = src_reg->u32_max_value; +s32 *dst_smin = &dst_reg->s32_min_value; +s32 *dst_smax = &dst_reg->s32_max_value; +u32 *dst_umin = &dst_reg->u32_min_value; +u32 *dst_umax = &dst_reg->u32_max_value; +s32 tmp_prod[4 ]; -if (smin_val <0 || dst_reg->s32_min_value <0 ) { -/* Ain't nobody got time to multiply that sign */ -__mark_reg32_unbounded (dst_reg); -return ; - } -/* Both values are positive, so we can work with unsigned and -* copy the result to signed (unless it exceeds S32_MAX). kernel/bpf/verifier.c static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, · line 14131
Quoted 10 lines
kernel/bpf/verifier.c +5
-5-*/ -if (umax_val >U16_MAX || dst_reg->u32_max_value >U16_MAX ) { -/* Potential overflow, we know nothing */ -__mark_reg32_unbounded (dst_reg); -return ; +if (check_mul_overflow (*dst_umax, src_reg->u32_max_value, dst_umax) || +check_mul_overflow (*dst_umin, src_reg->u32_min_value, dst_umin)) { +/* Overflow possible, we know nothing */ + dst_reg->u32_min_value =0 ; + dst_reg->u32_max_value =U32_MAX ;It would be cleaner to use:
*dst_umin= 0;*dst_umax=U32_MAX;
Quoted 5 lines
to make it obvious that though check_mul_overflow()-s wrote something into
dst_umaxanddst_umin,
we clean them up with these two assignments.Just like
scalar32_min_max_add()does already.
Thanks, Alexei.
I'll fix it up and follow up with a v4 soon.
Quoted 19 lines
Quoted 7 lines
kernel/bpf/verifier.c +2
-3} - dst_reg->u32_min_value *= umin_val; - dst_reg->u32_max_value *= umax_val; -if (dst_reg->u32_max_value >S32_MAX ) { + if (check_mul_overflow(*dst_smin, src_reg->s32_min_value, &tmp_prod[0]) || + check_mul_overflow(*dst_smin, src_reg->s32_max_value, &tmp_prod[1]) ||kernel/bpf/verifier.c static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, · line 14142
Quoted 10 lines
kernel/bpf/verifier.c +4
-2+ check_mul_overflow(*dst_smax, src_reg->s32_min_value, &tmp_prod[2]) || + check_mul_overflow(*dst_smax, src_reg->s32_max_value, &tmp_prod[3])) { /* Overflow possible, we know nothing */ dst_reg->s32_min_value = S32_MIN; dst_reg->s32_max_value = S32_MAX; } else { - dst_reg->s32_min_value = dst_reg->u32_min_value; - dst_reg->s32_max_value = dst_reg->u32_max_value; + dst_reg->s32_min_value =min_array (tmp_prod,4 ); + dst_reg->s32_max_value =max_array (tmp_prod,4 );dst_smin/smax here too.
PATCH v3 2/2 selftests/bpf: Add testcases for BPF_MUL
In reply to Matan ShachnaiThe previous commit improves precision of BPF_MUL. Add tests to exercise updated BPF_MUL.
--- .../selftests/bpf/progs/verifier_bounds.c | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+)
tools/testing/selftests/bpf/progs/verifier_bounds.c +134 -0
@@ -1200,4 +1200,138 @@ l0_%=: r0 = 0; \ : __clobber_all); } +SEC("tc") +__description("multiply mixed sign bounds. test 1") +__success __log_level(2) +__msg("r6 *= r7 {{.*}}; R6_w=scalar(smin=umin=0x1bc16d5cd4927ee1,smax=umax=0x1bc16d674ec80000,smax32=0x7ffffeff,umax32=0xfffffeff,var_off=(0x1bc16d4000000000; 0x3ffffffeff))") +__naked void mult_mixed0_sign (void ) +{ + asmvolatile ( +"call %[bpf_get_prandom_u32];" +"r6 = r0;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= 0xf;" +"r6 -= 1000000000;" +"r7 &= 0xf;" +"r7 -= 2000000000;" +"r6 *= r7;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("multiply mixed sign bounds. test 2") +__success __log_level(2) +__msg("r6 *= r7 {{.*}}; R6_w=scalar(smin=smin32=-100,smax=smax32=200)") +__naked void mult_mixed1_sign (void ) +{ + asmvolatile ( +"call %[bpf_get_prandom_u32];" +"r6 = r0;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= 0xf;" +"r6 -= 0xa;" +"r7 &= 0xf;" +"r7 -= 0x14;" +"r6 *= r7;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("multiply negative bounds") +__success __log_level(2) +__msg("r6 *= r7 {{.*}}; R6_w=scalar(smin=umin=smin32=umin32=0x3ff280b0,smax=umax=smax32=umax32=0x3fff0001,var_off=(0x3ff00000; 0xf81ff))") +__naked void mult_sign_bounds (void ) +{ + asmvolatile ( +"r8 = 0x7fff;" +"call %[bpf_get_prandom_u32];" +"r6 = r0;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= 0xa;" +"r6 -= r8;" +"r7 &= 0xf;" +"r7 -= r8;" +"r6 *= r7;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("multiply bounds that don't cross signed boundary") +__success __log_level(2) +__msg("r8 *= r6 {{.*}}; R6_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=11,var_off=(0x0; 0xb)) R8_w=scalar(smin=0,smax=umax=0x7b96bb0a94a3a7cd,var_off=(0x0; 0x7fffffffffffffff))") +__naked void mult_no_sign_crossing (void ) +{ + asmvolatile ( +"r6 = 0xb;" +"r8 = 0xb3c3f8c99262687 ll;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= r7;" +"r8 *= r6;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("multiplication overflow, result in unbounded reg. test 1") +__success __log_level(2) +__msg("r6 *= r7 {{.*}}; R6_w=scalar()") +__naked void mult_unsign_ovf (void ) +{ + asmvolatile ( +"r8 = 0x7ffffffffff ll;" +"call %[bpf_get_prandom_u32];" +"r6 = r0;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= 0x7fffffff;" +"r7 &= r8;" +"r6 *= r7;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} + +SEC("tc") +__description("multiplication overflow, result in unbounded reg. test 2") +__success __log_level(2) +__msg("r6 *= r7 {{.*}}; R6_w=scalar()") +__naked void mult_sign_ovf (void ) +{ + asmvolatile ( +"r8 = 0x7ffffffff ll;" +"call %[bpf_get_prandom_u32];" +"r6 = r0;" +"call %[bpf_get_prandom_u32];" +"r7 = r0;" +"r6 &= 0xa;" +"r6 -= r8;" +"r7 &= 0x7fffffff;" +"r6 *= r7;" +"exit" + : + : __imm(bpf_get_prandom_u32), +__imm (bpf_skb_store_bytes) + : __clobber_all); +} char _license[] SEC("license") = "GPL";
Signature · 1 line
2.25.1