Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp4714014pxb; Tue, 25 Jan 2022 17:20:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7waNF24Lu3a6cx69/gDVpyr/H67nDIPOrucAr8henEEsPST0Rmqm2DXIMfYwxIPN/l15p X-Received: by 2002:a17:906:9503:: with SMTP id u3mr18010285ejx.423.1643160049169; Tue, 25 Jan 2022 17:20:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643160049; cv=none; d=google.com; s=arc-20160816; b=FzRG43vwaDWlMW25doB606pQyvZG0c2tBpLO8qeXgMqgfFwblvGQhJOhPbwZv6qnNw 4NCDyYbd5oIKvyZYgO5BRbT+Ofe2+5S1u7T0Y37k9bqmfGRWGzRpl0HFbDNsjuJlIGpk 4/vfiCjuIi8dyosMfRg1dHSrhCOKQoEmIE2TX7GeJmgvzV+xYrPNIPs9PLncghN4LqC3 dLbKZuUDgJH/xgXsxXaKJsKnzCJ/QGUiCmQbLGxKpPxyrJszE5xNHafXLG89Kx9BgUuu NW0f5pmQJ2pzO80XfvXKgufNrrTTq0QQLpjt+Lu+Lfo/r9jCZvuecPvr2057I5pGzJ2E UWNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=nS7LCEFQzsBf00GEKWT2QOKMgnGbsj9iR7Wclg4KMEg=; b=EZ6UZfIwwvijwLSgPtfOGM5t5+583tMbmg/mx4vQ2/JtYj3HF0J/7XJvOwGQ+4r/Mp DmSh2YJWT6wC4jn5JIVrRUYdk67uY0h5otW22JTiSYVO1D7FepramFQueZ6FSYKbpvbf bWQlyzwLcKsktTowkhc8lw2VIJRyscAeiG4hQ5K09dvLv0LjCm/YTKTPtqc5qZYU9DUX j0iUClEN67XlBW5rn8mHoD+Y7z1jmf9svJ7hKnKFdEWiyXmpqKhE3P272a/+hBCcp2+c 61E5K48b0NJnpNOIZkxthXtZ4iEexZrhocrrmTya1JWQAcRO5ADtNsr7FWLfZGVNGFta h34A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OvIlRzdN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c6si7232623edm.481.2022.01.25.17.20.25; Tue, 25 Jan 2022 17:20:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OvIlRzdN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355792AbiAYQ7G (ORCPT + 99 others); Tue, 25 Jan 2022 11:59:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1586531AbiAYQ5Q (ORCPT ); Tue, 25 Jan 2022 11:57:16 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82444C0617A3; Tue, 25 Jan 2022 08:54:45 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 498CAB81626; Tue, 25 Jan 2022 16:54:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45D50C340E0; Tue, 25 Jan 2022 16:54:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643129683; bh=rw+3G6xtiUqoEm6IJINS6kg0qGUDlHZ60ZFR3a2STdw=; h=Date:From:To:Cc:Subject:From; b=OvIlRzdNs0u3TKQbD+zqONaqes09wji3XfQ/OdhWSiJNI9x4HiJ6+hDJe6TWjcmVo Ixl108Kp3dLorankwVLvgTuyHHjX6dv9J418QqD93hLZMvID0odhe0s9dRt45GxE0C uf81M+1H3xmwlOxJzOxNz3Dv3/S85GOuNa6vr3Ml0wKRzV8YU7RKvknFgUkLynWLqD advRLLvcLI1Bmp8p967m0/NEgWMKyrIyiNfP5SYb68OaYrvOF6Ih8YXhouE22ViX4s LsuwuzZX0hr4fX3VQWRkOwRgoKXc+VYn5kkEYZHa5sDjcpm+AqxS6jzFIx5FP3o+nq EKQeXrzA/m93Q== Date: Tue, 25 Jan 2022 11:01:28 -0600 From: "Gustavo A. R. Silva" To: Ido Schimmel , Petr Machata , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] mlxsw: spectrum_kvdl: Use struct_size() helper in kzalloc() Message-ID: <20220125170128.GA60918@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c:229:24: warning: using sizeof on a flexible structure Link: https://github.com/KSPP/linux/issues/174 Signed-off-by: Gustavo A. R. Silva --- drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c index a9fff8adc75e..d20e794e01ca 100644 --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c @@ -213,7 +213,6 @@ mlxsw_sp1_kvdl_part_init(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp1_kvdl_part *part; bool need_update = true; unsigned int nr_entries; - size_t usage_size; u64 resource_size; int err; @@ -225,8 +224,8 @@ mlxsw_sp1_kvdl_part_init(struct mlxsw_sp *mlxsw_sp, } nr_entries = div_u64(resource_size, info->alloc_size); - usage_size = BITS_TO_LONGS(nr_entries) * sizeof(unsigned long); - part = kzalloc(sizeof(*part) + usage_size, GFP_KERNEL); + part = kzalloc(struct_size(part, usage, BITS_TO_LONGS(nr_entries)), + GFP_KERNEL); if (!part) return ERR_PTR(-ENOMEM); -- 2.27.0