Skip to contentSkip to conversation

xfrm: refuse route optimization mode for ESP

PATCH New Kyuwon Cho 1 participant 3 messages Nov 25

Subsystems (3)
lore
Share & tools
lore

Kyuwon Cho

author
#0 lore raw

v2 cover xfrm: refuse route optimization mode for ESP

From: ChoKyuWon <[email protected]>

An ESP state in route optimization mode can be created, and its first packet hits the WARN_ON_ONCE()s in xfrm_state_mtu() and xfrm_inner_mode_input(). Patch 1 accepts the mode in verify_newsa_info() only for DSTOPTS and ROUTING, as in v1. Patch 2, as Sabrina suggested, also makes ah_init_state(), esp_init_state() and esp6_init_state() refuse the modes they do not support, like ah6_init_state() already does.

To Sabrina's questions: XFRM_MODE_IN_TRIGGER is already refused by verify_newsa_info() ("Unsupported mode"). AH or ESP over IPv4 cannot get route optimization mode at all: xfrm4_mode_map has no entry for it, so __xfrm_init_state() fails with -EPROTONOSUPPORT before ah_init_state() runs. So only ESP over IPv6 reaches these WARNs, and the IPv4 part of patch 2 is for consistency.

The issue was found by an LLM-guided test generator that we are developing as a research prototype.

v2:
- Patch 1: say that esp6_init_state() accepts any mode instead of "treats" an unknown mode as transport (Sabrina).
- Patch 2 is new (Sabrina, Steffen).
v1: https://lore.kernel.org/netdev/[email protected]/

The patches were written with LLM assistance; we reviewed and tested them on the ipsec tree. ESP over IPv6 in route optimization mode is refused with -EINVAL with patch 1, with patch 2 alone, and with both, and the reproducer no longer warns. ESP and AH in transport, tunnel and BEET mode over IPv4 and IPv6, and DSTOPTS and ROUTING states in route optimization mode, are still accepted.

Kyuwon Cho (2):
xfrm: allow route optimization mode only for DSTOPTS and ROUTING
states
xfrm: refuse modes that AH and ESP do not support

 net/ipv4/ah4.c       | 11 ++++++++++-
 net/ipv4/esp4.c      | 18 +++++++++++++++---
 net/ipv6/esp6.c      |  6 +++++-
 net/xfrm/xfrm_user.c |  9 ++++++++-
 4 files changed, 38 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 xfrm: allow route optimization mode only for DSTOPTS and ROUTING states

verify_newsa_info() accepts XFRM_MODE_ROUTEOPTIMIZATION with any protocol. esp6_init_state() accepts any mode and only adjusts header_len for the ones it knows, so an ESP state in that mode is created, and the first packet through it hits the WARN_ON_ONCE()s in xfrm_state_mtu() and xfrm_inner_mode_input(), which assume that route optimization never comes with ESP:

  WARNING: net/xfrm/xfrm_state.c:3264 at xfrm_state_mtu+0x239/0x3d0
  RIP: 0010:xfrm_state_mtu (net/xfrm/xfrm_state.c:3264)
  Call Trace:
   <TASK>
   xfrm_bundle_create (net/xfrm/xfrm_policy.c:3992 net/xfrm/xfrm_policy.c:2785)
   xfrm_resolve_and_create_bundle (net/xfrm/xfrm_policy.c:2885)
   xfrm_lookup_with_ifid (net/xfrm/xfrm_policy.c:3114 net/xfrm/xfrm_policy.c:3245)
   xfrm_lookup_route (net/xfrm/xfrm_policy.c:3344 net/xfrm/xfrm_policy.c:3355)
   ip6_dst_lookup_flow (net/ipv6/ip6_output.c:1299)
   udpv6_sendmsg (net/ipv6/udp.c:1699)

  WARNING: net/xfrm/xfrm_input.c:449 at xfrm_input+0x2176/0x5f50
  RIP: 0010:xfrm_input (net/xfrm/xfrm_input.c:449 net/xfrm/xfrm_input.c:705)
  Call Trace:
   <IRQ>
   xfrm6_esp_rcv (net/ipv6/xfrm6_protocol.c:119)
   ip6_protocol_deliver_rcu (net/ipv6/ip6_input.c:479)

Accept the mode only for the Mobile IPv6 protocols, as IP-TFS mode is already limited to ESP.

Fixes c2d305e51038 ("xfrm: remove input indirection from xfrm_mode")
Fixes c7b37c769d2a ("xfrm: remove get_mtu indirection from xfrm_type")
---
 net/xfrm/xfrm_user.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/xfrm/xfrm_user.c +8 -1
