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: 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: 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 --- 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 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 Signed-off-by: Kyuwon Cho --- 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