Skip to contentSkip to conversation

fuse: fix io-uring abort races and cancel null deref

PATCH Superseded Joanne Koong 1 participant 8 messages May 16 – Jun 8

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Joanne Koong

author maintainer
#0 lore raw

v2 cover fuse: fix io-uring abort races and cancel null deref

Patch 1 and 2 address races between abort and fuse uring ring creation / registration. Patch 3 fixes fuse_uring_cancel() moving cancelled entries (which have no fuse_req) to ent_in_userspace, which can crash ent_list_request_expired(). It also removes the queue_refs > 0 gate in fuse_uring_abort() so that request flushing always happens, since cancel now decrements queue_refs directly.

For some more context, please see the discussions in [1].

[1] https://lore.kernel.org/linux-fsdevel/20251021-io-uring-fixes-cancel-mem-leak-v1-0-26b78b2c973c@ddn.com/

Changelog
--------
v1: https://lore.kernel.org/fuse-devel/[email protected]/
v1 -> v2:
* Reorder commits 1 and 2 and edit commit 1 message (Bernd)

Joanne Koong (3):
fuse: fix race between registration and connection abortion
fuse: check connection abort during ring creation
fuse: fix moving cancelled entry to ent_in_userspace list

 fs/fuse/dev_uring.c   | 32 ++++++++++++++++++++++++--------
 fs/fuse/dev_uring_i.h |  6 +++---
 2 files changed, 27 insertions(+), 11 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH v2 1/3 fuse: fix race between registration and connection abortion

This fixes this race:
- thread a: io_uring_enter -> register sqe -> fuse_uring_create_ring_ent -> allocate ent but doesn't grab queue_ref yet
- thread b: fuse_conn_destroy() -> fuse_chan_abort() -> fuse_uring_abort() is a no-op due to queue ref being 0
- thread a: grabs the queue_ref, queue_ref is now 1, rest of fuse_uring_do_register() logic executes
- thread b: fuse_chan_abort() returns, fuse_chan_wait_aborted() now runs and calls
"wait_event(ring->stop_waitq, atomic_read(&ring->queue_refs) == 0);"
The abort/unmount thread will hang indefinitely in unkillable state as nothing will decrement queue_refs or wake stop_waitq, and the ring, queue, and ent are leaked.

Fix this by checking fch->connected under fch->lock after the created ent has grabbed a ref count on the queue. This ensures that in the scenario above, it is guaranteed that we either release the queue ref and wake up stop_waitq (in case fuse_chan_wait_aborted() is already waiting) in fuse_uring_do_register() when we detect !fch->connected, or if the connection is aborted after the check, it is guaranteed that the async teardown worker will be running in the background cleaning up ents and decrementing the ent's ref on the queue, which will unblock the eventual queue and ring teardown.

Fixes 24fe962c86f5 ("fuse: {io-uring} Handle SQEs - register commands")
---
 fs/fuse/dev_uring.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
fs/fuse/dev_uring.c +16 -6
index e467b23e6895..99ebb7c9cc61 100644
@@ -973,15 +973,26 @@ static bool is_ring_ready(struct fuse_ring *ring, int current_qid)
 /*
  * fuse_uring_req_fetch command handling
  */
-static void fuse_uring_do_register(struct fuse_ring_ent *ent,
-				   struct io_uring_cmd *cmd,
-				   unsigned int issue_flags)
+static int fuse_uring_do_register(struct fuse_ring_ent *ent,
+				  struct io_uring_cmd *cmd,
+				  unsigned int issue_flags)
 {
 	struct fuse_ring_queue *queue = ent->queue;
 	struct fuse_ring *ring = queue->ring;
 	struct fuse_chan *fch = ring->chan;
 	struct fuse_iqueue *fiq = &fch->iq;
 
+	spin_lock(&fch->lock);
+	/* abort teardown path is running or has run */
+	if (!fch->connected) {
+		spin_unlock(&fch->lock);
+		if (atomic_dec_and_test(&ring->queue_refs))
+			wake_up_all(&ring->stop_waitq);
+		kfree(ent);
+		return -ECONNABORTED;
+	}
+	spin_unlock(&fch->lock);
+
 	fuse_uring_prepare_cancel(cmd, issue_flags, ent);
 
 	spin_lock(&queue->lock);
@@ -998,6 +1009,7 @@ static void fuse_uring_do_register(struct fuse_ring_ent *ent,
 			wake_up_all(&fch->blocked_waitq);
 		}
 	}