index 217f65faa2bd..c4bbfd638d73 100644
@@ -477,9 +477,16 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
 	switch (p->mode) {
 	case XFRM_MODE_TRANSPORT:
 	case XFRM_MODE_TUNNEL:
-	case XFRM_MODE_ROUTEOPTIMIZATION:
 	case XFRM_MODE_BEET:
 		break;
+	case XFRM_MODE_ROUTEOPTIMIZATION:
+		if (p->id.proto != IPPROTO_DSTOPTS &&
+		    p->id.proto != IPPROTO_ROUTING) {
+			NL_SET_ERR_MSG(extack,
+				       "Route optimization mode requires DSTOPTS or ROUTING");
+			goto out;
+		}
+		break;
 	case XFRM_MODE_IPTFS:
 		if (p->id.proto != IPPROTO_ESP) {
 			NL_SET_ERR_MSG(extack, "IP-TFS mode only supported with ESP");
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/2 xfrm: refuse modes that AH and ESP do not support

In reply to Kyuwon Cho

ah6_init_state() refuses any mode but transport, tunnel and BEET, but ah_init_state(), esp_init_state() and esp6_init_state() accept any mode and only adjust header_len for the ones they know. For ESP over IPv6, a state in route optimization mode then reaches the WARN_ON_ONCE()s for unknown modes in xfrm_state_mtu() and xfrm_inner_mode_input(), as long as verify_newsa_info() lets the mode through. Over IPv4 there is no route optimization mode, and __xfrm_init_state() refuses it before the type is set up.

Make these functions refuse the modes they do not support, as ah6_init_state() does, so that each type checks its own modes. ESP keeps IP-TFS.

Fixes c2d305e51038 ("xfrm: remove input indirection from xfrm_mode")
Fixes c7b37c769d2a ("xfrm: remove get_mtu indirection from xfrm_type")
---
 net/ipv4/ah4.c  | 11 ++++++++++-
 net/ipv4/esp4.c | 18 +++++++++++++++---
 net/ipv6/esp6.c |  6 +++++-
 3 files changed, 30 insertions(+), 5 deletions(-)
net/ipv4/ah4.c +10 -1
index 6fd642d2278d..32094169718c 100644
@@ -535,8 +535,17 @@ static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
 	else
 		x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
 						  ahp->icv_trunc_len);
-	if (x->props.mode == XFRM_MODE_TUNNEL)
+	switch (x->props.mode) {
+	case XFRM_MODE_BEET:
+	case XFRM_MODE_TRANSPORT:
+		break;
+	case XFRM_MODE_TUNNEL:
 		x->props.header_len += sizeof(struct iphdr);
+		break;
+	default:
+		NL_SET_ERR_MSG(extack, "Invalid mode requested for AH");
+		goto error;
+	}
 	x->data = ahp;
 
 	return 0;
net/ipv4/esp4.c +15 -3
index 111bea0bd260..1bb7c4b24cc6 100644
@@ -1148,10 +1148,22 @@ static int esp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
 
 	x->props.header_len = sizeof(struct ip_esp_hdr) +
 			      crypto_aead_ivsize(aead);
-	if (x->props.mode == XFRM_MODE_TUNNEL)
+	switch (x->props.mode) {
+	case XFRM_MODE_BEET:
+		if (x->sel.family != AF_INET6)
+			x->props.header_len += IPV4_BEET_PHMAXLEN;
+		break;
+	case XFRM_MODE_TRANSPORT:
+	case XFRM_MODE_IPTFS:
+		break;
+	case XFRM_MODE_TUNNEL:
 		x->props.header_len += sizeof(struct iphdr);
-	else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
-		x->props.header_len += IPV4_BEET_PHMAXLEN;
+		break;
+	default:
+		NL_SET_ERR_MSG(extack, "Invalid mode requested for ESP");
+		err = -EINVAL;
+		goto error;
+	}
 	if (x->encap) {
 		struct xfrm_encap_tmpl *encap = x->encap;
 
net/ipv6/esp6.c +5 -1
index b287c807b018..ced910d44b47 100644
@@ -1192,12 +1192,16 @@ static int esp6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
 			x->props.header_len += IPV4_BEET_PHMAXLEN +
 					       (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
 		break;
-	default:
 	case XFRM_MODE_TRANSPORT:
+	case XFRM_MODE_IPTFS:
 		break;
 	case XFRM_MODE_TUNNEL:
 		x->props.header_len += sizeof(struct ipv6hdr);
 		break;
+	default:
+		NL_SET_ERR_MSG(extack, "Invalid mode requested for ESP");
+		err = -EINVAL;
+		goto error;
 	}
 
 	if (x->encap) {
Signature · 1 line
2.43.0