]> git.baikalelectronics.ru Git - kernel.git/commit
net: hns: Prefer struct_size over open coded arithmetic
authorLen Baker <len.baker@gmx.com>
Mon, 11 Oct 2021 09:01:00 +0000 (11:01 +0200)
committerDavid S. Miller <davem@davemloft.net>
Tue, 12 Oct 2021 10:23:11 +0000 (11:23 +0100)
commit4864997a7a0ca6c99cb04e2fa65389dbae3315c1
tree254d06ce66486cec8db696859b2a7f8a6c9d9c76
parent86ee179a9732856cadf1f95bf56ed42f60447257
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>
drivers/net/ethernet/hisilicon/hns/hnae.h
drivers/net/ethernet/hisilicon/hns/hns_ae_adapt.c
drivers/net/ethernet/hisilicon/hns/hns_dsaf_main.h