+	return 0;
 }
 
 /*
@@ -1114,9 +1126,7 @@ static int fuse_uring_register(struct io_uring_cmd *cmd,
 	if (IS_ERR(ent))
 		return PTR_ERR(ent);
 
-	fuse_uring_do_register(ent, cmd, issue_flags);
-
-	return 0;
+	return fuse_uring_do_register(ent, cmd, issue_flags);
 }
 
 /*
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH v2 2/3 fuse: check connection abort during ring creation

In reply to Joanne Koong

Check fch->connected under fch->lock in fuse_uring_create() before attaching a new ring. Without this, a race between fuse_uring_create() and fuse_chan_abort() can result in the ring, queue, and fpq.processing table being created after fuse_uring_abort() has already run, leading to unnecessary allocation and teardown. These are eventually cleaned up by fuse_uring_destruct() but will linger until the process exits, even with the connection aborted.

---
 fs/fuse/dev_uring.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/fuse/dev_uring.c +4 -0
index 99ebb7c9cc61..d9108b5b5db8 100644
@@ -244,6 +244,10 @@ static struct fuse_ring *fuse_uring_create(struct fuse_chan *fch)
 	max_payload_size = max(max_payload_size, fch->max_pages * PAGE_SIZE);
 
 	spin_lock(&fch->lock);
+	if (!fch->connected) {
+		spin_unlock(&fch->lock);
+		goto out_err;
+	}
 	if (fch->ring) {
 		/* race, another thread created the ring in the meantime */
 		spin_unlock(&fch->lock);
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH v2 3/3 fuse: fix moving cancelled entry to ent_in_userspace list

In reply to Joanne Koong

fuse_uring_cancel() moves entries that are available (these have no reqs attached) to the ent_in_userspace list. ent_list_request_expired() checks the first entry on ent_in_userspace and dereferences ent->fuse_req unconditionally, which will crash on a cancelled entry that was moved to this list.

Fix this by freeing the entry and dropping queue_refs directly in fuse_uring_cancel(). This is safe because cancel is the cancel handler itself - after io_uring_cmd_done(), no more cancels will be dispatched for this command, and teardown serializes with cancel via queue->lock.

Since cancel now decrements queue_refs, fuse_uring_abort() must no longer gate fuse_uring_abort_end_requests() on queue_refs > 0, as cancelled entries may have already dropped queue_refs while requests are still queued. Remove the gate so abort always flushes requests and stops queues.

Fixes 4fea593e625c ("fuse: optimize over-io-uring request expiration check")
---
 fs/fuse/dev_uring.c   | 6 ++++--
 fs/fuse/dev_uring_i.h | 6 +++---
 2 files changed, 7 insertions(+), 5 deletions(-)
fs/fuse/dev_uring.c +4 -2
index d9108b5b5db8..f4ba64a1796a 100644
@@ -511,8 +511,7 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
 	queue = ent->queue;
 	spin_lock(&queue->lock);
 	if (ent->state == FRRS_AVAILABLE) {
-		ent->state = FRRS_USERSPACE;
-		list_move_tail(&ent->list, &queue->ent_in_userspace);
+		list_del_init(&ent->list);
 		need_cmd_done = true;
 		ent->cmd = NULL;
 	}
@@ -521,6 +520,9 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
 	if (need_cmd_done) {
 		/* no queue lock to avoid lock order issues */
 		io_uring_cmd_done(cmd, -ENOTCONN, issue_flags);
+		kfree(ent);
+		if (atomic_dec_and_test(&queue->ring->queue_refs))
+			wake_up_all(&queue->ring->stop_waitq);
 	}
 }
 
fs/fuse/dev_uring_i.h +3 -3
index 368f4d0790eb..22ec67e39ee0 100644
@@ -150,10 +150,10 @@ static inline void fuse_uring_abort(struct fuse_chan *fch)
 	if (ring == NULL)
 		return;
 
-	if (atomic_read(&ring->queue_refs) > 0) {
-		fuse_uring_abort_end_requests(ring);
+	fuse_uring_abort_end_requests(ring);
+
+	if (atomic_read(&ring->queue_refs) > 0)
 		fuse_uring_stop_queues(ring);
-	}
 }
 
 static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch)
Signature · 1 line
2.52.0
author maintainer
#4 lore raw

Re: [PATCH v2 3/3] fuse: fix moving cancelled entry to ent_in_userspace list

Quoted 18 lines · Joanne Koong

fuse_uring_cancel() moves entries that are available (these have no reqs attached) to the ent_in_userspace list. ent_list_request_expired() checks the first entry on ent_in_userspace and dereferences ent->fuse_req unconditionally, which will crash on a cancelled entry that was moved to this list.

