Skip to content

Commit c36f055

Browse files
congwangdavem330
authored andcommitted
genetlink: fix memory leaks in genl_family_rcv_msg_dumpit()
There are two kinds of memory leaks in genl_family_rcv_msg_dumpit(): 1. Before we call ops->start(), whenever an error happens, we forget to free the memory allocated in genl_family_rcv_msg_dumpit(). 2. When ops->start() fails, the 'info' has been already installed on the per socket control block, so we should not free it here. More importantly, nlk->cb_running is still false at this point, so netlink_sock_destruct() cannot free it either. The first kind of memory leaks is easier to resolve, but the second one requires some deeper thoughts. After reviewing how netfilter handles this, the most elegant solution I find is just to use a similar way to allocate the memory, that is, moving memory allocations from caller into ops->start(). With this, we can solve both kinds of memory leaks: for 1), no memory allocation happens before ops->start(); for 2), ops->start() handles its own failures and 'info' is installed to the socket control block only when success. The only ugliness here is we have to pass all local variables on stack via a struct, but this is not hard to understand. Alternatively, we can introduce a ops->free() to solve this too, but it is overkill as only genetlink has this problem so far. Fixes: 1927f41 ("net: genetlink: introduce dump info struct to be available during dumpit op") Reported-by: [email protected] Cc: "Jason A. Donenfeld" <[email protected]> Cc: Florian Westphal <[email protected]> Cc: Pablo Neira Ayuso <[email protected]> Cc: Jiri Pirko <[email protected]> Cc: YueHaibing <[email protected]> Cc: Shaochun Chen <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ef1c755 commit c36f055

File tree

1 file changed

+58
-36
lines changed

1 file changed

+58
-36
lines changed

net/netlink/genetlink.c

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,58 @@ static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
513513
kfree(attrbuf);
514514
}
515515

516-
static int genl_lock_start(struct netlink_callback *cb)
516+
struct genl_start_context {
517+
const struct genl_family *family;
518+
struct nlmsghdr *nlh;
519+
struct netlink_ext_ack *extack;
520+
const struct genl_ops *ops;
521+
int hdrlen;
522+
};
523+
524+
static int genl_start(struct netlink_callback *cb)
517525
{
518-
const struct genl_ops *ops = genl_dumpit_info(cb)->ops;
526+
struct genl_start_context *ctx = cb->data;
527+
const struct genl_ops *ops = ctx->ops;
528+
struct genl_dumpit_info *info;
529+
struct nlattr **attrs = NULL;
519530
int rc = 0;
520531

532+
if (ops->validate & GENL_DONT_VALIDATE_DUMP)
533+
goto no_attrs;
534+
535+
if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
536+
return -EINVAL;
537+
538+
attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
539+
ops, ctx->hdrlen,
540+
GENL_DONT_VALIDATE_DUMP_STRICT,
541+
true);
542+
if (IS_ERR(attrs))
543+
return PTR_ERR(attrs);
544+
545+
no_attrs:
546+
info = genl_dumpit_info_alloc();
547+
if (!info) {
548+
kfree(attrs);
549+
return -ENOMEM;
550+
}
551+
info->family = ctx->family;
552+
info->ops = ops;
553+
info->attrs = attrs;
554+
555+
cb->data = info;
521556
if (ops->start) {
522-
genl_lock();
557+
if (!ctx->family->parallel_ops)
558+
genl_lock();
523559
rc = ops->start(cb);
524-
genl_unlock();
560+
if (!ctx->family->parallel_ops)
561+
genl_unlock();
562+
}
563+
564+
if (rc) {
565+
kfree(attrs);
566+
genl_dumpit_info_free(info);
567+
cb->data = NULL;
525568
}
526569
return rc;
527570
}
@@ -548,7 +591,7 @@ static int genl_lock_done(struct netlink_callback *cb)
548591
rc = ops->done(cb);
549592
genl_unlock();
550593
}
551-
genl_family_rcv_msg_attrs_free(info->family, info->attrs, true);
594+
genl_family_rcv_msg_attrs_free(info->family, info->attrs, false);
552595
genl_dumpit_info_free(info);
553596
return rc;
554597
}
@@ -573,56 +616,35 @@ static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
573616
const struct genl_ops *ops,
574617
int hdrlen, struct net *net)
575618
{
576-
struct genl_dumpit_info *info;
577-
struct nlattr **attrs = NULL;
619+
struct genl_start_context ctx;
578620
int err;
579621

580622
if (!ops->dumpit)
581623
return -EOPNOTSUPP;
582624

583-
if (ops->validate & GENL_DONT_VALIDATE_DUMP)
584-
goto no_attrs;
585-
586-
if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
587-
return -EINVAL;
588-
589-
attrs = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
590-
ops, hdrlen,
591-
GENL_DONT_VALIDATE_DUMP_STRICT,
592-
true);
593-
if (IS_ERR(attrs))
594-
return PTR_ERR(attrs);
595-
596-
no_attrs:
597-
/* Allocate dumpit info. It is going to be freed by done() callback. */
598-
info = genl_dumpit_info_alloc();
599-
if (!info) {
600-
genl_family_rcv_msg_attrs_free(family, attrs, true);
601-
return -ENOMEM;
602-
}
603-
604-
info->family = family;
605-
info->ops = ops;
606-
info->attrs = attrs;
625+
ctx.family = family;
626+
ctx.nlh = nlh;
627+
ctx.extack = extack;
628+
ctx.ops = ops;
629+
ctx.hdrlen = hdrlen;
607630

608631
if (!family->parallel_ops) {
609632
struct netlink_dump_control c = {
610633
.module = family->module,
611-
.data = info,
612-
.start = genl_lock_start,
634+
.data = &ctx,
635+
.start = genl_start,
613636
.dump = genl_lock_dumpit,
614637
.done = genl_lock_done,
615638
};
616639

617640
genl_unlock();
618641
err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
619642
genl_lock();
620-
621643
} else {
622644
struct netlink_dump_control c = {
623645
.module = family->module,
624-
.data = info,
625-
.start = ops->start,
646+
.data = &ctx,
647+
.start = genl_start,
626648
.dump = ops->dumpit,
627649
.done = genl_parallel_done,
628650
};

0 commit comments

Comments
 (0)