Skip to content

Commit 1841b98

Browse files
lxindavem330
authored andcommitted
lwtunnel: check erspan options before allocating tun_info
As Jakub suggested on another patch, it's better to do the check on erspan options before allocating memory. Signed-off-by: Xin Long <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7b6a70f commit 1841b98

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

net/ipv4/ip_tunnel_core.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ static int ip_tun_parse_opts_erspan(struct nlattr *attr,
321321
{
322322
struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
323323
int err;
324+
u8 ver;
324325

325326
err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
326327
erspan_opt_policy, extack);
@@ -330,24 +331,31 @@ static int ip_tun_parse_opts_erspan(struct nlattr *attr,
330331
if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
331332
return -EINVAL;
332333

334+
ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
335+
if (ver == 1) {
336+
if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
337+
return -EINVAL;
338+
} else if (ver == 2) {
339+
if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
340+
!tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
341+
return -EINVAL;
342+
} else {
343+
return -EINVAL;
344+
}
345+
333346
if (info) {
334347
struct erspan_metadata *md =
335348
ip_tunnel_info_opts(info) + opts_len;
336349

337-
attr = tb[LWTUNNEL_IP_OPT_ERSPAN_VER];
338-
md->version = nla_get_u8(attr);
339-
340-
if (md->version == 1 && tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]) {
350+
md->version = ver;
351+
if (ver == 1) {
341352
attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
342353
md->u.index = nla_get_be32(attr);
343-
} else if (md->version == 2 && tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] &&
344-
tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]) {
354+
} else {
345355
attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
346356
md->u.md2.dir = nla_get_u8(attr);
347357
attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
348358
set_hwid(&md->u.md2, nla_get_u8(attr));
349-
} else {
350-
return -EINVAL;
351359
}
352360

353361
info->key.tun_flags |= TUNNEL_ERSPAN_OPT;

0 commit comments

Comments
 (0)