rtnetlink: fix initial rtnl pushdown fallout
Subsystems (3)
cover rtnetlink: fix initial rtnl pushdown fallout
This series fixes various bugs and splats reported since the allow-handler-to-run-with-no-rtnl series went in.
Last patch adds a script that can be used to add further tests in case more bugs are reported.
In case you prefer reverting the original series instead of fixing fallout I can resend this patch on its own.
PATCH 1/6 rtnetlink: use rcu_dereference_raw to silence rcu splat
Ido reports a rcu splat in __rtnl_register.
The splat is correct; as rtnl_register doesn't grab any logs and doesn't use rcu locks either. It has always been like this. handler families are not registered in parallel so there are no races wrt. the kmalloc ordering.
The only reason to use rcu_dereference in the first place was to avoid sparse from complaining about this.
Thus this switches to _raw() to not have rcu checks here.
The alternative is to add rtnl locking to register/unregister, however, I don't see a compelling reason to do so as this has been lockless for the past twenty years or so.
--- net/core/rtnetlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/rtnetlink.c +1 -1
@@ -172,7 +172,7 @@ int __rtnl_register(int protocol, int msgtype,BUG_ON (protocol <0 || protocol >RTNL_FAMILY_MAX ); msgindex =rtm_msgindex (msgtype); - tab =rcu_dereference (rtnl_msg_handlers[protocol]); + tab =rcu_dereference_raw (rtnl_msg_handlers[protocol]);if (tab ==NULL ) { tab =kcalloc (RTM_NR_MSGTYPES ,sizeof (*tab),GFP_KERNEL );if (tab ==NULL )
Signature · 1 line
2.13.0
PATCH 2/6 rtnetlink: do not use RTM_GETLINK directly
In reply to Florian WestphalUserspace sends RTM_GETLINK type, but the kernel substracts RTM_BASE from this, i.e. 'type' doesn't contain RTM_GETLINK anymore but instead RTM_GETLINK - RTM_BASE.
This caused the calcit callback to not be invoked when it should have been (and vice versa).
While at it, also fix a off-by one when checking family index. vs handler array size.
--- net/core/rtnetlink.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/rtnetlink.c +2 -2
@@ -4167,7 +4167,7 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,if (kind !=2 && !netlink_net_capable (skb,CAP_NET_ADMIN ))return -EPERM ; -if (family >ARRAY_SIZE (rtnl_msg_handlers)) +if (family >=ARRAY_SIZE (rtnl_msg_handlers)) family =PF_UNSPEC ;rcu_read_lock ();
@@ -4196,7 +4196,7 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,refcount_inc (&rtnl_msg_handlers_ref[family]); -if (type ==RTM_GETLINK ) +if (type ==RTM_GETLINK -RTM_BASE ) min_dump_alloc =rtnl_calcit (skb, nlh);rcu_read_unlock ();
Signature · 1 line
2.13.0
PATCH 3/6 rtnetlink: switch rtnl_link_get_slave_info_data_size to rcu
In reply to Florian WestphalDavid Ahern reports following splat:
RTNL: assertion failed at net/core/dev.c (5717)
netdev_master_upper_dev_get+0x5f/0x70 if_nlmsg_size+0x158/0x240 rtnl_calcit.isra.26+0xa3/0xf0
rtnl_link_get_slave_info_data_size currently assumes RTNL protection, but there appears to be no hard requirement for this, so use rcu instead.
At the time of this writing, there are three 'get_slave_size' callbacks (now invoked under rcu): bond_get_slave_size, vrf_get_slave_size and br_port_get_slave_size, all return constant only (i.e. they don't sleep).
--- net/core/rtnetlink.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/core/rtnetlink.c +12 -4
@@ -402,16 +402,24 @@ static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) {struct net_device *master_dev;const struct rtnl_link_ops *ops; +size_t size =0 ; - master_dev =netdev_master_upper_dev_get ((struct net_device *) dev); +rcu_read_lock (); + + master_dev =netdev_master_upper_dev_get_rcu ((struct net_device *)dev);if (!master_dev) -return 0 ; + goto out; + ops = master_dev->rtnl_link_ops;if (!ops || !ops->get_slave_size) -return 0 ; + goto out;/* IFLA_INFO_SLAVE_DATA + nested data */ -return nla_total_size (sizeof (struct nlattr )) + + size =nla_total_size (sizeof (struct nlattr )) + ops->get_slave_size (master_dev, dev); + +out: +rcu_read_unlock (); +return size; }static size_t rtnl_link_get_size (const struct net_device *dev)
Signature · 1 line
2.13.0
PATCH 4/6 rtnetlink: init handler refcounts to 1
In reply to Florian WestphalIf using CONFIG_REFCOUNT_FULL=y we get following splat:
refcount_t: increment on 0; use-after-free.
WARNING: CPU: 0 PID: 304 at lib/refcount.c:152 refcount_inc+0x47/0x50 Call Trace: rtnetlink_rcv_msg+0x191/0x260 ...
This warning is harmless (0 is "no callback running", not "memory was freed").
Use '1' as the new 'no handler is running' base instead of 0 to avoid this.
--- net/core/rtnetlink.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/core/rtnetlink.c +6 -1
@@ -262,7 +262,7 @@ void rtnl_unregister_all(int protocol)synchronize_net (); -while (refcount_read (&rtnl_msg_handlers_ref[protocol]) >0 ) +while (refcount_read (&rtnl_msg_handlers_ref[protocol]) >1 )schedule ();kfree (handlers); }
@@ -4324,6 +4324,11 @@ static struct pernet_operations rtnetlink_net_ops = {void __init rtnetlink_init (void ) { +int i; + +for (i =0 ; i <ARRAY_SIZE (rtnl_msg_handlers_ref); i++) +refcount_set (&rtnl_msg_handlers_ref[i],1 ); +if (register_pernet_subsys (&rtnetlink_net_ops))panic ("rtnetlink_init: cannot initialize rtnetlink\n" );
Signature · 1 line
2.13.0
PATCH 5/6 rtnetlink: fallback to UNSPEC if current family has no doit callback
In reply to Florian WestphalWe need to use PF_UNSPEC in case the requested family has no doit callback, otherwise this now fails with EOPNOTSUPP instead of running the unspec doit callback, as before.
--- net/core/rtnetlink.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/rtnetlink.c +6 -0
@@ -4221,6 +4221,12 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,return err; } + doit =READ_ONCE (handlers[type].doit); +if (!doit) { + family =PF_UNSPEC ; + handlers =rcu_dereference (rtnl_msg_handlers[family]); + } + flags =READ_ONCE (handlers[type].flags);if (flags &RTNL_FLAG_DOIT_UNLOCKED ) {refcount_inc (&rtnl_msg_handlers_ref[family]);
Signature · 1 line
2.13.0
PATCH 6/6 selftests: add rtnetlink test script
In reply to Florian Westphaladd a simple script to exercise some rtnetlink call paths, so KASAN, lockdep etc. can yell at developer before patches are sent upstream.
This can be extended to also cover bond, team, vrf and the like.
---
This test crashes the kernel, fix is already queued here:
https://patchwork.ozlabs.org/patch/800144/
tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/rtnetlink.sh | 199 +++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/net/rtnetlink.sh
tools/testing/selftests/net/Makefile +1 -1
@@ -3,7 +3,7 @@ CFLAGS = -Wall -Wl,--no-as-needed -O2 -g CFLAGS += -I../../../../usr/include/ -TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh +TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh rtnetlink.sh TEST_GEN_FILES = socket TEST_GEN_FILES += psock_fanout psock_tpacket TEST_GEN_FILES += reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa
tools/testing/selftests/net/rtnetlink.sh new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.13.0
Re: [PATCH net-next 1/6] rtnetlink: use rcu_dereference_raw to silence rcu splat
In reply to Florian WestphalQuoted 11 lines · Florian Westphal
Ido reports a rcu splat in
__rtnl_register.
The splat is correct; asrtnl_registerdoesn't grab any logs and doesn't use rcu locks either. It has always been like this. handler families are not registered in parallel so there are no races wrt. the kmalloc ordering.The only reason to use
rcu_dereferencein the first place was to avoid sparse from complaining about this.Thus this switches to
_raw()to not have rcu checks here.
Quoted 7 lines
The alternative is to add rtnl locking to register/unregister, however, I don't see a compelling reason to do so as this has been lockless for the past twenty years or so.
Fixes 6853dd4881 ("rtnetlink: protect handler table with rcu")Reported-by Ido Schimmel <[email protected]>Signed-off-by Florian Westphal <[email protected]>
Thanks!
Re: [PATCH net-next 3/6] rtnetlink: switch rtnl_link_get_slave_info_data_size to rcu
In reply to Florian WestphalQuoted 13 lines · Florian Westphal
David Ahern reports following splat:
RTNL: assertion failed atnet/core/dev.c(5717)netdev_master_upper_dev_get+0x5f/0x70 if_nlmsg_size+0x158/0x240 rtnl_calcit.isra.26+0xa3/0xf0
rtnl_link_get_slave_info_data_sizecurrently assumes RTNL protection, but there appears to be no hard requirement for this, so use rcu instead.At the time of this writing, there are three '
get_slave_size' callbacks (now invoked under rcu):bond_get_slave_size,vrf_get_slave_sizeandbr_port_get_slave_size, all return constant only (i.e. they don't sleep).
Quoted 6 lines
Fixes 6853dd488119 ("rtnetlink: protect handler table with rcu")Reported-by David Ahern <[email protected]>Signed-off-by Florian Westphal <[email protected]>--- net/core/rtnetlink.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
David Miller
maintainerFrom: Florian Westphal <[email protected]>
Date: Thu, 10 Aug 2017 16:52:56 +0200
Quoted 7 lines
This series fixes various bugs and splats reported since the allow-handler-to-run-with-no-rtnl series went in.
Last patch adds a script that can be used to add further tests in case more bugs are reported.
In case you prefer reverting the original series instead of fixing fallout I can resend this patch on its own.
Series applied, thanks Florian.