Commit 0ce85a5686 for frr

commit 0ce85a56863a64bbe5521b98b9dfae58a15ef777
Author: Louis Scalbert <louis.scalbert@6wind.com>
Date:   Wed Nov 18 12:07:06 2026 +0100

    zebra: fix nexthop tracking leak with retain

    Start "zebra --retain" and CTRL+C immediately results in memory leaks.

    > root@r1:/tmp# /usr/lib/frr/zebra --retain
    > ^C
    > =================================================================
    > ==2856697==ERROR: LeakSanitizer: detected memory leaks
    >
    > Direct leak of 192 byte(s) in 2 object(s) allocated from:
    >     #0 0x7f7eaf2f4610 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    >     #1 0x7f7eaec7fcec in qcalloc lib/memory.c:111
    >     #2 0x7f7eaed801bf in route_table_init_with_delegate lib/table.c:46
    >     #3 0x7f7eaed8307e in route_table_init lib/table.c:629
    >     #4 0x55d5d665db8e in zebra_vrf_enable zebra/zebra_vrf.c:152
    >     #5 0x7f7eaeda6e45 in vrf_enable lib/vrf.c:316
    >     #6 0x7f7eaeda7c25 in vrf_init lib/vrf.c:579
    >     #7 0x55d5d6660267 in zebra_vrf_init zebra/zebra_vrf.c:676
    >     #8 0x55d5d66058c0 in zebra_ns_init zebra/zebra_ns.c:452
    >     #9 0x55d5d64c2ca5 in main zebra/main.c:518
    >     #10 0x7f7eae835ca7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    >
    > Direct leak of 192 byte(s) in 2 object(s) allocated from:
    >     #0 0x7f7eaf2f4610 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    >     #1 0x7f7eaec7fcec in qcalloc lib/memory.c:111
    >     #2 0x7f7eaed801bf in route_table_init_with_delegate lib/table.c:46
    >     #3 0x7f7eaed8307e in route_table_init lib/table.c:629
    >     #4 0x55d5d665db06 in zebra_vrf_enable zebra/zebra_vrf.c:148
    >     #5 0x7f7eaeda6e45 in vrf_enable lib/vrf.c:316
    >     #6 0x55d5d64a17b2 in interface_vrf_change zebra/interface.c:1606
    >     #7 0x55d5d64a3f37 in zebra_if_dplane_ifp_handling zebra/interface.c:2074
    >     #8 0x55d5d64a6a70 in zebra_if_dplane_result zebra/interface.c:2463
    >     #9 0x55d5d6640e30 in rib_process_dplane_results zebra/zebra_rib.c:5594
    >     #10 0x7f7eaed9da9b in event_call lib/event.c:2754
    >     #11 0x7f7eaec49717 in frr_run lib/libfrr.c:1258
    >     #12 0x55d5d64c2da9 in main zebra/main.c:580
    >     #13 0x7f7eae835ca7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    >
    > Direct leak of 192 byte(s) in 2 object(s) allocated from:
    >     #0 0x7f7eaf2f4610 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    >     #1 0x7f7eaec7fcec in qcalloc lib/memory.c:111
    >     #2 0x7f7eaed801bf in route_table_init_with_delegate lib/table.c:46
    >     #3 0x7f7eaed8307e in route_table_init lib/table.c:629
    >     #4 0x55d5d665db8e in zebra_vrf_enable zebra/zebra_vrf.c:152
    >     #5 0x7f7eaeda6e45 in vrf_enable lib/vrf.c:316
    >     #6 0x55d5d64a17b2 in interface_vrf_change zebra/interface.c:1606
    >     #7 0x55d5d64a3f37 in zebra_if_dplane_ifp_handling zebra/interface.c:2074
    >     #8 0x55d5d64a6a70 in zebra_if_dplane_result zebra/interface.c:2463
    >     #9 0x55d5d6640e30 in rib_process_dplane_results zebra/zebra_rib.c:5594
    >     #10 0x7f7eaed9da9b in event_call lib/event.c:2754
    >     #11 0x7f7eaec49717 in frr_run lib/libfrr.c:1258
    >     #12 0x55d5d64c2da9 in main zebra/main.c:580
    >     #13 0x7f7eae835ca7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    >
    > Direct leak of 192 byte(s) in 2 object(s) allocated from:
    >     #0 0x7f7eaf2f4610 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
    >     #1 0x7f7eaec7fcec in qcalloc lib/memory.c:111
    >     #2 0x7f7eaed801bf in route_table_init_with_delegate lib/table.c:46
    >     #3 0x7f7eaed8307e in route_table_init lib/table.c:629
    >     #4 0x55d5d665db06 in zebra_vrf_enable zebra/zebra_vrf.c:148
    >     #5 0x7f7eaeda6e45 in vrf_enable lib/vrf.c:316
    >     #6 0x7f7eaeda7c25 in vrf_init lib/vrf.c:579
    >     #7 0x55d5d6660267 in zebra_vrf_init zebra/zebra_vrf.c:676
    >     #8 0x55d5d66058c0 in zebra_ns_init zebra/zebra_ns.c:452
    >     #9 0x55d5d64c2ca5 in main zebra/main.c:518
    >     #10 0x7f7eae835ca7 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

    Free the nexthop tracking tables before the early return. These tables
    are internal to zebra and are not needed anymore, even when the routes
    are retained in the kernel.

    Fixes: 9d97533e37 ("zebra: Refine cleanup when a VRF with configuration gets deleted")
    Signed-off-by: Louis Scalbert <louis.scalbert@6wind.com>

diff --git a/zebra/zebra_vrf.c b/zebra/zebra_vrf.c
index 8284dd2a65..7364dc22e4 100644
--- a/zebra/zebra_vrf.c
+++ b/zebra/zebra_vrf.c
@@ -247,17 +247,17 @@ static int zebra_vrf_disable(struct vrf *vrf)
 	 */
 	zebra_vrf_delete_update(zvrf);

-	/* If asked to retain routes, there's nothing more to do. */
-	if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
-		return 0;
-
-	/* Remove all routes. */
 	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
 		route_table_finish(zvrf->rnh_table[afi]);
 		zvrf->rnh_table[afi] = NULL;
 		route_table_finish(zvrf->rnh_table_multicast[afi]);
 		zvrf->rnh_table_multicast[afi] = NULL;
+	}

+	if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
+		return 0;
+
+	for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
 		for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
 			rib_close_table(zvrf->table[afi][safi]);
 	}