Skip to contentSkip to conversation

genetlink: get rid of family->attrbuf

PATCH In review Cong Wang 2 participants 2 messages Jun 27, 2020 – Jun 30, 2020

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 genetlink: get rid of family->attrbuf

genl_family_rcv_msg_attrs_parse() reuses the global family->attrbuf when family->parallel_ops is false. However, family->attrbuf is not protected by any lock on the genl_family_rcv_msg_doit() code path.

This leads to several different consequences, one of them is UAF, like the following:

genl_family_rcv_msg_doit(): genl_start():

					  genl_family_rcv_msg_attrs_parse()
					    attrbuf = family->attrbuf
					    __nlmsg_parse(attrbuf);
  genl_family_rcv_msg_attrs_parse()
    attrbuf = family->attrbuf
    __nlmsg_parse(attrbuf);
					  info->attrs = attrs;
					  cb->data = info;

netlink_unicast_kernel():
consume_skb()

					genl_lock_dumpit():
					  genl_dumpit_info(cb)->attrs

Note family->attrbuf is an array of pointers to the skb data, once the skb is freed, any dereference of family->attrbuf will be a UAF.

Maybe we could serialize the family->attrbuf with genl_mutex too, but that would make the locking more complicated. Instead, we can just get rid of family->attrbuf and always allocate attrbuf from heap like the family->parallel_ops==true code path. This may add some performance overhead but comparing with taking the global genl_mutex, it still looks better.

Fixes 75cdbdd08900 ("net: ieee802154: have genetlink code to parse the attrs during dumpit")
Fixes 057af7071344 ("net: tipc: have genetlink code to parse the attrs during dumpit")
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Cc Jiri Pirko <[email protected]>
---
 include/net/genetlink.h |  2 --
 net/netlink/genetlink.c | 48 +++++++++++------------------------------
 2 files changed, 13 insertions(+), 37 deletions(-)
include/net/genetlink.h +0 -2
index 74950663bb00..ad71ed4f55ff 100644
@@ -41,7 +41,6 @@ struct genl_info;
  *	Note that unbind() will not be called symmetrically if the
  *	generic netlink family is removed while there are still open
  *	sockets.
- * @attrbuf: buffer to store parsed attributes (private)
  * @mcgrps: multicast groups used by this family
  * @n_mcgrps: number of multicast groups
  * @mcgrp_offset: starting number of multicast group IDs in this family
@@ -66,7 +65,6 @@ struct genl_family {
 					     struct genl_info *info);
 	int			(*mcast_bind)(struct net *net, int group);
 	void			(*mcast_unbind)(struct net *net, int group);
-	struct nlattr **	attrbuf;	/* private */
 	const struct genl_ops *	ops;
 	const struct genl_multicast_group *mcgrps;
 	unsigned int		n_ops;
net/netlink/genetlink.c +13 -35

116 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#1 lore raw

From: Cong Wang <[email protected]>
Date: Sat, 27 Jun 2020 00:12:24 -0700

Quoted 32 lines

genl_family_rcv_msg_attrs_parse() reuses the global family->attrbuf when family->parallel_ops is false. However, family->attrbuf is not protected by any lock on the genl_family_rcv_msg_doit() code path.

This leads to several different consequences, one of them is UAF, like the following:

genl_family_rcv_msg_doit(): genl_start():

					  genl_family_rcv_msg_attrs_parse()
					    attrbuf = family->attrbuf
					    __nlmsg_parse(attrbuf);
  genl_family_rcv_msg_attrs_parse()
    attrbuf = family->attrbuf
    __nlmsg_parse(attrbuf);
					  info->attrs = attrs;
					  cb->data = info;

netlink_unicast_kernel():
consume_skb()

					genl_lock_dumpit():
					  genl_dumpit_info(cb)->attrs

Note family->attrbuf is an array of pointers to the skb data, once the skb is freed, any dereference of family->attrbuf will be a UAF.

Maybe we could serialize the family->attrbuf with genl_mutex too, but that would make the locking more complicated. Instead, we can just get rid of family->attrbuf and always allocate attrbuf from heap like the family->parallel_ops==true code path. This may add some performance overhead but comparing with taking the global genl_mutex, it still looks better.

Quoted 9 lines
Fixes 75cdbdd08900 ("net: ieee802154: have genetlink code to parse the attrs during dumpit")
Fixes 057af7071344 ("net: tipc: have genetlink code to parse the attrs during dumpit")
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Reported-and-tested-by [email protected]
Cc Jiri Pirko <[email protected]>

Applied and queued up for -stable, thank you.