netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects
@ 2025-12-21 14:48 Ido Schimmel
  2025-12-21 14:48 ` [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion Ido Schimmel
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ido Schimmel @ 2025-12-21 14:48 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, pabeni, edumazet, dsahern, horms, penguin-kernel,
	Ido Schimmel

When a nexthop object is deleted, it is marked as dead and then
fib_table_flush() is called to flush all the routes that are using the
dead nexthop.

The current logic in fib_table_flush() is to only flush error routes
(e.g., blackhole) when it is called as part of network namespace
dismantle (i.e., with flush_all=true). Therefore, error routes are not
flushed when their nexthop object is deleted:

 # ip link add name dummy1 up type dummy
 # ip nexthop add id 1 dev dummy1
 # ip route add 198.51.100.1/32 nhid 1
 # ip route add blackhole 198.51.100.2/32 nhid 1
 # ip nexthop del id 1
 # ip route show
 blackhole 198.51.100.2 nhid 1 dev dummy1

As such, they keep holding a reference on the nexthop object which in
turn holds a reference on the nexthop device, resulting in a reference
count leak:

 # ip link del dev dummy1
 [   70.516258] unregister_netdevice: waiting for dummy1 to become free. Usage count = 2

Fix by flushing error routes when their nexthop is marked as dead.

IPv6 does not suffer from this problem.

Fixes: 493ced1ac47c ("ipv4: Allow routes to use nexthop objects")
Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Closes: https://lore.kernel.org/netdev/d943f806-4da6-4970-ac28-b9373b0e63ac@I-love.SAKURA.ne.jp/
Reported-by: syzbot+881d65229ca4f9ae8c84@syzkaller.appspotmail.com
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
---
 net/ipv4/fib_trie.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 59a6f0a9638f..7e2c17fec3fc 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -2053,10 +2053,11 @@ int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
 				continue;
 			}
 
-			/* Do not flush error routes if network namespace is
-			 * not being dismantled
+			/* When not flushing the entire table, skip error
+			 * routes that are not marked for deletion.
 			 */
-			if (!flush_all && fib_props[fa->fa_type].error) {
+			if (!flush_all && fib_props[fa->fa_type].error &&
+			    !(fi->fib_flags & RTNH_F_DEAD)) {
 				slen = fa->fa_slen;
 				continue;
 			}
-- 
2.52.0


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

* [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion
  2025-12-21 14:48 [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects Ido Schimmel
@ 2025-12-21 14:48 ` Ido Schimmel
  2025-12-22 23:13   ` David Ahern
  2025-12-22 23:12 ` [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects David Ahern
  2025-12-30  9:40 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Ido Schimmel @ 2025-12-21 14:48 UTC (permalink / raw)
  To: netdev
  Cc: davem, kuba, pabeni, edumazet, dsahern, horms, penguin-kernel,
	Ido Schimmel

Add test cases that check that error routes (e.g., blackhole) are
deleted when their nexthop is deleted.

Output without "ipv4: Fix reference count leak when using error routes
with nexthop objects":

 # ./fib_nexthops.sh -t "ipv4_fcnal ipv6_fcnal"

 IPv4 functional
 ----------------------
 [...]
       WARNING: Unexpected route entry
 TEST: Error route removed on nexthop deletion                       [FAIL]

 IPv6
 ----------------------
 [...]
 TEST: Error route removed on nexthop deletion                       [ OK ]

 Tests passed:  20
 Tests failed:   1
 Tests skipped:  0

Output with "ipv4: Fix reference count leak when using error routes
with nexthop objects":

 # ./fib_nexthops.sh -t "ipv4_fcnal ipv6_fcnal"

 IPv4 functional
 ----------------------
 [...]
 TEST: Error route removed on nexthop deletion                       [ OK ]

 IPv6
 ----------------------
 [...]
 TEST: Error route removed on nexthop deletion                       [ OK ]

 Tests passed:  21
 Tests failed:   0
 Tests skipped:  0

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
---
 tools/testing/selftests/net/fib_nexthops.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/tools/testing/selftests/net/fib_nexthops.sh b/tools/testing/selftests/net/fib_nexthops.sh
index 2b0a90581e2f..21026b667667 100755
--- a/tools/testing/selftests/net/fib_nexthops.sh
+++ b/tools/testing/selftests/net/fib_nexthops.sh
@@ -800,6 +800,14 @@ ipv6_fcnal()
 	set +e
 	check_nexthop "dev veth1" ""
 	log_test $? 0 "Nexthops removed on admin down"
+
+	# error routes should be deleted when their nexthop is deleted
+	run_cmd "$IP li set dev veth1 up"
+	run_cmd "$IP -6 nexthop add id 58 dev veth1"
+	run_cmd "$IP ro add blackhole 2001:db8:101::1/128 nhid 58"
+	run_cmd "$IP nexthop del id 58"
+	check_route6 "2001:db8:101::1" ""
+	log_test $? 0 "Error route removed on nexthop deletion"
 }
 
 ipv6_grp_refs()
