From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Steffen Klassert <steffen.klassert@secunet.com>,
Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, Florian Westphal <fw@strlen.de>,
Sabrina Dubroca <sd@queasysnail.net>,
Hui Peng <benquike@gmail.com>,
netdev@vger.kernel.org, ipsec@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support
Date: Wed, 25 Nov 2026 11:42:28 +0900 [thread overview]
Message-ID: <e5abf3005007d9bf3eab4b640d513856f8eacf6c.1795573672.git.kyuwoncho18@gmail.com> (raw)
In-Reply-To: <cover.1795573672.git.kyuwoncho18@gmail.com>
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
prev parent reply other threads:[~2026-11-25 2:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e5abf3005007d9bf3eab4b640d513856f8eacf6c.1795573672.git.kyuwoncho18@gmail.com \
--to=kyuwoncho18@gmail.com \
--cc=benquike@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fw@strlen.de \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=ipsec@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=steffen.klassert@secunet.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®