Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp56077pxt; Wed, 4 Aug 2021 15:39:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwdfwIDkVYjFp+Bl0GX/nYV40gaDrqAGBqM8fkeCRRjBeI6Tss+qxuvjLPQ0TXG/HsKbwq X-Received: by 2002:a17:907:76cd:: with SMTP id kf13mr1454337ejc.310.1628116739958; Wed, 04 Aug 2021 15:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628116739; cv=none; d=google.com; s=arc-20160816; b=Dti6D21afbWuuk8+Ly8/el/DXTSfKU30QqEs+Q0zCoFZ9QQKlFEYB9+BIInXkujcUH ebzWsITseGEvQcAjqsu5gwTqKfZ75DlQjrWxcSax0GQ0HG2T9UkNybY7wfkS1+bRWcyx B3Cb7DFX4yyZoU5TFvQ9hh+T0LOiNQyW7rcOn4HaruvtnD8XRt3YMInpTkFqKVCiR1JL 9ez5QLaJN4tbSbMVBOTtt63vCah64FC0Z1bwzX+d4MCcu/UxpdfarhZhx76waIAPqEzy NOEf+w7E4teFxz2118rOOnPwaHnDBwPYHXM9ZJoe7+1XkWOUHf4iY7B4Imsvy+z1zb5h muXQ== 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=unGtaupqkTS0IHBicb4JEV9CQeV0CXPk45T3N6nI2cg=; b=WAmpSWJeuUaWp7xSM3LhAqKxcyQNDon+nC11V8dL1DFB9LFMKfUlSQpT8rVrB/e+YN bZM/aiW7wz0n/el5uXqJvldQqpC4Ta9GG+YVULytbMVTmgJCO0YqyHugEqBZInLIfEtJ N7iqi/f2M7kknLQ2cl6cCQtL5boJHk+qO7I3XD0OEl8HKiBBuOyzj9RNQUqfSoKL9wUd pdZXPSQva9wyP02lz3kl4ja+PAdWl6/I3KWHdo9N7U29n6HzdeflqFZX/+D7IRr79eOD BRdbftqHmzfpM8Kez6EJyjZPOsP/XelHUYsy01TJr6XgB2pp9RZeidmcJfyXy6aps/EK hyjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JlTvXNk9; 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 h26si3336102eds.228.2021.08.04.15.38.34; Wed, 04 Aug 2021 15:38:59 -0700 (PDT) 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=JlTvXNk9; 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 S232849AbhHDVl0 (ORCPT + 99 others); Wed, 4 Aug 2021 17:41:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:48564 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231893AbhHDVlZ (ORCPT ); Wed, 4 Aug 2021 17:41:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7BE7360C3F; Wed, 4 Aug 2021 21:41:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628113272; bh=19rsvQG9EMQz8ZhU8YsqRzqcn4Eqt5DitxY+bamsUcM=; h=Date:From:To:Cc:Subject:From; b=JlTvXNk9yjFt4kBLUd2m7/d5ZyfZPTcIYF7Hc9Omr28oBjW62I0Gdd1V/ih/72y+h XKSDdi2e4o6F36xUgFDGD1LK0FAzqkSutClEEsZZYOA1MkEWzk8GX6tbmju4DksBXJ BOHwNy56fXnWQwyoa26p8CT+a8Vc1rXgurUmwUm17XGqa0Neh9WawGnrv2E/zjOR1/ CS4iK3w7V1qBFTUZVkHwCUmbTr6fsFeuI/NAsURQSkvtzCp0xIz/0+WXd8UM4L+MUS E62czxYC/LrHuQimjN92RM2A6v23PBIRumwwMwbNB1WmfGRVo7wu8dMqsICo3RC3Fx 7kOZ12XC/Znxg== Date: Wed, 4 Aug 2021 16:43:52 -0500 From: "Gustavo A. R. Silva" To: "David S. Miller" , Jakub Kicinski , Hideaki YOSHIFUJI , David Ahern Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH][next] net/ipv6/mcast: Use struct_size() helper Message-ID: <20210804214352.GA46670@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 Replace IP6_SFLSIZE() with struct_size() helper in order to avoid any potential type mistakes or integer overflows that, in the worst scenario, could lead to heap overflows. Signed-off-by: Gustavo A. R. Silva --- include/net/if_inet6.h | 3 --- net/ipv6/mcast.c | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/net/if_inet6.h b/include/net/if_inet6.h index 71bb4cc4d05d..42235c178b06 100644 --- a/include/net/if_inet6.h +++ b/include/net/if_inet6.h @@ -82,9 +82,6 @@ struct ip6_sf_socklist { struct in6_addr sl_addr[]; }; -#define IP6_SFLSIZE(count) (sizeof(struct ip6_sf_socklist) + \ - (count) * sizeof(struct in6_addr)) - #define IP6_SFBLOCK 10 /* allocate this many at once */ struct ipv6_mc_socklist { diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c index 54ec163fbafa..cd951faa2fac 100644 --- a/net/ipv6/mcast.c +++ b/net/ipv6/mcast.c @@ -447,7 +447,8 @@ int ip6_mc_source(int add, int omode, struct sock *sk, if (psl) count += psl->sl_max; - newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_KERNEL); + newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count), + GFP_KERNEL); if (!newpsl) { err = -ENOBUFS; goto done; @@ -457,7 +458,8 @@ int ip6_mc_source(int add, int omode, struct sock *sk, if (psl) { for (i = 0; i < psl->sl_count; i++) newpsl->sl_addr[i] = psl->sl_addr[i]; - atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); + atomic_sub(struct_size(psl, sl_addr, psl->sl_max), + &sk->sk_omem_alloc); kfree_rcu(psl, rcu); } psl = newpsl; @@ -525,8 +527,9 @@ int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf, goto done; } if (gsf->gf_numsrc) { - newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc), - GFP_KERNEL); + newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, + gsf->gf_numsrc), + GFP_KERNEL); if (!newpsl) { err = -ENOBUFS; goto done; @@ -543,7 +546,8 @@ int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf, newpsl->sl_count, newpsl->sl_addr, 0); if (err) { mutex_unlock(&idev->mc_lock); - sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max)); + sock_kfree_s(sk, newpsl, struct_size(newpsl, sl_addr, + newpsl->sl_max)); goto done; } mutex_unlock(&idev->mc_lock); @@ -559,7 +563,8 @@ int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf, if (psl) { ip6_mc_del_src(idev, group, pmc->sfmode, psl->sl_count, psl->sl_addr, 0); - atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); + atomic_sub(struct_size(psl, sl_addr, psl->sl_max), + &sk->sk_omem_alloc); kfree_rcu(psl, rcu); } else { ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0); @@ -2607,7 +2612,8 @@ static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, psl->sl_count, psl->sl_addr, 0); RCU_INIT_POINTER(iml->sflist, NULL); - atomic_sub(IP6_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); + atomic_sub(struct_size(psl, sl_addr, psl->sl_max), + &sk->sk_omem_alloc); kfree_rcu(psl, rcu); } -- 2.27.0