Skip to contentSkip to conversation

rtnetlink: Fix problem with buffer allocation

PATCH Applied Greg Rose 3 participants 6 messages Feb 21, 2012 – Mar 5, 2012

Subsystems (2)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Greg Rose

author
#0 lore raw

PATCH v2 1/1 rtnetlink: Fix problem with buffer allocation

Implement a new netlink attribute type IFLA_EXT_MASK. The mask is a 32 bit value that can be used to indicate to the kernel that certain extended ifinfo values are requested by the user application. At this time the only mask value defined is RTEXT_FILTER_VF to indicate that the user wants the ifinfo dump to send information about the VFs belonging to the interface.

This patch fixes a bug in which certain applications do not have large enough buffers to accommodate the extra information returned by the kernel with large numbers of SR-IOV virtual functions. Those applications will not send the new netlink attribute with the interface info dump request netlink messages so they will not get unexpectedly large request buffers returned by the kernel.

Modifies the rtnl_calcit function to traverse the list of net devices and compute the minimum buffer size that can hold the info dumps of all matching devices based upon the filter passed in via the new netlink attribute filter mask. If no filter mask is sent then the buffer allocation defaults to NLMSG_GOODSIZE.

With this change it is possible to add yet to be defined netlink attributes to the dump request which should make it fairly extensible in the future.

---

 include/linux/if_link.h   |    1 +
 include/linux/rtnetlink.h |    3 ++
 include/net/rtnetlink.h   |    2 +
 net/core/rtnetlink.c      |   78 +++++++++++++++++++++++++++++++++------------
 4 files changed, 62 insertions(+), 22 deletions(-)
include/linux/if_link.h +1 -0
index c52d4b5..4b24ff4 100644
@@ -137,6 +137,7 @@ enum {
 	IFLA_AF_SPEC,
 	IFLA_GROUP,		/* Group the device belongs to */
 	IFLA_NET_NS_FD,
+	IFLA_EXT_MASK,		/* Extended info mask, VFs, etc */
 	__IFLA_MAX
 };
 
include/linux/rtnetlink.h +3 -0
index 8e872ea..577592e 100644
@@ -602,6 +602,9 @@ struct tcamsg {
 #define TCA_ACT_TAB 1 /* attr type must be >=1 */	
 #define TCAA_MAX 1
 
+/* New extended info filters for IFLA_EXT_MASK */
+#define RTEXT_FILTER_VF		(1 << 0)
+
 /* End of information exported to user level */
 
 #ifdef __KERNEL__
include/net/rtnetlink.h +1 -1
index 678f1ff..3702939 100644
@@ -6,7 +6,7 @@
 
 typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *, void *);
 typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
-typedef u16 (*rtnl_calcit_func)(struct sk_buff *);
+typedef u16 (*rtnl_calcit_func)(struct sk_buff *, struct nlmsghdr *);
 
 extern int	__rtnl_register(int protocol, int msgtype,
 				rtnl_doit_func, rtnl_dumpit_func,
net/core/rtnetlink.c +57 -21

191 lines not on this page · raw message

maintainer Applied
#1 lore raw

From: Greg Rose <[email protected]>
Date: Tue, 21 Feb 2012 13:51:28 -0800

Quoted 20 lines

Implement a new netlink attribute type IFLA_EXT_MASK. The mask is a 32 bit value that can be used to indicate to the kernel that certain extended ifinfo values are requested by the user application. At this time the only mask value defined is RTEXT_FILTER_VF to indicate that the user wants the ifinfo dump to send information about the VFs belonging to the interface.

This patch fixes a bug in which certain applications do not have large enough buffers to accommodate the extra information returned by the kernel with large numbers of SR-IOV virtual functions. Those applications will not send the new netlink attribute with the interface info dump request netlink messages so they will not get unexpectedly large request buffers returned by the kernel.

Modifies the rtnl_calcit function to traverse the list of net devices and compute the minimum buffer size that can hold the info dumps of all matching devices based upon the filter passed in via the new netlink attribute filter mask. If no filter mask is sent then the buffer allocation defaults to NLMSG_GOODSIZE.

Quoted 6 lines

With this change it is possible to add yet to be defined netlink attributes to the dump request which should make it fairly extensible in the future.

Applied, with the minor change suggested by Eric in that we can use the non-RCU list traversal in the calcit function because we hold the RTNL always when this is invoked.

Thanks!

#2 lore raw
Quoted 34 lines

-----Original Message-----
From: David Miller [mailto:[email protected]]
Sent: Tuesday, February 21, 2012 1:58 PM
To: Rose, Gregory V
Cc: [email protected]
Subject: Re: [PATCH V2] rtnetlink: Fix problem with buffer allocation

From: Greg Rose <[email protected]>
Date: Tue, 21 Feb 2012 13:51:28 -0800

Quoted 24 lines

Implement a new netlink attribute type IFLA_EXT_MASK. The mask is a 32 bit value that can be used to indicate to the kernel that certain extended ifinfo values are requested by the user application. At this time the only mask value defined is RTEXT_FILTER_VF to indicate that the user wants the ifinfo dump to send information about the VFs belonging to the interface.

This patch fixes a bug in which certain applications do not have large enough buffers to accommodate the extra information returned by the kernel with large numbers of SR-IOV virtual functions. Those applications will not send the new netlink attribute with the interface info dump request netlink messages so they will not get unexpectedly large request buffers returned by the kernel.

Modifies the rtnl_calcit function to traverse the list of net devices and compute the minimum buffer size that can hold the info dumps of all matching devices based upon the filter passed in via the new netlink attribute filter mask. If no filter mask is sent then the buffer allocation defaults to NLMSG_GOODSIZE.

With this change it is possible to add yet to be defined netlink attributes to the dump request which should make it fairly extensible in the future.

Quoted 6 lines
Quoted 2 lines

Applied, with the minor change suggested by Eric in that we can use the non-RCU list traversal in the calcit function because we hold the RTNL always when this is invoked.

Oh good, that saves me a bit of work.

Quoted 2 lines

Thanks!

Thanks for working with me on this. I learned a lot about how netlink actually works during the process though at the obvious cost of taking longer to get it done than someone who is more familiar with the net core code than I am. Valuable experience for me and hopefully it will set us up well for future development.

- Greg

#3 lore raw

PATCH 1/1 rtnetlink: fix rtnl_calcit() and rtnl_dump_ifinfo()

In reply to David Miller

nlmsg_parse() might return an error, so test its return value before potential random memory accesses.

Errors introduced in commit 115c9b81928 (rtnetlink: Fix problem with buffer allocation)

---
 net/core/rtnetlink.c |   18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
net/core/rtnetlink.c +10 -8
index 606a6e8..f965dce 100644
@@ -1060,11 +1060,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 	rcu_read_lock();
 	cb->seq = net->dev_base_seq;
 
-	nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
-		    ifla_policy);
+	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
+			ifla_policy) >= 0) {
 
-	if (tb[IFLA_EXT_MASK])
-		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+		if (tb[IFLA_EXT_MASK])
+			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	}
 
 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
 		idx = 0;
