l2tp: fixes for l2tp_ip and l2tp_ip6 socket handling
Subsystems (3)
cover l2tp: fixes for l2tp_ip and l2tp_ip6 socket handling
This series addresses problems found while working on commit 32c231164b76 ("l2tp: fix racy SOCK_ZAPPED flag check in l2tp_ip{,6}_bind()").
The first three patches fix races in socket's connect, recv and bind operations. The last two ones fix scenarios where l2tp fails to correctly lookup its userspace sockets.
Apart from the last patch, which is l2tp_ip6 specific, every patch fixes the same problem in the L2TP IPv4 and IPv6 code.
Guillaume Nault (5):
l2tp: lock socket before checking flags in connect()
l2tp: hold socket before dropping lock in l2tp_ip{,6}_recv()
l2tp: fix racy socket lookup in l2tp_ip and l2tp_ip6 bind()
l2tp: fix lookup for sockets not bound to a device in l2tp_ip
l2tp: fix address test in __l2tp_ip6_bind_lookup()
include/net/ipv6.h | 2 ++ net/ipv6/datagram.c | 4 ++- net/l2tp/l2tp_ip.c | 61 +++++++++++++++++++++-------------------- net/l2tp/l2tp_ip6.c | 79 ++++++++++++++++++++++++++++------------------------- 4 files changed, 79 insertions(+), 67 deletions(-)
Signature · 1 line
2.10.2
PATCH 2/5 l2tp: hold socket before dropping lock in l2tp_ip{,6}_recv()
Socket must be held while under the protection of the l2tp lock; there is no guarantee that sk remains valid after the read_unlock_bh() call.
Same issue for l2tp_ip and l2tp_ip6.
--- net/l2tp/l2tp_ip.c | 11 ++++++----- net/l2tp/l2tp_ip6.c | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-)
net/l2tp/l2tp_ip.c +6 -5
@@ -183,14 +183,15 @@ static int l2tp_ip_recv(struct sk_buff *skb)read_lock_bh (&l2tp_ip_lock); sk =__l2tp_ip_bind_lookup (net, iph->daddr,0 , tunnel_id); +if (!sk) { +read_unlock_bh (&l2tp_ip_lock); + goto discard; + } + +sock_hold (sk);read_unlock_bh (&l2tp_ip_lock); } -if (sk ==NULL ) - goto discard; - -sock_hold (sk); -if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_put;
net/l2tp/l2tp_ip6.c +6 -5
@@ -198,14 +198,15 @@ static int l2tp_ip6_recv(struct sk_buff *skb)read_lock_bh (&l2tp_ip6_lock); sk =__l2tp_ip6_bind_lookup (net, &iph->daddr,0 , tunnel_id); +if (!sk) { +read_unlock_bh (&l2tp_ip6_lock); + goto discard; + } + +sock_hold (sk);read_unlock_bh (&l2tp_ip6_lock); } -if (sk ==NULL ) - goto discard; - -sock_hold (sk); -if (!xfrm6_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_put;
Signature · 1 line
2.10.2
PATCH 1/5 l2tp: lock socket before checking flags in connect()
In reply to Guillaume NaultSocket flags aren't updated atomically, so the socket must be locked while reading the SOCK_ZAPPED flag.
This issue exists for both l2tp_ip and l2tp_ip6. For IPv6, this patch also brings error handling for __ip6_datagram_connect() failures.
--- include/net/ipv6.h | 2 ++ net/ipv6/datagram.c | 4 +++- net/l2tp/l2tp_ip.c | 19 ++++++++++++------- net/l2tp/l2tp_ip6.c | 16 +++++++++++----- 4 files changed, 28 insertions(+), 13 deletions(-)
include/net/ipv6.h +2 -0
@@ -970,6 +970,8 @@ int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,int compat_ipv6_getsockopt (struct sock *sk,int level,int optname,char __user *optval,int __user *optlen); +int __ip6_datagram_connect (struct sock *sk,struct sockaddr *addr, +int addr_len);int ip6_datagram_connect (struct sock *sk,struct sockaddr *addr,int addr_len);int ip6_datagram_connect_v6_only (struct sock *sk,struct sockaddr *addr,int addr_len);
net/ipv6/datagram.c +3 -1
@@ -139,7 +139,8 @@ void ip6_datagram_release_cb(struct sock *sk) }EXPORT_SYMBOL_GPL (ip6_datagram_release_cb); -static int __ip6_datagram_connect (struct sock *sk,struct sockaddr *uaddr,int addr_len) +int __ip6_datagram_connect (struct sock *sk,struct sockaddr *uaddr, +int addr_len) {struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;struct inet_sock *inet =inet_sk (sk);
@@ -252,6 +253,7 @@ static int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int a out:return err; } +EXPORT_SYMBOL_GPL (__ip6_datagram_connect);int ip6_datagram_connect (struct sock *sk,struct sockaddr *uaddr,int addr_len) {
net/l2tp/l2tp_ip.c +12 -7
@@ -308,21 +308,24 @@ static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_lenstruct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;int rc; -if (sock_flag (sk,SOCK_ZAPPED ))/* Must bind first - autobinding does not work */ -return -EINVAL ; -if (addr_len <sizeof (*lsa))return -EINVAL ;if (ipv4_is_multicast (lsa->l2tp_addr.s_addr))return -EINVAL ; - rc =ip4_datagram_connect (sk, uaddr, addr_len); -if (rc <0 ) -return rc; -lock_sock (sk); +/* Must bind first - autobinding does not work */ +if (sock_flag (sk,SOCK_ZAPPED )) { + rc = -EINVAL ; + goto out_sk; + } + + rc =__ip4_datagram_connect (sk, uaddr, addr_len); +if (rc <0 ) + goto out_sk; +l2tp_ip_sk (sk)->peer_conn_id = lsa->l2tp_conn_id;write_lock_bh (&l2tp_ip_lock);
@@ -330,7 +333,9 @@ static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_lensk_add_bind_node (sk, &l2tp_ip_bind_table);write_unlock_bh (&l2tp_ip_lock); +out_sk:release_sock (sk); +return rc; }
net/l2tp/l2tp_ip6.c +11 -5
@@ -371,9 +371,6 @@ static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,int addr_type;int rc; -if (sock_flag (sk,SOCK_ZAPPED ))/* Must bind first - autobinding does not work */ -return -EINVAL ; -if (addr_len <sizeof (*lsa))return -EINVAL ;
@@ -390,10 +387,18 @@ static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,return -EINVAL ; } - rc =ip6_datagram_connect (sk, uaddr, addr_len); -lock_sock (sk); +/* Must bind first - autobinding does not work */ +if (sock_flag (sk,SOCK_ZAPPED )) { + rc = -EINVAL ; + goto out_sk; + } + + rc =__ip6_datagram_connect (sk, uaddr, addr_len); +if (rc <0 ) + goto out_sk; +l2tp_ip6_sk (sk)->peer_conn_id = lsa->l2tp_conn_id;write_lock_bh (&l2tp_ip6_lock);
@@ -401,6 +406,7 @@ static int l2tp_ip6_connect(struct sock *sk, struct sockaddr *uaddr,sk_add_bind_node (sk, &l2tp_ip6_bind_table);write_unlock_bh (&l2tp_ip6_lock); +out_sk:release_sock (sk);return rc;
Signature · 1 line
2.10.2
PATCH 3/5 l2tp: fix racy socket lookup in l2tp_ip and l2tp_ip6 bind()
In reply to Guillaume NaultIt's not enough to check for sockets bound to same address at the beginning of l2tp_ip{,6}_bind(): even if no socket is found at that time, a socket with the same address could be bound before we take the l2tp lock again.
This patch moves the lookup right before inserting the new socket, so that no change can ever happen to the list between address lookup and socket insertion.
Care is taken to avoid side effects on the socket in case of failure. That is, modifications of the socket are done after the lookup, when binding is guaranteed to succeed, and before releasing the l2tp lock, so that concurrent lookups will always see fully initialised sockets.
For l2tp_ip6, the lookup is now always done with the correct bound device. Before this patch, when binding to a link-local address, the lookup was done with the original sk->sk_bound_dev_if, which was later overwritten with addr->l2tp_scope_id. Lookup is now performed with the final sk->sk_bound_dev_if value.
Finally, the (addr_len >= sizeof(struct sockaddr_in6)) check has been
dropped: addr is a sockaddr_l2tpip6 not sockaddr_in6 and addr_len has already been checked at this point (this part of the code seems to have been copy-pasted from net/ipv6/raw.c).
--- net/l2tp/l2tp_ip.c | 25 ++++++++++--------------- net/l2tp/l2tp_ip6.c | 43 ++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 38 deletions(-)
net/l2tp/l2tp_ip.c +10 -15
@@ -257,14 +257,6 @@ static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)if (addr->l2tp_family !=AF_INET )return -EINVAL ; - ret = -EADDRINUSE ; -read_lock_bh (&l2tp_ip_lock); -if (__l2tp_ip_bind_lookup (net, addr->l2tp_addr.s_addr, - sk->sk_bound_dev_if, addr->l2tp_conn_id)) - goto out_in_use; - -read_unlock_bh (&l2tp_ip_lock); -lock_sock (sk);if (!sock_flag (sk,SOCK_ZAPPED )) goto out;
@@ -282,14 +274,22 @@ static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;if (chk_addr_ret ==RTN_MULTICAST || chk_addr_ret ==RTN_BROADCAST ) inet->inet_saddr =0 ;/* Use device */ -sk_dst_reset (sk); +write_lock_bh (&l2tp_ip_lock); +if (__l2tp_ip_bind_lookup (net, addr->l2tp_addr.s_addr, + sk->sk_bound_dev_if, addr->l2tp_conn_id)) { +write_unlock_bh (&l2tp_ip_lock); + ret = -EADDRINUSE ; + goto out; + } + +sk_dst_reset (sk);l2tp_ip_sk (sk)->conn_id = addr->l2tp_conn_id; -write_lock_bh (&l2tp_ip_lock);sk_add_bind_node (sk, &l2tp_ip_bind_table);sk_del_node_init (sk);write_unlock_bh (&l2tp_ip_lock); + ret =0 ;sock_reset_flag (sk,SOCK_ZAPPED );
@@ -297,11 +297,6 @@ static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)release_sock (sk);return ret; - -out_in_use: -read_unlock_bh (&l2tp_ip_lock); - -return ret; }static int l2tp_ip_connect (struct sock *sk,struct sockaddr *uaddr,int addr_len)
net/l2tp/l2tp_ip6.c +20 -23
90 lines not on this page · raw message
Signature · 1 line
2.10.2
PATCH 4/5 l2tp: fix lookup for sockets not bound to a device in l2tp_ip
In reply to Guillaume NaultWhen looking up an l2tp socket, we must consider a null netdevice id as wild card. There are currently two problems caused by __l2tp_ip_bind_lookup() not considering 'dif' as wild card when set to 0:
* A socket bound to a device (i.e. with sk->sk_bound_dev_if != 0) never receives any packet. Since __l2tp_ip_bind_lookup() is called with dif == 0 in l2tp_ip_recv(), sk->sk_bound_dev_if is always different from 'dif' so the socket doesn't match.
* Two sockets, one bound to a device but not the other, can be bound to the same address. If the first socket binding to the address is the one that is also bound to a device, the second socket can bind to the same address without __l2tp_ip_bind_lookup() noticing the overlap.
To fix this issue, we need to consider that any null device index, be it 'sk->sk_bound_dev_if' or 'dif', matches with any other value. We also need to pass the input device index to __l2tp_ip_bind_lookup() on reception so that sockets bound to a device never receive packets from other devices.
This patch fixes l2tp_ip6 in the same way.
--- net/l2tp/l2tp_ip.c | 6 ++++-- net/l2tp/l2tp_ip6.c | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-)
net/l2tp/l2tp_ip.c +4 -2
@@ -61,7 +61,8 @@ static struct sock *__l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int difif ((l2tp->conn_id == tunnel_id) &&net_eq (sock_net (sk), net) && !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) && - !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)) + (!sk->sk_bound_dev_if || !dif || + sk->sk_bound_dev_if == dif)) goto found; }
@@ -182,7 +183,8 @@ static int l2tp_ip_recv(struct sk_buff *skb)struct iphdr *iph = (struct iphdr *)skb_network_header (skb);read_lock_bh (&l2tp_ip_lock); - sk =__l2tp_ip_bind_lookup (net, iph->daddr,0 , tunnel_id); + sk =__l2tp_ip_bind_lookup (net, iph->daddr,inet_iif (skb), + tunnel_id);if (!sk) {read_unlock_bh (&l2tp_ip_lock); goto discard;
net/l2tp/l2tp_ip6.c +4 -3
@@ -73,7 +73,8 @@ static struct sock *__l2tp_ip6_bind_lookup(struct net *net,if ((l2tp->conn_id == tunnel_id) &&net_eq (sock_net (sk), net) && !(addr &&ipv6_addr_equal (addr, laddr)) && - !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)) + (!sk->sk_bound_dev_if || !dif || + sk->sk_bound_dev_if == dif)) goto found; }
@@ -196,8 +197,8 @@ static int l2tp_ip6_recv(struct sk_buff *skb)struct ipv6hdr *iph =ipv6_hdr (skb);read_lock_bh (&l2tp_ip6_lock); - sk =__l2tp_ip6_bind_lookup (net, &iph->daddr, -0 , tunnel_id); + sk =__l2tp_ip6_bind_lookup (net, &iph->daddr,inet6_iif (skb), + tunnel_id);if (!sk) {read_unlock_bh (&l2tp_ip6_lock); goto discard;
Signature · 1 line
2.10.2
PATCH 5/5 l2tp: fix address test in __l2tp_ip6_bind_lookup()
In reply to Guillaume NaultThe '!(addr && ipv6_addr_equal(addr, laddr))' part of the conditional matches if addr is NULL or if addr != laddr.
But the intend of __l2tp_ip6_bind_lookup() is to find a sockets with the same address, so the ipv6_addr_equal() condition needs to be inverted.
For better clarity and consistency with the rest of the expression, the (!X || X == Y) notation is used instead of !(X && X != Y).
--- net/l2tp/l2tp_ip6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/l2tp/l2tp_ip6.c +1 -1
@@ -72,7 +72,7 @@ static struct sock *__l2tp_ip6_bind_lookup(struct net *net,if ((l2tp->conn_id == tunnel_id) &&net_eq (sock_net (sk), net) && - !(addr &&ipv6_addr_equal (addr, laddr)) && + (!addr ||ipv6_addr_equal (addr, laddr)) && (!sk->sk_bound_dev_if || !dif || sk->sk_bound_dev_if == dif)) goto found;
Signature · 1 line
2.10.2
kbuild test robot
build botRe: [PATCH net 3/5] l2tp: fix racy socket lookup in l2tp_ip and l2tp_ip6 bind()
In reply to Guillaume Naulturl: https://github.com/0day-ci/linux/commits/Guillaume-Nault/l2tp-fixes-for-l2tp_ip-and-l2tp_ip6-socket-handling/20161129-043208 config: x86_64-rhel (attached as .config) compiler: gcc-6 (Debian…
Hi Guillaume,
[auto build test WARNING on net/master]
url: https://github.com/0day-ci/linux/commits/Guillaume-Nault/l2tp-fixes-for-l2tp_ip-and-l2tp_ip6-socket-handling/20161129-043208
config: x86_64-rhel (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
All warnings (new ones prefixed by >>):
net/l2tp/l2tp_ip.c: In function 'l2tp_ip_bind':
>> net/l2tp/l2tp_ip.c:299:9: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
return ret;
^~~vim +/ret +299 net/l2tp/l2tp_ip.c
3fb4e5ea Guillaume Nault 2016-11-28 283 goto out;
3fb4e5ea Guillaume Nault 2016-11-28 284 }
3fb4e5ea Guillaume Nault 2016-11-28 285
3fb4e5ea Guillaume Nault 2016-11-28 286 sk_dst_reset(sk);
0d76751f James Chapman 2010-04-02 287 l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
0d76751f James Chapman 2010-04-02 288
0d76751f James Chapman 2010-04-02 289 sk_add_bind_node(sk, &l2tp_ip_bind_table);
0d76751f James Chapman 2010-04-02 290 sk_del_node_init(sk);
0d76751f James Chapman 2010-04-02 291 write_unlock_bh(&l2tp_ip_lock);
3fb4e5ea Guillaume Nault 2016-11-28 292
0d76751f James Chapman 2010-04-02 293 ret = 0;
c51ce497 James Chapman 2012-05-29 294 sock_reset_flag(sk, SOCK_ZAPPED);
c51ce497 James Chapman 2012-05-29 295
0d76751f James Chapman 2010-04-02 296 out:
0d76751f James Chapman 2010-04-02 297 release_sock(sk);
0d76751f James Chapman 2010-04-02 298
0d76751f James Chapman 2010-04-02 @299 return ret;
0d76751f James Chapman 2010-04-02 300 }
0d76751f James Chapman 2010-04-02 301
0d76751f James Chapman 2010-04-02 302 static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
0d76751f James Chapman 2010-04-02 303 {
0d76751f James Chapman 2010-04-02 304 struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
de3c7a18 James Chapman 2012-04-29 305 int rc;
0d76751f James Chapman 2010-04-02 306
0d76751f James Chapman 2010-04-02 307 if (addr_len < sizeof(*lsa)):::::: The code at line 299 was first introduced by commit
:::::: 0d76751fad7739014485ba5bd388d4f1b4fd4143 l2tp: Add L2TPv3 IP encapsulation (no UDP) support
:::::: TO: James Chapman <[email protected]>
:::::: CC: David S. Miller <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH net 3/5] l2tp: fix racy socket lookup in l2tp_ip and l2tp_ip6 bind()
Quoted 14 lines · kbuild test robot
Hi Guillaume,
[auto build test WARNING on net/master]
url: https://github.com/0day-ci/linux/commits/Guillaume-Nault/l2tp-fixes-for-l2tp_ip-and-l2tp_ip6-socket-handling/20161129-043208
config: x86_64-rhel (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:# save the attached .config to linux build tree make ARCH=x86_64Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
Quoted 7 lines
All warnings (new ones prefixed by >>):
net/l2tp/l2tp_ip.c: In function 'l2tp_ip_bind':Quoted 1 line
Quoted 1 line
net/l2tp/l2tp_ip.c:299:9: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]return ret; ^~~
I should have seen this earlier, sorry.
This is now fixed in v2.