Skip to contentSkip to conversation

Nfnetlink and ipset fixes

PATCH Applied Tomasz Bursztyka 6 participants 12 messages Jun 28, 2012 – Jul 4, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Nfnetlink and ipset fixes

Hi,

The 2 first patches fixes the same issue, but in 2 different place.

Patch 1:

So here is how the issue can be reprocuced:
Create a netlink message, targeting ipset subsystem. And send a command on id 0 (IPSET_CMD_NONE):
This will lead to a crash on line 188 of nfnetlink.c since nc->call is NULL (so you get a NULL dereference).

Or Patch 2:

Same issue.
It fixes ipset to handle the IPSET_CMD_NONE, return -EOPNOTSUPP.

Patch 3:

While doing patch 1 I figured out rcu_read_unlock() is not called in case nla_parse() error, so it fixes it.

Br,

Tomasz Bursztyka (3):
nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg
ipset: Handle properly an IPSET_CMD_NONE
nfnetlink: Unlock a previously locked rcu_read in nfnetlink_rcv_msg

 net/netfilter/ipset/ip_set_core.c |   12 ++++++++++++
 net/netfilter/nfnetlink.c         |    8 ++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.8.6
#1 lore raw

PATCH 1/3 nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg

nfnetlink_rcv_msg() might call a NULL callback which will cause NULL pointer dereference.

---
 net/netfilter/nfnetlink.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
net/netfilter/nfnetlink.c +3 -1
index 3e797d1..4acdd76 100644
@@ -184,9 +184,11 @@ replay:
 					lockdep_is_held(&nfnl_mutex)) != ss ||
 			    nfnetlink_find_client(type, ss) != nc)
 				err = -EAGAIN;
-			else
+			else if (nc->call)
 				err = nc->call(net->nfnl, skb, nlh,
 						   (const struct nlattr **)cda);
+			else
+				err = -EINVAL;
 			nfnl_unlock();
 		}
 		if (err == -EAGAIN)
Signature · 1 line
1.7.8.6
#2 lore raw

Re: [PATCH 1/3] nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg

Quoted 13 lines · Tomasz Bursztyka

nfnetlink_rcv_msg() might call a NULL callback which will cause NULL pointer dereference.

---
 net/netfilter/nfnetlink.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
net/netfilter/nfnetlink.c +0 -0
index 3e797d1..4acdd76 100644

net/netfilter/nfnetlink.c replay: · line 191

Quoted 9 lines
net/netfilter/nfnetlink.c +3 -1
@@ -184,9 +184,11 @@ replay:
 					lockdep_is_held(&nfnl_mutex)) != ss ||
 			    nfnetlink_find_client(type, ss) != nc)
 				err = -EAGAIN;
-			else
+			else if (nc->call)
 				err = nc->call(net->nfnl, skb, nlh,
 						   (const struct nlattr **)cda);
+			else
+				err = -EINVAL;

Hm, I think this is redundant. We got the ipset problem fixed with 2/3. I think we can add some BUG_ON instead as all nc->call needs to be defined. Otherwise, spot a bug message so it is fixed.

#3 lore raw

Re: [PATCH 1/3] nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg

Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Tomasz Bursztyka

nfnetlink_rcv_msg() might call a NULL callback which will cause NULL pointer dereference.

Quoted 16 lines

net/netfilter/nfnetlink.c replay: · line 191

Quoted 12 lines

---

net/netfilter/nfnetlink.c +3 -1
@@ -184,9 +184,11 @@ replay:
 					lockdep_is_held(&nfnl_mutex)) != ss ||
 			    nfnetlink_find_client(type, ss) != nc)
 				err = -EAGAIN;
-			else
+			else if (nc->call)
 				err = nc->call(net->nfnl, skb, nlh,
 						   (const struct nlattr **)cda);
+			else
+				err = -EINVAL;

Hm, I think this is redundant. We got the ipset problem fixed with 2/3. I think we can add some BUG_ON instead as all nc->call needs to be defined. Otherwise, spot a bug message so it is fixed.

Seriously, we have run into NULL dereferences at this place so often that it should not be discarded as a PEBKAC issue. Yes, it is the programmer's responsibility to fill in .call, but error handling is anytime better than driving one's machine into the pit stop due to a NULL deref or BUG_ON.

maintainer
#4 lore raw

Re: [PATCH 1/3] nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg

Quoted 18 lines · Jan Engelhardt
Quoted 16 lines · Pablo Neira Ayuso

