Skip to contentSkip to conversation

xfrm: iptfs: fix deadlock in iptfs_destroy_state

PATCH In review Dudu Lu 2 participants 2 messages Apr 13 – Apr 15

Subsystems (3)
lore
Share & tools
lore

Dudu Lu

author
#0 lore raw

PATCH 1/1 xfrm: iptfs: fix deadlock in iptfs_destroy_state

iptfs_destroy_state() acquires x->lock (spin_lock_bh) and then calls hrtimer_cancel(&xtfs->iptfs_timer). The timer callback iptfs_delay_timer() also acquires x->lock (spin_lock). If the timer fires on another CPU during destroy, hrtimer_cancel() waits for the callback to complete, but the callback is blocked trying to acquire the same lock — a classic ABBA deadlock.

The same pattern exists for drop_timer: destroy holds drop_lock and calls hrtimer_cancel(&xtfs->drop_timer), while iptfs_drop_timer() also acquires drop_lock.

Fix by cancelling the timers before acquiring the locks. The timer callbacks check for state validity, so a late cancel is safe. The queue splice is still done under the lock for consistency.

Fixes 4b3faf610cc6 ("xfrm: iptfs: add new iptfs xfrm mode impl")
---
 net/xfrm/xfrm_iptfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/xfrm/xfrm_iptfs.c +4 -4
index 97bc979e55ba..11291b87158c 100644
@@ -2708,8 +2708,10 @@ static void iptfs_destroy_state(struct xfrm_state *x)
 	if (!xtfs)
 		return;
 
-	spin_lock_bh(&xtfs->x->lock);
 	hrtimer_cancel(&xtfs->iptfs_timer);
+	hrtimer_cancel(&xtfs->drop_timer);
+
+	spin_lock_bh(&xtfs->x->lock);
 	__skb_queue_head_init(&list);
 	skb_queue_splice_init(&xtfs->queue, &list);
 	spin_unlock_bh(&xtfs->x->lock);
@@ -2717,9 +2719,7 @@ static void iptfs_destroy_state(struct xfrm_state *x)
 	while ((skb = __skb_dequeue(&list)))
 		kfree_skb(skb);
 
-	spin_lock_bh(&xtfs->drop_lock);
-	hrtimer_cancel(&xtfs->drop_timer);
-	spin_unlock_bh(&xtfs->drop_lock);
+	/* drop_timer already cancelled above */
 
 	if (xtfs->ra_newskb)
 		kfree_skb(xtfs->ra_newskb);
Signature · 1 line
2.39.3 (Apple Git-145)
reviewer
#1 lore raw

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
xfrm: iptfs: fix deadlock in iptfs_destroy_state

This commit resolves an ABBA deadlock in iptfs_destroy_state() by moving the cancellation of iptfs_timer and drop_timer outside of their respective spinlocks.

Quoted 6 lines
net/xfrm/xfrm_iptfs.c +0 -0
index 97bc979e55baf..11291b87158cb 100644
@@ -2708,8 +2708,10 @@ static void iptfs_destroy_state(struct xfrm_state *x)
 	if (!xtfs)

net/xfrm/xfrm_iptfs.c static void iptfs_destroy_state(struct xfrm_state *x) · line 2717

Quoted 10 lines
net/xfrm/xfrm_iptfs.c +3 -1
 		return;
 
-	spin_lock_bh(&xtfs->x->lock);
 	hrtimer_cancel(&xtfs->iptfs_timer);
+	hrtimer_cancel(&xtfs->drop_timer);
+
+	spin_lock_bh(&xtfs->x->lock);
 	__skb_queue_head_init(&list);
 	skb_queue_splice_init(&xtfs->queue, &list);
 	spin_unlock_bh(&xtfs->x->lock);

Is it safe to call hrtimer_cancel() and skb_queue_splice_init() on xtfs here if it was created via iptfs_clone_state() but failed initialization?

If iptfs_clone_state() uses kmemdup() to shallow-copy the xtfs structure from the original state, the copy includes xtfs->queue and the hrtimer structs (iptfs_timer and drop_timer). Reinitialization only happens later in iptfs_init_state() -> __iptfs_init_state().

If x->type->init_state() fails during __xfrm_init_state(), the state is destroyed via xfrm_state_put() before iptfs_init_state() is called.

On this destruction path, does calling hrtimer_cancel() result in a regression where it operates on the copied timers? If the original timer was queued, the copied timer has the HRTIMER_STATE_ENQUEUED bit set, but its rb_node is not natively in the rbtree. Calling hrtimer_cancel() might call rb_erase_cached() using the cloned node's pointers (which point to the original node's parent/children).

Additionally, since xtfs->queue is a shallow copy, its next/prev pointers would point to the original state's queue. Could splicing it here lead to a regression by modifying the original state's skb queue? If the original queue was empty, it points to itself, and this code might dequeue it and call kfree_skb() on an address inside orig->mode_data.