@@ -1459,6 +1467,13 @@ ipv4_fcnal()
 
 	run_cmd "$IP ro del 172.16.102.0/24"
 	log_test $? 0 "Delete route when not specifying nexthop attributes"
+
+	# error routes should be deleted when their nexthop is deleted
+	run_cmd "$IP nexthop add id 23 dev veth1"
+	run_cmd "$IP ro add blackhole 172.16.102.100/32 nhid 23"
+	run_cmd "$IP nexthop del id 23"
+	check_route "172.16.102.100" ""
+	log_test $? 0 "Error route removed on nexthop deletion"
 }
 
 ipv4_grp_fcnal()
-- 
2.52.0


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

* Re: [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects
  2025-12-21 14:48 [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects Ido Schimmel
  2025-12-21 14:48 ` [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion Ido Schimmel
@ 2025-12-22 23:12 ` David Ahern
  2025-12-30  9:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: David Ahern @ 2025-12-22 23:12 UTC (permalink / raw)
  To: Ido Schimmel, netdev; +Cc: davem, kuba, pabeni, edumazet, horms, penguin-kernel

On 12/21/25 7:48 AM, Ido Schimmel wrote:
> When a nexthop object is deleted, it is marked as dead and then
> fib_table_flush() is called to flush all the routes that are using the
> dead nexthop.
> 
> The current logic in fib_table_flush() is to only flush error routes
> (e.g., blackhole) when it is called as part of network namespace
> dismantle (i.e., with flush_all=true). Therefore, error routes are not
> flushed when their nexthop object is deleted:
> 
>  # ip link add name dummy1 up type dummy
>  # ip nexthop add id 1 dev dummy1
>  # ip route add 198.51.100.1/32 nhid 1
>  # ip route add blackhole 198.51.100.2/32 nhid 1
>  # ip nexthop del id 1
>  # ip route show
>  blackhole 198.51.100.2 nhid 1 dev dummy1
> 
> As such, they keep holding a reference on the nexthop object which in
> turn holds a reference on the nexthop device, resulting in a reference
> count leak:
> 
>  # ip link del dev dummy1
>  [   70.516258] unregister_netdevice: waiting for dummy1 to become free. Usage count = 2
> 
> Fix by flushing error routes when their nexthop is marked as dead.
> 
> IPv6 does not suffer from this problem.
> 
> Fixes: 493ced1ac47c ("ipv4: Allow routes to use nexthop objects")
> Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> Closes: https://lore.kernel.org/netdev/d943f806-4da6-4970-ac28-b9373b0e63ac@I-love.SAKURA.ne.jp/
> Reported-by: syzbot+881d65229ca4f9ae8c84@syzkaller.appspotmail.com
> Signed-off-by: Ido Schimmel <idosch@nvidia.com>
> ---
>  net/ipv4/fib_trie.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
> index 59a6f0a9638f..7e2c17fec3fc 100644
> --- a/net/ipv4/fib_trie.c
> +++ b/net/ipv4/fib_trie.c
> @@ -2053,10 +2053,11 @@ int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
>  				continue;
>  			}
>  
> -			/* Do not flush error routes if network namespace is
> -			 * not being dismantled
> +			/* When not flushing the entire table, skip error
> +			 * routes that are not marked for deletion.
>  			 */
> -			if (!flush_all && fib_props[fa->fa_type].error) {
> +			if (!flush_all && fib_props[fa->fa_type].error &&
> +			    !(fi->fib_flags & RTNH_F_DEAD)) {
>  				slen = fa->fa_slen;
>  				continue;
>  			}


Reviewed-by: David Ahern <dsahern@kernel.org>

thanks for the fast turnaround, Ido.

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

* Re: [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion
  2025-12-21 14:48 ` [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion Ido Schimmel
@ 2025-12-22 23:13   ` David Ahern
  0 siblings, 0 replies; 5+ messages in thread
From: David Ahern @ 2025-12-22 23:13 UTC (permalink / raw)
  To: Ido Schimmel, netdev; +Cc: davem, kuba, pabeni, edumazet, horms, penguin-kernel

On 12/21/25 7:48 AM, Ido Schimmel wrote:
> Add test cases that check that error routes (e.g., blackhole) are
> deleted when their nexthop is deleted.
> 
> Output without "ipv4: Fix reference count leak when using error routes
> with nexthop objects":
> 
>  # ./fib_nexthops.sh -t "ipv4_fcnal ipv6_fcnal"
> 
>  IPv4 functional
>  ----------------------
>  [...]
>        WARNING: Unexpected route entry
>  TEST: Error route removed on nexthop deletion                       [FAIL]
> 
>  IPv6
>  ----------------------
>  [...]
>  TEST: Error route removed on nexthop deletion                       [ OK ]
> 
>  Tests passed:  20
>  Tests failed:   1
>  Tests skipped:  0
> 
> Output with "ipv4: Fix reference count leak when using error routes
> with nexthop objects":
> 
>  # ./fib_nexthops.sh -t "ipv4_fcnal ipv6_fcnal"
> 
>  IPv4 functional
>  ----------------------
>  [...]
>  TEST: Error route removed on nexthop deletion                       [ OK ]
> 
>  IPv6
>  ----------------------
>  [...]
>  TEST: Error route removed on nexthop deletion                       [ OK ]
> 
>  Tests passed:  21
>  Tests failed:   0
>  Tests skipped:  0
> 
> Signed-off-by: Ido Schimmel <idosch@nvidia.com>
> ---
>  tools/testing/selftests/net/fib_nexthops.sh | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 

Reviewed-by: David Ahern <dsahern@kernel.org>


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

* Re: [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects
  2025-12-21 14:48 [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects Ido Schimmel
  2025-12-21 14:48 ` [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion Ido Schimmel
  2025-12-22 23:12 ` [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects David Ahern
@ 2025-12-30  9:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-12-30  9:40 UTC (permalink / raw)
  To: Ido Schimmel
  Cc: netdev, davem, kuba, pabeni, edumazet, dsahern, horms,
	penguin-kernel

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Sun, 21 Dec 2025 16:48:28 +0200 you wrote:
> When a nexthop object is deleted, it is marked as dead and then
> fib_table_flush() is called to flush all the routes that are using the
> dead nexthop.
> 
> The current logic in fib_table_flush() is to only flush error routes
> (e.g., blackhole) when it is called as part of network namespace
> dismantle (i.e., with flush_all=true). Therefore, error routes are not
> flushed when their nexthop object is deleted:
> 
> [...]

Here is the summary with links:
  - [net,1/2] ipv4: Fix reference count leak when using error routes with nexthop objects
    https://git.kernel.org/netdev/net/c/ac782f4e3bfc
  - [net,2/2] selftests: fib_nexthops: Add test cases for error routes deletion
    https://git.kernel.org/netdev/net/c/44741e9de29b

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2025-12-30  9:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-21 14:48 [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects Ido Schimmel
2025-12-21 14:48 ` [PATCH net 2/2] selftests: fib_nexthops: Add test cases for error routes deletion Ido Schimmel
2025-12-22 23:13   ` David Ahern
2025-12-22 23:12 ` [PATCH net 1/2] ipv4: Fix reference count leak when using error routes with nexthop objects David Ahern
2025-12-30  9:40 ` patchwork-bot+netdevbpf

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).