Skip to contentSkip to conversation

Mitigate a side channel in routing exception caches

PATCH Superseded Ido Schimmel 2 participants 8 messages Aug 26 – Aug 27

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ido Schimmel

author maintainer
#0 lore raw

cover Mitigate a side channel in routing exception caches

When an ICMP error that quotes a UDP packet is locally delivered, the kernel only creates a routing exception if the quoted packet matches a socket. This allows an off-path attacker to conduct a side-channel attack on the routing exception caches in order to discover the ephemeral ports used by connected UDP sockets.

Previous mitigations tried to make it harder for attackers to find hash collisions in these caches and make the eviction of exceptions less predictable. Amit Klein and Noam Caspi demonstrated that both of these mitigations can be bypassed.

This patchset tries to mitigate such attacks by always creating an exception, even if UDP socket matching failed. The exception is created by the same helpers that are used when the quoted packet did not originate from a socket, so that neither the occupancy of the cache nor the contents of the exception are indicative of the success or failure of UDP socket matching.

Note that this mitigation does not make it easier for attackers to fill these caches, since they can already create exceptions with little to no validation. For example, by sending an ICMP error that quotes an ICMP Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Patch #1 fixes a pre-existing bug in the handling of ICMPv6 Redirect Message packets. Discovered while writing the selftest.

Patch #2 creates an exception from the IPv4 UDP code when socket matching failed. Other socket types do not need this: raw sockets have no ports, and for TCP the ICMP error is discarded unless the quoted sequence number is in window.

Patch #3 does the same for IPv6.

Patch #4 adds a selftest.

Ido Schimmel (4):
ipv6: Fix redirect exception creation for UDP/RAW sockets
ipv4: udp: Create exceptions when socket matching failed
ipv6: udp: Create exceptions when socket matching failed
selftests: net: Add exception cache tests

 net/ipv4/route.c                              |   8 +-
 net/ipv4/udp.c                                |  10 +
 net/ipv6/route.c                              |   2 +-
 net/ipv6/udp.c                                |  17 +-
 tools/testing/selftests/net/Makefile          |   1 +
 .../testing/selftests/net/exception_cache.sh  | 501 ++++++++++++++++++
 6 files changed, 534 insertions(+), 5 deletions(-)
 create mode 100755 tools/testing/selftests/net/exception_cache.sh
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/4 ipv6: Fix redirect exception creation for UDP/RAW sockets

When an ICMP Redirect Message is matched to a socket, both IPv4 and IPv6 verify that the source IP of the ICMP packet is the current gateway for the quoted packet. Both also pass the socket's bound device as the expected nexthop device.

The difference is that IPv4 treats "oif=0" as "any", whereas IPv6 always requires an exact match (see ip6_redirect_nh_match()), since the gateway address is usually a link-local address.

Therefore, when an IPv6 UDP/RAW socket is not bound to a device, the above verification fails and an exception is not created. This also happens when the socket is bound to a VRF, as l3mdev_update_flow() resets the oif to 0.

Fix this by passing the ifindex of the ingress device as the expected nexthop device. This is consistent with the existing callers of ip6_redirect(). Note that for ICMPv6 Redirect Message packets the VRF driver does not reset skb->dev to the VRF device, so skb->dev is always correct, even when it is a VRF port.

Fixes b55b76b22144 ("ipv6:introduce function to find route for redirect")
---
 net/ipv6/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/route.c +1 -1
index 6a40c5074543..9658939511e0 100644
@@ -3255,7 +3255,7 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
 
 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
 {
-	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
+	ip6_redirect(skb, sock_net(sk), skb->dev->ifindex,
 		     READ_ONCE(sk->sk_mark), sk_uid(sk));
 }
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 2/4 ipv4: udp: Create exceptions when socket matching failed

In reply to Ido Schimmel

Currently, when ICMP Fragmentation Needed and Redirect Message packets are locally delivered and quote a UDP packet, a FIB nexthop exception (FNHE) is created only if the kernel can match the UDP packet to an existing socket.

This behavior allows off-path attackers to conduct a side-channel attack on the FNHE cache in order to discover the ephemeral port used by a connected UDP socket.

Commit 6457378fe796 ("ipv4: use siphash instead of Jenkins in fnhe_hashfun()") and commit 67d6d681e15b ("ipv4: make exception cache less predictible") tried to mitigate such attacks by making it harder for attackers to discover hash collisions in the FNHE cache and by randomizing the number of exceptions a hash bucket can hold, respectively. Unfortunately, both of the mitigations can be bypassed.

