netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org,
	pabeni@redhat.com, edumazet@google.com, fw@strlen.de
Subject: [PATCH net-next 03/16] netfilter: Use kmemdup_array instead of kmemdup for multiple allocation
Date: Fri,  6 Sep 2024 01:29:07 +0200	[thread overview]
Message-ID: <20240905232920.5481-4-pablo@netfilter.org> (raw)
In-Reply-To: <20240905232920.5481-1-pablo@netfilter.org>

From: Yan Zhen <yanzhen@vivo.com>

When we are allocating an array, using kmemdup_array() to take care about
multiplication and possible overflows.

Also it makes auditing the code easier.

Signed-off-by: Yan Zhen <yanzhen@vivo.com>
Reviewed-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/bridge/netfilter/ebtables.c | 2 +-
 net/ipv4/netfilter/arp_tables.c | 2 +-
 net/ipv4/netfilter/ip_tables.c  | 2 +-
 net/ipv6/netfilter/ip6_tables.c | 2 +-
 net/netfilter/nf_nat_core.c     | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
index cbd0e3586c3f..3e67d4aff419 100644
--- a/net/bridge/netfilter/ebtables.c
+++ b/net/bridge/netfilter/ebtables.c
@@ -1256,7 +1256,7 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table,
 		goto free_unlock;
 	}
 
-	ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
+	ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
 	if (!ops) {
 		ret = -ENOMEM;
 		if (newinfo->nentries)
diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index 42c34e8952da..1cdd9c28ab2d 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -1547,7 +1547,7 @@ int arpt_register_table(struct net *net,
 		goto out_free;
 	}
 
-	ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
+	ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
 	if (!ops) {
 		ret = -ENOMEM;
 		goto out_free;
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index 97e754ddc155..3d101613f27f 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -1767,7 +1767,7 @@ int ipt_register_table(struct net *net, const struct xt_table *table,
 		goto out_free;
 	}
 
-	ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
+	ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
 	if (!ops) {
 		ret = -ENOMEM;
 		goto out_free;
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index 131f7bb2110d..7d5602950ae7 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -1773,7 +1773,7 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,
 		goto out_free;
 	}
 
-	ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
+	ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
 	if (!ops) {
 		ret = -ENOMEM;
 		goto out_free;
diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c
index 016c816d91cb..6d8da6dddf99 100644
--- a/net/netfilter/nf_nat_core.c
+++ b/net/netfilter/nf_nat_core.c
@@ -1104,7 +1104,7 @@ int nf_nat_register_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops,
 	if (!nat_proto_net->nat_hook_ops) {
 		WARN_ON(nat_proto_net->users != 0);
 
-		nat_ops = kmemdup(orig_nat_ops, sizeof(*orig_nat_ops) * ops_count, GFP_KERNEL);
+		nat_ops = kmemdup_array(orig_nat_ops, ops_count, sizeof(*orig_nat_ops), GFP_KERNEL);
 		if (!nat_ops) {
 			mutex_unlock(&nf_nat_proto_mutex);
 			return -ENOMEM;
-- 
2.30.2


  parent reply	other threads:[~2024-09-05 23:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-05 23:29 [PATCH net-next 00/16] Netfilter updates for net-next Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 01/16] netfilter: ctnetlink: support CTA_FILTER for flush Pablo Neira Ayuso
2024-09-07  2:10   ` patchwork-bot+netdevbpf
2024-09-05 23:29 ` [PATCH net-next 02/16] netfilter: nft_counter: Use u64_stats_t for statistic Pablo Neira Ayuso
2024-09-05 23:29 ` Pablo Neira Ayuso [this message]
2024-09-05 23:29 ` [PATCH net-next 04/16] netfilter: conntrack: Convert to use ERR_CAST() Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 05/16] netfilter: nf_tables: drop unused 3rd argument from validate callback ops Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 06/16] netfilter: nf_tables: Correct spelling in nf_tables.h Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 07/16] netfilter: nf_tables: Add missing Kernel doc Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 08/16] netfilter: nf_tables: elements with timeout below CONFIG_HZ never expire Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 09/16] netfilter: nf_tables: reject element expiration with no timeout Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 10/16] netfilter: nf_tables: reject expiration higher than timeout Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 11/16] netfilter: nf_tables: remove annotation to access set timeout while holding lock Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 12/16] netfilter: nft_dynset: annotate data-races around set timeout Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 13/16] netfilter: nf_tables: annotate data-races around element expiration Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 14/16] netfilter: nf_tables: consolidate timeout extension for elements Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 15/16] netfilter: nf_tables: zero timeout means element never times out Pablo Neira Ayuso
2024-09-05 23:29 ` [PATCH net-next 16/16] netfilter: nf_tables: set element timeout update support Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240905232920.5481-4-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fw@strlen.de \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).