Fix this by freeing the entry and dropping queue_refs directly in fuse_uring_cancel(). This is safe because cancel is the cancel handler itself - after io_uring_cmd_done(), no more cancels will be dispatched for this command, and teardown serializes with cancel via queue->lock.

Since cancel now decrements queue_refs, fuse_uring_abort() must no longer gate fuse_uring_abort_end_requests() on queue_refs > 0, as cancelled entries may have already dropped queue_refs while requests are still queued. Remove the gate so abort always flushes requests and stops queues.

Quoted 7 lines
Fixes 4fea593e625c ("fuse: optimize over-io-uring request expiration check")

Bernd added his Reviewed-by to this as well [1], but had accidentally added it to v1 instead of this v2 series, but this patch is identical for both series.

Thanks,
Joanne

[1] https://lore.kernel.org/all/[email protected]/

Quoted 50 lines
---
 fs/fuse/dev_uring.c   | 6 ++++--
 fs/fuse/dev_uring_i.h | 6 +++---
 2 files changed, 7 insertions(+), 5 deletions(-)
fs/fuse/dev_uring.c +4 -2
index d9108b5b5db8..f4ba64a1796a 100644
@@ -511,8 +511,7 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
        queue = ent->queue;
        spin_lock(&queue->lock);
        if (ent->state == FRRS_AVAILABLE) {
-               ent->state = FRRS_USERSPACE;
-               list_move_tail(&ent->list, &queue->ent_in_userspace);
+               list_del_init(&ent->list);
                need_cmd_done = true;
                ent->cmd = NULL;
        }
@@ -521,6 +520,9 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
        if (need_cmd_done) {
                /* no queue lock to avoid lock order issues */
                io_uring_cmd_done(cmd, -ENOTCONN, issue_flags);
+               kfree(ent);
+               if (atomic_dec_and_test(&queue->ring->queue_refs))
+                       wake_up_all(&queue->ring->stop_waitq);
        }
 }
 
fs/fuse/dev_uring_i.h +3 -4
index 368f4d0790eb..22ec67e39ee0 100644
@@ -150,10 +150,10 @@ static inline void fuse_uring_abort(struct fuse_chan *fch)
        if (ring == NULL)
                return;
 
-       if (atomic_read(&ring->queue_refs) > 0) {
-               fuse_uring_abort_end_requests(ring);
+       fuse_uring_abort_end_requests(ring);
+
+       if (atomic_read(&ring->queue_refs) > 0)
                fuse_uring_stop_queues(ring);
-       }
 }
 
 static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch)
--

2.52.0

author maintainer
#5 lore raw

Re: [PATCH v2 3/3] fuse: fix moving cancelled entry to ent_in_userspace list

Quoted 20 lines · Joanne Koong
Quoted 18 lines · Joanne Koong

fuse_uring_cancel() moves entries that are available (these have no reqs attached) to the ent_in_userspace list. ent_list_request_expired() checks the first entry on ent_in_userspace and dereferences ent->fuse_req unconditionally, which will crash on a cancelled entry that was moved to this list.

Fix this by freeing the entry and dropping queue_refs directly in fuse_uring_cancel(). This is safe because cancel is the cancel handler itself - after io_uring_cmd_done(), no more cancels will be dispatched for this command, and teardown serializes with cancel via queue->lock.

Since cancel now decrements queue_refs, fuse_uring_abort() must no longer gate fuse_uring_abort_end_requests() on queue_refs > 0, as cancelled entries may have already dropped queue_refs while requests are still queued. Remove the gate so abort always flushes requests and stops queues.

Quoted 7 lines
Quoted 7 lines
Fixes 4fea593e625c ("fuse: optimize over-io-uring request expiration check")

I added co-developed-by tags because I wanted to give attribution to the discussion we had in [1] but checkpatch flags this for not having signed-off-bys. I'll submit a v3 that changes these to Suggested-by.

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/adiiTGjP1tqZfIrI@fedora/

Quoted 60 lines

Bernd added his Reviewed-by to this as well [1], but had accidentally added it to v1 instead of this v2 series, but this patch is identical for both series.

Thanks,
Joanne

[1] https://lore.kernel.org/all/[email protected]/

Quoted 50 lines
---
 fs/fuse/dev_uring.c   | 6 ++++--
 fs/fuse/dev_uring_i.h | 6 +++---
 2 files changed, 7 insertions(+), 5 deletions(-)
