Commit b65ce380 authored by Cong Wang's avatar Cong Wang Committed by David S. Miller
Browse files

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 c36f0555
("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: c36f0555 ("genetlink: fix memory leaks in genl_family_rcv_msg_dumpit()")
Reported-by: default avatarIdo Schimmel <idosch@idosch.org>
Signed-off-by: default avatarCong Wang <xiyou.wangcong@gmail.com>
Reviewed-by: default avatarIdo Schimmel <idosch@mellanox.com>
Tested-by: default avatarIdo Schimmel <idosch@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 18dbd4cd
Loading
Loading
Loading
Loading
+12 −16
Original line number Original line Diff line number Diff line
@@ -474,8 +474,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
				struct netlink_ext_ack *extack,
				struct netlink_ext_ack *extack,
				const struct genl_ops *ops,
				const struct genl_ops *ops,
				int hdrlen,
				int hdrlen,
				enum genl_validate_flags no_strict_flag,
				enum genl_validate_flags no_strict_flag)
				bool parallel)
{
{
	enum netlink_validation validate = ops->validate & no_strict_flag ?
	enum netlink_validation validate = ops->validate & no_strict_flag ?
					   NL_VALIDATE_LIBERAL :
					   NL_VALIDATE_LIBERAL :
@@ -486,7 +485,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
	if (!family->maxattr)
	if (!family->maxattr)
		return NULL;
		return NULL;


	if (parallel) {
	if (family->parallel_ops) {
		attrbuf = kmalloc_array(family->maxattr + 1,
		attrbuf = kmalloc_array(family->maxattr + 1,
					sizeof(struct nlattr *), GFP_KERNEL);
					sizeof(struct nlattr *), GFP_KERNEL);
		if (!attrbuf)
		if (!attrbuf)
@@ -498,7 +497,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
	err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
	err = __nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
			    family->policy, validate, extack);
			    family->policy, validate, extack);
	if (err) {
	if (err) {
		if (parallel)
		if (family->parallel_ops)
			kfree(attrbuf);
			kfree(attrbuf);
		return ERR_PTR(err);
		return ERR_PTR(err);
	}
	}
@@ -506,10 +505,9 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
}
}


static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
static void genl_family_rcv_msg_attrs_free(const struct genl_family *family,
					   struct nlattr **attrbuf,
					   struct nlattr **attrbuf)
					   bool parallel)
{
{
	if (parallel)
	if (family->parallel_ops)
		kfree(attrbuf);
		kfree(attrbuf);
}
}


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


	attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
	attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
						ops, ctx->hdrlen,
						ops, ctx->hdrlen,
						GENL_DONT_VALIDATE_DUMP_STRICT,
						GENL_DONT_VALIDATE_DUMP_STRICT);
						true);
	if (IS_ERR(attrs))
	if (IS_ERR(attrs))
		return PTR_ERR(attrs);
		return PTR_ERR(attrs);


no_attrs:
no_attrs:
	info = genl_dumpit_info_alloc();
	info = genl_dumpit_info_alloc();
	if (!info) {
	if (!info) {
		kfree(attrs);
		genl_family_rcv_msg_attrs_free(ctx->family, attrs);
		return -ENOMEM;
		return -ENOMEM;
	}
	}
	info->family = ctx->family;
	info->family = ctx->family;
@@ -562,7 +559,7 @@ no_attrs:
	}
	}


	if (rc) {
	if (rc) {
		kfree(attrs);
		genl_family_rcv_msg_attrs_free(info->family, info->attrs);
		genl_dumpit_info_free(info);
		genl_dumpit_info_free(info);
		cb->data = NULL;
		cb->data = NULL;
	}
	}
@@ -591,7 +588,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, false);
	genl_family_rcv_msg_attrs_free(info->family, info->attrs);
	genl_dumpit_info_free(info);
	genl_dumpit_info_free(info);
	return rc;
	return rc;
}
}
@@ -604,7 +601,7 @@ static int genl_parallel_done(struct netlink_callback *cb)


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


	attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
	attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
						  ops, hdrlen,
						  ops, hdrlen,
						  GENL_DONT_VALIDATE_STRICT,
						  GENL_DONT_VALIDATE_STRICT);
						  family->parallel_ops);
	if (IS_ERR(attrbuf))
	if (IS_ERR(attrbuf))
		return PTR_ERR(attrbuf);
		return PTR_ERR(attrbuf);


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


out:
out:
	genl_family_rcv_msg_attrs_free(family, attrbuf, family->parallel_ops);
	genl_family_rcv_msg_attrs_free(family, attrbuf);


	return err;
	return err;
}
}