mirror of
https://github.com/hardkernel/linux.git
synced 2026-06-06 02:50:49 +09:00
RDMA/core: Use size_{add,sub,mul}() in calls to struct_size()
[ Upstream commit 81760bedc65194ff38e1e4faefd5f9f0c95c19a4 ] If, for any reason, the open-coded arithmetic causes a wraparound, the protection that `struct_size()` provides against potential integer overflows is defeated. Fix this by hardening calls to `struct_size()` with `size_add()`, `size_sub()` and `size_mul()`. Fixes:467f432a52("RDMA/core: Split port and device counter sysfs attributes") Fixes:a4676388e2("RDMA/core: Simplify how the gid_attrs sysfs is created") Fixes:e9dd5daf88("IB/umad: Refactor code to use cdev_device_add()") Fixes:324e227ea7("RDMA/device: Add ib_device_get_by_netdev()") Fixes:5aad26a7ea("IB/core: Use struct_size() in kzalloc()") Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/r/ZQdt4NsJFwwOYxUR@work Signed-off-by: Leon Romanovsky <leon@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
e39b84448f
commit
bb55130d02
@@ -804,7 +804,7 @@ static int alloc_port_data(struct ib_device *device)
|
|||||||
* empty slots at the beginning.
|
* empty slots at the beginning.
|
||||||
*/
|
*/
|
||||||
pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
|
pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata,
|
||||||
rdma_end_port(device) + 1),
|
size_add(rdma_end_port(device), 1)),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!pdata_rcu)
|
if (!pdata_rcu)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|||||||
@@ -2220,7 +2220,9 @@ static int ib_sa_add_one(struct ib_device *device)
|
|||||||
s = rdma_start_port(device);
|
s = rdma_start_port(device);
|
||||||
e = rdma_end_port(device);
|
e = rdma_end_port(device);
|
||||||
|
|
||||||
sa_dev = kzalloc(struct_size(sa_dev, port, e - s + 1), GFP_KERNEL);
|
sa_dev = kzalloc(struct_size(sa_dev, port,
|
||||||
|
size_add(size_sub(e, s), 1)),
|
||||||
|
GFP_KERNEL);
|
||||||
if (!sa_dev)
|
if (!sa_dev)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
|||||||
@@ -903,7 +903,7 @@ alloc_hw_stats_device(struct ib_device *ibdev)
|
|||||||
* Two extra attribue elements here, one for the lifespan entry and
|
* Two extra attribue elements here, one for the lifespan entry and
|
||||||
* one to NULL terminate the list for the sysfs core code
|
* one to NULL terminate the list for the sysfs core code
|
||||||
*/
|
*/
|
||||||
data = kzalloc(struct_size(data, attrs, stats->num_counters + 1),
|
data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!data)
|
if (!data)
|
||||||
goto err_free_stats;
|
goto err_free_stats;
|
||||||
@@ -1009,7 +1009,7 @@ alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
|
|||||||
* Two extra attribue elements here, one for the lifespan entry and
|
* Two extra attribue elements here, one for the lifespan entry and
|
||||||
* one to NULL terminate the list for the sysfs core code
|
* one to NULL terminate the list for the sysfs core code
|
||||||
*/
|
*/
|
||||||
data = kzalloc(struct_size(data, attrs, stats->num_counters + 1),
|
data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!data)
|
if (!data)
|
||||||
goto err_free_stats;
|
goto err_free_stats;
|
||||||
@@ -1140,7 +1140,7 @@ static int setup_gid_attrs(struct ib_port *port,
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
gid_attr_group = kzalloc(struct_size(gid_attr_group, attrs_list,
|
gid_attr_group = kzalloc(struct_size(gid_attr_group, attrs_list,
|
||||||
attr->gid_tbl_len * 2),
|
size_mul(attr->gid_tbl_len, 2)),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!gid_attr_group)
|
if (!gid_attr_group)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
@@ -1205,8 +1205,8 @@ static struct ib_port *setup_port(struct ib_core_device *coredev, int port_num,
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
p = kvzalloc(struct_size(p, attrs_list,
|
p = kvzalloc(struct_size(p, attrs_list,
|
||||||
attr->gid_tbl_len + attr->pkey_tbl_len),
|
size_add(attr->gid_tbl_len, attr->pkey_tbl_len)),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!p)
|
if (!p)
|
||||||
return ERR_PTR(-ENOMEM);
|
return ERR_PTR(-ENOMEM);
|
||||||
p->ibdev = device;
|
p->ibdev = device;
|
||||||
|
|||||||
@@ -1373,7 +1373,9 @@ static int ib_umad_add_one(struct ib_device *device)
|
|||||||
s = rdma_start_port(device);
|
s = rdma_start_port(device);
|
||||||
e = rdma_end_port(device);
|
e = rdma_end_port(device);
|
||||||
|
|
||||||
umad_dev = kzalloc(struct_size(umad_dev, ports, e - s + 1), GFP_KERNEL);
|
umad_dev = kzalloc(struct_size(umad_dev, ports,
|
||||||
|
size_add(size_sub(e, s), 1)),
|
||||||
|
GFP_KERNEL);
|
||||||
if (!umad_dev)
|
if (!umad_dev)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user