net/netfilter/nfnetlink.c replay: · line 191

Quoted 11 lines · Tomasz Bursztyka

---

net/netfilter/nfnetlink.c +3 -1
@@ -184,9 +184,11 @@ replay:
 					lockdep_is_held(&nfnl_mutex)) != ss ||
 			    nfnetlink_find_client(type, ss) != nc)
 				err = -EAGAIN;
-			else
+			else if (nc->call)
 				err = nc->call(net->nfnl, skb, nlh,
 						   (const struct nlattr **)cda);
+			else
+				err = -EINVAL;

Hm, I think this is redundant. We got the ipset problem fixed with 2/3. I think we can add some BUG_ON instead as all nc->call needs to be defined. Otherwise, spot a bug message so it is fixed.

Quoted 5 lines

Seriously, we have run into NULL dereferences at this place so often that it should not be discarded as a PEBKAC issue. Yes, it is the programmer's responsibility to fill in .call, but error handling is anytime better than driving one's machine into the pit stop due to a NULL deref or BUG_ON.

Agreed. I think this should be WARN_ON_ONCE + ret EINVAL. We also do this with various places that e.g. fill netlink messages so that adding new attributes without bumping allocation sizes is reported without crashes. Since we've started to move the performance-critical nfnetlink stuff over to ->call_rcu the extra NULL test should not harm fastpath either.

#5 lore raw

Re: [PATCH 1/3] nfnetlink: Check callbacks before using those in nfnetlink_rcv_msg

Quoted 25 lines · Florian Westphal
Quoted 18 lines · Jan Engelhardt
Quoted 16 lines · Pablo Neira Ayuso

net/netfilter/nfnetlink.c replay: · line 191

Quoted 11 lines · Tomasz Bursztyka

---

net/netfilter/nfnetlink.c +3 -1
@@ -184,9 +184,11 @@ replay:
 					lockdep_is_held(&nfnl_mutex)) != ss ||
 			    nfnetlink_find_client(type, ss) != nc)
 				err = -EAGAIN;
-			else
+			else if (nc->call)
 				err = nc->call(net->nfnl, skb, nlh,
 						   (const struct nlattr **)cda);
+			else
+				err = -EINVAL;

Hm, I think this is redundant. We got the ipset problem fixed with 2/3. I think we can add some BUG_ON instead as all nc->call needs to be defined. Otherwise, spot a bug message so it is fixed.

Quoted 5 lines

Seriously, we have run into NULL dereferences at this place so often that it should not be discarded as a PEBKAC issue. Yes, it is the programmer's responsibility to fill in .call, but error handling is anytime better than driving one's machine into the pit stop due to a NULL deref or BUG_ON.

Quoted 6 lines

Agreed. I think this should be WARN_ON_ONCE + ret EINVAL. We also do this with various places that e.g. fill netlink messages so that adding new attributes without bumping allocation sizes is reported without crashes. Since we've started to move the performance-critical nfnetlink stuff over to ->call_rcu the extra NULL test should not harm fastpath either.

OK, fair enough. I've enqueued this to nf-next.

#6 lore raw

PATCH 2/3 ipset: Handle properly an IPSET_CMD_NONE

In reply to Tomasz Bursztyka
---
 net/netfilter/ipset/ip_set_core.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
net/netfilter/ipset/ip_set_core.c +12 -0
index 819c342..9730882 100644
@@ -640,6 +640,14 @@ find_free_id(const char *name, ip_set_id_t *index, struct ip_set **set)
 }
 
 static int
+ip_set_none(struct sock *ctnl, struct sk_buff *skb,
+	    const struct nlmsghdr *nlh,
+	    const struct nlattr * const attr[])
+{
+	return -EOPNOTSUPP;
+}
+
+static int
 ip_set_create(struct sock *ctnl, struct sk_buff *skb,
 	      const struct nlmsghdr *nlh,
 	      const struct nlattr * const attr[])
@@ -1539,6 +1547,10 @@ nlmsg_failure:
 }
 
 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
+	[IPSET_CMD_NONE]	= {
+		.call		= ip_set_none,
+		.attr_count	= IPSET_ATTR_CMD_MAX,
+	},
 	[IPSET_CMD_CREATE]	= {
 		.call		= ip_set_create,
 		.attr_count	= IPSET_ATTR_CMD_MAX,
Signature · 1 line
1.7.8.6
Acked-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] ipset: Handle properly an IPSET_CMD_NONE

Quoted 1 line · Tomasz Bursztyka

