bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.
@ 2021-10-29 16:31 Alexei Starovoitov
  2021-10-29 18:28 ` Yonghong Song
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2021-10-29 16:31 UTC (permalink / raw)
  To: davem; +Cc: daniel, andrii, netdev, bpf, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Before this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))

After this fix:
166: (b5) if r2 <= 0x1 goto pc+22
from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))

While processing BPF_JLE the reg_set_min_max() would set true_reg->umax_value = 1
and call __reg_combine_64_into_32(true_reg).

Without the fix it would not pass the condition:
if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value))

since umin_value == 0 at this point.
Before commit 10bf4e83167c the umin was incorrectly ingored.
The commit 10bf4e83167c fixed the correctness issue, but pessimized
propagation of 64-bit min max into 32-bit min max and corresponding var_off.

Fixes: 10bf4e83167c ("bpf: Fix propagation of 32 bit unsigned bounds from 64 bit bounds")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/verifier.c                               | 2 +-
 tools/testing/selftests/bpf/verifier/array_access.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 3c8aa7df1773..29671ed49ee8 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1425,7 +1425,7 @@ static bool __reg64_bound_s32(s64 a)
 
 static bool __reg64_bound_u32(u64 a)
 {
-	return a > U32_MIN && a < U32_MAX;
+	return a >= U32_MIN && a <= U32_MAX;
 }
 
 static void __reg_combine_64_into_32(struct bpf_reg_state *reg)
diff --git a/tools/testing/selftests/bpf/verifier/array_access.c b/tools/testing/selftests/bpf/verifier/array_access.c
index 1b1c798e9248..1b138cd2b187 100644
--- a/tools/testing/selftests/bpf/verifier/array_access.c
+++ b/tools/testing/selftests/bpf/verifier/array_access.c
@@ -186,7 +186,7 @@
 	},
 	.fixup_map_hash_48b = { 3 },
 	.errstr_unpriv = "R0 leaks addr",
-	.errstr = "R0 unbounded memory access",
+	.errstr = "invalid access to map value, value_size=48 off=44 size=8",
 	.result_unpriv = REJECT,
 	.result = REJECT,
 	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.
  2021-10-29 16:31 [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off Alexei Starovoitov
@ 2021-10-29 18:28 ` Yonghong Song
  2021-10-29 19:22   ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2021-10-29 18:28 UTC (permalink / raw)
  To: Alexei Starovoitov, davem; +Cc: daniel, andrii, netdev, bpf, kernel-team



On 10/29/21 9:31 AM, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> Before this fix:
> 166: (b5) if r2 <= 0x1 goto pc+22
> from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))
> 
> After this fix:
> 166: (b5) if r2 <= 0x1 goto pc+22
> from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))
> 
> While processing BPF_JLE the reg_set_min_max() would set true_reg->umax_value = 1
> and call __reg_combine_64_into_32(true_reg).
> 
> Without the fix it would not pass the condition:
> if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value))
> 
> since umin_value == 0 at this point.
> Before commit 10bf4e83167c the umin was incorrectly ingored.
> The commit 10bf4e83167c fixed the correctness issue, but pessimized
> propagation of 64-bit min max into 32-bit min max and corresponding var_off.
> 
> Fixes: 10bf4e83167c ("bpf: Fix propagation of 32 bit unsigned bounds from 64 bit bounds")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>

See an unrelated nits below.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   kernel/bpf/verifier.c                               | 2 +-
>   tools/testing/selftests/bpf/verifier/array_access.c | 2 +-
>   2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 3c8aa7df1773..29671ed49ee8 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -1425,7 +1425,7 @@ static bool __reg64_bound_s32(s64 a)

We have
static bool __reg64_bound_s32(s64 a)
{
         return a > S32_MIN && a < S32_MAX;
}

Should we change to
	return a >= S32_MIN && a <= S32_MAX
?

