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: 1927f41a22 ("net: genetlink: introduce dump info struct to be available during dumpit op")
Reported-by: syzbot+21f04f481f449c8db840@syzkaller.appspotmail.com
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: Florian Westphal <fw@strlen.de>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: Jiri Pirko <jiri@mellanox.com>
Cc: YueHaibing <yuehaibing@huawei.com>
Cc: Shaochun Chen <cscnull@gmail.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Cong Wang 2020-06-02 21:49:10 -07:00 committed by David S. Miller
parent ef1c75593e
commit c36f055591
1 changed files with 58 additions and 36 deletions

View File

@ -513,15 +513,58 @@ static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
kfree(attrbuf); kfree(attrbuf);
} }
static int genl_lock_start(struct netlink_callback *cb) struct genl_start_context {
const struct genl_family *family;
struct nlmsghdr *nlh;
struct netlink_ext_ack *extack;
const struct genl_ops *ops;
int hdrlen;
};
static int genl_start(struct netlink_callback *cb)
{ {
const struct genl_ops *ops = genl_dumpit_info(cb)->ops; struct genl_start_context *ctx = cb->data;
const struct genl_ops *ops = ctx->ops;
struct genl_dumpit_info *info;
struct nlattr **attrs = NULL;
int rc = 0; int rc = 0;
if (ops->validate & GENL_DONT_VALIDATE_DUMP)
goto no_attrs;
if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
return -EINVAL;
attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
ops, ctx->hdrlen,
GENL_DONT_VALIDATE_DUMP_STRICT,
true);
if (IS_ERR(attrs))
return PTR_ERR(attrs);
no_attrs:
info = genl_dumpit_info_alloc();
if (!info) {
kfree(attrs);
return -ENOMEM;
}
info->family = ctx->family;
info->ops = ops;
info->attrs = attrs;
cb->data = info;
if (ops->start) { if (ops->start) {
genl_lock(); if (!ctx->family->parallel_ops)
genl_lock();
rc = ops->start(cb); rc = ops->start(cb);
genl_unlock(); if (!ctx->family->parallel_ops)
genl_unlock();
}
if (rc) {
kfree(attrs);
genl_dumpit_info_free(info);
cb->data = NULL;
} }
return rc; return rc;
} }
@ -548,7 +591,7 @@ static int genl_lock_done(struct netlink_callback *cb)
rc = ops->done(cb); rc = ops->done(cb);
genl_unlock(); genl_unlock();
} }
genl_family_rcv_msg_attrs_free(info->family, info->attrs, true); genl_family_rcv_msg_attrs_free(info->family, info->attrs, false);
genl_dumpit_info_free(info); genl_dumpit_info_free(info);
return rc; return rc;
} }
@ -573,43 +616,23 @@ static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
const struct genl_ops *ops, const struct genl_ops *ops,
int hdrlen, struct net *net) int hdrlen, struct net *net)
{ {
struct genl_dumpit_info *info; struct genl_start_context ctx;
struct nlattr **attrs = NULL;
int err; int err;
if (!ops->dumpit) if (!ops->dumpit)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (ops->validate & GENL_DONT_VALIDATE_DUMP) ctx.family = family;
goto no_attrs; ctx.nlh = nlh;
ctx.extack = extack;
if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) ctx.ops = ops;
return -EINVAL; ctx.hdrlen = hdrlen;
attrs = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
ops, hdrlen,
GENL_DONT_VALIDATE_DUMP_STRICT,
true);
if (IS_ERR(attrs))
return PTR_ERR(attrs);
no_attrs:
/* Allocate dumpit info. It is going to be freed by done() callback. */
info = genl_dumpit_info_alloc();
if (!info) {
genl_family_rcv_msg_attrs_free(family, attrs, true);
return -ENOMEM;
}
info->family = family;
info->ops = ops;
info->attrs = attrs;
if (!family->parallel_ops) { if (!family->parallel_ops) {
struct netlink_dump_control c = { struct netlink_dump_control c = {
.module = family->module, .module = family->module,
.data = info, .data = &ctx,
.start = genl_lock_start, .start = genl_start,
.dump = genl_lock_dumpit, .dump = genl_lock_dumpit,
.done = genl_lock_done, .done = genl_lock_done,
}; };
@ -617,12 +640,11 @@ no_attrs:
genl_unlock(); genl_unlock();
err = __netlink_dump_start(net->genl_sock, skb, nlh, &c); err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
genl_lock(); genl_lock();
} else { } else {
struct netlink_dump_control c = { struct netlink_dump_control c = {
.module = family->module, .module = family->module,
.data = info, .data = &ctx,
.start = ops->start, .start = genl_start,
.dump = ops->dumpit, .dump = ops->dumpit,
.done = genl_parallel_done, .done = genl_parallel_done,
}; };