Instead, mitigate such attacks by always creating a FNHE, even if socket matching failed. Do that by calling ipv4_update_pmtu() and ipv4_redirect(), the helpers used when the quoted packet did not originate from a socket. The resulting FNHE is indistinguishable from the one created when socket matching succeeded, both in terms of cache occupancy and in terms of its contents.

Pass an oif of 0, in a similar fashion to icmp_err().

Note that this does not allow attackers to create FNHEs that they could not create before, as both helpers can already be reached with little to no validation. For example, by sending an ICMP error that quotes an ICMP Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Also create a FNHE when a socket does not wish to accept PMTU updates (e.g., by setting 'IP_PMTUDISC_OMIT'). Otherwise, the fact that a FNHE was not created can indicate to an off-path attacker that a socket exists. This applies to all ipv4_sk_update_pmtu() callers, so ping and raw sockets that decline PMTU updates will now create a FNHE as well. Such sockets are not affected by it, as ip_skb_dst_mtu() uses the MTU of the device for them.

Fixes 4895c771c7f0 ("ipv4: Add FIB nexthop exceptions.")
---
 net/ipv4/route.c |  8 +++++---
 net/ipv4/udp.c   | 10 ++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
net/ipv4/route.c +5 -3
index 37674d76f90f..692298078700 100644
@@ -1157,10 +1157,12 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
 	bool new = false;
 	struct net *net = sock_net(sk);
 
-	bh_lock_sock(sk);
+	if (!ip_sk_accept_pmtu(sk)) {
+		ipv4_update_pmtu(skb, net, mtu, 0, sk->sk_protocol);
+		return;
+	}
 
-	if (!ip_sk_accept_pmtu(sk))
-		goto out;
+	bh_lock_sock(sk);
 
 	odst = sk_dst_get(sk);
 
net/ipv4/udp.c +10 -0
index af9603217444..6981526bd59c 100644
@@ -900,6 +900,15 @@ static struct sock *__udp4_lib_err_encap(struct net *net,
 	return sk;
 }
 
