Skip to content

Commit 7bb39a3

Browse files
Len Bakerdavem330
authored andcommitted
net: hns: Prefer struct_size over open coded arithmetic
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors. So, take the opportunity to refactor the hnae_handle structure to switch the last member to flexible array, changing the code accordingly. Also, fix the comment in the hnae_vf_cb structure to inform that the ae_handle member must be the last member. Then, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kzalloc() function. This code was detected with the help of Coccinelle and audited and fixed manually. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments Signed-off-by: Len Baker <len.baker@gmx.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 249ae94 commit 7bb39a3

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

drivers/net/ethernet/hisilicon/hns/hnae.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ struct hnae_handle {
558558
enum hnae_media_type media_type;
559559
struct list_head node; /* list to hnae_ae_dev->handle_list */
560560
struct hnae_buf_ops *bops; /* operation for the buffer */
561-
struct hnae_queue **qs; /* array base of all queues */
561+
struct hnae_queue *qs[]; /* flexible array of all queues */
562562
};
563563

564564
#define ring_to_dev(ring) ((ring)->q->dev->dev)

drivers/net/ethernet/hisilicon/hns/hns_ae_adapt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ static struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
8181
vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_id);
8282
qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_id);
8383

84-
vf_cb = kzalloc(sizeof(*vf_cb) +
85-
qnum_per_vf * sizeof(struct hnae_queue *), GFP_KERNEL);
84+
vf_cb = kzalloc(struct_size(vf_cb, ae_handle.qs, qnum_per_vf),
85+
GFP_KERNEL);
8686
if (unlikely(!vf_cb)) {
8787
dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
8888
ae_handle = ERR_PTR(-ENOMEM);
@@ -108,7 +108,6 @@ static struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
108108
goto vf_id_err;
109109
}
110110

111-
ae_handle->qs = (struct hnae_queue **)(&ae_handle->qs + 1);
112111
for (i = 0; i < qnum_per_vf; i++) {
113112
ae_handle->qs[i] = &ring_pair_cb->q;
114113
ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];

drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ struct hnae_vf_cb {
210210
u8 port_index;
211211
struct hns_mac_cb *mac_cb;
212212
struct dsaf_device *dsaf_dev;
213-
struct hnae_handle ae_handle; /* must be the last number */
213+
struct hnae_handle ae_handle; /* must be the last member */
214214
};
215215

216216
struct dsaf_int_xge_src {

0 commit comments

Comments
 (0)