pull request (net): ipsec 2023-06-20
Subsystems (3)
Steffen Klassert
maintainercover pull request (net): ipsec 2023-06-20
1) Treat already-verified secpath entries as optional to fix nested IPsec tunnels with xfrm interfaces. Fix from Benedict Wong.
2) Make sure that policies always checked on the input path with xfrm interfaces. Fix from Benedict Wong.
3) Add missing calls to delete offloaded policies. Fix from Leon Romanovsky.
4) Fix inbound ipv4/udp/esp packets to UDPv6 dualstack sockets. We need to make sure to call the handler of the correct address family.
Fix from Maciej Żenczykowski.
5) Use the address faqmily from the xfrm_state selector for BEET mode input.
Fix from Herbert Xu.
6) Linearize the skb after offloading if needed. This was forgotten when the ESP software GRO codepath was created. Fix from Sebastian Andrzej Siewior.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 67caf26d769e0cb17dba182b0acae015c7aa5881:
Merge tag 'for-net-2023-05-19' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth (2023-05-19 22:48:26 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git tags/ipsec-2023-06-20
for you to fetch changes up to f015b900bc3285322029b4a7d132d6aeb0e51857:
xfrm: Linearize the skb after offloading if needed. (2023-06-16 10:29:50 +0200)
----------------------------------------------------------------
ipsec-2023-06-20
----------------------------------------------------------------
Benedict Wong (2):
xfrm: Treat already-verified secpath entries as optional
xfrm: Ensure policies always checked on XFRM-I input path
Herbert Xu (1):
xfrm: Use xfrm_state selector for BEET input
Leon Romanovsky (1):
xfrm: add missed call to delete offloaded policies
Maciej Żenczykowski (1):
xfrm: fix inbound ipv4/udp/esp packets to UDPv6 dualstack sockets
Sebastian Andrzej Siewior (1):
xfrm: Linearize the skb after offloading if needed.
include/net/xfrm.h | 1 + net/ipv4/esp4_offload.c | 3 +++ net/ipv4/xfrm4_input.c | 1 + net/ipv6/esp6_offload.c | 3 +++ net/ipv6/xfrm6_input.c | 3 +++ net/xfrm/xfrm_input.c | 8 +++---- net/xfrm/xfrm_interface_core.c | 54 ++++++++++++++++++++++++++++++++++++++---- net/xfrm/xfrm_policy.c | 14 +++++++++++ 8 files changed, 79 insertions(+), 8 deletions(-)
Steffen Klassert
maintainerPATCH 1/6 xfrm: Treat already-verified secpath entries as optional
From: Benedict Wong <[email protected]>
This change allows inbound traffic through nested IPsec tunnels to successfully match policies and templates, while retaining the secpath stack trace as necessary for netfilter policies.
Specifically, this patch marks secpath entries that have already matched against a relevant policy as having been verified, allowing it to be treated as optional and skipped after a tunnel decapsulation (during which the src/dst/proto/etc may have changed, and the correct policy chain no long be resolvable).
This approach is taken as opposed to the iteration in b0355dbbf13c, where the secpath was cleared, since that breaks subsequent validations that rely on the existence of the secpath entries (netfilter policies, or transport-in-tunnel mode, where policies remain resolvable).
Test: Tested against Android Kernel Unit Tests
Test: Tested against Android CTS
--- include/net/xfrm.h | 1 + net/xfrm/xfrm_input.c | 1 + net/xfrm/xfrm_policy.c | 12 ++++++++++++ 3 files changed, 14 insertions(+)
include/net/xfrm.h +1 -0
@@ -1054,6 +1054,7 @@ struct xfrm_offload {struct sec_path {int len;int olen; +int verified_cnt;struct xfrm_state *xvec[XFRM_MAX_DEPTH ];struct xfrm_offload ovec[XFRM_MAX_OFFLOAD_DEPTH ];
net/xfrm/xfrm_input.c +1 -0
@@ -131,6 +131,7 @@ struct sec_path *secpath_set(struct sk_buff *skb)memset (sp->ovec,0 ,sizeof (sp->ovec)); sp->olen =0 ; sp->len =0 ; + sp->verified_cnt =0 ;return sp; }
net/xfrm/xfrm_policy.c +12 -0
@@ -3349,6 +3349,13 @@ xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int starif (xfrm_state_ok (tmpl, sp->xvec[idx], family, if_id))return ++idx;if (sp->xvec[idx]->props.mode !=XFRM_MODE_TRANSPORT ) { +if (idx < sp->verified_cnt) { +/* Secpath entry previously verified, consider optional and +* continue searching +*/ +continue ; + } +if (start ==-1 ) start =-2 -idx;break ;
@@ -3723,6 +3730,9 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,* Order is _important_. Later we will implement * some barriers, but at the moment barriers * are implied between each two transformations. +* Upon success, marks secpath entries as having been +* verified to allow them to be skipped in future policy +* checks (e.g. nested tunnels). */ for (i = xfrm_nr-1 , k =0 ; i >=0 ; i--) { k =xfrm_policy_ok (tpp[i], sp, k, family, if_id);
@@ -3741,6 +3751,8 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, }xfrm_pols_put (pols, npols); + sp->verified_cnt = k; +return 1 ; }XFRM_INC_STATS (net,LINUX_MIB_XFRMINPOLBLOCK );
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 2/6 xfrm: Ensure policies always checked on XFRM-I input path
In reply to Steffen KlassertFrom: Benedict Wong <[email protected]>
This change adds methods in the XFRM-I input path that ensures that policies are checked prior to processing of the subsequent decapsulated packet, after which the relevant policies may no longer be resolvable (due to changing src/dst/proto/etc).
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
Test: Verified against Android CTS
--- net/xfrm/xfrm_interface_core.c | 54 +++++++++++++++++++++++++++++++--- 1 file changed, 50 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 ,
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 3/6 xfrm: add missed call to delete offloaded policies
In reply to Steffen KlassertFrom: Leon Romanovsky <[email protected]>
Offloaded policies are deleted through two flows: netdev is going down and policy flush.
In both cases, the code lacks relevant call to delete offloaded policy.
--- net/xfrm/xfrm_policy.c | 2 ++ 1 file changed, 2 insertions(+)
net/xfrm/xfrm_policy.c +2 -0
@@ -1831,6 +1831,7 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)__xfrm_policy_unlink (pol, dir);spin_unlock_bh (&net->xfrm.xfrm_policy_lock); +xfrm_dev_policy_delete (pol); cnt++;xfrm_audit_policy_delete (pol,1 , task_valid);xfrm_policy_kill (pol);
@@ -1869,6 +1870,7 @@ int xfrm_dev_policy_flush(struct net *net, struct net_device *dev,__xfrm_policy_unlink (pol, dir);spin_unlock_bh (&net->xfrm.xfrm_policy_lock); +xfrm_dev_policy_delete (pol); cnt++;xfrm_audit_policy_delete (pol,1 , task_valid);xfrm_policy_kill (pol);
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 4/6 xfrm: fix inbound ipv4/udp/esp packets to UDPv6 dualstack sockets
In reply to Steffen KlassertFrom: Maciej Żenczykowski <[email protected]>
Before Linux v5.8 an AF_INET6 SOCK_DGRAM (udp/udplite) socket with SOL_UDP, UDP_ENCAP, UDP_ENCAP_ESPINUDP{,_NON_IKE} enabled would just unconditionally use xfrm4_udp_encap_rcv(), afterwards such a socket would use the newly added xfrm6_udp_encap_rcv() which only handles IPv6 packets.
--- net/ipv4/xfrm4_input.c | 1 + net/ipv6/xfrm6_input.c | 3 +++ 2 files changed, 4 insertions(+)
net/ipv4/xfrm4_input.c +1 -0
@@ -164,6 +164,7 @@ int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)kfree_skb (skb);return 0 ; } +EXPORT_SYMBOL (xfrm4_udp_encap_rcv);int xfrm4_rcv (struct sk_buff *skb) {
net/ipv6/xfrm6_input.c +3 -0
@@ -86,6 +86,9 @@ int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)__be32 *udpdata32;__u16 encap_type = up->encap_type; +if (skb->protocol ==htons (ETH_P_IP )) +return xfrm4_udp_encap_rcv (sk, skb); +/* if this is not encapsulated socket, then just return now */ if (!encap_type)return 1 ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 5/6 xfrm: Use xfrm_state selector for BEET input
In reply to Steffen KlassertFrom: Herbert Xu <[email protected]>
For BEET the inner address and therefore family is stored in the xfrm_state selector. Use that when decapsulating an input packet instead of incorrectly relying on a non-existent tunnel protocol.
--- net/xfrm/xfrm_input.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
net/xfrm/xfrm_input.c +3 -4
@@ -331,11 +331,10 @@ xfrm_inner_mode_encap_remove(struct xfrm_state *x, {switch (x->props.mode) {case XFRM_MODE_BEET : -switch (XFRM_MODE_SKB_CB (skb)->protocol) { -case IPPROTO_IPIP : -case IPPROTO_BEETPH : +switch (x->sel.family) { +case AF_INET :return xfrm4_remove_beet_encap (x, skb); -case IPPROTO_IPV6 : +case AF_INET6 :return xfrm6_remove_beet_encap (x, skb); }break ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 6/6 xfrm: Linearize the skb after offloading if needed.
In reply to Steffen KlassertFrom: Sebastian Andrzej Siewior <[email protected]>
With offloading enabled, esp_xmit() gets invoked very late, from within validate_xmit_xfrm() which is after validate_xmit_skb() validates and linearizes the skb if the underlying device does not support fragments.
esp_output_tail() may add a fragment to the skb while adding the auth tag/ IV. Devices without the proper support will then send skb->data points to with the correct length so the packet will have garbage at the end. A pcap sniffer will claim that the proper data has been sent since it parses the skb properly.
It is not affected with INET_ESP_OFFLOAD disabled.
Linearize the skb after offloading if the sending hardware requires it. It was tested on v4, v6 has been adopted.
--- net/ipv4/esp4_offload.c | 3 +++ net/ipv6/esp6_offload.c | 3 +++ 2 files changed, 6 insertions(+)
net/ipv4/esp4_offload.c +3 -0
@@ -340,6 +340,9 @@ static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_secpath_reset (skb); +if (skb_needs_linearize (skb, skb->dev->features) && +__skb_linearize (skb)) +return -ENOMEM ;return 0 ; }
net/ipv6/esp6_offload.c +3 -0
@@ -374,6 +374,9 @@ static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_featuressecpath_reset (skb); +if (skb_needs_linearize (skb, skb->dev->features) && +__skb_linearize (skb)) +return -ENOMEM ;return 0 ; }
Signature · 1 line
2.34.1