Netdev List
 help / color / mirror / Atom feed
* [PATCH ipsec v2 0/2] xfrm: refuse route optimization mode for ESP
@ 2026-11-25  2:37 Kyuwon Cho
  2026-11-25  2:42 ` [PATCH ipsec v2 1/2] xfrm: allow route optimization mode only for DSTOPTS and ROUTING states Kyuwon Cho
  2026-11-25  2:42 ` [PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support Kyuwon Cho
  0 siblings, 2 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-11-25  2:37 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Florian Westphal, Sabrina Dubroca, Hui Peng, netdev,
	ipsec, linux-kernel

From: ChoKyuWon <kyuwoncho18@gmail.com>

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/179550330841.535592.4607028415857424638@gmail.com/

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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH ipsec v2 1/2] xfrm: allow route optimization mode only for DSTOPTS and ROUTING states
  2026-11-25  2:37 [PATCH ipsec v2 0/2] xfrm: refuse route optimization mode for ESP Kyuwon Cho
@ 2026-11-25  2:42 ` Kyuwon Cho
  2026-11-25  2:42 ` [PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support Kyuwon Cho
  1 sibling, 0 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-11-25  2:42 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Florian Westphal, Sabrina Dubroca, Hui Peng, netdev,
	ipsec, linux-kernel

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")
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
 net/xfrm/xfrm_user.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 217f65faa2bd..c4bbfd638d73 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -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");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support
  2026-11-25  2:37 [PATCH ipsec v2 0/2] xfrm: refuse route optimization mode for ESP Kyuwon Cho
  2026-11-25  2:42 ` [PATCH ipsec v2 1/2] xfrm: allow route optimization mode only for DSTOPTS and ROUTING states Kyuwon Cho
@ 2026-11-25  2:42 ` Kyuwon Cho
  1 sibling, 0 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-11-25  2:42 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Florian Westphal, Sabrina Dubroca, Hui Peng, netdev,
	ipsec, linux-kernel

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")
Suggested-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
 net/ipv4/ah4.c  | 11 ++++++++++-
 net/ipv4/esp4.c | 18 +++++++++++++++---
 net/ipv6/esp6.c |  6 +++++-
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
index 6fd642d2278d..32094169718c 100644
--- a/net/ipv4/ah4.c
+++ b/net/ipv4/ah4.c
@@ -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;
diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c
index 111bea0bd260..1bb7c4b24cc6 100644
--- a/net/ipv4/esp4.c
+++ b/net/ipv4/esp4.c
@@ -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;
 
diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c
index b287c807b018..ced910d44b47 100644
--- a/net/ipv6/esp6.c
+++ b/net/ipv6/esp6.c
@@ -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) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  2:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  2:37 [PATCH ipsec v2 0/2] xfrm: refuse route optimization mode for ESP Kyuwon Cho
2026-11-25  2:42 ` [PATCH ipsec v2 1/2] xfrm: allow route optimization mode only for DSTOPTS and ROUTING states Kyuwon Cho
2026-11-25  2:42 ` [PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support Kyuwon Cho

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox