Skip to contentSkip to conversation

platform/surface: aggregator: Fix use-after-free in ssh_ptl_shutdown()

PATCH In review Wentao Liang 3 participants 3 messages Sep 17 – Nov 3

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/surface: aggregator: Fix use-after-free in ssh_ptl_shutdown()

ssh_packet_put() is called on the loop variable of both list_for_each_entry() loops in ssh_ptl_shutdown(), freeing the packet before the iterator advances past it. Use list_for_each_entry_safe() and move pending packets to complete_p, as their links use pending_node, so that each loop walks a single list.

Fixes c167b9c7e3d6 ("platform/surface: Add Surface Aggregator subsystem")
---
 drivers/platform/surface/aggregator/ssh_packet_layer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/surface/aggregator/ssh_packet_layer.c +3 -3
index 3dd22856570f..871780828419 100644
@@ -1983,13 +1983,13 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
 		smp_mb__before_atomic();
 		clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
 
-		list_move_tail(&p->pending_node, &complete_q);
+		list_move_tail(&p->pending_node, &complete_p);
 	}
 	atomic_set(&ptl->pending.count, 0);
 	spin_unlock(&ptl->pending.lock);
 
 	/* Complete and drop packets on complete_q. */
-	list_for_each_entry(p, &complete_q, queue_node) {
+	list_for_each_entry_safe(p, n, &complete_q, queue_node) {
 		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
 			__ssh_ptl_complete(p, -ESHUTDOWN);
 
@@ -1997,7 +1997,7 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
 	}
 
 	/* Complete and drop packets on complete_p. */
-	list_for_each_entry(p, &complete_p, pending_node) {
+	list_for_each_entry_safe(p, n, &complete_p, pending_node) {
 		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
 			__ssh_ptl_complete(p, -ESHUTDOWN);
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 5 lines · Wentao Liang

ssh_packet_put() is called on the loop variable of both list_for_each_entry() loops in ssh_ptl_shutdown(), freeing the packet before the iterator advances past it. Use list_for_each_entry_safe() and move pending packets to complete_p, as their links use pending_node, so that each loop walks a single list.

It sounds like there are two independent issues as per your description, so two patches would be needed.

Quoted 37 lines
Fixes c167b9c7e3d6 ("platform/surface: Add Surface Aggregator subsystem")
---
 drivers/platform/surface/aggregator/ssh_packet_layer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/surface/aggregator/ssh_packet_layer.c +3 -3
index 3dd22856570f..871780828419 100644
@@ -1983,13 +1983,13 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
 		smp_mb__before_atomic();
 		clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
 
-		list_move_tail(&p->pending_node, &complete_q);
+		list_move_tail(&p->pending_node, &complete_p);
 	}
 	atomic_set(&ptl->pending.count, 0);
 	spin_unlock(&ptl->pending.lock);
 
 	/* Complete and drop packets on complete_q. */
-	list_for_each_entry(p, &complete_q, queue_node) {
+	list_for_each_entry_safe(p, n, &complete_q, queue_node) {
 		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
 			__ssh_ptl_complete(p, -ESHUTDOWN);
 
@@ -1997,7 +1997,7 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
 	}
 
 	/* Complete and drop packets on complete_p. */
-	list_for_each_entry(p, &complete_p, pending_node) {
+	list_for_each_entry_safe(p, n, &complete_p, pending_node) {
 		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
 			__ssh_ptl_complete(p, -ESHUTDOWN);
 
Signature · 1 line
 i.
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Wentao Liang

Am 17.09.2026 um 15:56 schrieb Wentao Liang:

Quoted 9 lines

ssh_packet_put() is called on the loop variable of both list_for_each_entry() loops in ssh_ptl_shutdown(), freeing the packet before the iterator advances past it. Use list_for_each_entry_safe() and move pending packets to complete_p, as their links use pending_node, so that each loop walks a single list.

Fixes c167b9c7e3d6 ("platform/surface: Add Surface Aggregator subsystem")

Thanks! Overall valid fix, but similar to Ilpo I'd prefer this to be two separate patches, and a better commit message for the typo. See below.

With that, feel free to add

Quoted 9 lines
---
  drivers/platform/surface/aggregator/ssh_packet_layer.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/surface/aggregator/ssh_packet_layer.c +0 -0
index 3dd22856570f..871780828419 100644

drivers/platform/surface/aggregator/ssh_packet_layer.c void ssh_ptl_shutdown(struct ssh_ptl *ptl) · line 1986

Quoted 5 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c +1 -1
@@ -1983,13 +1983,13 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
  		smp_mb__before_atomic();
  		clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
 
-		list_move_tail(&p->pending_node, &complete_q);
+		list_move_tail(&p->pending_node, &complete_p);

This is fixes typo and should be addressed in a separate patch.

Quoted 19 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c +2 -2
  	}
  	atomic_set(&ptl->pending.count, 0);
  	spin_unlock(&ptl->pending.lock);
 
  	/* Complete and drop packets on complete_q. */
-	list_for_each_entry(p, &complete_q, queue_node) {
+	list_for_each_entry_safe(p, n, &complete_q, queue_node) {
  		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  			__ssh_ptl_complete(p, -ESHUTDOWN);
 
@@ -1997,7 +1997,7 @@ void ssh_ptl_shutdown(struct ssh_ptl *ptl)
  	}
 
  	/* Complete and drop packets on complete_p. */
-	list_for_each_entry(p, &complete_p, pending_node) {
+	list_for_each_entry_safe(p, n, &complete_p, pending_node) {
  		if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
  			__ssh_ptl_complete(p, -ESHUTDOWN);