Skip to content

Commit 8222d59

Browse files
herbertxklassert
authored andcommitted
xfrm: Zero padding when dumping algos and encap
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: [email protected] Reported-by: Hyunwoo Kim <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Reviewed-by: Sabrina Dubroca <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent 2038cc5 commit 8222d59

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

net/xfrm/xfrm_user.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
10121012
return -EMSGSIZE;
10131013

10141014
ap = nla_data(nla);
1015-
memcpy(ap, aead, sizeof(*aead));
1015+
strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
1016+
ap->alg_key_len = aead->alg_key_len;
1017+
ap->alg_icv_len = aead->alg_icv_len;
10161018

10171019
if (redact_secret && aead->alg_key_len)
10181020
memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
@@ -1032,7 +1034,8 @@ static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
10321034
return -EMSGSIZE;
10331035

10341036
ap = nla_data(nla);
1035-
memcpy(ap, ealg, sizeof(*ealg));
1037+
strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
1038+
ap->alg_key_len = ealg->alg_key_len;
10361039

10371040
if (redact_secret && ealg->alg_key_len)
10381041
memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
@@ -1043,6 +1046,40 @@ static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
10431046
return 0;
10441047
}
10451048

1049+
static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
1050+
{
1051+
struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
1052+
struct xfrm_algo *ap;
1053+
1054+
if (!nla)
1055+
return -EMSGSIZE;
1056+
1057+
ap = nla_data(nla);
1058+
strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
1059+
ap->alg_key_len = 0;
1060+
1061+
return 0;
1062+
}
1063+
1064+
static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
1065+
{
1066+
struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
1067+
struct xfrm_encap_tmpl *uep;
1068+
1069+
if (!nla)
1070+
return -EMSGSIZE;
1071+
1072+
uep = nla_data(nla);
1073+
memset(uep, 0, sizeof(*uep));
1074+
1075+
uep->encap_type = ep->encap_type;
1076+
uep->encap_sport = ep->encap_sport;
1077+
uep->encap_dport = ep->encap_dport;
1078+
uep->encap_oa = ep->encap_oa;
1079+
1080+
return 0;
1081+
}
1082+
10461083
static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
10471084
{
10481085
int ret = 0;
@@ -1098,12 +1135,12 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
10981135
goto out;
10991136
}
11001137
if (x->calg) {
1101-
ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1138+
ret = copy_to_user_calg(x->calg, skb);
11021139
if (ret)
11031140
goto out;
11041141
}
11051142
if (x->encap) {
1106-
ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1143+
ret = copy_to_user_encap(x->encap, skb);
11071144
if (ret)
11081145
goto out;
11091146
}

0 commit comments

Comments
 (0)