pull request (net): ipsec 2023-02-08
Subsystems (3)
Steffen Klassert
maintainercover pull request (net): ipsec 2023-02-08
1) Fix policy checks for nested IPsec tunnels when using xfrm interfaces. From Benedict Wong.
2) Fix netlink message expression on 32=>64-bit
messages translators. From Anastasia Belova.
3) Prevent potential spectre v1 gadget in xfrm_xlate32_attr. From Eric Dumazet.
4) Always consistently use time64_t in xfrm_timer_handler. From Eric Dumazet.
5) Fix KCSAN reported bug: Multiple cpus can update use_time at the same time. From Eric Dumazet.
6) Fix SCP copy from IPv4 to IPv6 on interfamily tunnel. From Christian Hopps.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 571f3dd0d01b62ec63a4039320dbdbcd54ae8fb0:
Merge tag 'rxrpc-fixes-20230107' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs (2023-01-07 23:10:33 +0000)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git tags/ipsec-2023-02-08
for you to fetch changes up to 6028da3f125fec34425dbd5fec18e85d372b2af6:
xfrm: fix bug with DSCP copy to v6 from v4 tunnel (2023-01-30 11:31:58 +0100)
----------------------------------------------------------------
ipsec-2023-02-08
----------------------------------------------------------------
Anastasia Belova (1):
xfrm: compat: change expression for switch in xfrm_xlate64
Benedict Wong (1):
Fix XFRM-I support for nested ESP tunnels
Christian Hopps (1):
xfrm: fix bug with DSCP copy to v6 from v4 tunnel
Eric Dumazet (3):
xfrm/compat: prevent potential spectre v1 gadget in xfrm_xlate32_attr()
xfrm: consistently use time64_t in xfrm_timer_handler()
xfrm: annotate data-race around use_time
net/xfrm/xfrm_compat.c | 4 +++- net/xfrm/xfrm_input.c | 3 +-- net/xfrm/xfrm_interface_core.c | 54 ++++++++++++++++++++++++++++++++++++++---- net/xfrm/xfrm_policy.c | 14 +++++++---- net/xfrm/xfrm_state.c | 18 +++++++------- 5 files changed, 73 insertions(+), 20 deletions(-)
Steffen Klassert
maintainerPATCH 1/6 Fix XFRM-I support for nested ESP tunnels
From: Benedict Wong <[email protected]>
This change adds support for nested IPsec tunnels by ensuring that XFRM-I verifies existing policies before decapsulating a subsequent policies. Addtionally, this clears the secpath entries after policies are verified, ensuring that previous tunnels with no-longer-valid do not pollute subsequent policy checks.
This is necessary especially for nested tunnels, as the IP addresses, protocol and ports may all change, thus not matching the previous policies. In order to ensure that packets match the relevant inbound templates, the xfrm_policy_check should be done before handing off to the inner XFRM protocol to decrypt and decapsulate.
Notably, raw ESP/AH packets did not perform policy checks inherently, whereas all other encapsulated packets (UDP, TCP encapsulated) do policy checks after calling xfrm_input handling in the respective encapsulation layer.
Test: Verified with additional Android Kernel Unit tests
--- net/xfrm/xfrm_interface_core.c | 54 +++++++++++++++++++++++++++++++--- net/xfrm/xfrm_policy.c | 3 ++ 2 files changed, 53 insertions(+), 4 deletions(-)
net/xfrm/xfrm_interface_core.c +50 -4
@@ -310,6 +310,52 @@ static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet) skb->mark =0 ; } +static int xfrmi_input (struct sk_buff *skb,int nexthdr,__be32 spi, +int encap_type,unsigned short family) +{ +struct sec_path *sp; + + sp =skb_sec_path (skb); +if (sp && (sp->len || sp->olen) && + !xfrm_policy_check (NULL ,XFRM_POLICY_IN , skb, family)) + goto discard; + +XFRM_SPI_SKB_CB (skb)->family = family; +if (family ==AF_INET ) { +XFRM_SPI_SKB_CB (skb)->daddroff = offsetof(struct iphdr , daddr); +XFRM_TUNNEL_SKB_CB (skb)->tunnel.ip4 =NULL ; + }else { +XFRM_SPI_SKB_CB (skb)->daddroff = offsetof(struct ipv6hdr , daddr); +XFRM_TUNNEL_SKB_CB (skb)->tunnel.ip6 =NULL ; + } + +return xfrm_input (skb, nexthdr, spi, encap_type); +discard: +kfree_skb (skb); +return 0 ; +} + +static int xfrmi4_rcv (struct sk_buff *skb) +{ +return xfrmi_input (skb,ip_hdr (skb)->protocol,0 ,0 ,AF_INET ); +} + +static int xfrmi6_rcv (struct sk_buff *skb) +{ +return xfrmi_input (skb,skb_network_header (skb)[IP6CB (skb)->nhoff], +0 ,0 ,AF_INET6 ); +} + +static int xfrmi4_input (struct sk_buff *skb,int nexthdr,__be32 spi,int encap_type) +{ +return xfrmi_input (skb, nexthdr, spi, encap_type,AF_INET ); +} + +static int xfrmi6_input (struct sk_buff *skb,int nexthdr,__be32 spi,int encap_type) +{ +return xfrmi_input (skb, nexthdr, spi, encap_type,AF_INET6 ); +} +static int xfrmi_rcv_cb (struct sk_buff *skb,int err) {const struct xfrm_mode *inner_mode;
@@ -945,8 +991,8 @@ static struct pernet_operations xfrmi_net_ops = { };static struct xfrm6_protocol xfrmi_esp6_protocol__read_mostly = { - .handler = xfrm6_rcv, - .input_handler = xfrm_input, + .handler = xfrmi6_rcv, + .input_handler = xfrmi6_input, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi6_err, .priority =10 ,
@@ -996,8 +1042,8 @@ static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = {#endif static struct xfrm4_protocol xfrmi_esp4_protocol__read_mostly = { - .handler = xfrm4_rcv, - .input_handler = xfrm_input, + .handler = xfrmi4_rcv, + .input_handler = xfrmi4_input, .cb_handler = xfrmi_rcv_cb, .err_handler = xfrmi4_err, .priority =10 ,
net/xfrm/xfrm_policy.c +3 -0
@@ -3742,6 +3742,9 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, goto reject; } +if (if_id) +secpath_reset (skb); +xfrm_pols_put (pols, npols);return 1 ; }
Signature · 1 line
2.34.1
Re: [PATCH 1/6] Fix XFRM-I support for nested ESP tunnels
This series was applied to netdev/net.git (master) by Steffen Klassert <[email protected]>:
Hello:
This series was applied to netdev/net.git (master) by Steffen Klassert <[email protected]>:
Quoted 9 lines · On Wed, 8 Feb 2023 12:43:17 +0100 you wrote:
From: Benedict Wong <[email protected]>
This change adds support for nested IPsec tunnels by ensuring that XFRM-I verifies existing policies before decapsulating a subsequent policies. Addtionally, this clears the secpath entries after policies are verified, ensuring that previous tunnels with no-longer-valid do not pollute subsequent policy checks.
[...]
Here is the summary with links:
- [1/6] Fix XFRM-I support for nested ESP tunnels
https://git.kernel.org/netdev/net/c/b0355dbbf13c - [2/6] xfrm: compat: change expression for switch in xfrm_xlate64 https://git.kernel.org/netdev/net/c/eb6c59b735aa - [3/6] xfrm/compat: prevent potential spectre v1 gadget in xfrm_xlate32_attr() https://git.kernel.org/netdev/net/c/b6ee89638538 - [4/6] xfrm: consistently use time64_t in xfrm_timer_handler() https://git.kernel.org/netdev/net/c/195e4aac74ce - [5/6] xfrm: annotate data-race around use_time https://git.kernel.org/netdev/net/c/0a9e5794b21e - [6/6] xfrm: fix bug with DSCP copy to v6 from v4 tunnel https://git.kernel.org/netdev/net/c/6028da3f125f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Steffen Klassert
maintainerPATCH 2/6 xfrm: compat: change expression for switch in xfrm_xlate64
In reply to Steffen KlassertFrom: Anastasia Belova <[email protected]>
Compare XFRM_MSG_NEWSPDINFO (value from netlink configuration messages enum) with nlh_src->nlmsg_type instead of nlh_src->nlmsg_type - XFRM_MSG_BASE.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
--- net/xfrm/xfrm_compat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/xfrm/xfrm_compat.c +1 -1
@@ -302,7 +302,7 @@ static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src)nla_for_each_attr (nla, attrs, len, remaining) {int err; -switch (type) { +switch (nlh_src->nlmsg_type) {case XFRM_MSG_NEWSPDINFO : err =xfrm_nla_cpy (dst, nla,nla_len (nla));break ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 3/6 xfrm/compat: prevent potential spectre v1 gadget in xfrm_xlate32_attr()
In reply to Steffen KlassertFrom: Eric Dumazet <[email protected]>
int type = nla_type(nla);
if (type > XFRMA_MAX) {
return -EOPNOTSUPP;
}@type is then used as an array index and can be used as a Spectre v1 gadget.
if (nla_len(nla) < compat_policy[type].len) {array_index_nospec() can be used to prevent leaking content of kernel memory to malicious users.
--- net/xfrm/xfrm_compat.c | 2 ++ 1 file changed, 2 insertions(+)
net/xfrm/xfrm_compat.c +2 -0
@@ -5,6 +5,7 @@* Based on code and translator idea by: Florian Westphal <[email protected]> */ #include <linux/compat.h> +#include <linux/nospec.h> #include <linux/xfrm.h> #include <net/xfrm.h>
@@ -437,6 +438,7 @@ static int xfrm_xlate32_attr(void *dst, const struct nlattr *nla,NL_SET_ERR_MSG (extack,"Bad attribute" );return -EOPNOTSUPP ; } + type =array_index_nospec (type,XFRMA_MAX +1 );if (nla_len (nla) < compat_policy[type].len) {NL_SET_ERR_MSG (extack,"Attribute bad length" );return -EOPNOTSUPP ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 4/6 xfrm: consistently use time64_t in xfrm_timer_handler()
In reply to Steffen KlassertFrom: Eric Dumazet <[email protected]>
For some reason, blamed commit did the right thing in xfrm_policy_timer() but did not in xfrm_timer_handler()
--- net/xfrm/xfrm_state.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/xfrm/xfrm_state.c +4 -4
@@ -577,7 +577,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)if (x->km.state ==XFRM_STATE_EXPIRED ) goto expired;if (x->lft.hard_add_expires_seconds) { -long tmo = x->lft.hard_add_expires_seconds + +time64_t tmo = x->lft.hard_add_expires_seconds + x->curlft.add_time - now;if (tmo <=0 ) {if (x->xflags &XFRM_SOFT_EXPIRE ) {
@@ -594,7 +594,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) next = tmo; }if (x->lft.hard_use_expires_seconds) { -long tmo = x->lft.hard_use_expires_seconds + +time64_t tmo = x->lft.hard_use_expires_seconds + (x->curlft.use_time ? : now) - now;if (tmo <=0 ) goto expired;
@@ -604,7 +604,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)if (x->km.dying) goto resched;if (x->lft.soft_add_expires_seconds) { -long tmo = x->lft.soft_add_expires_seconds + +time64_t tmo = x->lft.soft_add_expires_seconds + x->curlft.add_time - now;if (tmo <=0 ) { warn =1 ;
@@ -616,7 +616,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) } }if (x->lft.soft_use_expires_seconds) { -long tmo = x->lft.soft_use_expires_seconds + +time64_t tmo = x->lft.soft_use_expires_seconds + (x->curlft.use_time ? : now) - now;if (tmo <=0 ) warn =1 ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 5/6 xfrm: annotate data-race around use_time
In reply to Steffen KlassertFrom: Eric Dumazet <[email protected]>
KCSAN reported multiple cpus can update use_time
at the same time.
Adds READ_ONCE()/WRITE_ONCE() annotations.
Note that 32bit arches are not fully protected, but they will probably no longer be supported/used in 2106.
BUG: KCSAN: data-race in __xfrm_policy_check / __xfrm_policy_check
write to 0xffff88813e7ec108 of 8 bytes by interrupt on cpu 0:
__xfrm_policy_check+0x6ae/0x17f0 net/xfrm/xfrm_policy.c:3664 __xfrm_policy_check2 include/net/xfrm.h:1174 [inline] xfrm_policy_check include/net/xfrm.h:1179 [inline] xfrm6_policy_check+0x2e9/0x320 include/net/xfrm.h:1189 udpv6_queue_rcv_one_skb+0x48/0xa30 net/ipv6/udp.c:703 udpv6_queue_rcv_skb+0x2d6/0x310 net/ipv6/udp.c:792 udp6_unicast_rcv_skb+0x16b/0x190 net/ipv6/udp.c:935 __udp6_lib_rcv+0x84b/0x9b0 net/ipv6/udp.c:1020 udpv6_rcv+0x4b/0x50 net/ipv6/udp.c:1133 ip6_protocol_deliver_rcu+0x99e/0x1020 net/ipv6/ip6_input.c:439 ip6_input_finish net/ipv6/ip6_input.c:484 [inline] NF_HOOK include/linux/netfilter.h:302 [inline] ip6_input+0xca/0x180 net/ipv6/ip6_input.c:493 dst_input include/net/dst.h:454 [inline] ip6_rcv_finish+0x1e9/0x2d0 net/ipv6/ip6_input.c:79 NF_HOOK include/linux/netfilter.h:302 [inline] ipv6_rcv+0x85/0x140 net/ipv6/ip6_input.c:309 __netif_receive_skb_one_core net/core/dev.c:5482 [inline] __netif_receive_skb+0x8b/0x1b0 net/core/dev.c:5596 process_backlog+0x23f/0x3b0 net/core/dev.c:5924 __napi_poll+0x65/0x390 net/core/dev.c:6485 napi_poll net/core/dev.c:6552 [inline] net_rx_action+0x37e/0x730 net/core/dev.c:6663 __do_softirq+0xf2/0x2c7 kernel/softirq.c:571 do_softirq+0xb1/0xf0 kernel/softirq.c:472 __local_bh_enable_ip+0x6f/0x80 kernel/softirq.c:396 __raw_read_unlock_bh include/linux/rwlock_api_smp.h:257 [inline] _raw_read_unlock_bh+0x17/0x20 kernel/locking/spinlock.c:284 wg_socket_send_skb_to_peer+0x107/0x120 drivers/net/wireguard/socket.c:184 wg_packet_create_data_done drivers/net/wireguard/send.c:251 [inline] wg_packet_tx_worker+0x142/0x360 drivers/net/wireguard/send.c:276 process_one_work+0x3d3/0x720 kernel/workqueue.c:2289 worker_thread+0x618/0xa70 kernel/workqueue.c:2436 kthread+0x1a9/0x1e0 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308
write to 0xffff88813e7ec108 of 8 bytes by interrupt on cpu 1:
__xfrm_policy_check+0x6ae/0x17f0 net/xfrm/xfrm_policy.c:3664 __xfrm_policy_check2 include/net/xfrm.h:1174 [inline] xfrm_policy_check include/net/xfrm.h:1179 [inline] xfrm6_policy_check+0x2e9/0x320 include/net/xfrm.h:1189 udpv6_queue_rcv_one_skb+0x48/0xa30 net/ipv6/udp.c:703 udpv6_queue_rcv_skb+0x2d6/0x310 net/ipv6/udp.c:792 udp6_unicast_rcv_skb+0x16b/0x190 net/ipv6/udp.c:935 __udp6_lib_rcv+0x84b/0x9b0 net/ipv6/udp.c:1020 udpv6_rcv+0x4b/0x50 net/ipv6/udp.c:1133 ip6_protocol_deliver_rcu+0x99e/0x1020 net/ipv6/ip6_input.c:439 ip6_input_finish net/ipv6/ip6_input.c:484 [inline] NF_HOOK include/linux/netfilter.h:302 [inline] ip6_input+0xca/0x180 net/ipv6/ip6_input.c:493 dst_input include/net/dst.h:454 [inline] ip6_rcv_finish+0x1e9/0x2d0 net/ipv6/ip6_input.c:79 NF_HOOK include/linux/netfilter.h:302 [inline] ipv6_rcv+0x85/0x140 net/ipv6/ip6_input.c:309 __netif_receive_skb_one_core net/core/dev.c:5482 [inline] __netif_receive_skb+0x8b/0x1b0 net/core/dev.c:5596 process_backlog+0x23f/0x3b0 net/core/dev.c:5924 __napi_poll+0x65/0x390 net/core/dev.c:6485 napi_poll net/core/dev.c:6552 [inline] net_rx_action+0x37e/0x730 net/core/dev.c:6663 __do_softirq+0xf2/0x2c7 kernel/softirq.c:571 do_softirq+0xb1/0xf0 kernel/softirq.c:472 __local_bh_enable_ip+0x6f/0x80 kernel/softirq.c:396 __raw_read_unlock_bh include/linux/rwlock_api_smp.h:257 [inline] _raw_read_unlock_bh+0x17/0x20 kernel/locking/spinlock.c:284 wg_socket_send_skb_to_peer+0x107/0x120 drivers/net/wireguard/socket.c:184 wg_packet_create_data_done drivers/net/wireguard/send.c:251 [inline] wg_packet_tx_worker+0x142/0x360 drivers/net/wireguard/send.c:276 process_one_work+0x3d3/0x720 kernel/workqueue.c:2289 worker_thread+0x618/0xa70 kernel/workqueue.c:2436 kthread+0x1a9/0x1e0 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308
value changed: 0x0000000063c62d6f -> 0x0000000063c62d70
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 4185 Comm: kworker/1:2 Tainted: G W 6.2.0-rc4-syzkaller-00009-gd532dd102151-dirty #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022 Workqueue: wg-crypt-wg0 wg_packet_tx_worker
--- net/xfrm/xfrm_policy.c | 11 +++++++---- net/xfrm/xfrm_state.c | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-)
net/xfrm/xfrm_policy.c +7 -4
@@ -336,7 +336,7 @@ static void xfrm_policy_timer(struct timer_list *t) }if (xp->lft.hard_use_expires_seconds) {time64_t tmo = xp->lft.hard_use_expires_seconds + - (xp->curlft.use_time ? : xp->curlft.add_time) - now; + (READ_ONCE (xp->curlft.use_time) ? : xp->curlft.add_time) - now;if (tmo <=0 ) goto expired;if (tmo < next)
@@ -354,7 +354,7 @@ static void xfrm_policy_timer(struct timer_list *t) }if (xp->lft.soft_use_expires_seconds) {time64_t tmo = xp->lft.soft_use_expires_seconds + - (xp->curlft.use_time ? : xp->curlft.add_time) - now; + (READ_ONCE (xp->curlft.use_time) ? : xp->curlft.add_time) - now;if (tmo <=0 ) { warn =1 ; tmo =XFRM_KM_TIMEOUT ;
@@ -3661,7 +3661,8 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,return 1 ; } - pol->curlft.use_time =ktime_get_real_seconds (); +/* This lockless write can happen from different cpus. */ +WRITE_ONCE (pol->curlft.use_time,ktime_get_real_seconds ()); pols[0 ] = pol; npols++;
@@ -3676,7 +3677,9 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,xfrm_pol_put (pols[0 ]);return 0 ; } - pols[1 ]->curlft.use_time =ktime_get_real_seconds (); +/* This write can happen from different cpus. */ +WRITE_ONCE (pols[1 ]->curlft.use_time, +ktime_get_real_seconds ()); npols++; } }
net/xfrm/xfrm_state.c +5 -5
@@ -595,7 +595,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) }if (x->lft.hard_use_expires_seconds) {time64_t tmo = x->lft.hard_use_expires_seconds + - (x->curlft.use_time ? : now) - now; + (READ_ONCE (x->curlft.use_time) ? : now) - now;if (tmo <=0 ) goto expired;if (tmo < next)
@@ -617,7 +617,7 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) }if (x->lft.soft_use_expires_seconds) {time64_t tmo = x->lft.soft_use_expires_seconds + - (x->curlft.use_time ? : now) - now; + (READ_ONCE (x->curlft.use_time) ? : now) - now;if (tmo <=0 ) warn =1 ;else if (tmo < next)
@@ -1906,7 +1906,7 @@ int xfrm_state_update(struct xfrm_state *x)hrtimer_start (&x1->mtimer,ktime_set (1 ,0 ),HRTIMER_MODE_REL_SOFT ); -if (x1->curlft.use_time) +if (READ_ONCE (x1->curlft.use_time))xfrm_state_check_expire (x1);if (x->props.smark.m || x->props.smark.v || x->if_id) {
@@ -1940,8 +1940,8 @@ int xfrm_state_check_expire(struct xfrm_state *x) {xfrm_dev_state_update_curlft (x); -if (!x->curlft.use_time) - x->curlft.use_time =ktime_get_real_seconds (); +if (!READ_ONCE (x->curlft.use_time)) +WRITE_ONCE (x->curlft.use_time,ktime_get_real_seconds ());if (x->curlft.bytes >= x->lft.hard_byte_limit || x->curlft.packets >= x->lft.hard_packet_limit) {
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 6/6 xfrm: fix bug with DSCP copy to v6 from v4 tunnel
In reply to Steffen KlassertFrom: Christian Hopps <[email protected]>
When copying the DSCP bits for decap-dscp into IPv6 don't assume the outer encap is always IPv6. Instead, as with the inner IPv4 case, copy the DSCP bits from the correctly saved "tos" value in the control block.
--- net/xfrm/xfrm_input.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/xfrm/xfrm_input.c +1 -2
@@ -279,8 +279,7 @@ static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb) goto out;if (x->props.flags &XFRM_STATE_DECAP_DSCP ) -ipv6_copy_dscp (ipv6_get_dsfield (ipv6_hdr (skb)), -ipipv6_hdr (skb)); +ipv6_copy_dscp (XFRM_MODE_SKB_CB (skb)->tos,ipipv6_hdr (skb));if (!(x->props.flags &XFRM_STATE_NOECN ))ipip6_ecn_decapsulate (skb);
Signature · 1 line
2.34.1