pull request (net): ipsec 2020-11-04
Subsystems (3)
Steffen Klassert
maintainer1) Fix packet receiving of standard IP tunnels when the xfrm_interface module is installed. From Xin Long.
2) Fix a race condition between spi allocating and hash list resizing. From zhuoliang zhang.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 3fdd47c3b40ac48e6e6e5904cf24d12e6e073a96:
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost (2020-10-08 14:25:46 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master
for you to fetch changes up to a779d91314ca7208b7feb3ad817b62904397c56d:
net: xfrm: fix a race condition during allocing spi (2020-10-23 09:08:55 +0200)
----------------------------------------------------------------
Xin Long (1):
xfrm: interface: fix the priorities for ipip and ipv6 tunnels
zhuoliang zhang (1):
net: xfrm: fix a race condition during allocing spi
net/ipv4/xfrm4_tunnel.c | 4 ++-- net/ipv6/xfrm6_tunnel.c | 4 ++-- net/xfrm/xfrm_interface.c | 8 ++++---- net/xfrm/xfrm_state.c | 8 +++++--- 4 files changed, 13 insertions(+), 11 deletions(-)
Steffen Klassert
maintainerPATCH 1/2 xfrm: interface: fix the priorities for ipip and ipv6 tunnels
From: Xin Long <[email protected]>
As Nicolas noticed in his case, when xfrm_interface module is installed the standard IP tunnels will break in receiving packets.
This is caused by the IP tunnel handlers with a higher priority in xfrm interface processing incoming packets by xfrm_input(), which would drop the packets and return 0 instead when anything wrong happens.
Rather than changing xfrm_input(), this patch is to adjust the priority for the IP tunnel handlers in xfrm interface, so that the packets would go to xfrmi's later than the others', as the others' would not drop the packets when the handlers couldn't process them.
Note that IPCOMP also defines its own IPIP tunnel handler and it calls xfrm_input() as well, so we must make its priority lower than xfrmi's, which means having xfrmi loaded would still break IPCOMP. We may seek another way to fix it in xfrm_input() in the future.
--- net/ipv4/xfrm4_tunnel.c | 4 ++-- net/ipv6/xfrm6_tunnel.c | 4 ++-- net/xfrm/xfrm_interface.c | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-)
net/ipv4/xfrm4_tunnel.c +2 -2
@@ -64,14 +64,14 @@ static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)static struct xfrm_tunnel xfrm_tunnel_handler__read_mostly = { .handler = xfrm_tunnel_rcv, .err_handler = xfrm_tunnel_err, - .priority =3 , + .priority =4 , };#if IS_ENABLED (CONFIG_IPV6 )static struct xfrm_tunnel xfrm64_tunnel_handler__read_mostly = { .handler = xfrm_tunnel_rcv, .err_handler = xfrm_tunnel_err, - .priority =2 , + .priority =3 , };#endif
net/ipv6/xfrm6_tunnel.c +2 -2
@@ -303,13 +303,13 @@ static const struct xfrm_type xfrm6_tunnel_type = {static struct xfrm6_tunnel xfrm6_tunnel_handler__read_mostly = { .handler = xfrm6_tunnel_rcv, .err_handler = xfrm6_tunnel_err, - .priority =2 , + .priority =3 , };static struct xfrm6_tunnel xfrm46_tunnel_handler__read_mostly = { .handler = xfrm6_tunnel_rcv, .err_handler = xfrm6_tunnel_err, - .priority =2 , + .priority =3 , };static int __net_init xfrm6_tunnel_net_init (struct net *net)
net/xfrm/xfrm_interface.c +4 -4
@@ -830,14 +830,14 @@ static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = { .handler = xfrmi6_rcv_tunnel, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi6_err, - .priority = -1, + .priority = 2, };static struct xfrm6_tunnel xfrmi_ip6ip_handler__read_mostly = { .handler = xfrmi6_rcv_tunnel, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi6_err, - .priority =-1 , + .priority =2 , };#endif
@@ -875,14 +875,14 @@ static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = { .handler = xfrmi4_rcv_tunnel, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi4_err, - .priority = -1, + .priority = 3, };static struct xfrm_tunnel xfrmi_ipip6_handler__read_mostly = { .handler = xfrmi4_rcv_tunnel, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi4_err, - .priority =-1 , + .priority =2 , };#endif
Signature · 1 line
2.17.1
Steffen Klassert
maintainerPATCH 2/2 net: xfrm: fix a race condition during allocing spi
In reply to Steffen KlassertFrom: zhuoliang zhang <[email protected]>
we found that the following race condition exists in xfrm_alloc_userspi flow:
user thread state_hash_work thread
---- ----xfrm_alloc_userspi()
__find_acq_core()
/*alloc new xfrm_state:x*/
xfrm_state_alloc()
/*schedule state_hash_work thread*/
xfrm_hash_grow_check() xfrm_hash_resize()
xfrm_alloc_spi /*hold lock*/
x->id.spi = htonl(spi) spin_lock_bh(&net->xfrm.xfrm_state_lock)
/*waiting lock release*/ xfrm_hash_transfer()
spin_lock_bh(&net->xfrm.xfrm_state_lock) /*add x into hlist:net->xfrm.state_byspi*/
hlist_add_head_rcu(&x->byspi)
spin_unlock_bh(&net->xfrm.xfrm_state_lock)
/*add x into hlist:net->xfrm.state_byspi 2 times*/
hlist_add_head_rcu(&x->byspi)1. a new state x is alloced in xfrm_state_alloc() and added into the bydst hlist in __find_acq_core() on the LHS;
2. on the RHS, state_hash_work thread travels the old bydst and tranfers every xfrm_state (include x) into the new bydst hlist and new byspi hlist;
3. user thread on the LHS gets the lock and adds x into the new byspi hlist again.
So the same xfrm_state (x) is added into the same list_hash (net->xfrm.state_byspi) 2 times that makes the list_hash become an inifite loop.
To fix the race, x->id.spi = htonl(spi) in the xfrm_alloc_spi() is moved to the back of spin_lock_bh, sothat state_hash_work thread no longer add x which id.spi is zero into the hash_list.
--- net/xfrm/xfrm_state.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
net/xfrm/xfrm_state.c +5 -3
@@ -2004,6 +2004,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)int err = -ENOENT ;__be32 minspi =htonl (low);__be32 maxspi =htonl (high); +__be32 newspi =0 ;u32 mark = x->mark.v & x->mark.m;spin_lock_bh (&x->lock);
@@ -2022,21 +2023,22 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)xfrm_state_put (x0); goto unlock; } - x->id.spi = minspi; + newspi = minspi; }else {u32 spi =0 ;for (h =0 ; h < high-low+1 ; h++) { spi = low +prandom_u32 ()%(high-low+1 ); x0 =xfrm_state_lookup (net, mark, &x->id.daddr,htonl (spi), x->id.proto, x->props.family);if (x0 ==NULL ) { - x->id.spi =htonl (spi); + newspi =htonl (spi);break ; }xfrm_state_put (x0); } } -if (x->id.spi) { +if (newspi) {spin_lock_bh (&net->xfrm.xfrm_state_lock); + x->id.spi = newspi; h =xfrm_spi_hash (net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);hlist_add_head_rcu (&x->byspi, net->xfrm.state_byspi + h);spin_unlock_bh (&net->xfrm.xfrm_state_lock);
Signature · 1 line
2.17.1
Jakub Kicinski
maintainerQuoted 5 lines · On Wed, 4 Nov 2020 10:00:08 +0100 Steffen Klassert wrote:
1) Fix packet receiving of standard IP tunnels when the
xfrm_interfacemodule is installed. From Xin Long.2) Fix a race condition between spi allocating and hash list resizing. From zhuoliang zhang.
Pulled, thank you!