fuse: Use d_alloc_trylock() in fuse_direntplus_link()
Subsystems (3)
PATCH 1/6 fuse: Use d_alloc_trylock() in fuse_direntplus_link()
From: NeilBrown <[email protected]>
fuse uses the results of readdir to prime the dcache. Using d_alloc_parallel() can block if there is a concurrent lookup. Blocking in that case is pointless as the lookup will add info to the dcache and there is no value in the readdir waiting to see if it should add the info too.
Also this call to d_alloc_parallel() is made while the parent directory is locked. A proposed change to locking will lock the parent later, after d_alloc_parallel(). This means it won't be safe to wait in d_alloc_parallel() while holding the directory lock.
So change to use d_alloc_trylock(), which doesn't require full_name_hash and d_lookup to be called first.
If d_alloc_trylock() fails with -EWOULDBLOCK or for any other reason, the consequence is only that fuse_force_forget() is called, which is required to keep the lookup count on the node consistent between client and server. It does not cause the readdir to fail.
--- fs/fuse/readdir.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
fs/fuse/readdir.c +5 -7
@@ -11,6 +11,7 @@#include <linux/pagemap.h> #include <linux/highmem.h> #include <linux/vmalloc.h> +#include <linux/namei.h> static bool fuse_use_readdirplus (struct inode *dir,struct dir_context *ctx) {
@@ -195,14 +196,11 @@ static int fuse_direntplus_link(struct file *file, fc =get_fuse_conn (dir); epoch =atomic_read (&fc->epoch); - name.hash =full_name_hash (parent, name.name, name.len); - dentry =d_lookup (parent, &name); -if (!dentry) { retry: - dentry =d_alloc_parallel (parent, &name); -if (IS_ERR (dentry)) -return PTR_ERR (dentry); - } + dentry =d_alloc_trylock (parent, &name); +if (IS_ERR (dentry)) +return PTR_ERR (dentry); +if (!d_in_lookup (dentry)) {struct fuse_inode *fi; inode =d_inode (dentry);
Signature · 1 line
2.56.0
PATCH 2/6 fuse: fix possible UAF in fuse_direntplus_link()
In fuse_direntplus_link() a use-after-free occurs on struct fuse_inode when linking a newly allocated in-lookup dentry during FUSE_READDIRPLUS processing fails inside d_splice_alias().
When d_in_lookup(dentry) is true, fuse_iget() returns an inode with an incremented reference count and fi->nlookup, which is then passed to d_splice_alias(). Inside d_splice_alias_ops(), if the directory inode has an existing alias and splicing fails with -ELOOP in d_ancestor() or -ESTALE in __d_unalias(), the function drops the alias reference via dput(new) and unconditionally consumes the caller's inode reference via iput(inode) before returning the error pointer.
If the existing alias is concurrently unhashed and dropped, iput(inode) drops the final inode reference and evicts the inode immediately via fuse_evict_inode(), sending a FUSE_FORGET request for fi->nlookup and freeing struct fuse_inode via RCU. Because fuse_direntplus_link() subsequently checks !IS_ERR(inode) outside an RCU read-side critical section, it dereferences the freed inode to acquire fi->lock and decrement fi->nlookup, and its caller parse_dirplusfile() then invokes fuse_force_forget() on the error return, issuing a duplicate FUSE_FORGET request to the server.
--- fs/fuse/readdir.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
fs/fuse/readdir.c +12 -8
@@ -236,9 +236,13 @@ static int fuse_direntplus_link(struct file *file, inode =fuse_iget (dir->i_sb, o->nodeid, o->generation, &o->attr,ATTR_TIMEOUT (o), attr_version, evict_ctr); -if (!inode) - inode =ERR_PTR (-ENOMEM ); +if (!inode) { +d_lookup_done (dentry); +dput (dentry); +return -ENOMEM ; + } +ihold (inode);/* d_splice_alias() eats a reference */ alias =d_splice_alias (inode, dentry);d_lookup_done (dentry);if (alias) {
@@ -246,15 +250,15 @@ static int fuse_direntplus_link(struct file *file, dentry = alias; }if (IS_ERR (dentry)) { -if (!IS_ERR (inode)) { -struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_inode *fi =get_fuse_inode (inode); -spin_lock (&fi->lock); - fi->nlookup--; -spin_unlock (&fi->lock); - } +spin_lock (&fi->lock); + fi->nlookup--; +spin_unlock (&fi->lock); +iput (inode);return PTR_ERR (dentry); } +iput (inode); }if (fc->readdirplus_auto)set_bit (FUSE_I_INIT_RDPLUS , &get_fuse_inode (inode)->state);
Signature · 1 line
2.56.0
PATCH 3/6 fuse: zero the partial EOF page in copy_file_range()
In reply to Miklos SzerediIn __fuse_copy_file_range() extending a destination file across a hole via copy_file_range() fails to zero the tail of a partial EOF folio when the destination offset starts on a page beyond the old i_size.
Commit 34b5c4a6e4fb ("fuse: zero the partial EOF page when extending a file") already fixed several cases of this bug but missed the copy_file_range() case.
--- fs/fuse/file.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/fuse/file.c +9 -0
@@ -2999,6 +2999,7 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,struct fuse_write_out outarg;struct fuse_copy_file_range_out outarg_64;u64 bytes_copied; +loff_t old_size;ssize_t err;/* mark unstable when write-back is not used, and file_out gets * extended */
@@ -3023,6 +3024,14 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,if (err) goto out; +/* +* Copying to an offset past EOF extends the file over a hole. Drop the +* cache over the newly exposed range before the new size is published. +*/ + old_size =i_size_read (inode_out); +if (pos_out > old_size) +truncate_pagecache_range (inode_out, old_size, pos_out -1 ); +/* * Write out dirty pages in the destination file before sending the COPY * request to userspace. After the request is completed, drop the
Signature · 1 line
2.56.0
PATCH 4/6 selftests/fuse: test post-EOF page zeroing on copy_file_range()
In reply to Miklos SzerediExtend the test for the case of copy_file_range() extending the file over a hole.
--- .../filesystems/fuse/write_extend_eof_test.c | 143 +++++++++++++++--- 1 file changed, 125 insertions(+), 18 deletions(-)
tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c +125 -18
@@ -25,24 +25,46 @@#include <sys/mman.h> #include <sys/mount.h> #include <sys/stat.h> +#include <sys/syscall.h> #include <sys/uio.h> #include <linux/fuse.h> #include "../../kselftest_harness.h" #define FUSE_ROOT_ID 1 -#define FILE_INO 2 +#define FILE_INO 2/* "file", the file being extended */ +#define SRC_INO 3/* "src", copy_file_range() source */ #define MAX_WRITE (128 * 1024)#define BACKING_SIZE (4 * 1024 * 1024)#define POLLUTE 0xee +/* Called directly: the libc wrapper needs off64_t and a recent enough glibc. */ +static ssize_t sys_copy_file_range (int fd_in,int64_t *off_in,int fd_out, +int64_t *off_out,size_t len, +unsigned int flags) +{ +return syscall (__NR_copy_file_range, fd_in, off_in, fd_out, off_out, + len, flags); +} + +/* Per-file server state. */ +struct backing_file { +unsigned char data[BACKING_SIZE ];/* authoritative bytes */ +uint64_t size; +}; +/* Server-side state, shared with the responder thread. */ struct server {int fd; -unsigned char backing[BACKING_SIZE ];/* authoritative bytes */ -uint64_t size; +unsigned int copy_ops;/* FUSE_COPY_FILE_RANGE* seen */ +struct backing_file file[2 ];/* FILE_INO, SRC_INO */ }; +static struct backing_file *lookup_file (struct server *s,uint64_t nodeid) +{ +return &s->file[nodeid ==SRC_INO ]; +} +static void reply (int fd,uint64_t unique,int error,void *data,size_t len) {struct fuse_out_header oh = {
@@ -109,17 +131,22 @@ static void *server_thread(void *arg) out.attr_valid =3600 ;fill_attr (&out.attr, ih->nodeid, root ? (S_IFDIR |0755 ) : (S_IFREG |0644 ), - root ?0 : s->size); + root ?0 :lookup_file (s, ih->nodeid)->size);reply (s->fd, ih->unique,0 , &out,sizeof (out));break ; }case FUSE_LOOKUP : { +const char *name = (const char *)(ih +1 );struct fuse_entry_out out = {0 }; +uint64_t nodeid; - out.nodeid =FILE_INO ; +/* Every name exists; "src" is the second file. */ + nodeid =strcmp (name,"src" ) ?FILE_INO :SRC_INO ; + out.nodeid = nodeid; out.attr_valid =3600 ; out.entry_valid =3600 ; -fill_attr (&out.attr,FILE_INO ,S_IFREG |0644 , s->size); +fill_attr (&out.attr, nodeid,S_IFREG |0644 , +lookup_file (s, nodeid)->size);reply (s->fd, ih->unique,0 , &out,sizeof (out));break ; }
@@ -134,6 +161,7 @@ static void *server_thread(void *arg) }case FUSE_READ : {struct fuse_read_in *in = (void *)(ih +1 ); +struct backing_file *f =lookup_file (s, ih->nodeid);uint64_t off = in->offset;uint32_t size = in->size;
@@ -141,11 +169,12 @@ static void *server_thread(void *arg) size =0 ; else if (off + size > BACKING_SIZE) size =BACKING_SIZE - off; -reply (s->fd, ih->unique,0 , s->backing + off, size); +reply (s->fd, ih->unique,0 , f->data + off, size);break ; }case FUSE_WRITE : {struct fuse_write_in *in = (void *)(ih +1 ); +struct backing_file *f =lookup_file (s, ih->nodeid);struct fuse_write_out out = {0 };uint64_t off = in->offset;uint32_t size = in->size;
@@ -155,9 +184,9 @@ static void *server_thread(void *arg)if (off + c >BACKING_SIZE ) c =BACKING_SIZE - off; -memcpy (s->backing + off, in +1 , c); -if (off + c > s->size) - s->size = off + c; +memcpy (f->data + off, in +1 , c); +if (off + c > f->size) + f->size = off + c; } out.size = size;reply (s->fd, ih->unique,0 , &out,sizeof (out));
@@ -165,32 +194,70 @@ static void *server_thread(void *arg) }case FUSE_SETATTR : {struct fuse_setattr_in *in = (void *)(ih +1 ); +struct backing_file *f =lookup_file (s, ih->nodeid);struct fuse_attr_out out = {0 };if ((in->valid &FATTR_SIZE ) && in->size <=BACKING_SIZE ) { -if (in->size > s->size) -memset (s->backing + s->size,0 , - in->size - s->size); - s->size = in->size; +if (in->size > f->size) +memset (f->data + f->size,0 , + in->size - f->size); + f->size = in->size; } out.attr_valid =3600 ; -fill_attr (&out.attr, ih->nodeid,S_IFREG |0644 , s->size); +fill_attr (&out.attr, ih->nodeid,S_IFREG |0644 , f->size);reply (s->fd, ih->unique,0 , &out,sizeof (out));break ; }case FUSE_FALLOCATE : {struct fuse_fallocate_in *in = (void *)(ih +1 ); +struct backing_file *f =lookup_file (s, ih->nodeid);uint64_t end = in->offset + in->length;/* Only plain (size-extending) fallocate is used here. */ if (!(in->mode &FALLOC_FL_KEEP_SIZE ) && - end <=BACKING_SIZE && end > s->size) { -memset (s->backing + s->size,0 , end - s->size); - s->size = end; + end <=BACKING_SIZE && end > f->size) { +memset (f->data + f->size,0 , end - f->size); + f->size = end; }reply (s->fd, ih->unique,0 ,NULL ,0 );break ; } +case FUSE_COPY_FILE_RANGE : +case FUSE_COPY_FILE_RANGE_64 : { +struct fuse_copy_file_range_in *in = (void *)(ih +1 ); +struct backing_file *src =lookup_file (s, ih->nodeid); +struct backing_file *dst; +struct fuse_copy_file_range_out out64 = {0 }; +struct fuse_write_out out = {0 }; +uint64_t off_in = in->off_in; +uint64_t off_out = in->off_out; +uint64_t len = in->len; + + dst =lookup_file (s, in->nodeid_out); + s->copy_ops++; +if (off_in >=BACKING_SIZE || off_out >=BACKING_SIZE ) { + len =0 ; + }else { +if (off_in + len >BACKING_SIZE ) + len =BACKING_SIZE - off_in; +if (off_out + len >BACKING_SIZE ) + len =BACKING_SIZE - off_out; +/* The hole left behind stays zero. */ +memmove (dst->data + off_out, src->data + off_in, + len); +if (off_out + len > dst->size) + dst->size = off_out + len; + } +if (ih->opcode ==FUSE_COPY_FILE_RANGE ) { + out.size = len; +reply (s->fd, ih->unique,0 , &out,sizeof (out)); + }else { + out64.bytes_copied = len; +reply (s->fd, ih->unique,0 , &out64, +sizeof (out64)); + } +break ; + }case FUSE_FLUSH :case FUSE_RELEASE :case FUSE_RELEASEDIR :
@@ -337,6 +404,46 @@ TEST_F(fuse, fallocate_extend)assert_tail_zeroed (_metadata, self); } +/* +* Extend via copy_file_range() landing on a page strictly past the old EOF +* page, so neither the copied range nor the flush of either file touches the +* old EOF folio. The source has to be a separate file: copying within one +* file would flush it from the source offset on, and writeback zeroes the +* post-EOF tail on the way out, hiding the bug. +*/ +TEST_F(fuse, copy_file_range_extend) +{ +int64_t off_in =0 ; +int64_t off_out; +char path[128 ]; +char *buf; +ssize_t ret; +int src; + +pollute_eof_tail (_metadata, self); + off_out =5 * self->page; + +snprintf (path,sizeof (path),"%s/src" , self->dir); + src =open (path,O_RDWR |O_CREAT |O_TRUNC ,0644 ); +ASSERT_GE (src,0 ); + + buf =malloc (self->page); +ASSERT_NE (NULL , buf); +memset (buf,'C' , self->page); +ASSERT_EQ (self->page,pwrite (src, buf, self->page,0 )); +free (buf); + + ret =sys_copy_file_range (src, &off_in, self->fd, &off_out, + self->page,0 ); +ASSERT_EQ (self->page, ret); +close (src); + +/* A splice fallback would extend via the write path instead. */ +ASSERT_NE (0u , self->srv->copy_ops); + +assert_tail_zeroed (_metadata, self); +} +/* A write landing inside the old EOF page must not clobber its own data. */ TEST_F(fuse, extend_into_eof_page_preserves_data) {
Signature · 1 line
2.56.0
PATCH 5/6 fuse: reject unmovable folios before stealing them
In reply to Miklos Szeredifuse_check_folio() rejects folios that can never be moved into the page cache, but it runs after pipe_buf_try_steal(), i.e. after the folio has already been removed from its mapping. For the conditions that are already knowable before the steal (swap backed, anonymous, mapped) this is needlessly late.
This is not a correctness problem. page_cache_pipe_buf_try_steal() ends up in remove_mapping(), which refuses dirty folios, and a tmpfs folio holding data is dirty, so it is never stolen in the first place. A swap backed folio that does get stolen is either zero filled (clean, reinstantiated on the next read) or already in the swap cache (the data is in swap), and shmem_recalc_inode() fixes up the block accounting. The fallback copy then reads the folio contents, which are unaffected by the cleared uptodate and mappedtodisk flags, and the pipe buffer still holds the only reference, so the folio is freed on release.
It is wasteful though: a clean tmpfs folio is dropped from the source mapping for nothing, and each rejected folio produces an unratelimited dump_page() splat, which a fuse server splicing from tmpfs with SPLICE_F_MOVE can use to spam the kernel log.
Note that commit 15c20483d915 ("fuse: validate folio state before pipe_buf_try_steal() in fuse_try_move_folio()") claimed to do this, but its diff only moved the large folio check and the oldfolio WARN_ON()s.
Test those three conditions before the steal. The remaining fuse_check_folio() checks cannot be hoisted, since a folio that is still in a page cache legitimately has ->mapping set and PG_uptodate.
--- fs/fuse/dev.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/fuse/dev.c +10 -0
@@ -1156,6 +1156,16 @@ static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliopif (folio_test_large (newfolio)) goto out_fallback; +/* +* A swap backed, anonymous or mapped folio can never be moved into the +* page cache. fuse_check_folio() would catch these below, but only +* after the steal has already removed the folio from its mapping, so +* check them here, while the answer is still meaningful. +*/ +if (folio_test_swapbacked (newfolio) ||folio_test_anon (newfolio) || +folio_mapped (newfolio)) + goto out_fallback; +/* * This is a new and locked page, it shouldn't be mapped or * have any special flags on it
Signature · 1 line
2.56.0
PATCH 6/6 fuse: derive short read EOF from the request offset
In reply to Miklos Szeredifuse_short_read() computed the new file size as
folio_pos(ap->folios[0]) + num_read
which assumes that the read started at the beginning of the first folio. That is not true in general: fuse_do_readfolio() may be asked to read part of a folio only, in which case it passes the offset within the folio in descs[0].offset and correctly adds it to the position of the request. If such a read came back short, the EOF derived from it would be descs[0].offset bytes too low and i_size would be truncated accordingly.
fuse_send_readpages() made the same assumption for the position of the read request itself, even though fuse_copy_folios() fills the first folio starting at descs[0].offset.
Neither is reachable today: sub-folio reads only originate from the iomap buffered write path, which requires fc->writeback_cache, and in that case fuse_short_read() does not update the size at all; readahead always starts on a folio boundary. Don't rely on that, though.
Add fuse_folios_pos() to compute the position of the first byte covered by a folio array and use it for both read requests. Make fuse_short_read() take the fuse_io_args and derive EOF from ia->read.in.offset, i.e. the offset the request was actually sent with, so that the two can no longer disagree. fuse_do_readfolio() now keeps the attribute version in ia.read.attr_ver, as the readahead path already did.
No functional change intended.
--- fs/fuse/file.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-)
fs/fuse/file.c +25 -10
@@ -845,8 +845,19 @@ static void fuse_read_update_size(struct inode *inode, loff_t size,spin_unlock (&fi->lock); } -static void fuse_short_read (struct inode *inode,u64 attr_ver,size_t num_read, -struct fuse_args_pages *ap) +/* +* Position of the first byte covered by the folios in @ap. +* +* The first folio may be read partially, so the descriptor offset is part of +* the position. +*/ +static loff_t fuse_folios_pos (struct fuse_args_pages *ap) +{ +return folio_pos (ap->folios[0 ]) + ap->descs[0 ].offset; +} + +static void fuse_short_read (struct inode *inode,struct fuse_io_args *ia, +size_t num_read) {struct fuse_conn *fc =get_fuse_conn (inode);
@@ -856,8 +867,13 @@ static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,* reached the client fs yet. So the hole is not present there. */ if (!fc->writeback_cache) { -loff_t pos =folio_pos (ap->folios[0 ]) + num_read; -fuse_read_update_size (inode, pos, attr_ver); +/* +* Derive EOF from the offset the request was sent with, which +* is not necessarily the start of the first folio. +*/ +loff_t pos = ia->read.in.offset + num_read; + +fuse_read_update_size (inode, pos, ia->read.attr_ver); } }
@@ -866,7 +882,6 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio, {struct inode *inode = folio->mapping->host;struct fuse_mount *fm =get_fuse_mount (inode); -loff_t pos =folio_pos (folio) + off;struct fuse_folio_desc desc = { .offset = off, .length = len,
@@ -878,10 +893,10 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio, .ap.folios = &folio, .ap.descs = &desc, }; +loff_t pos =fuse_folios_pos (&ia.ap);ssize_t res; -u64 attr_ver; - attr_ver =fuse_get_attr_version (fm->fc); + ia.read.attr_ver =fuse_get_attr_version (fm->fc);/* * Don't overflow end offset.
@@ -906,7 +921,7 @@ static int fuse_do_readfolio(struct file *file, struct folio *folio,* Short read means EOF. If file size is larger, truncate it */ if (res < len) -fuse_short_read (inode, attr_ver, res, &ia.ap); +fuse_short_read (inode, &ia, res);return 0 ; }
@@ -1069,7 +1084,7 @@ static void fuse_readpages_end(struct fuse_args *args, int err)* Short read means EOF. If file size is larger, truncate it */ if (!err && num_read < count) -fuse_short_read (inode, ia->read.attr_ver, num_read, ap); +fuse_short_read (inode, ia, num_read);fuse_invalidate_atime (inode);
@@ -1090,7 +1105,7 @@ static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,struct fuse_file *ff = file->private_data;struct fuse_mount *fm = ff->fm;struct fuse_args_pages *ap = &ia->ap; -loff_t pos =folio_pos (ap->folios[0 ]); +loff_t pos =fuse_folios_pos (ap);ssize_t res;int err;
Signature · 1 line
2.56.0