>   
>   static bool __reg64_bound_u32(u64 a)
>   {
> -	return a > U32_MIN && a < U32_MAX;
> +	return a >= U32_MIN && a <= U32_MAX;
>   }
>   
>   static void __reg_combine_64_into_32(struct bpf_reg_state *reg)
> diff --git a/tools/testing/selftests/bpf/verifier/array_access.c b/tools/testing/selftests/bpf/verifier/array_access.c
> index 1b1c798e9248..1b138cd2b187 100644
> --- a/tools/testing/selftests/bpf/verifier/array_access.c
> +++ b/tools/testing/selftests/bpf/verifier/array_access.c
> @@ -186,7 +186,7 @@
>   	},
>   	.fixup_map_hash_48b = { 3 },
>   	.errstr_unpriv = "R0 leaks addr",
> -	.errstr = "R0 unbounded memory access",
> +	.errstr = "invalid access to map value, value_size=48 off=44 size=8",
>   	.result_unpriv = REJECT,
>   	.result = REJECT,
>   	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.
  2021-10-29 18:28 ` Yonghong Song
@ 2021-10-29 19:22   ` Alexei Starovoitov
  2021-10-29 23:28     ` Daniel Borkmann
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2021-10-29 19:22 UTC (permalink / raw)
  To: Yonghong Song
  Cc: David S. Miller, Daniel Borkmann, Andrii Nakryiko,
	Network Development, bpf, Kernel Team

On Fri, Oct 29, 2021 at 11:29 AM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 10/29/21 9:31 AM, Alexei Starovoitov wrote:
> > From: Alexei Starovoitov <ast@kernel.org>
> >
> > Before this fix:
> > 166: (b5) if r2 <= 0x1 goto pc+22
> > from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))
> >
> > After this fix:
> > 166: (b5) if r2 <= 0x1 goto pc+22
> > from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))
> >
> > While processing BPF_JLE the reg_set_min_max() would set true_reg->umax_value = 1
> > and call __reg_combine_64_into_32(true_reg).
> >
> > Without the fix it would not pass the condition:
> > if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value))
> >
> > since umin_value == 0 at this point.
> > Before commit 10bf4e83167c the umin was incorrectly ingored.
> > The commit 10bf4e83167c fixed the correctness issue, but pessimized
> > propagation of 64-bit min max into 32-bit min max and corresponding var_off.
> >
> > Fixes: 10bf4e83167c ("bpf: Fix propagation of 32 bit unsigned bounds from 64 bit bounds")
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
>
> See an unrelated nits below.
>
> Acked-by: Yonghong Song <yhs@fb.com>
>
> > ---
> >   kernel/bpf/verifier.c                               | 2 +-
> >   tools/testing/selftests/bpf/verifier/array_access.c | 2 +-
> >   2 files changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > index 3c8aa7df1773..29671ed49ee8 100644
> > --- a/kernel/bpf/verifier.c
> > +++ b/kernel/bpf/verifier.c
> > @@ -1425,7 +1425,7 @@ static bool __reg64_bound_s32(s64 a)
>
> We have
> static bool __reg64_bound_s32(s64 a)
> {
>          return a > S32_MIN && a < S32_MAX;
> }
>
> Should we change to
>         return a >= S32_MIN && a <= S32_MAX
> ?

Probably, but I haven't investigated that yet.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off.
  2021-10-29 19:22   ` Alexei Starovoitov
@ 2021-10-29 23:28     ` Daniel Borkmann
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2021-10-29 23:28 UTC (permalink / raw)
  To: Alexei Starovoitov, Yonghong Song
  Cc: David S. Miller, Andrii Nakryiko, Network Development, bpf, Kernel Team

On 10/29/21 9:22 PM, Alexei Starovoitov wrote:
> On Fri, Oct 29, 2021 at 11:29 AM Yonghong Song <yhs@fb.com> wrote:
>> On 10/29/21 9:31 AM, Alexei Starovoitov wrote:
>>> From: Alexei Starovoitov <ast@kernel.org>
>>>
>>> Before this fix:
>>> 166: (b5) if r2 <= 0x1 goto pc+22
>>> from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0xffffffff))
>>>
>>> After this fix:
>>> 166: (b5) if r2 <= 0x1 goto pc+22
>>> from 166 to 189: R2=invP(id=1,umax_value=1,var_off=(0x0; 0x1))
>>>
>>> While processing BPF_JLE the reg_set_min_max() would set true_reg->umax_value = 1
>>> and call __reg_combine_64_into_32(true_reg).
>>>
>>> Without the fix it would not pass the condition:
>>> if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value))
>>>
>>> since umin_value == 0 at this point.
>>> Before commit 10bf4e83167c the umin was incorrectly ingored.
>>> The commit 10bf4e83167c fixed the correctness issue, but pessimized
>>> propagation of 64-bit min max into 32-bit min max and corresponding var_off.
>>>
>>> Fixes: 10bf4e83167c ("bpf: Fix propagation of 32 bit unsigned bounds from 64 bit bounds")
>>> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
>>
>> See an unrelated nits below.
>>
>> Acked-by: Yonghong Song <yhs@fb.com>
>>
>>> ---
>>>    kernel/bpf/verifier.c                               | 2 +-
>>>    tools/testing/selftests/bpf/verifier/array_access.c | 2 +-
>>>    2 files changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index 3c8aa7df1773..29671ed49ee8 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -1425,7 +1425,7 @@ static bool __reg64_bound_s32(s64 a)
>>
>> We have
>> static bool __reg64_bound_s32(s64 a)
>> {
>>           return a > S32_MIN && a < S32_MAX;
>> }
>>
>> Should we change to
>>          return a >= S32_MIN && a <= S32_MAX
>> ?
> 
> Probably, but I haven't investigated that yet.

Fix looks good to me as well, we should make it consistent if so given it's the same
logic, but some tests for the S32 would be good if we don't have them yet.

Thanks!
Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-29 23:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29 16:31 [PATCH bpf-next] bpf: Fix propagation of bounds from 64-bit min/max into 32-bit and var_off Alexei Starovoitov
2021-10-29 18:28 ` Yonghong Song
2021-10-29 19:22   ` Alexei Starovoitov
2021-10-29 23:28     ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).