Never occured to me to send IPSET_CMD_NONE :-).

Best regards,
Jozsef

Quoted 42 lines
---
 net/netfilter/ipset/ip_set_core.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
net/netfilter/ipset/ip_set_core.c +12 -0
index 819c342..9730882 100644
@@ -640,6 +640,14 @@ find_free_id(const char *name, ip_set_id_t *index, struct ip_set **set)
 }
 
 static int
+ip_set_none(struct sock *ctnl, struct sk_buff *skb,
+	    const struct nlmsghdr *nlh,
+	    const struct nlattr * const attr[])
+{
+	return -EOPNOTSUPP;
+}
+
+static int
 ip_set_create(struct sock *ctnl, struct sk_buff *skb,
 	      const struct nlmsghdr *nlh,
 	      const struct nlattr * const attr[])
@@ -1539,6 +1547,10 @@ nlmsg_failure:
 }
 
 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
+	[IPSET_CMD_NONE]	= {
+		.call		= ip_set_none,
+		.attr_count	= IPSET_ATTR_CMD_MAX,
+	},
 	[IPSET_CMD_CREATE]	= {
 		.call		= ip_set_create,
 		.attr_count	= IPSET_ATTR_CMD_MAX,
Signature · 6 lines
1.7.8.6

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics, Hungarian Academy of Sciences

          H-1525 Budapest 114, POB. 49, Hungary
#9 lore raw

PATCH 3/3 nfnetlink: Unlock a previously locked rcu_read in nfnetlink_rcv_msg

In reply to Tomasz Bursztyka
---
 net/netfilter/nfnetlink.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
net/netfilter/nfnetlink.c +3 -1
index 4acdd76..a6366ae 100644
@@ -169,8 +169,10 @@ replay:
 
 		err = nla_parse(cda, ss->cb[cb_id].attr_count,
 				attr, attrlen, ss->cb[cb_id].policy);
-		if (err < 0)
+		if (err < 0) {
+			rcu_read_unlock();
 			return err;
+		}
 
 		if (nc->call_rcu) {
 			err = nc->call_rcu(net->nfnl, skb, nlh,
Signature · 1 line
1.7.8.6
Acked-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] nfnetlink: Unlock a previously locked rcu_read in nfnetlink_rcv_msg

Quoted 11 lines · Tomasz Bursztyka
---
 net/netfilter/nfnetlink.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
net/netfilter/nfnetlink.c +0 -0
index 4acdd76..a6366ae 100644
@@ -169,8 +169,10 @@ replay:
 

net/netfilter/nfnetlink.c replay: · line 178

Quoted 10 lines
net/netfilter/nfnetlink.c +3 -1
 		err = nla_parse(cda, ss->cb[cb_id].attr_count,
 				attr, attrlen, ss->cb[cb_id].policy);
-		if (err < 0)
+		if (err < 0) {
+			rcu_read_unlock();
 			return err;
+		}
 
 		if (nc->call_rcu) {
 			err = nc->call_rcu(net->nfnl, skb, nlh,

Nice catch, thanks.

Please someone add in changelog to ease stable team work :

Bug added in commit 6b75e3e8d664a9a (netfilter: nfnetlink: add RCU in nfnetlink_rcv_msg())

maintainer Applied
#11 lore raw

Re: [PATCH 3/3] nfnetlink: Unlock a previously locked rcu_read in nfnetlink_rcv_msg

Quoted 25 lines · Eric Dumazet
Quoted 11 lines · Tomasz Bursztyka
---
 net/netfilter/nfnetlink.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
net/netfilter/nfnetlink.c +0 -0
index 4acdd76..a6366ae 100644
@@ -169,8 +169,10 @@ replay:
 

net/netfilter/nfnetlink.c replay: · line 178

Quoted 10 lines
net/netfilter/nfnetlink.c +3 -1
 		err = nla_parse(cda, ss->cb[cb_id].attr_count,
 				attr, attrlen, ss->cb[cb_id].policy);
-		if (err < 0)
+		if (err < 0) {
+			rcu_read_unlock();
 			return err;
+		}
 
 		if (nc->call_rcu) {
 			err = nc->call_rcu(net->nfnl, skb, nlh,

Nice catch, thanks.

Quoted 6 lines

Please someone add in changelog to ease stable team work :

Bug added in commit 6b75e3e8d664a9a (netfilter: nfnetlink: add RCU in nfnetlink_rcv_msg())

Applied, thanks.