Skip to contentSkip to conversation

RDMA/multicast: Fix rb-tree corruption on MGID reassignment in join_handler

PATCH Applied lirongqing 3 participants 4 messages Sep 17 – Sep 23 InfiniBand

lore
Share & tools
lore

lirongqing

author
#0 lore raw

PATCH 1/1 RDMA/multicast: Fix rb-tree corruption on MGID reassignment in join_handler

From: Li RongQing <[email protected]>

When the SA assigns a wildcard (mgid0) join a concrete MGID that already has a group in the port table, join_handler() erases the group from the tree and calls mcast_insert() with allow_duplicates=false. mcast_insert() then finds the existing group and returns it without re-inserting, but join_handler() discards the return value, leaving the group erased yet still alive.

When the orphaned group's refcount later drops to zero, release_group() calls rb_erase() on the node that is no longer in the tree, corrupting the rb-tree (a stale parent pointer causes a valid subtree link to be overwritten with NULL).

Clear the node with RB_CLEAR_NODE() after the erase in join_handler() so release_group() can detect via RB_EMPTY_NODE() that it is already out of the tree and skip the second erase. A successful re-insert by mcast_insert() restores the in-tree state via rb_link_node().

---
 drivers/infiniband/core/multicast.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/infiniband/core/multicast.c +6 -1
index bea7df3..f549fd9 100644
@@ -189,7 +189,10 @@ static void release_group(struct mcast_group *group)
 
 	spin_lock_irqsave(&port->lock, flags);
 	if (atomic_dec_and_test(&group->refcount)) {
-		rb_erase(&group->node, &port->table);
+
+		if (!RB_EMPTY_NODE(&group->node))
+			rb_erase(&group->node, &port->table);
+
 		spin_unlock_irqrestore(&port->lock, flags);
 		kfree(group);
 		deref_port(port);
@@ -533,6 +536,8 @@ static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
 		group->rec = *rec;
 		if (mgids_changed) {
 			rb_erase(&group->node, &group->port->table);
+
+			RB_CLEAR_NODE(&group->node);
 			is_mgid0 = !memcmp(&mgid0, &group->rec.mgid,
 					   sizeof(mgid0));
 			mcast_insert(group->port, group, is_mgid0);
Signature · 1 line
2.9.4
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

From: Li RongQing <[email protected]>

When the SA assigns a wildcard (mgid0) join a concrete MGID that already has a group in the port table, join_handler() erases the group from the tree and calls mcast_insert() with allow_duplicates=false. mcast_insert() then finds the existing group and returns it without re-inserting, but join_handler() discards the return value, leaving the group erased yet still alive.

When the orphaned group's refcount later drops to zero, release_group() calls rb_erase() on the node that is no longer in the tree, corrupting the rb-tree (a stale parent pointer causes a valid subtree link to be overwritten with NULL).

Quoted 6 lines

Clear the node with RB_CLEAR_NODE() after the erase in join_handler() so release_group() can detect via RB_EMPTY_NODE() that it is already out of the tree and skip the second erase. A successful re-insert by mcast_insert() restores the in-tree state via rb_link_node().

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to lirongqing
Quoted 19 lines · lirongqing

From: Li RongQing <[email protected]>

When the SA assigns a wildcard (mgid0) join a concrete MGID that already has a group in the port table, join_handler() erases the group from the tree and calls mcast_insert() with allow_duplicates=false. mcast_insert() then finds the existing group and returns it without re-inserting, but join_handler() discards the return value, leaving the group erased yet still alive.

When the orphaned group's refcount later drops to zero, release_group() calls rb_erase() on the node that is no longer in the tree, corrupting the rb-tree (a stale parent pointer causes a valid subtree link to be overwritten with NULL).

Clear the node with RB_CLEAR_NODE() after the erase in join_handler() so release_group() can detect via RB_EMPTY_NODE() that it is already out of the tree and skip the second erase. A successful re-insert by mcast_insert() restores the in-tree state via rb_link_node().

Quoted 4 lines
---
 drivers/infiniband/core/multicast.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

I'm not sure whether this change makes any difference, but at least it is harmless.

Thanks

#3 lore raw
In reply to lirongqing
Quoted 8 lines · lirongqing

When the SA assigns a wildcard (mgid0) join a concrete MGID that already has a group in the port table, join_handler() erases the group from the tree and calls mcast_insert() with allow_duplicates=false. mcast_insert() then finds the existing group and returns it without re-inserting, but join_handler() discards the return value, leaving the group erased yet still alive.

[...]

Applied, thanks!

[1/1] RDMA/multicast: Fix rb-tree corruption on MGID reassignment in join_handler

      https://git.kernel.org/rdma/rdma/c/d1fa82ef992bc1

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>