xfrm: clean up xfrm protocol checks
Subsystems (3)
PATCH v2 1/1 xfrm: clean up xfrm protocol checks
In commit 6a53b7593233 ("xfrm: check id proto in validate_tmpl()") I introduced a check for xfrm protocol, but according to Herbert IPSEC_PROTO_ANY should only be used as a wildcard for lookup, so it should be removed from validate_tmpl().
And, IPSEC_PROTO_ANY is expected to only match 3 IPSec-specific protocols, this is why xfrm_state_flush() could still miss IPPROTO_ROUTING, which leads that those entries are left in net->xfrm.state_all before exit net. Fix this by replacing IPSEC_PROTO_ANY with zero.
This patch also extracts the check from validate_tmpl() to xfrm_id_proto_valid() and uses it in parse_ipsecrequest(). With this, no other protocols should be added into xfrm.
--- include/net/xfrm.h | 17 +++++++++++++++++ net/ipv6/xfrm6_tunnel.c | 2 +- net/key/af_key.c | 4 +++- net/xfrm/xfrm_state.c | 2 +- net/xfrm/xfrm_user.c | 14 +------------- 5 files changed, 23 insertions(+), 16 deletions(-)
include/net/xfrm.h +17 -0
@@ -1404,6 +1404,23 @@ static inline int xfrm_state_kern(const struct xfrm_state *x)return atomic_read (&x->tunnel_users); } +static inline bool xfrm_id_proto_valid (u8 proto) +{ +switch (proto) { +case IPPROTO_AH : +case IPPROTO_ESP : +case IPPROTO_COMP : +#if IS_ENABLED (CONFIG_IPV6 ) +case IPPROTO_ROUTING : +case IPPROTO_DSTOPTS : +#endif +return true; +default : +return false; + } +} + +/* IPSEC_PROTO_ANY only matches 3 IPsec protocols, 0 could match all. */ static inline int xfrm_id_proto_match (u8 proto,u8 userproto) { return (!userproto || proto == userproto ||
net/ipv6/xfrm6_tunnel.c +1 -1
@@ -345,7 +345,7 @@ static void __net_exit xfrm6_tunnel_net_exit(struct net *net)unsigned int i;xfrm_flush_gc (); -xfrm_state_flush (net,IPSEC_PROTO_ANY , false, true); +xfrm_state_flush (net,0 , false, true);for (i =0 ; i <XFRM6_TUNNEL_SPI_BYADDR_HSIZE ; i++)WARN_ON_ONCE (!hlist_empty (&xfrm6_tn->spi_byaddr[i]));
net/key/af_key.c +3 -1
@@ -1951,8 +1951,10 @@ parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)if (rq->sadb_x_ipsecrequest_mode ==0 )return -EINVAL ; +if (!xfrm_id_proto_valid (rq->sadb_x_ipsecrequest_proto)) +return -EINVAL ; - t->id.proto = rq->sadb_x_ipsecrequest_proto;/* XXX check proto */ + t->id.proto = rq->sadb_x_ipsecrequest_proto;if ((mode =pfkey_mode_to_xfrm (rq->sadb_x_ipsecrequest_mode)) <0 )return -EINVAL ; t->mode = mode;
net/xfrm/xfrm_state.c +1 -1
@@ -2384,7 +2384,7 @@ void xfrm_state_fini(struct net *net)flush_work (&net->xfrm.state_hash_work);flush_work (&xfrm_state_gc_work); -xfrm_state_flush (net,IPSEC_PROTO_ANY , false, true); +xfrm_state_flush (net,0 , false, true);WARN_ON (!list_empty (&net->xfrm.state_all));
net/xfrm/xfrm_user.c +1 -13
@@ -1513,20 +1513,8 @@ static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)return -EINVAL ; } -switch (ut[i].id.proto) { -case IPPROTO_AH : -case IPPROTO_ESP : -case IPPROTO_COMP : -#if IS_ENABLED (CONFIG_IPV6 ) -case IPPROTO_ROUTING : -case IPPROTO_DSTOPTS : -#endif -case IPSEC_PROTO_ANY : -break ; -default : +if (!xfrm_id_proto_valid (ut[i].id.proto))return -EINVAL ; - } - }return 0 ;
Signature · 1 line
2.20.1
Quoted 20 lines · Cong Wang
In commit
6a53b7593233("xfrm: check id proto invalidate_tmpl()") I introduced a check for xfrm protocol, but according to HerbertIPSEC_PROTO_ANYshould only be used as a wildcard for lookup, so it should be removed fromvalidate_tmpl().And,
IPSEC_PROTO_ANYis expected to only match 3 IPSec-specific protocols, this is whyxfrm_state_flush()could still missIPPROTO_ROUTING, which leads that those entries are left innet->xfrm.state_allbefore exit net. Fix this by replacingIPSEC_PROTO_ANYwith zero.This patch also extracts the check from
validate_tmpl()toxfrm_id_proto_valid()and uses it inparse_ipsecrequest(). With this, no other protocols should be added into xfrm.Fixes 6a53b7593233 ("xfrm: check id proto in validate_tmpl()")Reported-by [email protected]Cc Steffen Klassert <[email protected]>Cc Herbert Xu <[email protected]>Signed-off-by Cong Wang <[email protected]>
Quoted 7 lines
--- include/net/xfrm.h | 17 +++++++++++++++++ net/ipv6/xfrm6_tunnel.c | 2 +- net/key/af_key.c | 4 +++- net/xfrm/xfrm_state.c | 2 +- net/xfrm/xfrm_user.c | 14 +------------- 5 files changed, 23 insertions(+), 16 deletions(-)
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Steffen Klassert
maintainerQuoted 29 lines · Herbert Xu
Quoted 20 lines · Cong Wang
In commit
6a53b7593233("xfrm: check id proto invalidate_tmpl()") I introduced a check for xfrm protocol, but according to HerbertIPSEC_PROTO_ANYshould only be used as a wildcard for lookup, so it should be removed fromvalidate_tmpl().And,
IPSEC_PROTO_ANYis expected to only match 3 IPSec-specific protocols, this is whyxfrm_state_flush()could still missIPPROTO_ROUTING, which leads that those entries are left innet->xfrm.state_allbefore exit net. Fix this by replacingIPSEC_PROTO_ANYwith zero.This patch also extracts the check from
validate_tmpl()toxfrm_id_proto_valid()and uses it inparse_ipsecrequest(). With this, no other protocols should be added into xfrm.Fixes 6a53b7593233 ("xfrm: check id proto in validate_tmpl()")Reported-by [email protected]Cc Steffen Klassert <[email protected]>Cc Herbert Xu <[email protected]>Signed-off-by Cong Wang <[email protected]>Quoted 7 lines
--- include/net/xfrm.h | 17 +++++++++++++++++ net/ipv6/xfrm6_tunnel.c | 2 +- net/key/af_key.c | 4 +++- net/xfrm/xfrm_state.c | 2 +- net/xfrm/xfrm_user.c | 14 +------------- 5 files changed, 23 insertions(+), 16 deletions(-)
Quoted 2 lines
Acked-by Herbert Xu <[email protected]>
Patch applied, thanks everyone!