From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25C10C433E0 for ; Mon, 22 Jun 2020 23:02:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E7A5F2075A for ; Mon, 22 Jun 2020 23:02:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592866943; bh=6/yCpyjWjtd5QDSuX8I0LLUq4Bd1x+0uiGkmMMhUS0A=; h=Date:From:To:Cc:Subject:List-ID:From; b=kKH5SXTVlXXbr97fHqyaNQsk+6I6OG7u3PATgxX99x5FBVfCgseehlgFfnYLucECt mUKO5Y9uCMvZj59V/kmWN5D8qvSpsUBZem6ewTgfTL03PWPla4xZaAXGpMB8OQtQ7n sa9efkZ+uZ5IJax28YTUswEdUMehaqLasi2HYwsY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731402AbgFVXCN (ORCPT ); Mon, 22 Jun 2020 19:02:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:43148 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731268AbgFVXCN (ORCPT ); Mon, 22 Jun 2020 19:02:13 -0400 Received: from embeddedor (unknown [189.207.59.248]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C730720738; Mon, 22 Jun 2020 23:02:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592866932; bh=6/yCpyjWjtd5QDSuX8I0LLUq4Bd1x+0uiGkmMMhUS0A=; h=Date:From:To:Cc:Subject:From; b=dw14yVKDOtzqB8fZZ4UnYecQOb8+XUo3yiNw6ZP4jeGZSEZAUftgZfmGvIRbGQUN4 bDnsUxgzkhrEC0oCczHV/H2jrRHIrtqi2GanhqG0myZQnLatWFsusiz5QIR8agR57B 8KvzrCM3bOy9TJDABAoVE4H7f3akJ72VywG+iwyg= Date: Mon, 22 Jun 2020 18:07:41 -0500 From: "Gustavo A. R. Silva" To: "David S. Miller" , Jakub Kicinski , Alexey Kuznetsov , Hideaki YOSHIFUJI Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH][next] net: ipv6: Use struct_size() helper and kcalloc() Message-ID: <20200622230741.GA28911@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. Also, remove unnecessary function ipv6_rpl_srh_alloc_size() and replace kzalloc() with kcalloc(), which has a 2-factor argument form for multiplication. This code was detected with the help of Coccinelle and, audited and fixed manually. Signed-off-by: Gustavo A. R. Silva --- include/net/rpl.h | 6 ------ net/ipv6/exthdrs.c | 2 +- net/ipv6/rpl_iptunnel.c | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/include/net/rpl.h b/include/net/rpl.h index dceff60e8baf..308ef0a05cae 100644 --- a/include/net/rpl.h +++ b/include/net/rpl.h @@ -26,12 +26,6 @@ static inline void rpl_exit(void) {} /* Worst decompression memory usage ipv6 address (16) + pad 7 */ #define IPV6_RPL_SRH_WORST_SWAP_SIZE (sizeof(struct in6_addr) + 7) -static inline size_t ipv6_rpl_srh_alloc_size(unsigned char n) -{ - return sizeof(struct ipv6_rpl_sr_hdr) + - ((n + 1) * sizeof(struct in6_addr)); -} - size_t ipv6_rpl_srh_size(unsigned char n, unsigned char cmpri, unsigned char cmpre); diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index 5a8bbcdcaf2b..e9b366994475 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -580,7 +580,7 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb) hdr->segments_left--; i = n - hdr->segments_left; - buf = kzalloc(ipv6_rpl_srh_alloc_size(n + 1) * 2, GFP_ATOMIC); + buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC); if (unlikely(!buf)) { kfree_skb(skb); return -1; diff --git a/net/ipv6/rpl_iptunnel.c b/net/ipv6/rpl_iptunnel.c index c3ececd7cfc1..5fdf3ebb953f 100644 --- a/net/ipv6/rpl_iptunnel.c +++ b/net/ipv6/rpl_iptunnel.c @@ -136,8 +136,7 @@ static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rlwt, oldhdr = ipv6_hdr(skb); - buf = kzalloc(ipv6_rpl_srh_alloc_size(srh->segments_left - 1) * 2, - GFP_ATOMIC); + buf = kcalloc(struct_size(srh, segments.addr, srh->segments_left), 2, GFP_ATOMIC); if (!buf) return -ENOMEM; -- 2.27.0