Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

NTB/msi: Use struct_size() helper in devm_kzalloc()

Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/ntb/msi.c:46:23: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Signed-off-by: Jon Mason <jdmason@kudzu.us>

authored by

Gustavo A. R. Silva and committed by
Jon Mason
30532568 e783362e

+2 -4
+2 -4
drivers/ntb/msi.c
··· 33 33 { 34 34 phys_addr_t mw_phys_addr; 35 35 resource_size_t mw_size; 36 - size_t struct_size; 37 36 int peer_widx; 38 37 int peers; 39 38 int ret; ··· 42 43 if (peers <= 0) 43 44 return -EINVAL; 44 45 45 - struct_size = sizeof(*ntb->msi) + sizeof(*ntb->msi->peer_mws) * peers; 46 - 47 - ntb->msi = devm_kzalloc(&ntb->dev, struct_size, GFP_KERNEL); 46 + ntb->msi = devm_kzalloc(&ntb->dev, struct_size(ntb->msi, peer_mws, peers), 47 + GFP_KERNEL); 48 48 if (!ntb->msi) 49 49 return -ENOMEM; 50 50