netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] batman-adv: Fix orig node refcnt leak when creating neigh node
@ 2020-09-18  3:03 Xiyu Yang
  2020-09-18  6:22 ` Sven Eckelmann
  0 siblings, 1 reply; 2+ messages in thread
From: Xiyu Yang @ 2020-09-18  3:03 UTC (permalink / raw)
  To: Marek Lindner, Simon Wunderlich, Antonio Quartulli,
	Sven Eckelmann, David S. Miller, Jakub Kicinski, b.a.t.m.a.n,
	netdev, linux-kernel
  Cc: yuanxzhang, kjlu, Xiyu Yang, Xin Tan

batadv_neigh_node_create() is used to create a neigh node object, whose
fields will be initialized with the specific object. When a new
reference of the specific object is created during the initialization,
its refcount should be increased.

However, when "neigh_node" object initializes its orig_node field with
the "orig_node" object, the function forgets to hold the refcount of the
"orig_node", causing a potential refcount leak and use-after-free issue
for the reason that the object can be freed in other places.

Fix this issue by increasing the refcount of orig_node object during the
initialization and adding corresponding batadv_orig_node_put() in
batadv_neigh_node_release().

Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 net/batman-adv/originator.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c
index 805d8969bdfb..d6c2296f8e35 100644
--- a/net/batman-adv/originator.c
+++ b/net/batman-adv/originator.c
@@ -306,6 +306,8 @@ static void batadv_neigh_node_release(struct kref *ref)
 
 	batadv_hardif_put(neigh_node->if_incoming);
 
+	batadv_orig_node_put(neigh_node->orig_node);
+
 	kfree_rcu(neigh_node, rcu);
 }
 
@@ -685,6 +687,7 @@ batadv_neigh_node_create(struct batadv_orig_node *orig_node,
 	kref_get(&hard_iface->refcount);
 	ether_addr_copy(neigh_node->addr, neigh_addr);
 	neigh_node->if_incoming = hard_iface;
+	kref_get(&orig_node->refcount);
 	neigh_node->orig_node = orig_node;
 	neigh_node->last_seen = jiffies;
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] batman-adv: Fix orig node refcnt leak when creating neigh node
  2020-09-18  3:03 [PATCH] batman-adv: Fix orig node refcnt leak when creating neigh node Xiyu Yang
@ 2020-09-18  6:22 ` Sven Eckelmann
  0 siblings, 0 replies; 2+ messages in thread
From: Sven Eckelmann @ 2020-09-18  6:22 UTC (permalink / raw)
  To: Marek Lindner, Simon Wunderlich, Antonio Quartulli,
	David S. Miller, Jakub Kicinski, b.a.t.m.a.n, netdev,
	linux-kernel, Xiyu Yang
  Cc: yuanxzhang, kjlu, Xiyu Yang, Xin Tan

[-- Attachment #1: Type: text/plain, Size: 1416 bytes --]

On Friday, 18 September 2020 05:03:19 CEST Xiyu Yang wrote:
> batadv_neigh_node_create() is used to create a neigh node object, whose
> fields will be initialized with the specific object. When a new
> reference of the specific object is created during the initialization,
> its refcount should be increased.
> 
> However, when "neigh_node" object initializes its orig_node field with
> the "orig_node" object, the function forgets to hold the refcount of the
> "orig_node", causing a potential refcount leak and use-after-free issue
> for the reason that the object can be freed in other places.
> 
> Fix this issue by increasing the refcount of orig_node object during the
> initialization and adding corresponding batadv_orig_node_put() in
> batadv_neigh_node_release().


I will most likely not add this patch because I have concerns that this would 
need an active garbage collector to fix the reference counter loop.

Please check batadv_neigh_node::orig_node (whose reference counter you've just 
incremented) and batadv_orig_node::neigh_list (with batadv_neigh_node). And at 
the same time the batadv_neigh_node_release and batadv_orig_node_release. So 
the originator will only free the reference (and thus potentially call 
batadv_neigh_node_release) when its own reference counter is zero. But it 
cannot become zero because the neigh_node is holding a reference to this 
originator.

Kind regards,
	Sven

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-18  6:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-18  3:03 [PATCH] batman-adv: Fix orig node refcnt leak when creating neigh node Xiyu Yang
2020-09-18  6:22 ` Sven Eckelmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).