git: c15a606d2f44 - main - route/fib_algo: Fix nexthop reference leak on FLM_BATCH

From: Pouria Mousavizadeh Tehrani <pouria_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 19:11:22 UTC
The branch main has been updated by pouria:

URL: https://cgit.FreeBSD.org/src/commit/?id=c15a606d2f4443e3f3ba775532f0f499efaff3b7

commit c15a606d2f4443e3f3ba775532f0f499efaff3b7
Author:     Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
AuthorDate: 2026-11-18 19:00:11 +0000
Commit:     Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
CommitDate: 2026-11-18 19:09:00 +0000

    route/fib_algo: Fix nexthop reference leak on FLM_BATCH
    
    handle_rtable_change_cb() references the new nexthop before calling
    the algorithm.  If the algorithm asks to batch the change by
    returning FLM_BATCH, queue_rtable_change() references the same
    nexthop again for the queued entry, while apply_rtable_changes()
    only unreferences the old nexthop of each entry.
    
    Reference new nhop once in handle_rtable_change_cb(), before
    deciding whether the change is queued or passed to the algorithm,
    and do not reference it again in fill_change_entry().  This also
    leaves the queue reallocation as the only way for
    queue_rtable_change() to fail.
    
    Currently, dxr is the only algorithm returning FLM_BATCH.
    
    Fixes:          6b8ef0d428c9 ("Add batched update support for the fib algo.")
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D61577
---
 sys/net/route/fib_algo.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/sys/net/route/fib_algo.c b/sys/net/route/fib_algo.c
index edeac703fedc..92de90183257 100644
--- a/sys/net/route/fib_algo.c
+++ b/sys/net/route/fib_algo.c
@@ -734,7 +734,11 @@ apply_rtable_changes(struct fib_data *fd)
 	return (result == FLM_SUCCESS);
 }
 
-static bool
+/*
+ * Fills in queue entry @ce with the change described by @rc.
+ * The caller holds the reference to the new nexthop.
+ */
+static void
 fill_change_entry(struct fib_data *fd, struct fib_change_entry *ce, struct rib_cmd_info *rc)
 {
 	int plen = 0;
@@ -755,12 +759,6 @@ fill_change_entry(struct fib_data *fd, struct fib_change_entry *ce, struct rib_c
 	ce->plen = plen;
 	ce->nh_old = rc->rc_nh_old;
 	ce->nh_new = rc->rc_nh_new;
-	if (ce->nh_new != NULL) {
-		if (fib_ref_nhop(fd, ce->nh_new) == 0)
-			return (false);
-	}
-
-	return (true);
 }
 
 static bool
@@ -787,7 +785,9 @@ queue_rtable_change(struct fib_data *fd, struct rib_cmd_info *rc)
 		q->size = q_size;
 	}
 
-	return (fill_change_entry(fd, &q->entries[q->count++], rc));
+	fill_change_entry(fd, &q->entries[q->count++], rc);
+
+	return (true);
 }
 
 /*
@@ -827,21 +827,6 @@ handle_rtable_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
 		return;
 	}
 
-	/*
-	 * Algo requested updates to be delivered in batches.
-	 * Add the current change to the queue and return.
-	 */
-	if (fd->fd_batch) {
-		if (immediate_sync) {
-			if (!queue_rtable_change(fd, rc) || !apply_rtable_changes(fd))
-				rebuild_fd(fd, "batch sync failed");
-		} else {
-			if (!queue_rtable_change(fd, rc))
-				schedule_fd_rebuild(fd, "batch queue failed");
-		}
-		return;
-	}
-
 	/*
 	 * Maintain guarantee that every nexthop returned by the dataplane
 	 *  lookup has > 0 refcount, so can be safely referenced within current
@@ -857,6 +842,21 @@ handle_rtable_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
 		}
 	}
 
+	/*
+	 * Algo requested updates to be delivered in batches.
+	 * Add the current change to the queue and return.
+	 */
+	if (fd->fd_batch) {
+		if (immediate_sync) {
+			if (!queue_rtable_change(fd, rc) || !apply_rtable_changes(fd))
+				rebuild_fd(fd, "batch sync failed");
+		} else {
+			if (!queue_rtable_change(fd, rc))
+				schedule_fd_rebuild(fd, "batch queue failed");
+		}
+		return;
+	}
+
 	result = fd->fd_flm->flm_change_rib_item_cb(rnh, rc, fd->fd_algo_data);
 
 	switch (result) {