xfrm: Zero padding when dumping algos and encap

[ Upstream commit 8222d5910d ]

When copying data to user-space we should ensure that only valid
data is copied over.  Padding in structures may be filled with
random (possibly sensitve) data and should never be given directly
to user-space.

This patch fixes the copying of xfrm algorithms and the encap
template in xfrm_user so that padding is zeroed.

Reported-by: syzbot+fa5414772d5c445dac3c@syzkaller.appspotmail.com
Reported-by: Hyunwoo Kim <v4bel@theori.io>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Herbert Xu 2023-02-09 09:09:52 +08:00 committed by Greg Kroah-Hartman
parent 7258c58f6e
commit 0725daaa9a

View File

@ -906,7 +906,9 @@ static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
return -EMSGSIZE;
ap = nla_data(nla);
memcpy(ap, aead, sizeof(*aead));
strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
ap->alg_key_len = aead->alg_key_len;
ap->alg_icv_len = aead->alg_icv_len;
if (redact_secret && aead->alg_key_len)
memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
@ -926,7 +928,8 @@ static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
return -EMSGSIZE;
ap = nla_data(nla);
memcpy(ap, ealg, sizeof(*ealg));
strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
ap->alg_key_len = ealg->alg_key_len;
if (redact_secret && ealg->alg_key_len)
memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
@ -937,6 +940,40 @@ static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
return 0;
}
static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
{
struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
struct xfrm_algo *ap;
if (!nla)
return -EMSGSIZE;
ap = nla_data(nla);
strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
ap->alg_key_len = 0;
return 0;
}
static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
{
struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
struct xfrm_encap_tmpl *uep;
if (!nla)
return -EMSGSIZE;
uep = nla_data(nla);
memset(uep, 0, sizeof(*uep));
uep->encap_type = ep->encap_type;
uep->encap_sport = ep->encap_sport;
uep->encap_dport = ep->encap_dport;
uep->encap_oa = ep->encap_oa;
return 0;
}
static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
{
int ret = 0;
@ -992,12 +1029,12 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
goto out;
}
if (x->calg) {
ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
ret = copy_to_user_calg(x->calg, skb);
if (ret)
goto out;
}
if (x->encap) {
ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
ret = copy_to_user_encap(x->encap, skb);
if (ret)
goto out;
}