From: Gustavo A. R. Silva Date: Thu, 13 May 2021 23:01:55 +0000 (-0500) Subject: vfio/iommu_type1: Use struct_size() for kzalloc() X-Git-Tag: v5.13-rc5~14^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=78b238147e4d241bc1681d2559477c995f9dcb0a;p=thirdparty%2Fkernel%2Flinux.git vfio/iommu_type1: Use struct_size() for 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. This code was detected with the help of Coccinelle and, audited and fixed manually. Signed-off-by: Gustavo A. R. Silva Message-Id: <20210513230155.GA217517@embeddedor> Signed-off-by: Alex Williamson --- diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index a0747c35a7781..a3e925a41b0de 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2795,7 +2795,7 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, return 0; } - size = sizeof(*cap_iovas) + (iovas * sizeof(*cap_iovas->iova_ranges)); + size = struct_size(cap_iovas, iova_ranges, iovas); cap_iovas = kzalloc(size, GFP_KERNEL); if (!cap_iovas)