bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
@ 2019-10-17 10:57 Toke Høiland-Jørgensen
  2019-10-17 18:52 ` Jakub Kicinski
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Toke Høiland-Jørgensen @ 2019-10-17 10:57 UTC (permalink / raw)
  To: daniel, ast; +Cc: Toke Høiland-Jørgensen, bpf, netdev, Tetsuo Handa

Tetsuo pointed out that without an explicit cast, the cost calculation for
devmap_hash type maps could overflow on 32-bit builds. This adds the
missing cast.

Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 kernel/bpf/devmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
index a0a1153da5ae..e34fac6022eb 100644
--- a/kernel/bpf/devmap.c
+++ b/kernel/bpf/devmap.c
@@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
 
 		if (!dtab->n_buckets) /* Overflow check */
 			return -EINVAL;
-		cost += sizeof(struct hlist_head) * dtab->n_buckets;
+		cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;
 	}
 
 	/* if map size is larger than memlock limit, reject it */
-- 
2.23.0


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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-17 10:57 [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds Toke Høiland-Jørgensen
@ 2019-10-17 18:52 ` Jakub Kicinski
  2019-10-18  9:15   ` Toke Høiland-Jørgensen
  2019-10-18 16:44 ` Yonghong Song
  2019-10-18 23:25 ` Alexei Starovoitov
  2 siblings, 1 reply; 8+ messages in thread
From: Jakub Kicinski @ 2019-10-17 18:52 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen; +Cc: daniel, ast, bpf, netdev, Tetsuo Handa

On Thu, 17 Oct 2019 12:57:02 +0200, Toke Høiland-Jørgensen wrote:
> Tetsuo pointed out that without an explicit cast, the cost calculation for
> devmap_hash type maps could overflow on 32-bit builds. This adds the
> missing cast.
> 
> Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
> Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---
>  kernel/bpf/devmap.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
> index a0a1153da5ae..e34fac6022eb 100644
> --- a/kernel/bpf/devmap.c
> +++ b/kernel/bpf/devmap.c
> @@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
>  
>  		if (!dtab->n_buckets) /* Overflow check */
>  			return -EINVAL;
> -		cost += sizeof(struct hlist_head) * dtab->n_buckets;
> +		cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;

array_size()?

>  	}
>  
>  	/* if map size is larger than memlock limit, reject it */


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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-17 18:52 ` Jakub Kicinski
@ 2019-10-18  9:15   ` Toke Høiland-Jørgensen
  2019-10-18 17:19     ` Jakub Kicinski
  0 siblings, 1 reply; 8+ messages in thread
From: Toke Høiland-Jørgensen @ 2019-10-18  9:15 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: daniel, ast, bpf, netdev, Tetsuo Handa

Jakub Kicinski <jakub.kicinski@netronome.com> writes:

> On Thu, 17 Oct 2019 12:57:02 +0200, Toke Høiland-Jørgensen wrote:
>> Tetsuo pointed out that without an explicit cast, the cost calculation for
>> devmap_hash type maps could overflow on 32-bit builds. This adds the
>> missing cast.
>> 
>> Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
>> Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
>> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>> ---
>>  kernel/bpf/devmap.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
>> index a0a1153da5ae..e34fac6022eb 100644
>> --- a/kernel/bpf/devmap.c
>> +++ b/kernel/bpf/devmap.c
>> @@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
>>  
>>  		if (!dtab->n_buckets) /* Overflow check */
>>  			return -EINVAL;
>> -		cost += sizeof(struct hlist_head) * dtab->n_buckets;
>> +		cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;
>
> array_size()?

Well, array_size does this:

	if (check_mul_overflow(a, b, &bytes))
		return SIZE_MAX;

However, we don't to return SIZE_MAX on overflow, we want the
calculation itself to be done in 64 bits so it won't overflow... Or?