+static void udp_err_no_sk(struct net *net, struct sk_buff *skb, int type,
+			  int code, u32 info)
+{
+	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
+		ipv4_update_pmtu(skb, net, info, 0, IPPROTO_UDP);
+	else if (type == ICMP_REDIRECT)
+		ipv4_redirect(skb, net, 0, IPPROTO_UDP);
+}
+
 /*
  * This routine is called by the ICMP module when it gets some
  * sort of error condition.  If err < 0 then the socket should
@@ -938,6 +947,7 @@ int udp_err(struct sk_buff *skb, u32 info)
 			sk = ERR_PTR(-ENOENT);
 
 		if (IS_ERR(sk)) {
+			udp_err_no_sk(net, skb, type, code, info);
 			__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
 			return PTR_ERR(sk);
 		}
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 3/4 ipv6: udp: Create exceptions when socket matching failed

In reply to Ido Schimmel

Currently, when ICMPv6 Packet Too Big and Redirect Message packets are locally delivered and quote a UDP packet, an exception is created in the IPv6 exception cache only if the kernel can match the UDP packet to an existing socket.

This behavior allows off-path attackers to conduct a side-channel attack on the exception cache in order to discover the ephemeral port used by a connected UDP socket.

Commit 4785305c05b2 ("ipv6: use siphash in rt6_exception_hash()") and commit a00df2caffed ("ipv6: make exception cache less predictible") tried to mitigate such attacks by making it harder for attackers to discover hash collisions in the exception cache and by randomizing the number of exceptions a hash bucket can hold, respectively. Unfortunately, both of the mitigations can be bypassed.

Instead, mitigate such attacks by always creating an exception, even if socket matching failed. Do that by calling ip6_update_pmtu() and ip6_redirect(), the helpers used when the quoted packet did not originate from a socket. The resulting exception is indistinguishable from the one created when socket matching succeeded, both in terms of cache occupancy and in terms of its contents.

Pass the ifindex of the ingress device and the default uid, in a similar fashion to icmpv6_err(). Unlike IPv4, an oif of 0 would not match any nexthop in ip6_redirect_nh_match() and no exception would be created in response to a Redirect Message.

Note that this does not allow attackers to create exceptions that they could not create before, as both helpers can already be reached with little to no validation. For example, by sending an ICMPv6 error that quotes an ICMPv6 Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Also create an exception when a socket does not wish to accept PMTU updates (e.g., by setting 'IPV6_PMTUDISC_OMIT'). Otherwise, the fact that an exception was not created can indicate to an off-path attacker that a socket exists. Unlike IPv4, the check is performed in udpv6_err() and not in ip6_sk_update_pmtu(), as its only other caller, rawv6_err(), does not consult ip6_sk_accept_pmtu() and therefore already creates an exception unconditionally.

Fixes 2b760fcf5cfb ("ipv6: hook up exception table to store dst cache")
---
 net/ipv6/udp.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
net/ipv6/udp.c +16 -1
index fd875908ac0c..df14fc2afe8a 100644
@@ -690,6 +690,17 @@ static struct sock *__udp6_lib_err_encap(struct net *net,
 	return sk;
 }
 
+static void udpv6_err_no_sk(struct net *net, struct sk_buff *skb, u8 type,
+			    __be32 info)
+{
+	if (type == ICMPV6_PKT_TOOBIG)
+		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0,
+				sock_net_uid(net, NULL));
+	else if (type == NDISC_REDIRECT)
+		ip6_redirect(skb, net, skb->dev->ifindex, 0,
+			     sock_net_uid(net, NULL));
+}
+
 static int udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 		     u8 type, u8 code, int offset, __be32 info)
 {
@@ -719,6 +730,7 @@ static int udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 			sk = ERR_PTR(-ENOENT);
 
 		if (IS_ERR(sk)) {
+			udpv6_err_no_sk(net, skb, type, info);
 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
 					  ICMP6_MIB_INERRORS);
 			return PTR_ERR(sk);
@@ -731,8 +743,11 @@ static int udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
 	np = inet6_sk(sk);
 
 	if (type == ICMPV6_PKT_TOOBIG) {
-		if (!ip6_sk_accept_pmtu(sk))
+		if (!ip6_sk_accept_pmtu(sk)) {
+			ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0,
+					sock_net_uid(net, NULL));
 			goto out;
+		}
 		ip6_sk_update_pmtu(skb, sk, info);
 		if (READ_ONCE(np->pmtudisc) != IPV6_PMTUDISC_DONT)
 			harderr = 1;
Signature · 1 line
2.55.0
author maintainer
#7 lore raw

PATCH 4/4 selftests: net: Add exception cache tests

In reply to Ido Schimmel

Add a test for the IPv4 and IPv6 exception caches, covering the exceptions that are created in response to ICMP errors quoting a UDP packet.

The topology consists of a host (h1) that reaches a remote host (h2) via a router (r1), with a second router (r2) attached to the segment shared by h1 and r1. UDP packets are injected using a packet socket, so that an ICMP error quoting them is only matched to a socket when one was opened separately with the same source port. PMTU errors are provoked by lowering the MTU of the far end of the path and redirects by pointing r1's route towards h2 back over the segment it received the packet from.

The following is tested for both address families and for both PMTU and redirect exceptions:

* An error that is not matched to a socket creates an exception that carries the new MTU or gateway.
* An error that is matched to a socket creates the same exception.

The PMTU tests further verify that a lower PMTU replaces the one stored in the exception whereas a higher one does not, and that a socket which disabled PMTU discovery using IP{,V6}_PMTUDISC_OMIT gets the same exception as the other cases.

Without "ipv4: udp: Create exceptions when socket matching failed" and "ipv6: udp: Create exceptions when socket matching failed", the tests that do not open a socket fail:

 # ./exception_cache.sh
 TEST: IPv4: PMTU: exception without a matching socket               [FAIL]
         No socket: exception does not carry an MTU of 1400
 TEST: IPv6: PMTU: exception without a matching socket               [FAIL]
         No socket: exception does not carry an MTU of 1400
 TEST: IPv4: PMTU: exception with a matching socket                  [ OK ]
 TEST: IPv6: PMTU: exception with a matching socket                  [ OK ]
 TEST: IPv4: PMTU: exception with a socket ignoring it               [FAIL]
         PMTU discovery disabled: exception does not carry an MTU of 1400
 TEST: IPv6: PMTU: exception with a socket ignoring it               [FAIL]
         PMTU discovery disabled: exception does not carry an MTU of 1400
 TEST: IPv4: Redirect: exception without a matching socket           [FAIL]
         No socket: exception does not carry the new gateway
 TEST: IPv6: Redirect: exception without a matching socket           [FAIL]
         No socket: exception does not carry the new gateway
 TEST: IPv4: Redirect: exception with a matching socket              [ OK ]
 TEST: IPv6: Redirect: exception with a matching socket              [ OK ]
Assisted-by Claude-Code:claude-opus-5
---
 tools/testing/selftests/net/Makefile          |   1 +
 .../testing/selftests/net/exception_cache.sh  | 501 ++++++++++++++++++
 2 files changed, 502 insertions(+)
 create mode 100755 tools/testing/selftests/net/exception_cache.sh
tools/testing/selftests/net/Makefile +1 -0
index 0f5c178bc224..517c09d60bef 100644
@@ -28,6 +28,7 @@ TEST_PROGS := \
 	double_udp_encap.sh \
 	drop_monitor_tests.sh \
 	ecmp_rehash.sh \
+	exception_cache.sh \
 	fcnal-ipv4.sh \
 	fcnal-ipv6.sh \
 	fcnal-other.sh \
tools/testing/selftests/net/exception_cache.sh new file +501 -0

501 lines not on this page · raw message

Signature · 1 line
2.55.0
Reviewed-by on v1 2/4
#3 lore raw

Re: [PATCH net 2/4] ipv4: udp: Create exceptions when socket matching failed

In reply to Ido Schimmel
Quoted 40 lines · Ido Schimmel

Currently, when ICMP Fragmentation Needed and Redirect Message packets are locally delivered and quote a UDP packet, a FIB nexthop exception (FNHE) is created only if the kernel can match the UDP packet to an existing socket.

This behavior allows off-path attackers to conduct a side-channel attack on the FNHE cache in order to discover the ephemeral port used by a connected UDP socket.

Commit 6457378fe796 ("ipv4: use siphash instead of Jenkins in fnhe_hashfun()") and commit 67d6d681e15b ("ipv4: make exception cache less predictible") tried to mitigate such attacks by making it harder for attackers to discover hash collisions in the FNHE cache and by randomizing the number of exceptions a hash bucket can hold, respectively. Unfortunately, both of the mitigations can be bypassed.

Instead, mitigate such attacks by always creating a FNHE, even if socket matching failed. Do that by calling ipv4_update_pmtu() and ipv4_redirect(), the helpers used when the quoted packet did not originate from a socket. The resulting FNHE is indistinguishable from the one created when socket matching succeeded, both in terms of cache occupancy and in terms of its contents.

Pass an oif of 0, in a similar fashion to icmp_err().

Note that this does not allow attackers to create FNHEs that they could not create before, as both helpers can already be reached with little to no validation. For example, by sending an ICMP error that quotes an ICMP Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Also create a FNHE when a socket does not wish to accept PMTU updates (e.g., by setting 'IP_PMTUDISC_OMIT'). Otherwise, the fact that a FNHE was not created can indicate to an off-path attacker that a socket exists. This applies to all ipv4_sk_update_pmtu() callers, so ping and raw sockets that decline PMTU updates will now create a FNHE as well. Such sockets are not affected by it, as ip_skb_dst_mtu() uses the MTU of the device for them.

Quoted 6 lines
Fixes 4895c771c7f0 ("ipv4: Add FIB nexthop exceptions.")
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH net 3/4] ipv6: udp: Create exceptions when socket matching failed

In reply to Ido Schimmel
Quoted 43 lines · Ido Schimmel

Currently, when ICMPv6 Packet Too Big and Redirect Message packets are locally delivered and quote a UDP packet, an exception is created in the IPv6 exception cache only if the kernel can match the UDP packet to an existing socket.

This behavior allows off-path attackers to conduct a side-channel attack on the exception cache in order to discover the ephemeral port used by a connected UDP socket.

Commit 4785305c05b2 ("ipv6: use siphash in rt6_exception_hash()") and commit a00df2caffed ("ipv6: make exception cache less predictible") tried to mitigate such attacks by making it harder for attackers to discover hash collisions in the exception cache and by randomizing the number of exceptions a hash bucket can hold, respectively. Unfortunately, both of the mitigations can be bypassed.

Instead, mitigate such attacks by always creating an exception, even if socket matching failed. Do that by calling ip6_update_pmtu() and ip6_redirect(), the helpers used when the quoted packet did not originate from a socket. The resulting exception is indistinguishable from the one created when socket matching succeeded, both in terms of cache occupancy and in terms of its contents.

Pass the ifindex of the ingress device and the default uid, in a similar fashion to icmpv6_err(). Unlike IPv4, an oif of 0 would not match any nexthop in ip6_redirect_nh_match() and no exception would be created in response to a Redirect Message.

Note that this does not allow attackers to create exceptions that they could not create before, as both helpers can already be reached with little to no validation. For example, by sending an ICMPv6 error that quotes an ICMPv6 Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Also create an exception when a socket does not wish to accept PMTU updates (e.g., by setting 'IPV6_PMTUDISC_OMIT'). Otherwise, the fact that an exception was not created can indicate to an off-path attacker that a socket exists. Unlike IPv4, the check is performed in udpv6_err() and not in ip6_sk_update_pmtu(), as its only other caller, rawv6_err(), does not consult ip6_sk_accept_pmtu() and therefore already creates an exception unconditionally.

Quoted 6 lines
Fixes 2b760fcf5cfb ("ipv6: hook up exception table to store dst cache")
author maintainer
#4 lore raw

Re: [PATCH net 2/4] ipv4: udp: Create exceptions when socket matching failed

In reply to Ido Schimmel
Quoted 31 lines · Ido Schimmel

Currently, when ICMP Fragmentation Needed and Redirect Message packets are locally delivered and quote a UDP packet, a FIB nexthop exception (FNHE) is created only if the kernel can match the UDP packet to an existing socket.

This behavior allows off-path attackers to conduct a side-channel attack on the FNHE cache in order to discover the ephemeral port used by a connected UDP socket.

Commit 6457378fe796 ("ipv4: use siphash instead of Jenkins in fnhe_hashfun()") and commit 67d6d681e15b ("ipv4: make exception cache less predictible") tried to mitigate such attacks by making it harder for attackers to discover hash collisions in the FNHE cache and by randomizing the number of exceptions a hash bucket can hold, respectively. Unfortunately, both of the mitigations can be bypassed.

Instead, mitigate such attacks by always creating a FNHE, even if socket matching failed. Do that by calling ipv4_update_pmtu() and ipv4_redirect(), the helpers used when the quoted packet did not originate from a socket. The resulting FNHE is indistinguishable from the one created when socket matching succeeded, both in terms of cache occupancy and in terms of its contents.

Pass an oif of 0, in a similar fashion to icmp_err().

Note that this does not allow attackers to create FNHEs that they could not create before, as both helpers can already be reached with little to no validation. For example, by sending an ICMP error that quotes an ICMP Echo Reply or one that quotes a UDP source port that matches a wildcard socket.

Quoted 7 lines

Also create a FNHE when a socket does not wish to accept PMTU updates (e.g., by setting 'IP_PMTUDISC_OMIT'). Otherwise, the fact that a FNHE was not created can indicate to an off-path attacker that a socket exists. This applies to all ipv4_sk_update_pmtu() callers, so ping and raw sockets that decline PMTU updates will now create a FNHE as well. Such sockets are not affected by it, as ip_skb_dst_mtu() uses the MTU of the device for them.

tl;dr - I will send v2 that calls udp_err_no_sk() unconditionally and remove the net/ipv4/route.c hunk. Same for patch 3. It means two route lookups in the good case (matched socket), but I think we can live with that.

Two comments from Sashiko [1]:

1. "Inverted signal". This is correct and it's something I considered, but it's impractical:

a. Without the patch, a wrong guess is cheap and an attacker can keep scanning. With the patch, each wrong guess requires the attacker to repeat the setup phase which brings the cache to a state where a wrong / correct guess is indicative of the presence of a socket. This is time-consuming and therefore impractical given that the sockets of interest are short lived.

b. It requires "non-default routing attributes" which is uncommon.

We can completely eliminate the "inverted signal" by calling udp_err_no_sk() unconditionally.

2. The comment regarding fou/gue seems valid, but it requires fou to be loaded which is again uncommon. Can be fixed by [2] or by simply calling udp_err_no_sk() unconditionally.

[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260826143735.1819315-1-idosch%40nvidia.com

[2]

net/ipv4/udp.c +3 -1
index 6981526bd59c..c32c416601da 100644
@@ -941,8 +941,10 @@ int udp_err(struct sk_buff *skb, u32 info)
 		/* No socket for error: try tunnels before discarding */
 		if (static_branch_unlikely(&udp_encap_needed_key)) {
 			sk = __udp4_lib_err_encap(net, iph, uh, sk, skb, info);
-			if (!sk)
+			if (!sk) {
+				udp_err_no_sk(net, skb, type, code, info);
 				return 0;
+			}
 		} else
 			sk = ERR_PTR(-ENOENT);