Skip to contentSkip to conversation

netfilter: nfnetlink_cthelper: fix runtime expectation policy updates

PATCH Superseded Pablo Neira Ayuso 2 participants 3 messages Mar 21, 2017 – Mar 22, 2017

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 netfilter: nfnetlink_cthelper: fix runtime expectation policy updates

We only allow runtime updates of expectation policies for timeout and maximum number of expectations, otherwise reject the update.

---
 net/netfilter/nfnetlink_cthelper.c | 68 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 66 insertions(+), 2 deletions(-)
net/netfilter/nfnetlink_cthelper.c +66 -2
index de8782345c86..ffb51b91e646 100644
@@ -254,6 +254,71 @@ nfnl_cthelper_create(const struct nlattr * const tb[],
 }
 
 static int
+nfnl_cthelper_update_policy_one(struct nf_conntrack_expect_policy *expect_policy,
+				const struct nlattr *attr)
+{
+	struct nlattr *tb[NFCTH_POLICY_MAX + 1];
+	int err;
+
+	err = nla_parse_nested(tb, NFCTH_POLICY_MAX, attr,
+			       nfnl_cthelper_expect_pol);
+	if (err < 0)
+		return err;
+
+	if (!tb[NFCTH_POLICY_NAME] ||
+	    !tb[NFCTH_POLICY_EXPECT_MAX] ||
+	    !tb[NFCTH_POLICY_EXPECT_TIMEOUT])
+		return -EINVAL;
+
+	if (nla_strcmp(tb[NFCTH_POLICY_NAME], expect_policy->name))
+		return -EBUSY;
+
+	expect_policy->max_expected =
+		ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX]));
+	expect_policy->timeout =
+		ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT]));
+
+	return 0;
+}
+
+static int
+nfnl_cthelper_update_policy(struct nf_conntrack_helper *helper,
+			    const struct nlattr *attr)
+{
+	struct nf_conntrack_expect_policy *expect_policy;
+	struct nlattr *tb[NFCTH_POLICY_SET_MAX + 1];
+	unsigned int class_max;
+	int i, err;
+
+	err = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr,
+			       nfnl_cthelper_expect_policy_set);
+	if (err < 0)
+		return err;
+
+	if (!tb[NFCTH_POLICY_SET_NUM])
+		return -EINVAL;
+
+	class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
+	if (helper->expect_class_max != class_max)
+		return -EBUSY;
+
+	for (i = 0; i < helper->expect_class_max; i++) {
+		if (!tb[NFCTH_POLICY_SET + i])
+			return -EINVAL;
+
+		expect_policy =
+			(struct nf_conntrack_expect_policy *)
+				&helper->expect_policy[i];
+		err = nfnl_cthelper_update_policy_one(expect_policy,
+						      tb[NFCTH_POLICY_SET + i]);
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
+}
+
+static int
 nfnl_cthelper_update(const struct nlattr * const tb[],
 		     struct nf_conntrack_helper *helper)
 {
@@ -263,8 +328,7 @@ nfnl_cthelper_update(const struct nlattr * const tb[],
 		return -EBUSY;
 
 	if (tb[NFCTH_POLICY]) {
-		ret = nfnl_cthelper_parse_expect_policy(helper,
-							tb[NFCTH_POLICY]);
+		ret = nfnl_cthelper_update_policy(helper, tb[NFCTH_POLICY]);
 		if (ret < 0)
 			return ret;
 	}
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 2/2 netfilter: cthelper: Fix memory leak

From: Jeffy Chen <[email protected]>

We have memory leaks of nf_conntrack_helper & expect_policy.

---
Heavily based on your original patch, so I'm keeping you as original author for this fix.

 net/netfilter/nfnetlink_cthelper.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nfnetlink_cthelper.c +9 -3
index ffb51b91e646..bc43d8635cb8 100644
@@ -214,7 +214,7 @@ nfnl_cthelper_create(const struct nlattr * const tb[],
 
 	ret = nfnl_cthelper_parse_expect_policy(helper, tb[NFCTH_POLICY]);
 	if (ret < 0)
-		goto err;
+		goto err1;
 
 	strncpy(helper->name, nla_data(tb[NFCTH_NAME]), NF_CT_HELPER_NAME_LEN);
 	helper->data_len = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN]));
@@ -245,10 +245,12 @@ nfnl_cthelper_create(const struct nlattr * const tb[],
 
 	ret = nf_conntrack_helper_register(helper);
 	if (ret < 0)
-		goto err;
+		goto err2;
 
 	return 0;
-err:
+err2:
+	kfree(helper->expect_policy);
+err1:
 	kfree(helper);
 	return ret;
 }
@@ -676,6 +678,8 @@ static int nfnl_cthelper_del(struct net *net, struct sock *nfnl,
 
 			found = true;
 			nf_conntrack_helper_unregister(cur);
+			kfree(cur->expect_policy);
+			kfree(cur);
 		}
 	}
 	/* Make sure we return success if we flush and there is no helpers */
@@ -739,6 +743,8 @@ static void __exit nfnl_cthelper_exit(void)
 				continue;
 
 			nf_conntrack_helper_unregister(cur);
+			kfree(cur->expect_policy);
+			kfree(cur);
 		}
 	}
 }
Signature · 1 line
2.1.4
Tested-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2 nf] netfilter: cthelper: Fix memory leak

Hi Pablo,

On chromebook kevin:

Quoted 18 lines · Pablo Neira Ayuso

From: Jeffy Chen <[email protected]>

We have memory leaks of nf_conntrack_helper & expect_policy.

---
Heavily based on your original patch, so I'm keeping you as original author for this fix.

  net/netfilter/nfnetlink_cthelper.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nfnetlink_cthelper.c +0 -0
index ffb51b91e646..bc43d8635cb8 100644

net/netfilter/nfnetlink_cthelper.c nfnl_cthelper_create(const struct nlattr * const tb[], · line 217

Quoted 5 lines
net/netfilter/nfnetlink_cthelper.c +1 -1
@@ -214,7 +214,7 @@ nfnl_cthelper_create(const struct nlattr * const tb[],
 
  	ret = nfnl_cthelper_parse_expect_policy(helper, tb[NFCTH_POLICY]);
  	if (ret < 0)
-		goto err;
+		goto err1;

it's safe to kfree a null pointer, so we can use the same goto label with err2. But it's up to you, just matter of taste :)

Quoted 37 lines
net/netfilter/nfnetlink_cthelper.c +8 -2
  	strncpy(helper->name, nla_data(tb[NFCTH_NAME]), NF_CT_HELPER_NAME_LEN);
  	helper->data_len = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN]));
@@ -245,10 +245,12 @@ nfnl_cthelper_create(const struct nlattr * const tb[],
 
  	ret = nf_conntrack_helper_register(helper);
  	if (ret < 0)
-		goto err;
+		goto err2;
 
  	return 0;
-err:
+err2:
+	kfree(helper->expect_policy);
+err1:
  	kfree(helper);
  	return ret;
  }
@@ -676,6 +678,8 @@ static int nfnl_cthelper_del(struct net *net, struct sock *nfnl,
 
  			found = true;
  			nf_conntrack_helper_unregister(cur);
+			kfree(cur->expect_policy);
+			kfree(cur);
  		}
  	}
  	/* Make sure we return success if we flush and there is no helpers */
@@ -739,6 +743,8 @@ static void __exit nfnl_cthelper_exit(void)
  				continue;
 
  			nf_conntrack_helper_unregister(cur);
+			kfree(cur->expect_policy);
+			kfree(cur);
  		}
  	}
  }