xdp: Prevent overflow in devmap_hash cost calculation for 32-bit builds
Subsystems (4)
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.
--- kernel/bpf/devmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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; }/* if map size is larger than memlock limit, reject it */
Signature · 1 line
2.23.0
Quoted 16 lines · Toke Høiland-Jørgensen
Tetsuo pointed out that without an explicit cast, the cost calculation for
devmap_hashtype 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 <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- kernel/bpf/devmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/devmap.c +0
-0
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 */
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_hashtype 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 <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- kernel/bpf/devmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/devmap.c +0
-0@@ -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
-1if (!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
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_hashtype 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 <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- kernel/bpf/devmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/devmap.c +0
-0@@ -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
-1if (!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_sizedoes this:if (check_mul_overflow(a, b, &bytes)) return SIZE_MAX;However, we don't to return
SIZE_MAXon 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 :)
Quoted 7 lines · Toke Høiland-Jørgensen
Tetsuo pointed out that without an explicit cast, the cost calculation for
devmap_hashtype 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 <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>
Quoted 18 lines
--- kernel/bpf/devmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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; }/* if map size is larger than memlock limit, reject it */
Quoted 8 lines · Toke Høiland-Jørgensen
Tetsuo pointed out that without an explicit cast, the cost calculation for
devmap_hashtype 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 <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>
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.
Quoted 3 lines · Alexei Starovoitov
I'm not sure that cleanup Jakub is proposing is possible or better. Not everything is
array_sizehere 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.
David Miller
maintainerFrom: Alexei Starovoitov <[email protected]>
Date: Fri, 18 Oct 2019 16:25:24 -0700
Quoted 1 line
gmail delivery lags by a day :(
Sorry, I'm working on that.