Skip to content

Commit 5b4c6e3

Browse files
GustavoARSilvaummakynes
authored andcommitted
netfilter: nf_tables: remove VLA usage
In preparation to enabling -Wvla, remove VLA and replace it with dynamic memory allocation. >From a security viewpoint, the use of Variable Length Arrays can be a vector for stack overflow attacks. Also, in general, as the code evolves it is easy to lose track of how big a VLA can get. Thus, we can end up having segfaults that are hard to debug. Also, fixed as part of the directive to remove all VLAs from the kernel: https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 1446385 commit 5b4c6e3

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

net/netfilter/nf_tables_api.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,16 +4357,20 @@ static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
43574357
const struct nft_object_type *type,
43584358
const struct nlattr *attr)
43594359
{
4360-
struct nlattr *tb[type->maxattr + 1];
4360+
struct nlattr **tb;
43614361
const struct nft_object_ops *ops;
43624362
struct nft_object *obj;
4363-
int err;
4363+
int err = -ENOMEM;
4364+
4365+
tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
4366+
if (!tb)
4367+
goto err1;
43644368

43654369
if (attr) {
43664370
err = nla_parse_nested(tb, type->maxattr, attr, type->policy,
43674371
NULL);
43684372
if (err < 0)
4369-
goto err1;
4373+
goto err2;
43704374
} else {
43714375
memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
43724376
}
@@ -4375,26 +4379,29 @@ static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
43754379
ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
43764380
if (IS_ERR(ops)) {
43774381
err = PTR_ERR(ops);
4378-
goto err1;
4382+
goto err2;
43794383
}
43804384
} else {
43814385
ops = type->ops;
43824386
}
43834387

43844388
err = -ENOMEM;
43854389
obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
4386-
if (obj == NULL)
4387-
goto err1;
4390+
if (!obj)
4391+
goto err2;
43884392

43894393
err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
43904394
if (err < 0)
4391-
goto err2;
4395+
goto err3;
43924396

43934397
obj->ops = ops;
43944398

4399+
kfree(tb);
43954400
return obj;
4396-
err2:
4401+
err3:
43974402
kfree(obj);
4403+
err2:
4404+
kfree(tb);
43984405
err1:
43994406
return ERR_PTR(err);
44004407
}

0 commit comments

Comments
 (0)