Skip to content

Commit b65ce38

Browse files
congwangdavem330
authored andcommitted
genetlink: clean up family attributes allocations
genl_family_rcv_msg_attrs_parse() and genl_family_rcv_msg_attrs_free() take a boolean parameter to determine whether allocate/free the family attrs. This is unnecessary as we can just check family->parallel_ops. More importantly, callers would not need to worry about pairing these parameters correctly after this patch. And this fixes a memory leak, as after commit c36f055 ("genetlink: fix memory leaks in genl_family_rcv_msg_dumpit()") we call genl_family_rcv_msg_attrs_parse() for both parallel and non-parallel cases. Fixes: c36f055 ("genetlink: fix memory leaks in genl_family_rcv_msg_dumpit()") Reported-by: Ido Schimmel <[email protected]> Signed-off-by: Cong Wang <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Tested-by: Ido Schimmel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 18dbd4c commit b65ce38

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

net/netlink/genetlink.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
474474
struct netlink_ext_ack *extack,
475475
const struct genl_ops *ops,
476476
int hdrlen,
477-
enum genl_validate_flags no_strict_flag,
478-
bool parallel)
477+
enum genl_validate_flags no_strict_flag)
479478
{
480479
enum netlink_validation validate = ops->validate & no_strict_flag ?
481480
NL_VALIDATE_LIBERAL :
@@ -486,7 +485,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
486485
if (!family->maxattr)
487486
return NULL;
488487

489-
if (parallel) {
488+
if (family->parallel_ops) {
490489
attrbuf = kmalloc_array(family->maxattr + 1,
491490
sizeof(struct nlattr *), GFP_KERNEL);
492491
if (!attrbuf)
@@ -498,18 +497,17 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
498497
err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
499498
family->policy, validate, extack);
500499
if (err) {
501-
if (parallel)
500+
if (family->parallel_ops)
502501
kfree(attrbuf);
503502
return ERR_PTR(err);
504503
}
505504
return attrbuf;
506505
}
507506

508507
static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
509-
struct nlattr **attrbuf,
510-
bool parallel)
508+
struct nlattr **attrbuf)
511509
{
512-
if (parallel)
510+
if (family->parallel_ops)
513511
kfree(attrbuf);
514512
}
515513

@@ -537,15 +535,14 @@ static int genl_start(struct netlink_callback *cb)
537535

538536
attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
539537
ops, ctx->hdrlen,
540-
GENL_DONT_VALIDATE_DUMP_STRICT,
541-
true);
538+
GENL_DONT_VALIDATE_DUMP_STRICT);
542539
if (IS_ERR(attrs))
543540
return PTR_ERR(attrs);
544541

545542
no_attrs:
546543
info = genl_dumpit_info_alloc();
547544
if (!info) {
548-
kfree(attrs);
545+
genl_family_rcv_msg_attrs_free(ctx->family, attrs);
549546
return -ENOMEM;
550547
}
551548
info->family = ctx->family;
@@ -562,7 +559,7 @@ static int genl_start(struct netlink_callback *cb)
562559
}
563560

564561
if (rc) {
565-
kfree(attrs);
562+
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
566563
genl_dumpit_info_free(info);
567564
cb->data = NULL;
568565
}
@@ -591,7 +588,7 @@ static int genl_lock_done(struct netlink_callback *cb)
591588
rc = ops->done(cb);
592589
genl_unlock();
593590
}
594-
genl_family_rcv_msg_attrs_free(info->family, info->attrs, false);
591+
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
595592
genl_dumpit_info_free(info);
596593
return rc;
597594
}
@@ -604,7 +601,7 @@ static int genl_parallel_done(struct netlink_callback *cb)
604601

605602
if (ops->done)
606603
rc = ops->done(cb);
607-
genl_family_rcv_msg_attrs_free(info->family, info->attrs, true);
604+
genl_family_rcv_msg_attrs_free(info->family, info->attrs);
608605
genl_dumpit_info_free(info);
609606
return rc;
610607
}
@@ -671,8 +668,7 @@ static int genl_family_rcv_msg_doit(const struct genl_family *family,
671668

672669
attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
673670
ops, hdrlen,
674-
GENL_DONT_VALIDATE_STRICT,
675-
family->parallel_ops);
671+
GENL_DONT_VALIDATE_STRICT);
676672
if (IS_ERR(attrbuf))
677673
return PTR_ERR(attrbuf);
678674

@@ -698,7 +694,7 @@ static int genl_family_rcv_msg_doit(const struct genl_family *family,
698694
family->post_doit(ops, skb, &info);
699695

700696
out:
701-
genl_family_rcv_msg_attrs_free(family, attrbuf, family->parallel_ops);
697+
genl_family_rcv_msg_attrs_free(family, attrbuf);
702698

703699
return err;
704700
}

0 commit comments

Comments
 (0)