]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fuse: fix pipe buffer lifetime for direct_io
authorMiklos Szeredi <mszeredi@redhat.com>
Mon, 7 Mar 2022 15:30:44 +0000 (16:30 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 15 Apr 2022 12:14:37 +0000 (14:14 +0200)
commit 0c4bcfdecb1ac0967619ee7ff44871d93c08c909 upstream.

In FOPEN_DIRECT_IO mode, fuse_file_write_iter() calls
fuse_direct_write_iter(), which normally calls fuse_direct_io(), which then
imports the write buffer with fuse_get_user_pages(), which uses
iov_iter_get_pages() to grab references to userspace pages instead of
actually copying memory.

On the filesystem device side, these pages can then either be read to
userspace (via fuse_dev_read()), or splice()d over into a pipe using
fuse_dev_splice_read() as pipe buffers with &nosteal_pipe_buf_ops.

This is wrong because after fuse_dev_do_read() unlocks the FUSE request,
the userspace filesystem can mark the request as completed, causing write()
to return. At that point, the userspace filesystem should no longer have
access to the pipe buffer.

Fix by copying pages coming from the user address space to new pipe
buffers.

Reported-by: Jann Horn <jannh@google.com>
Fixes: c3021629a0d8 ("fuse: support splice() reading from fuse device")
Cc: <stable@vger.kernel.org>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/fuse/dev.c
fs/fuse/file.c
fs/fuse/fuse_i.h

index d1dc545302528fdfb62bc37c46376f097dafe5fe..a5144ecd5babdbda9923ce13797c2d52eb200e16 100644 (file)
@@ -999,7 +999,17 @@ static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
 
        while (count) {
                if (cs->write && cs->pipebufs && page) {
-                       return fuse_ref_page(cs, page, offset, count);
+                       /*
+                        * Can't control lifetime of pipe buffers, so always
+                        * copy user pages.
+                        */
+                       if (cs->req->user_pages) {
+                               err = fuse_copy_fill(cs);
+                               if (err)
+                                       return err;
+                       } else {
+                               return fuse_ref_page(cs, page, offset, count);
+                       }
                } else if (!cs->len) {
                        if (cs->move_pages && page &&
                            offset == 0 && count == PAGE_SIZE) {
index ed9b3ebc0cca4c96f6fa654b927af676b288c37b..599a6eeed02e04ce37d97be2be43cc2523b2bc24 100644 (file)
@@ -1329,6 +1329,7 @@ static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
                        (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
        }
 
+       req->user_pages = true;
        if (write)
                req->in.argpages = 1;
        else
index 853d37ec81e0c1ccefd9b86223d0eec1bbd09eeb..1c754a02fb06bd5384509c7a219bfc8b8b721cfb 100644 (file)
@@ -313,6 +313,8 @@ struct fuse_req {
        /** refcount */
        refcount_t count;
 
+       bool user_pages;
+
        /** Unique ID for the interrupt request */
        u64 intr_unique;