diff mbox series

[net,1/2] rtnetlink: Fix rtnl_net_cmp_locks() when DEBUG is off

Message ID 20250212-rtnetlink_leak-v1-1-27bce9a3ac9a@bootlin.com (mailing list archive)
State Rejected
Delegated to: Netdev Maintainers
Headers show
Series rtnetlink: Fix small memory leaks | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/apply fail Patch does not apply to net-0

Commit Message

Bastien Curutchet (eBPF Foundation) Feb. 12, 2025, 8:23 a.m. UTC
rtnl_net_cmp_locks() always returns -1 if CONFIG_DEBUG_NET_SMALL_RTNL is
disabled. However, if CONFIG_DEBUG_NET_SMALL_RTNL is enabled, it returns 0
when both inputs are equal. It is then used by rtnl_nets_add() to call
put_net() if the net to be added is already present in the struct
rtnl_nets. As a result, when rtnl_nets_add() is called on an already
present net, put_net() is called only if DEBUG is on.

Add the input comparison in the DEBUG off case so that put_net() is always
called in this scenario.

Fixes: cbaaa6326bc5 ("rtnetlink: Introduce struct rtnl_nets and helpers.")
Cc: stable@vger.kernel.org
Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
---
 net/core/rtnetlink.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Kuniyuki Iwashima Feb. 12, 2025, 8:45 a.m. UTC | #1
From: "Bastien Curutchet (eBPF Foundation)" <bastien.curutchet@bootlin.com>
Date: Wed, 12 Feb 2025 09:23:47 +0100
> rtnl_net_cmp_locks() always returns -1 if CONFIG_DEBUG_NET_SMALL_RTNL is
> disabled. However, if CONFIG_DEBUG_NET_SMALL_RTNL is enabled, it returns 0
> when both inputs are equal. It is then used by rtnl_nets_add() to call
> put_net() if the net to be added is already present in the struct
> rtnl_nets. As a result, when rtnl_nets_add() is called on an already
> present net, put_net() is called only if DEBUG is on.

If CONFIG_DEBUG_NET_SMALL_RTNL is disabled, every duplicate net is
added to rtnl_nets, so put_net() is expected to be called for each
in rtnl_nets_destroy().
Bastien Curutchet (eBPF Foundation) Feb. 12, 2025, 9:07 a.m. UTC | #2
On 2/12/25 9:45 AM, Kuniyuki Iwashima wrote:
> From: "Bastien Curutchet (eBPF Foundation)" <bastien.curutchet@bootlin.com>
> Date: Wed, 12 Feb 2025 09:23:47 +0100
>> rtnl_net_cmp_locks() always returns -1 if CONFIG_DEBUG_NET_SMALL_RTNL is
>> disabled. However, if CONFIG_DEBUG_NET_SMALL_RTNL is enabled, it returns 0
>> when both inputs are equal. It is then used by rtnl_nets_add() to call
>> put_net() if the net to be added is already present in the struct
>> rtnl_nets. As a result, when rtnl_nets_add() is called on an already
>> present net, put_net() is called only if DEBUG is on.
> 
> If CONFIG_DEBUG_NET_SMALL_RTNL is disabled, every duplicate net is
> added to rtnl_nets, so put_net() is expected to be called for each
> in rtnl_nets_destroy().

I see, sorry for the irrelevant series then ...

Best regards,
Bastien
diff mbox series

Patch

diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index cb7fad8d1f95ff287810229c341de6a6d20a9c07..94111d3383788566f2296039e68549e2b40d5a4a 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -275,6 +275,9 @@  EXPORT_SYMBOL(lockdep_rtnl_net_is_held);
 #else
 static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b)
 {
+	if (net_eq(net_a, net_b))
+		return 0;
+
 	/* No need to swap */
 	return -1;
 }