fs/fuse/dev_uring.c +4 -2
index d9108b5b5db8..f4ba64a1796a 100644
@@ -511,8 +511,7 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
        queue = ent->queue;
        spin_lock(&queue->lock);
        if (ent->state == FRRS_AVAILABLE) {
-               ent->state = FRRS_USERSPACE;
-               list_move_tail(&ent->list, &queue->ent_in_userspace);
+               list_del_init(&ent->list);
                need_cmd_done = true;
                ent->cmd = NULL;
        }
@@ -521,6 +520,9 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
        if (need_cmd_done) {
                /* no queue lock to avoid lock order issues */
                io_uring_cmd_done(cmd, -ENOTCONN, issue_flags);
+               kfree(ent);
+               if (atomic_dec_and_test(&queue->ring->queue_refs))
+                       wake_up_all(&queue->ring->stop_waitq);
        }
 }
 
fs/fuse/dev_uring_i.h +3 -4
index 368f4d0790eb..22ec67e39ee0 100644
@@ -150,10 +150,10 @@ static inline void fuse_uring_abort(struct fuse_chan *fch)
        if (ring == NULL)
                return;
 
-       if (atomic_read(&ring->queue_refs) > 0) {
-               fuse_uring_abort_end_requests(ring);
+       fuse_uring_abort_end_requests(ring);
+
+       if (atomic_read(&ring->queue_refs) > 0)
                fuse_uring_stop_queues(ring);
-       }
 }
 
 static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch)
--

2.52.0

author maintainer
#6 lore raw
In reply to Joanne Koong
Quoted 23 lines · Joanne Koong

Patch 1 and 2 address races between abort and fuse uring ring creation / registration. Patch 3 fixes fuse_uring_cancel() moving cancelled entries (which have no fuse_req) to ent_in_userspace, which can crash ent_list_request_expired(). It also removes the queue_refs > 0 gate in fuse_uring_abort() so that request flushing always happens, since cancel now decrements queue_refs directly.

For some more context, please see the discussions in [1].

[1] https://lore.kernel.org/linux-fsdevel/20251021-io-uring-fixes-cancel-mem-leak-v1-0-26b78b2c973c@ddn.com/

Changelog
--------
v1: https://lore.kernel.org/fuse-devel/[email protected]/
v1 -> v2:
* Reorder commits 1 and 2 and edit commit 1 message (Bernd)

Joanne Koong (3):
fuse: fix race between registration and connection abortion
fuse: check connection abort during ring creation
fuse: fix moving cancelled entry to ent_in_userspace list

Quoted 7 lines
 fs/fuse/dev_uring.c   | 32 ++++++++++++++++++++++++--------
 fs/fuse/dev_uring_i.h |  6 +++---
 2 files changed, 27 insertions(+), 11 deletions(-)

--
2.52.0

Miklos, this series is ready from my side - could you pick it up for your tree?

Thanks,
Joanne

author maintainer
#7 lore raw
Quoted 29 lines · Joanne Koong
Quoted 27 lines · Joanne Koong

Patch 1 and 2 address races between abort and fuse uring ring creation / registration. Patch 3 fixes fuse_uring_cancel() moving cancelled entries (which have no fuse_req) to ent_in_userspace, which can crash ent_list_request_expired(). It also removes the queue_refs > 0 gate in fuse_uring_abort() so that request flushing always happens, since cancel now decrements queue_refs directly.

For some more context, please see the discussions in [1].

[1] https://lore.kernel.org/linux-fsdevel/20251021-io-uring-fixes-cancel-mem-leak-v1-0-26b78b2c973c@ddn.com/

Changelog
--------
v1: https://lore.kernel.org/fuse-devel/[email protected]/
v1 -> v2:
* Reorder commits 1 and 2 and edit commit 1 message (Bernd)

Joanne Koong (3):
fuse: fix race between registration and connection abortion
fuse: check connection abort during ring creation
fuse: fix moving cancelled entry to ent_in_userspace list

 fs/fuse/dev_uring.c   | 32 ++++++++++++++++++++++++--------
 fs/fuse/dev_uring_i.h |  6 +++---
 2 files changed, 27 insertions(+), 11 deletions(-)
Quoted 5 lines
Quoted 3 lines

--
2.52.0

Miklos, this series is ready from my side - could you pick it up for your tree?

Nvm, I'll submit a v3 that fixes up the tagging for the 3rd patch and adds Bernd's reviewed-by.

Thanks,
Joanne

Quoted 3 lines

Thanks,
Joanne