Skip to contentSkip to conversation

xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds

PATCH In review Toke Høiland-Jørgensen 5 participants 8 messages Oct 17, 2019 – Oct 19, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds

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")
---
 kernel/bpf/devmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/devmap.c +1 -1
index a0a1153da5ae..e34fac6022eb 100644
@@ -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 */
Signature · 1 line
2.23.0
#1 lore raw
Quoted 16 lines · Toke Høiland-Jørgensen

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")
---
 kernel/bpf/devmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/devmap.c +0 -0
index a0a1153da5ae..e34fac6022eb 100644

kernel/bpf/devmap.c static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) · line 131

Quoted 5 lines
kernel/bpf/devmap.c +1 -1
@@ -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()?

Quoted 3 lines
kernel/bpf/devmap.c +0 -0
 	}
 
 	/* if map size is larger than memlock limit, reject it */
#2 lore raw
Quoted 18 lines · Jakub Kicinski
Quoted 17 lines · Toke Høiland-Jørgensen

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")
---
 kernel/bpf/devmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/devmap.c +0 -0
index a0a1153da5ae..e34fac6022eb 100644
@@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
 
Quoted 6 lines

kernel/bpf/devmap.c static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) · line 131

Quoted 4 lines
kernel/bpf/devmap.c +1 -1
 		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

#3 lore raw
Quoted 27 lines · Toke Høiland-Jørgensen
Quoted 18 lines · Jakub Kicinski
Quoted 17 lines · Toke Høiland-Jørgensen

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")
---
 kernel/bpf/devmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/devmap.c +0 -0
index a0a1153da5ae..e34fac6022eb 100644
@@ -128,7 +128,7 @@ static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
 
Quoted 6 lines

kernel/bpf/devmap.c static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) · line 131

Quoted 4 lines
kernel/bpf/devmap.c +1 -1
 		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()?

Quoted 7 lines

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

Acked-by on v1 1/1
#4 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 7 lines · Toke Høiland-Jørgensen

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")
Quoted 18 lines
---
  kernel/bpf/devmap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/devmap.c +1 -1
index a0a1153da5ae..e34fac6022eb 100644
@@ -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 */
#5 lore raw
In reply to Toke Høiland-Jørgensen
Quoted 8 lines · Toke Høiland-Jørgensen

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

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.

#6 lore raw
Quoted 3 lines · Alexei Starovoitov

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.