fuse: re-lock request before replacing page cache folio
Subsystems (2)
PATCH 1/2 fuse: re-lock request before replacing page cache folio
fuse_try_move_folio() unlocks the request on entry but does not re-lock it on the success path. This means fuse_chan_abort() can end the request and free the fuse_io_args (eg fuse_readpages_end()) while the subsequent copy chain logic after fuse_try_move_folio() accesses the fuse_io_args, leading to use-after-free issues.
Fix this by calling lock_request() before replace_page_cache_folio(). This ensures the request is locked on the success path which will prevent the fuse_io_args from being freed while the later copying logic runs, and also ensures that the ap->folios[i]->mapping is never null since ap->folios[i] will always point to the newfolio after replace_page_cache_folio().
--- fs/fuse/dev.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-)
fs/fuse/dev.c +5 -14
@@ -1158,6 +1158,10 @@ static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliopif (WARN_ON (folio_test_mlocked (oldfolio))) goto out_fallback_unlock; + err =lock_request (cs->req); +if (err) + goto out_fallback_unlock; +replace_page_cache_folio (oldfolio, newfolio);folio_get (newfolio);
@@ -1171,20 +1175,7 @@ static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliop*/ pipe_buf_release (cs->pipe, buf); - err =0 ; -spin_lock (&cs->req->waitq.lock); -if (test_bit (FR_ABORTED , &cs->req->flags)) - err = -ENOENT ; -else - *foliop = newfolio; -spin_unlock (&cs->req->waitq.lock); - -if (err) { -folio_unlock (newfolio); -folio_put (newfolio); - goto out_put_old; - } - + *foliop = newfolio;folio_unlock (oldfolio);/* Drop ref for ap->pages[] array */ folio_put (oldfolio);
Signature · 1 line
2.52.0
PATCH 2/2 fuse: re-lock request before returning from fuse_ref_folio()
fuse_ref_folio() unlocks the request but does not re-lock it before returning. fuse_chan_abort() can end the request and the async end callback (eg fuse_writepage_free()) can free the args while the subsequent copy chain logic after fuse_ref_folio() accesses them, leading to use-after-free issues.
Fix this by locking the request in fuse_ref_folio() before returning.
--- fs/fuse/dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fuse/dev.c +1 -1
@@ -1227,7 +1227,7 @@ static int fuse_ref_folio(struct fuse_copy_state *cs, struct folio *folio, cs->nr_segs++; cs->len =0 ; -return 0 ; +return lock_request (cs->req); }/*
Signature · 1 line
2.52.0
Miklos Szeredi
maintainerQuoted 14 lines · Joanne Koong
fuse_try_move_folio()unlocks the request on entry but does not re-lock it on the success path. This meansfuse_chan_abort()can end the request and free thefuse_io_args(egfuse_readpages_end()) while the subsequent copy chain logic afterfuse_try_move_folio()accesses thefuse_io_args, leading to use-after-free issues.Fix this by calling
lock_request()beforereplace_page_cache_folio(). This ensures the request is locked on the success path which will prevent thefuse_io_argsfrom being freed while the later copying logic runs, and also ensures that the ap->folios[i]->mapping is never null since ap->folios[i] will always point to the newfolio afterreplace_page_cache_folio().
Quoted 4 lines
Fixes ce534fb05292 ("fuse: allow splice to move pages")Cc <[email protected]>Reported-by Lei Lu <[email protected]>Signed-off-by Joanne Koong <[email protected]>
Looks good, applied both.
Thanks,
Miklos