fuse: fix io-uring abort races and cancel null deref
Subsystems (3)
Joanne Koong
maintainerv2 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].
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
Joanne Koong
maintainerPATCH 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.
--- fs/fuse/dev_uring.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
fs/fuse/dev_uring.c +16 -6
@@ -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
Joanne Koong
maintainerPATCH v2 2/3 fuse: check connection abort during ring creation
In reply to Joanne KoongCheck 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
@@ -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
Joanne Koong
maintainerPATCH v2 3/3 fuse: fix moving cancelled entry to ent_in_userspace list
In reply to Joanne Koongfuse_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.
--- 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
@@ -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
@@ -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
Joanne Koong
maintainerRe: [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 theent_in_userspacelist.ent_list_request_expired()checks the first entry onent_in_userspaceand dereferencesent->fuse_requnconditionally, which will crash on a cancelled entry that was moved to this list.Fix this by freeing the entry and dropping
queue_refsdirectly infuse_uring_cancel(). This is safe because cancel is the cancel handler itself - afterio_uring_cmd_done(), no more cancels will be dispatched for this command, and teardown serializes with cancel viaqueue->lock.Since cancel now decrements
queue_refs,fuse_uring_abort()must no longer gatefuse_uring_abort_end_requests()onqueue_refs> 0, as cancelled entries may have already droppedqueue_refswhile requests are still queued. Remove the gate so abort always flushes requests and stops queues.
Quoted 7 lines
Reported-by Heechan Kang <[email protected]>Tested-by Heechan Kang <[email protected]>Fixes 4fea593e625c ("fuse: optimize over-io-uring request expiration check")Co-developed-by Jian Huang Li <[email protected]>Co-developed-by Horst Birthelmer <[email protected]>Signed-off-by Joanne Koong <[email protected]>
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@@ -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@@ -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
Joanne Koong
maintainerRe: [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 theent_in_userspacelist.ent_list_request_expired()checks the first entry onent_in_userspaceand dereferencesent->fuse_requnconditionally, which will crash on a cancelled entry that was moved to this list.Fix this by freeing the entry and dropping
queue_refsdirectly infuse_uring_cancel(). This is safe because cancel is the cancel handler itself - afterio_uring_cmd_done(), no more cancels will be dispatched for this command, and teardown serializes with cancel viaqueue->lock.Since cancel now decrements
queue_refs,fuse_uring_abort()must no longer gatefuse_uring_abort_end_requests()onqueue_refs> 0, as cancelled entries may have already droppedqueue_refswhile requests are still queued. Remove the gate so abort always flushes requests and stops queues.
Quoted 7 lines
Quoted 7 lines
Reported-by Heechan Kang <[email protected]>Tested-by Heechan Kang <[email protected]>Fixes 4fea593e625c ("fuse: optimize over-io-uring request expiration check")Co-developed-by Jian Huang Li <[email protected]>Co-developed-by Horst Birthelmer <[email protected]>Signed-off-by Joanne Koong <[email protected]>
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@@ -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@@ -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
Joanne Koong
maintainerQuoted 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 nofuse_req) toent_in_userspace, which can crashent_list_request_expired(). It also removes thequeue_refs> 0 gate infuse_uring_abort()so that request flushing always happens, since cancel now decrementsqueue_refsdirectly.For some more context, please see the discussions in [1].
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 toent_in_userspacelist
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
Joanne Koong
maintainerQuoted 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 nofuse_req) toent_in_userspace, which can crashent_list_request_expired(). It also removes thequeue_refs> 0 gate infuse_uring_abort()so that request flushing always happens, since cancel now decrementsqueue_refsdirectly.For some more context, please see the discussions in [1].
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 toent_in_userspacelistfs/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.0Miklos, 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