-Toke


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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-17 10:57 [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds Toke Høiland-Jørgensen
  2019-10-17 18:52 ` Jakub Kicinski
@ 2019-10-18 16:44 ` Yonghong Song
  2019-10-18 23:25 ` Alexei Starovoitov
  2 siblings, 0 replies; 8+ messages in thread
From: Yonghong Song @ 2019-10-18 16:44 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen, daniel, Alexei Starovoitov
  Cc: bpf, netdev, Tetsuo Handa



On 10/17/19 3:57 AM, Toke Høiland-Jørgensen wrote:
> Tetsuo pointed out that without an explicit cast, the cost calculation for
> devmap_hash type maps could overflow on 32-bit builds. This adds the
> missing cast.
> 
> Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
> Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>

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

> ---
>   kernel/bpf/devmap.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
> index a0a1153da5ae..e34fac6022eb 100644
> --- a/kernel/bpf/devmap.c
> +++ b/kernel/bpf/devmap.c
> @@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
>   
>   		if (!dtab->n_buckets) /* Overflow check */
>   			return -EINVAL;
> -		cost += sizeof(struct hlist_head) * dtab->n_buckets;
> +		cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;
>   	}
>   
>   	/* if map size is larger than memlock limit, reject it */
> 

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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-18  9:15   ` Toke Høiland-Jørgensen
@ 2019-10-18 17:19     ` Jakub Kicinski
  0 siblings, 0 replies; 8+ messages in thread
From: Jakub Kicinski @ 2019-10-18 17:19 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen; +Cc: daniel, ast, bpf, netdev, Tetsuo Handa

On Fri, 18 Oct 2019 11:15:39 +0200, Toke Høiland-Jørgensen wrote:
> Jakub Kicinski <jakub.kicinski@netronome.com> writes:
> 
> > On Thu, 17 Oct 2019 12:57:02 +0200, Toke Høiland-Jørgensen wrote:  
> >> Tetsuo pointed out that without an explicit cast, the cost calculation for
> >> devmap_hash type maps could overflow on 32-bit builds. This adds the
> >> missing cast.
> >> 
> >> Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
> >> Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
> >> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> >> ---
> >>  kernel/bpf/devmap.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >> 
> >> diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
> >> index a0a1153da5ae..e34fac6022eb 100644
> >> --- a/kernel/bpf/devmap.c
> >> +++ b/kernel/bpf/devmap.c
> >> @@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
> >>  
> >>  		if (!dtab->n_buckets) /* Overflow check */
> >>  			return -EINVAL;
> >> -		cost += sizeof(struct hlist_head) * dtab->n_buckets;
> >> +		cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;  
> >
> > array_size()?  
> 
> Well, array_size does this:
> 
> 	if (check_mul_overflow(a, b, &bytes))
> 		return SIZE_MAX;
> 
> However, we don't to return SIZE_MAX on overflow, we want the
> calculation itself to be done in 64 bits so it won't overflow... Or?

Note that array_size calculates on size_t, so it should be fine.
But looking at it, it seems all of this code uses the (u64) cast, 
so I guess that's fine. Clean up for another day :)

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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-17 10:57 [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds Toke Høiland-Jørgensen
  2019-10-17 18:52 ` Jakub Kicinski
  2019-10-18 16:44 ` Yonghong Song
@ 2019-10-18 23:25 ` Alexei Starovoitov
  2019-10-18 23:31   ` Jakub Kicinski
  2019-10-19  0:44   ` David Miller
  2 siblings, 2 replies; 8+ messages in thread
From: Alexei Starovoitov @ 2019-10-18 23:25 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Daniel Borkmann, Alexei Starovoitov, bpf, Network Development,
	Tetsuo Handa, Jakub Kicinski, Yonghong Song

On Fri, Oct 18, 2019 at 8:36 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> Tetsuo pointed out that without an explicit cast, the cost calculation for
> devmap_hash type maps could overflow on 32-bit builds. This adds the
> missing cast.
>
> Fixes: 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")
> Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>

gmail delivery lags by a day :(
I applied this patch along with Yonghong's ack to bpf tree
(though I don't have it in my mail box, but it's there in patchworks).

I'm not sure that cleanup Jakub is proposing is possible or better.
Not everything is array_size here and in other places
where cost is computed. u64 is imo much cleaner.

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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-18 23:25 ` Alexei Starovoitov
@ 2019-10-18 23:31   ` Jakub Kicinski
  2019-10-19  0:44   ` David Miller
  1 sibling, 0 replies; 8+ messages in thread
From: Jakub Kicinski @ 2019-10-18 23:31 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Toke Høiland-Jørgensen, Daniel Borkmann,
	Alexei Starovoitov, bpf, Network Development, Tetsuo Handa,
	Yonghong Song

On Fri, 18 Oct 2019 16:25:24 -0700, Alexei Starovoitov wrote:
> I'm not sure that cleanup Jakub is proposing is possible or better.
> Not everything is array_size here and in other places
> where cost is computed. u64 is imo much cleaner.

Right, adding multiple components with size_t is going to be
unnecessarily tricky.

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

* Re: [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
  2019-10-18 23:25 ` Alexei Starovoitov
  2019-10-18 23:31   ` Jakub Kicinski
@ 2019-10-19  0:44   ` David Miller
  1 sibling, 0 replies; 8+ messages in thread
From: David Miller @ 2019-10-19  0:44 UTC (permalink / raw)
  To: alexei.starovoitov
  Cc: toke, daniel, ast, bpf, netdev, penguin-kernel, kubakici, yhs

From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Date: Fri, 18 Oct 2019 16:25:24 -0700

> gmail delivery lags by a day :(

Sorry, I'm working on that.

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

end of thread, other threads:[~2019-10-19  0:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 10:57 [PATCH bpf] xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds Toke Høiland-Jørgensen
2019-10-17 18:52 ` Jakub Kicinski
2019-10-18  9:15   ` Toke Høiland-Jørgensen
2019-10-18 17:19     ` Jakub Kicinski
2019-10-18 16:44 ` Yonghong Song
2019-10-18 23:25 ` Alexei Starovoitov
2019-10-18 23:31   ` Jakub Kicinski
2019-10-19  0:44   ` David Miller

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).