xfrm: unify xfrm protocol checks
Subsystems (3)
PATCH 1/1 xfrm: unify xfrm protocol checks
In commit 6a53b7593233 ("xfrm: check id proto in validate_tmpl()") I introduced a check for xfrm protocol, but unfortunately xfrm_id_proto_match() could still miss IPPROTO_ROUTING which causes entries left in net->xfrm.state_all.
This patch extracts the check from validate_tmpl() to xfrm_id_proto_valid() and uses it in all other places, including the missing one in parse_ipsecrequest().
--- include/net/xfrm.h | 21 ++++++++++++++++++--- net/key/af_key.c | 4 +++- net/xfrm/xfrm_user.c | 14 +------------- 3 files changed, 22 insertions(+), 17 deletions(-)
include/net/xfrm.h +18 -3
@@ -1404,12 +1404,27 @@ 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 +case IPSEC_PROTO_ANY : +return true; +default : +return false; + } +} +static inline int xfrm_id_proto_match (u8 proto,u8 userproto) {return (!userproto || proto == userproto || - (userproto ==IPSEC_PROTO_ANY && (proto ==IPPROTO_AH || - proto ==IPPROTO_ESP || - proto ==IPPROTO_COMP ))); + (userproto ==IPSEC_PROTO_ANY &&xfrm_id_proto_valid (proto))); }/*
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_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
Herbert Xu
maintainerQuoted 16 lines · Cong Wang
include/net/xfrm.h +15
-0+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 +case IPSEC_PROTO_ANY : +return true; +default : +return false; + }
include/net/xfrm.h static inline int xfrm_state_kern(const struct xfrm_state *x) · line 1428
Quoted 10 lines
include/net/xfrm.h +3
-3+} +static inline int xfrm_id_proto_match (u8 proto,u8 userproto) {return (!userproto || proto == userproto || - (userproto ==IPSEC_PROTO_ANY && (proto ==IPPROTO_AH || - proto ==IPPROTO_ESP || - proto ==IPPROTO_COMP ))); + (userproto ==IPSEC_PROTO_ANY &&xfrm_id_proto_valid (proto))); }
This does not look right. IPSEC_PROTO_ANY should only be allowed in userproto and your patch is going to let it pass when it's in proto. Whether IPPROTO_ROUTING/IPPROTO_DSTOPTS should be allowed in this context is also not obvious.
Cheers,
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
Quoted 25 lines · Herbert Xu
Quoted 23 lines · Cong Wang
include/net/xfrm.h +17
-2+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 +case IPSEC_PROTO_ANY : +return true; +default : +return false; + } +} +static inline int xfrm_id_proto_match (u8 proto,u8 userproto) { return (!userproto || proto == userproto || - (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH || - proto == IPPROTO_ESP ||
Quoted 8 lines
include/net/xfrm.h static inline int xfrm_state_kern(const struct xfrm_state *x) · line 1428
Quoted 3 lines
include/net/xfrm.h +1
-1- proto == IPPROTO_COMP))); + (userproto == IPSEC_PROTO_ANY && xfrm_id_proto_valid(proto))); }This does not look right.
IPSEC_PROTO_ANYshould only be allowed in userproto and your patch is going to let it pass when it's in proto. Whether IPPROTO_ROUTING/IPPROTO_DSTOPTS should be allowed in this context is also not obvious.
IIRC, it is Steffen who suggested to add IPPROTO_ROUTING/IPPROTO_DSTOPTS back to commit 6a53b7593233. My xfrm knowledge is not enough to figure out IPPROTO_ROUTING/IPPROTO_DSTOPTS.
I just assume validate_tmpl() is correct.
Herbert Xu
maintainerQuoted 4 lines · Cong Wang
IIRC, it is Steffen who suggested to add IPPROTO_ROUTING/IPPROTO_DSTOPTS back to commit
6a53b7593233. My xfrm knowledge is not enough to figure out IPPROTO_ROUTING/IPPROTO_DSTOPTS.
OK I dug into the history of xfrm_id_proto_match and this is definitely not right. The intention appears to be that IPSEC_PROTO_ANY should only match genuine IPsec protocols, i.e., AH/ESP/COMP, while the special value of zero will match everything.
So I think what we should do is get rid of the validation function that you added in 6a5t3b7593233, and then change those internal functions which were incorrectly using IPSEC_PROTO_ANY to using zero instead.
Does anybody still use IPPROTO_ROUTING/IPPROTO_DSTOPTS? It's always a pain when people come and add features and then don't shoulder the burden of maintaining them.
Cheers,
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
Quoted 12 lines · Herbert Xu
Quoted 4 lines · Cong Wang
IIRC, it is Steffen who suggested to add IPPROTO_ROUTING/IPPROTO_DSTOPTS back to commit
6a53b7593233. My xfrm knowledge is not enough to figure out IPPROTO_ROUTING/IPPROTO_DSTOPTS.OK I dug into the history of
xfrm_id_proto_matchand this is definitely not right. The intention appears to be thatIPSEC_PROTO_ANYshould only match genuine IPsec protocols, i.e., AH/ESP/COMP, while the special value of zero will match everything.
Quoted 4 lines
So I think what we should do is get rid of the validation function that you added in 6a5t3b7593233, and then change those internal functions which were incorrectly using
IPSEC_PROTO_ANYto using zero instead.
Good point. Replacing IPSEC_PROTO_ANY with zero should work too, but on the other hand, id.proto is still never allowed to be any other protocol than these 6 listed, no?
Quoted 4 lines
Does anybody still use IPPROTO_ROUTING/IPPROTO_DSTOPTS? It's always a pain when people come and add features and then don't shoulder the burden of maintaining them.
Yeah, at least iproute2 does the same check:
static const struct typeent xfrmproto_types[] = {
{ "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
{ "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
{ "ipsec-any", IPSEC_PROTO_ANY },
{ NULL, -1 }
};Herbert Xu
maintainerQuoted 4 lines · Cong Wang
Good point. Replacing
IPSEC_PROTO_ANYwith zero should work too, but on the other hand, id.proto is still never allowed to be any other protocol than these 6 listed, no?
It should never be IPSEC_PROTO_ANY since that's used as a wildcard.
IOW if you're going to tighten up the check for the id.proto filed in an actual state, you should distinguish between the case of an ID that's used to add/modify a state vs. an ID that's be used to query a state. IPSEC_PROTO_ANY and zero should be denied in the first case and allowed in the second case.
Cheers,
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
Quoted 7 lines · Herbert Xu
Quoted 4 lines · Cong Wang
Good point. Replacing
IPSEC_PROTO_ANYwith zero should work too, but on the other hand, id.proto is still never allowed to be any other protocol than these 6 listed, no?
Quoted 7 lines
It should never be
IPSEC_PROTO_ANYsince that's used as a wildcard.IOW if you're going to tighten up the check for the id.proto filed in an actual state, you should distinguish between the case of an ID that's used to add/modify a state vs. an ID that's be used to query a state.
IPSEC_PROTO_ANYand zero should be denied in the first case and allowed in the second case.
Yeah, this makes sense. Let me see if I can figure this out correctly.
Thanks for the details!