diff mbox series

[iptables,1/3] nft: Fix potential memleaks in nft_*_rule_find()

Message ID 20190122101423.29281-2-phil@nwl.cc
State Accepted
Delegated to: Pablo Neira
Headers show
Series xtables: Fix multiple issues in rule matching code | expand

Commit Message

Phil Sutter Jan. 22, 2019, 10:14 a.m. UTC
These functions parse an nftnl_rule into a local instance of
iptables_command_state which potentially allocates memory (for matches
or target), so call ops->clear_cs() before returning to caller.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 iptables/nft-arp.c    | 12 ++++++++----
 iptables/nft-bridge.c | 14 +++++++++-----
 iptables/nft-shared.c | 14 +++++++++-----
 3 files changed, 26 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
index 399f83afff5cc..5f311753b939d 100644
--- a/iptables/nft-arp.c
+++ b/iptables/nft-arp.c
@@ -646,20 +646,24 @@  static bool nft_arp_rule_find(struct nft_family_ops *ops, struct nftnl_rule *r,
 {
 	const struct iptables_command_state *cs = data;
 	struct iptables_command_state this = {};
+	bool ret = false;
 
 	/* Delete by matching rule case */
 	nft_rule_to_iptables_command_state(r, &this);
 
 	if (!nft_arp_is_same(&cs->arp, &this.arp))
-		return false;
+		goto out;
 
 	if (!compare_targets(cs->target, this.target))
-		return false;
+		goto out;
 
 	if (this.jumpto && strcmp(cs->jumpto, this.jumpto) != 0)
-		return false;
+		goto out;
 
-	return true;
+	ret = true;
+out:
+	ops->clear_cs(&this);
+	return ret;
 }
 
 static void nft_arp_save_chain(const struct nftnl_chain *c, const char *policy)
diff --git a/iptables/nft-bridge.c b/iptables/nft-bridge.c
index ad583a60c424d..cbd671c38d2e1 100644
--- a/iptables/nft-bridge.c
+++ b/iptables/nft-bridge.c
@@ -547,30 +547,34 @@  static bool nft_bridge_rule_find(struct nft_family_ops *ops, struct nftnl_rule *
 {
 	struct iptables_command_state *cs = data;
 	struct iptables_command_state this = {};
+	bool ret = false;
 
 	nft_rule_to_ebtables_command_state(r, &this);
 
 	DEBUGP("comparing with... ");
 
 	if (!nft_bridge_is_same(cs, &this))
-		return false;
+		goto out;
 
 	if (!compare_matches(cs->matches, this.matches)) {
 		DEBUGP("Different matches\n");
-		return false;
+		goto out;
 	}
 
 	if (!compare_targets(cs->target, this.target)) {
 		DEBUGP("Different target\n");
-		return false;
+		goto out;
 	}
 
 	if (cs->jumpto != NULL && strcmp(cs->jumpto, this.jumpto) != 0) {
 		DEBUGP("Different verdict\n");
-		return false;
+		goto out;
 	}
 
-	return true;
+	ret = true;
+out:
+	ops->clear_cs(&this);
+	return ret;
 }
 
 static int xlate_ebmatches(const struct iptables_command_state *cs, struct xt_xlate *xl)
diff --git a/iptables/nft-shared.c b/iptables/nft-shared.c
index 05b091ea2c0d5..ca4e593656562 100644
--- a/iptables/nft-shared.c
+++ b/iptables/nft-shared.c
@@ -961,6 +961,7 @@  bool nft_ipv46_rule_find(struct nft_family_ops *ops,
 			 struct nftnl_rule *r, void *data)
 {
 	struct iptables_command_state *cs = data, this = {};
+	bool ret = false;
 
 	nft_rule_to_iptables_command_state(r, &this);
 
@@ -969,24 +970,27 @@  bool nft_ipv46_rule_find(struct nft_family_ops *ops,
 	nft_rule_print_save(r, NFT_RULE_APPEND, 0);
 #endif
 	if (!ops->is_same(cs, &this))
-		return false;
+		goto out;
 
 	if (!compare_matches(cs->matches, this.matches)) {
 		DEBUGP("Different matches\n");
-		return false;
+		goto out;
 	}
 
 	if (!compare_targets(cs->target, this.target)) {
 		DEBUGP("Different target\n");
-		return false;
+		goto out;
 	}
 
 	if (strcmp(cs->jumpto, this.jumpto) != 0) {
 		DEBUGP("Different verdict\n");
-		return false;
+		goto out;
 	}
 
-	return true;
+	ret = true;
+out:
+	ops->clear_cs(&this);
+	return ret;
 }
 
 void nft_check_xt_legacy(int family, bool is_ipt_save)