@@ -1900,10 +1901,11 @@ static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
 	u32 ext_filter_mask = 0;
 	u16 min_ifinfo_dump_size = 0;
 
-	nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX, ifla_policy);
-
-	if (tb[IFLA_EXT_MASK])
-		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
+			ifla_policy) >= 0) {
+		if (tb[IFLA_EXT_MASK])
+			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	}
 
 	if (!ext_filter_mask)
 		return NLMSG_GOODSIZE;
maintainer Applied
#4 lore raw

Re: [PATCH] rtnetlink: fix rtnl_calcit() and rtnl_dump_ifinfo()

From: Eric Dumazet <[email protected]>
Date: Sun, 04 Mar 2012 14:32:10 -0800

Quoted 8 lines

nlmsg_parse() might return an error, so test its return value before potential random memory accesses.

Errors introduced in commit 115c9b81928 (rtnetlink: Fix problem with buffer allocation)

Applied, thanks a lot Eric.

#5 lore raw

RE: [PATCH] rtnetlink: fix rtnl_calcit() and rtnl_dump_ifinfo()

In reply to Eric Dumazet
Quoted 58 lines

-----Original Message-----
From: Eric Dumazet [mailto:[email protected]]
Sent: Sunday, March 04, 2012 2:32 PM
To: David Miller
Cc: Rose, Gregory V; [email protected]
Subject: [PATCH] rtnetlink: fix rtnl_calcit() and rtnl_dump_ifinfo()

nlmsg_parse() might return an error, so test its return value before potential random memory accesses.

Errors introduced in commit 115c9b81928 (rtnetlink: Fix problem with buffer allocation)

---
 net/core/rtnetlink.c |   18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
net/core/rtnetlink.c +5 -4
index 606a6e8..f965dce 100644
@@ -1060,11 +1060,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb,
 struct netlink_callback *cb)
 	rcu_read_lock();
 	cb->seq = net->dev_base_seq;
 
-	nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
-		    ifla_policy);
+	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
+			ifla_policy) >= 0) {
 
-	if (tb[IFLA_EXT_MASK])
-		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+		if (tb[IFLA_EXT_MASK])
+			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	}
 
 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
net/core/rtnetlink.c +5 -4
 		idx = 0;
@@ -1900,10 +1901,11 @@ static u16 rtnl_calcit(struct sk_buff *skb, struct
 nlmsghdr *nlh)
 	u32 ext_filter_mask = 0;
 	u16 min_ifinfo_dump_size = 0;
 
-	nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
 ifla_policy);
-
-	if (tb[IFLA_EXT_MASK])
-		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
+			ifla_policy) >= 0) {
+		if (tb[IFLA_EXT_MASK])
+			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
+	}
 

net/core/rtnetlink.c static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) · line 1911

Quoted 2 lines
net/core/rtnetlink.c +0 -0
 	if (!ext_filter_mask)
 		return NLMSG_GOODSIZE;

I guess I misunderstood some of the internal workings of nlmsg_parse. Good catch.

Thanks,

- Greg