Commit 806b2a17c3 for frr

commit 806b2a17c3b1e0f9e0e3554bb7e422a38ec6ac53
Author: Lincoln Thurlow <lincoln@isi.edu>
Date:   Tue Dec 1 11:00:45 2026 -0800

    pathd: refuse duplicate segment list names

    pathd can crash when two segment lists get the same name, for example
    when a PCEP path names its segment list "<name>-<plsp_id>" and a
    configured segment list already has that name.

    srte_segment_list_add() ignores the result of RB_INSERT(), so it hands
    out a duplicate that is not in the tree, and removing it from the tree
    later dereferences a NULL pointer.

    This commit makes srte_segment_list_add() refuse a duplicate and
    return NULL, and srte_segment_list_del() only remove a segment list
    that is in the tree. A PCEP path whose segment list name is in use is
    rejected, and a configured segment list is refused when a PCEP segment
    list already has its name.

    Signed-off-by: Lincoln Thurlow <lincoln@isi.edu>

diff --git a/pathd/path_nb_config.c b/pathd/path_nb_config.c
index 4509a38789..88f5281ebd 100644
--- a/pathd/path_nb_config.c
+++ b/pathd/path_nb_config.c
@@ -6,6 +6,7 @@
 #include <zebra.h>
 #include <lib_errors.h>

+#include "printfrr.h"
 #include "northbound.h"
 #include "libfrr.h"

@@ -28,11 +29,25 @@ int pathd_srte_segment_list_create(struct nb_cb_create_args *args)
 	struct srte_segment_list *segment_list;
 	const char *name;

+	if (args->event == NB_EV_VALIDATE) {
+		/* Refuse names reserved for, or already used by PCEP */
+		name = yang_dnode_get_string(args->dnode, "name");
+		segment_list = srte_segment_list_find(name);
+		if (segment_list != NULL && segment_list->protocol_origin == SRTE_ORIGIN_PCEP) {
+			snprintfrr(args->errmsg, args->errmsg_len,
+				   "A PCEP-created segment list named %s already exists", name);
+			return NB_ERR_VALIDATION;
+		}
+		return NB_OK;
+	}
+
 	if (args->event != NB_EV_APPLY)
 		return NB_OK;

 	name = yang_dnode_get_string(args->dnode, "name");
 	segment_list = srte_segment_list_add(name);
+	/* VALIDATE refused a PCEP list's name, and YANG keys the others */
+	assert(segment_list != NULL);
 	nb_running_set_entry(args->dnode, segment_list);
 	SET_FLAG(segment_list->flags, F_SEGMENT_LIST_NEW);

diff --git a/pathd/path_pcep_config.c b/pathd/path_pcep_config.c
index 5ee71d053d..0a440e2227 100644
--- a/pathd/path_pcep_config.c
+++ b/pathd/path_pcep_config.c
@@ -400,6 +400,11 @@ int path_pcep_config_update_path(struct path *path)
 	segment_list_name = segment_list_name_buff;

 	segment_list = srte_segment_list_add(segment_list_name);
+	if (segment_list == NULL) {
+		zlog_warn("segment list name %s of path %s (plsp %u) is already in use, rejecting the path",
+			  segment_list_name, path->name, path->plsp_id);
+		return PATH_NB_ERR;
+	}
 	segment_list->protocol_origin = path->update_origin;
 	strlcpy(segment_list->originator, path->originator,
 		sizeof(segment_list->originator));
diff --git a/pathd/pathd.c b/pathd/pathd.c
index 475b29956a..0266f5248e 100644
--- a/pathd/pathd.c
+++ b/pathd/pathd.c
@@ -121,7 +121,8 @@ static void srte_policy_status_log(struct srte_policy *policy)
  * Adds a segment list to pathd.
  *
  * @param name The name of the segment list to add
- * @return The added segment list
+ * @return The added segment list, or NULL if a segment list with that
+ *	   name already exists
  */
 struct srte_segment_list *srte_segment_list_add(const char *name)
 {
@@ -130,7 +131,12 @@ struct srte_segment_list *srte_segment_list_add(const char *name)
 	segment_list = XCALLOC(MTYPE_PATH_SEGMENT_LIST, sizeof(*segment_list));
 	strlcpy(segment_list->name, name, sizeof(segment_list->name));
 	RB_INIT(srte_segment_entry_head, &segment_list->segments);
-	RB_INSERT(srte_segment_list_head, &srte_segment_lists, segment_list);
+	/* Refuse a name already in use */
+	if (RB_INSERT(srte_segment_list_head, &srte_segment_lists, segment_list) != NULL) {
+		zlog_warn("%s: a segment list named %s already exists", __func__, name);
+		XFREE(MTYPE_PATH_SEGMENT_LIST, segment_list);
+		return NULL;
+	}

 	return segment_list;
 }
@@ -150,7 +156,14 @@ void srte_segment_list_del(struct srte_segment_list *segment_list)
 			 &segment_list->segments, safe_seg) {
 		srte_segment_entry_del(segment);
 	}
-	RB_REMOVE(srte_segment_list_head, &srte_segment_lists, segment_list);
+
+	/* Remove this list, not another list with the same name */
+	if (RB_FIND(srte_segment_list_head, &srte_segment_lists, segment_list) == segment_list) {
+		RB_REMOVE(srte_segment_list_head, &srte_segment_lists, segment_list);
+	} else {
+		zlog_warn("%s: segment list %s is not in the segment list tree", __func__,
+			  segment_list->name);
+	}
 	XFREE(MTYPE_PATH_SEGMENT_LIST, segment_list);
 }