pull request (net): ipsec 2018-03-13
Subsystems (4)
Steffen Klassert
maintainer1) Refuse to insert 32 bit userspace socket policies on 64 bit systems like we do it for standard policies. We don't have a compat layer, so inserting socket policies from 32 bit userspace will lead to a broken configuration.
2) Make the policy hold queue work without the flowcache. Dummy bundles are not chached anymore, so we need to generate a new one on each lookup as long as the SAs are not yet in place.
3) Fix the validation of the esn replay attribute. The The sanity check in verify_replay() is bypassed if the XFRM_STATE_ESN flag is not set. Fix this by doing the sanity check uncoditionally.
From Florian Westphal.
4) After most of the dst_entry garbage collection code is removed, we may leak xfrm_dst entries as they are neither cached nor tracked somewhere. Fix this by reusing the 'uncached_list' to track xfrm_dst entries too. From Xin Long.
5) Fix a rcu_read_lock/rcu_read_unlock imbalance in xfrm_get_tos() From Xin Long.
6) Fix an infinite loop in xfrm_get_dst_nexthop. On transport mode we fetch the child dst_entry after we continue, so this pointer is never updated. Fix this by fetching it before we continue.
7) Fix ESN sequence number gap after IPsec GSO packets. We accidentally increment the sequence number counter on the xfrm_state by one packet too much in the ESN case. Fix this by setting the sequence number to the correct value.
8) Reset the ethernet protocol after decapsulation only if a mac header was set. Otherwise it breaks configurations with TUN devices. From Yossi Kuperman.
9) Fix __this_cpu_read() usage in preemptible code. Use this_cpu_read() instead in ipcomp_alloc_tfms(). From Greg Hackmann.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 743ffffefac1c670c6618742c923f6275d819604:
net: pxa168_eth: add netconsole support (2018-02-01 14:58:37 -0500)
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 0dcd7876029b58770f769cbb7b484e88e4a305e5:
net: xfrm: use preempt-safe this_cpu_read() in ipcomp_alloc_tfms() (2018-03-13 07:46:37 +0100)
----------------------------------------------------------------
Florian Westphal (1):
xfrm_user: uncoditionally validate esn replay attribute struct
Greg Hackmann (1):
net: xfrm: use preempt-safe this_cpu_read() in ipcomp_alloc_tfms()
Steffen Klassert (4):
xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems
xfrm: Fix policy hold queue after flowcache removal.
xfrm: Fix infinite loop in xfrm_get_dst_nexthop with transport mode.
xfrm: Fix ESN sequence number handling for IPsec GSO packets.
Xin Long (2):
xfrm: reuse uncached_list to track xdsts
xfrm: do not call rcu_read_unlock when afinfo is NULL in xfrm_get_tos
Yossi Kuperman (1):
xfrm: Verify MAC header exists before overwriting eth_hdr(skb)->h_proto
include/net/ip6_route.h | 3 +++ include/net/route.h | 3 +++ net/ipv4/route.c | 21 +++++++++++++-------- net/ipv4/xfrm4_mode_tunnel.c | 3 ++- net/ipv4/xfrm4_policy.c | 4 +++- net/ipv6/route.c | 4 ++-- net/ipv6/xfrm6_mode_tunnel.c | 3 ++- net/ipv6/xfrm6_policy.c | 5 +++++ net/xfrm/xfrm_ipcomp.c | 2 +- net/xfrm/xfrm_policy.c | 13 ++++++++----- net/xfrm/xfrm_replay.c | 2 +- net/xfrm/xfrm_state.c | 5 +++++ net/xfrm/xfrm_user.c | 21 ++++++++------------- 13 files changed, 56 insertions(+), 33 deletions(-)
Steffen Klassert
maintainerPATCH 1/9 xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems
We don't have a compat layer for xfrm, so userspace and kernel structures have different sizes in this case. This results in a broken configuration, so refuse to configure socket policies when trying to insert from 32 bit userspace as we do it already with policies inserted via netlink.
--- net/xfrm/xfrm_state.c | 5 +++++ 1 file changed, 5 insertions(+)
net/xfrm/xfrm_state.c +5 -0
@@ -2056,6 +2056,11 @@ int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlenstruct xfrm_mgr *km;struct xfrm_policy *pol =NULL ; +#ifdef CONFIG_COMPAT +if (in_compat_syscall ()) +return -EOPNOTSUPP ; +#endif +if (!optval && !optlen) {xfrm_sk_policy_insert (sk,XFRM_POLICY_IN ,NULL );xfrm_sk_policy_insert (sk,XFRM_POLICY_OUT ,NULL );
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 2/9 xfrm: Fix policy hold queue after flowcache removal.
In reply to Steffen KlassertNow that the flowcache is removed we need to generate a new dummy bundle every time we check if the needed SAs are in place because the dummy bundle is not cached anymore. Fix it by passing the XFRM_LOOKUP_QUEUE flag to xfrm_lookup(). This makes sure that we get a dummy bundle in case the SAs are not yet in place.
--- net/xfrm/xfrm_policy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_policy.c +1 -1
@@ -1891,7 +1891,7 @@ static void xfrm_policy_queue_process(struct timer_list *t)spin_unlock (&pq->hold_queue.lock);dst_hold (xfrm_dst_path (dst)); - dst =xfrm_lookup (net,xfrm_dst_path (dst), &fl, sk,0 ); + dst =xfrm_lookup (net,xfrm_dst_path (dst), &fl, sk,XFRM_LOOKUP_QUEUE );if (IS_ERR (dst)) goto purge_queue;
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 3/9 xfrm_user: uncoditionally validate esn replay attribute struct
In reply to Steffen KlassertFrom: Florian Westphal <[email protected]>
The sanity test added in ecd7918745234 can be bypassed, validation only occurs if XFRM_STATE_ESN flag is set, but rest of code doesn't care and just checks if the attribute itself is present.
So always validate. Alternative is to reject if we have the attribute without the flag but that would change abi.
--- net/xfrm/xfrm_user.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
net/xfrm/xfrm_user.c +8 -13
@@ -121,22 +121,17 @@ static inline int verify_replay(struct xfrm_usersa_info *p,struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL ];struct xfrm_replay_state_esn *rs; -if (p->flags &XFRM_STATE_ESN ) { -if (!rt) -return -EINVAL ; +if (!rt) +return (p->flags &XFRM_STATE_ESN ) ? -EINVAL :0 ; - rs =nla_data (rt); + rs =nla_data (rt); -if (rs->bmp_len >XFRMA_REPLAY_ESN_MAX /sizeof (rs->bmp[0 ]) /8 ) -return -EINVAL ; - -if (nla_len (rt) < (int )xfrm_replay_state_esn_len (rs) && -nla_len (rt) !=sizeof (*rs)) -return -EINVAL ; - } +if (rs->bmp_len >XFRMA_REPLAY_ESN_MAX /sizeof (rs->bmp[0 ]) /8 ) +return -EINVAL ; -if (!rt) -return 0 ; +if (nla_len (rt) < (int )xfrm_replay_state_esn_len (rs) && +nla_len (rt) !=sizeof (*rs)) +return -EINVAL ;/* As only ESP and AH support ESN feature. */ if ((p->id.proto !=IPPROTO_ESP ) && (p->id.proto !=IPPROTO_AH ))
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 4/9 xfrm: reuse uncached_list to track xdsts
In reply to Steffen KlassertFrom: Xin Long <[email protected]>
In early time, when freeing a xdst, it would be inserted into dst_garbage.list first. Then if it's refcnt was still held somewhere, later it would be put into dst_busy_list in dst_gc_task().
When one dev was being unregistered, the dev of these dsts in dst_busy_list would be set with loopback_dev and put this dev. So that this dev's removal wouldn't get blocked, and avoid the kmsg warning:
kernel:unregister_netdevice: waiting for veth0 to become \ free. Usage count = 2
However after Commit 52df157f17e5 ("xfrm: take refcnt of dst when creating struct xfrm_dst bundle"), the xdst will not be freed with dst gc, and this warning happens.
To fix it, we need to find these xdsts that are still held by others when removing the dev, and free xdst's dev and set it with loopback_dev.
But unfortunately after flow_cache for xfrm was deleted, no list tracks them anymore. So we need to save these xdsts somewhere to release the xdst's dev later.
To make this easier, this patch is to reuse uncached_list to track xdsts, so that the dev refcnt can be released in the event NETDEV_UNREGISTER process of fib_netdev_notifier.
Thanks to Florian, we could move forward this fix quickly.
--- include/net/ip6_route.h | 3 +++ include/net/route.h | 3 +++ net/ipv4/route.c | 21 +++++++++++++-------- net/ipv4/xfrm4_policy.c | 4 +++- net/ipv6/route.c | 4 ++-- net/ipv6/xfrm6_policy.c | 5 +++++ 6 files changed, 29 insertions(+), 11 deletions(-)
include/net/ip6_route.h +3 -0
@@ -179,6 +179,9 @@ void rt6_disable_ip(struct net_device *dev, unsigned long event);void rt6_sync_down_dev (struct net_device *dev,unsigned long event);void rt6_multipath_rebalance (struct rt6_info *rt); +void rt6_uncached_list_add (struct rt6_info *rt); +void rt6_uncached_list_del (struct rt6_info *rt); +static inline const struct rt6_info *skb_rt6_info (const struct sk_buff *skb) {const struct dst_entry *dst =skb_dst (skb);
include/net/route.h +3 -0
@@ -227,6 +227,9 @@ struct in_ifaddr;void fib_add_ifaddr (struct in_ifaddr *);void fib_del_ifaddr (struct in_ifaddr *,struct in_ifaddr *); +void rt_add_uncached_list (struct rtable *rt); +void rt_del_uncached_list (struct rtable *rt); +static inline void ip_rt_put (struct rtable *rt) {/* dst_release() accepts a NULL parameter.
net/ipv4/route.c +13 -8
@@ -1383,7 +1383,7 @@ struct uncached_list { static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list); -static void rt_add_uncached_list (struct rtable *rt) +void rt_add_uncached_list (struct rtable *rt) {struct uncached_list *ul =raw_cpu_ptr (&rt_uncached_list);
@@ -1394,14 +1394,8 @@ static void rt_add_uncached_list(struct rtable *rt)spin_unlock_bh (&ul->lock); } -static void ipv4_dst_destroy (struct dst_entry *dst) +void rt_del_uncached_list (struct rtable *rt) { -struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR (dst); -struct rtable *rt = (struct rtable *) dst; - -if (p != &dst_default_metrics &&refcount_dec_and_test (&p->refcnt)) -kfree (p); -if (!list_empty (&rt->rt_uncached)) {struct uncached_list *ul = rt->rt_uncached_list;
@@ -1411,6 +1405,17 @@ static void ipv4_dst_destroy(struct dst_entry *dst) } } +static void ipv4_dst_destroy (struct dst_entry *dst) +{ +struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR (dst); +struct rtable *rt = (struct rtable *)dst; + +if (p != &dst_default_metrics &&refcount_dec_and_test (&p->refcnt)) +kfree (p); + +rt_del_uncached_list (rt); +} +void rt_flush_dev (struct net_device *dev) {struct net *net =dev_net (dev);
net/ipv4/xfrm4_policy.c +3 -1
@@ -102,6 +102,7 @@ static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, xdst->u.rt.rt_pmtu = rt->rt_pmtu; xdst->u.rt.rt_table_id = rt->rt_table_id;INIT_LIST_HEAD (&xdst->u.rt.rt_uncached); +rt_add_uncached_list (&xdst->u.rt);return 0 ; }
@@ -241,7 +242,8 @@ static void xfrm4_dst_destroy(struct dst_entry *dst)struct xfrm_dst *xdst = (struct xfrm_dst *)dst;dst_destroy_metrics_generic (dst); - +if (xdst->u.rt.rt_uncached_list) +rt_del_uncached_list (&xdst->u.rt);xfrm_dst_destroy (xdst); }
net/ipv6/route.c +2 -2
@@ -128,7 +128,7 @@ struct uncached_list { static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); -static void rt6_uncached_list_add (struct rt6_info *rt) +void rt6_uncached_list_add (struct rt6_info *rt) {struct uncached_list *ul =raw_cpu_ptr (&rt6_uncached_list);
@@ -139,7 +139,7 @@ static void rt6_uncached_list_add(struct rt6_info *rt)spin_unlock_bh (&ul->lock); } -static void rt6_uncached_list_del (struct rt6_info *rt) +void rt6_uncached_list_del (struct rt6_info *rt) {if (!list_empty (&rt->rt6i_uncached)) {struct uncached_list *ul = rt->rt6i_uncached_list;
net/ipv6/xfrm6_policy.c +5 -0
@@ -113,6 +113,9 @@ static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway; xdst->u.rt6.rt6i_dst = rt->rt6i_dst; xdst->u.rt6.rt6i_src = rt->rt6i_src; +INIT_LIST_HEAD (&xdst->u.rt6.rt6i_uncached); +rt6_uncached_list_add (&xdst->u.rt6); +atomic_inc (&dev_net (dev)->ipv6.rt6_stats->fib_rt_uncache);return 0 ; }
@@ -244,6 +247,8 @@ static void xfrm6_dst_destroy(struct dst_entry *dst)if (likely (xdst->u.rt6.rt6i_idev))in6_dev_put (xdst->u.rt6.rt6i_idev);dst_destroy_metrics_generic (dst); +if (xdst->u.rt6.rt6i_uncached_list) +rt6_uncached_list_del (&xdst->u.rt6);xfrm_dst_destroy (xdst); }
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 5/9 xfrm: do not call rcu_read_unlock when afinfo is NULL in xfrm_get_tos
In reply to Steffen KlassertFrom: Xin Long <[email protected]>
When xfrm_policy_get_afinfo returns NULL, it will not hold rcu read lock. In this case, rcu_read_unlock should not be called in xfrm_get_tos, just like other places where it's calling xfrm_policy_get_afinfo.
--- net/xfrm/xfrm_policy.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/xfrm/xfrm_policy.c +5 -2
@@ -1458,10 +1458,13 @@ xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,static int xfrm_get_tos (const struct flowi *fl,int family) {const struct xfrm_policy_afinfo *afinfo; -int tos =0 ; +int tos; afinfo =xfrm_policy_get_afinfo (family); - tos = afinfo ? afinfo->get_tos (fl) :0 ; +if (!afinfo) +return 0 ; + + tos = afinfo->get_tos (fl);rcu_read_unlock ();
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 6/9 xfrm: Fix infinite loop in xfrm_get_dst_nexthop with transport mode.
In reply to Steffen KlassertOn transport mode we forget to fetch the child dst_entry before we continue the while loop, this leads to an infinite loop. Fix this by fetching the child dst_entry before we continue the while loop.
--- net/xfrm/xfrm_policy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_policy.c +2 -2
@@ -2732,14 +2732,14 @@ static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,while (dst->xfrm) {const struct xfrm_state *xfrm = dst->xfrm; + dst =xfrm_dst_child (dst); +if (xfrm->props.mode ==XFRM_MODE_TRANSPORT )continue ;if (xfrm->type->flags &XFRM_TYPE_REMOTE_COADDR ) daddr = xfrm->coaddr;else if (!(xfrm->type->flags &XFRM_TYPE_LOCAL_COADDR )) daddr = &xfrm->id.daddr; - - dst =xfrm_dst_child (dst); }return daddr; }
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 7/9 xfrm: Fix ESN sequence number handling for IPsec GSO packets.
In reply to Steffen KlassertWhen IPsec offloading was introduced, we accidentally incremented the sequence number counter on the xfrm_state by one packet too much in the ESN case. This leads to a sequence number gap of one packet after each GSO packet. Fix this by setting the sequence number to the correct value.
--- net/xfrm/xfrm_replay.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_replay.c +1 -1
@@ -660,7 +660,7 @@ static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff }else {XFRM_SKB_CB (skb)->seq.output.low = oseq +1 ;XFRM_SKB_CB (skb)->seq.output.hi = oseq_hi; - xo->seq.low = oseq = oseq +1 ; + xo->seq.low = oseq +1 ; xo->seq.hi = oseq_hi; oseq +=skb_shinfo (skb)->gso_segs; }
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 8/9 xfrm: Verify MAC header exists before overwriting eth_hdr(skb)->h_proto
In reply to Steffen KlassertFrom: Yossi Kuperman <[email protected]>
Artem Savkov reported that commit 5efec5c655dd leads to a packet loss under IPSec configuration. It appears that his setup consists of a TUN device, which does not have a MAC header.
Make sure MAC header exists.
Note: TUN device sets a MAC header pointer, although it does not have one.
--- net/ipv4/xfrm4_mode_tunnel.c | 3 ++- net/ipv6/xfrm6_mode_tunnel.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
net/ipv4/xfrm4_mode_tunnel.c +2 -1
@@ -92,7 +92,8 @@ static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)skb_reset_network_header (skb);skb_mac_header_rebuild (skb); -eth_hdr (skb)->h_proto = skb->protocol; +if (skb->mac_len) +eth_hdr (skb)->h_proto = skb->protocol; err =0 ;
net/ipv6/xfrm6_mode_tunnel.c +2 -1
@@ -92,7 +92,8 @@ static int xfrm6_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)skb_reset_network_header (skb);skb_mac_header_rebuild (skb); -eth_hdr (skb)->h_proto = skb->protocol; +if (skb->mac_len) +eth_hdr (skb)->h_proto = skb->protocol; err =0 ;
Signature · 1 line
2.14.1
Steffen Klassert
maintainerPATCH 9/9 net: xfrm: use preempt-safe this_cpu_read() in ipcomp_alloc_tfms()
In reply to Steffen KlassertFrom: Greg Hackmann <[email protected]>
f7c83bcbfaf5 ("net: xfrm: use __this_cpu_read per-cpu helper") added a __this_cpu_read() call inside ipcomp_alloc_tfms().
At the time, __this_cpu_read() required the caller to either not care about races or to handle preemption/interrupt issues. 3.15 tightened the rules around some per-cpu operations, and now __this_cpu_read() should never be used in a preemptible context. On 3.15 and later, we need to use this_cpu_read() instead.
syzkaller reported this leading to the following kernel BUG while fuzzing sendmsg:
BUG: using __this_cpu_read() in preemptible [00000000] code: repro/3101 caller is ipcomp_init_state+0x185/0x990 CPU: 3 PID: 3101 Comm: repro Not tainted 4.16.0-rc4-00123-g86f84779d8e9 #154 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 Call Trace: dump_stack+0xb9/0x115 check_preemption_disabled+0x1cb/0x1f0 ipcomp_init_state+0x185/0x990 ? __xfrm_init_state+0x876/0xc20 ? lock_downgrade+0x5e0/0x5e0 ipcomp4_init_state+0xaa/0x7c0 __xfrm_init_state+0x3eb/0xc20 xfrm_init_state+0x19/0x60 pfkey_add+0x20df/0x36f0 ? pfkey_broadcast+0x3dd/0x600 ? pfkey_sock_destruct+0x340/0x340 ? pfkey_seq_stop+0x80/0x80 ? __skb_clone+0x236/0x750 ? kmem_cache_alloc+0x1f6/0x260 ? pfkey_sock_destruct+0x340/0x340 ? pfkey_process+0x62a/0x6f0 pfkey_process+0x62a/0x6f0 ? pfkey_send_new_mapping+0x11c0/0x11c0 ? mutex_lock_io_nested+0x1390/0x1390 pfkey_sendmsg+0x383/0x750 ? dump_sp+0x430/0x430 sock_sendmsg+0xc0/0x100 ___sys_sendmsg+0x6c8/0x8b0 ? copy_msghdr_from_user+0x3b0/0x3b0 ? pagevec_lru_move_fn+0x144/0x1f0 ? find_held_lock+0x32/0x1c0 ? do_huge_pmd_anonymous_page+0xc43/0x11e0 ? lock_downgrade+0x5e0/0x5e0 ? get_kernel_page+0xb0/0xb0 ? _raw_spin_unlock+0x29/0x40 ? do_huge_pmd_anonymous_page+0x400/0x11e0 ? __handle_mm_fault+0x553/0x2460 ? __fget_light+0x163/0x1f0 ? __sys_sendmsg+0xc7/0x170 __sys_sendmsg+0xc7/0x170 ? SyS_shutdown+0x1a0/0x1a0 ? __do_page_fault+0x5a0/0xca0 ? lock_downgrade+0x5e0/0x5e0 SyS_sendmsg+0x27/0x40 ? __sys_sendmsg+0x170/0x170 do_syscall_64+0x19f/0x640 entry_SYSCALL_64_after_hwframe+0x42/0xb7 RIP: 0033:0x7f0ee73dfb79 RSP: 002b:00007ffe14fc15a8 EFLAGS: 00000207 ORIG_RAX: 000000000000002e RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f0ee73dfb79 RDX: 0000000000000000 RSI: 00000000208befc8 RDI: 0000000000000004 RBP: 00007ffe14fc15b0 R08: 00007ffe14fc15c0 R09: 00007ffe14fc15c0 R10: 0000000000000000 R11: 0000000000000207 R12: 0000000000400440 R13: 00007ffe14fc16b0 R14: 0000000000000000 R15: 0000000000000000
--- net/xfrm/xfrm_ipcomp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_ipcomp.c +1 -1
@@ -283,7 +283,7 @@ static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)struct crypto_comp *tfm;/* This can be any valid CPU ID so we don't need locking. */ - tfm =__this_cpu_read (*pos->tfms); + tfm =this_cpu_read (*pos->tfms);if (!strcmp (crypto_comp_name (tfm), alg_name)) { pos->users++;
Signature · 1 line
2.14.1
David Miller
maintainerFrom: Steffen Klassert <[email protected]>
Date: Tue, 13 Mar 2018 08:09:44 +0100
Quoted 1 line
Please pull or let me know if there are problems.
Pulled, thanks!