Skip to contentSkip to conversation

fuse: clear the old EOF tail on getattr size growth

PATCH New shaos 1 participant 1 message Nov 28

Subsystems (2)
lore
Share & tools
lore

shaos

author
#0 lore raw

PATCH 1/1 fuse: clear the old EOF tail on getattr size growth

Hello,

When a FUSE_GETATTR reply grows a cached regular file, the attribute update writes the larger i_size and then calls truncate_pagecache() at the new size. That does not clear bytes dirtied beyond the old EOF in the partial page. With FUSE_EXPLICIT_INVAL_DATA, the subsequent whole- mapping invalidation is skipped, so those stale bytes become visible through reads after the size change.

The attached PoC dirties 512 bytes beyond a 100-byte EOF in a MAP_SHARED mapping. A FUSE server then reports a size of 8192 in a GETATTR reply. Two fresh runs return 512 stale 'A' bytes from the page cache; writeback also carries those bytes to the server. The patched control returns zeroes and writes back no stale 'A' bytes.

Related fixes already in this tree clear the old EOF tail for buffered writes, setattr/truncate, and fallocate (34b5c4a6e4fb), writeback-cache writes (d9013d15b937), and copy_file_range() (9b82a8aed794). This report covers the separate attribute-refresh path: a larger size returned by FUSE_GETATTR is applied in fuse_change_attributes_i(), which still leaves the old EOF tail in the page cache when explicit data invalidation is enabled.

The trigger requires a cached, non-writeback FUSE file with FUSE_EXPLICIT_INVAL_DATA and FOPEN_KEEP_CACHE. It demonstrates stale file contents and writeback within that FUSE filesystem. No cross-user data disclosure or privilege escalation is demonstrated.

HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: v7.3-23088-g8d7fe9885c62f (Linux 7.4.0-rc4)
compiler:       gcc 11.4.0, GNU ld 2.38

The new range is cleared before i_size is published, following the same rule used by the other FUSE size-extension paths. The helper takes the inode lock for an extension and rechecks the attribute version and current size after acquiring it.

Console output:
https://gist.githubusercontent.com/shaos26/8adaad63a51780dac454db282187a389/raw/c0ad0595ad57975a6fe2b87e684e6b982037e584/console-output.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/8adaad63a51780dac454db282187a389/raw/c0ad0595ad57975a6fe2b87e684e6b982037e584/kernel-config.txt
PoC:
https://gist.githubusercontent.com/shaos26/8adaad63a51780dac454db282187a389/raw/c0ad0595ad57975a6fe2b87e684e6b982037e584/poc.c
Init script:
https://gist.githubusercontent.com/shaos26/8adaad63a51780dac454db282187a389/raw/c0ad0595ad57975a6fe2b87e684e6b982037e584/init
Fixed-kernel init script:
https://gist.githubusercontent.com/shaos26/8adaad63a51780dac454db282187a389/raw/c0ad0595ad57975a6fe2b87e684e6b982037e584/init-fixed

Build the kernel from the revision above using kernel-config.txt:

    work=$(mktemp -d /tmp/fuse-getattr.XXXXXX)
    cd "$work"
    gist_id=8adaad63a51780dac454db282187a389
    gist_revision=c0ad0595ad57975a6fe2b87e684e6b982037e584
    gist_base=https://gist.githubusercontent.com/shaos26
    gist=$gist_base/$gist_id/raw/$gist_revision
    for file in console-output.txt kernel-config.txt poc.c init \
        init-fixed; do
        curl -fL "$gist/$file" -o "$file"
    done
    git clone \
        https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
    cd linux
    git checkout 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
    mkdir build
    cp ../kernel-config.txt build/.config
    make O=build olddefconfig
    make O=build -j"$(nproc)" bzImage
    cd ..

Build the PoC and initramfs:

gcc -static -O2 -Wall -Wextra -o poc poc.c mkdir -p root/bin root/dev root/proc root/sys root/mnt cp /usr/bin/busybox root/bin/busybox
cp poc root/poc
cp init root/init
(cd root && find . -print0 | cpio --null -o -H newc) | \
gzip -9 > initramfs.cpio.gz

Run the unpatched kernel and then repeat with the proposed patch and init-fixed:

    timeout 60 qemu-system-x86_64 -m 2048 -smp 2 -machine q35 \
        -no-acpi -kernel linux/build/arch/x86/boot/bzImage \
        -initrd initramfs.cpio.gz \
        -append 'console=ttyS0 rdinit=/init panic=-1 acpi=off pci=noacpi' \
        -nographic -no-reboot -monitor none

The init script runs the positive case, a no-growth control, and a control without FUSE_EXPLICIT_INVAL_DATA. On the patched kernel, use init-fixed; rebuild the initramfs after replacing root/init:

cp init-fixed root/init
(cd root && find . -print0 | cpio --null -o -H newc) | \
gzip -9 > initramfs-fixed.cpio.gz

Boot that archive with the same QEMU command, replacing initramfs.cpio.gz with initramfs-fixed.cpio.gz. The full console output records both unpatched runs and the patched control.

Fixes ad2ba64dd489 ("fuse: allow filesystems to have precise control over data cache")

Thanks,

shaos

---
 fs/fuse/dir.c    | 21 +++++++++++++-------
 fs/fuse/fuse_i.h |  3 +++
 fs/fuse/inode.c  | 45 ++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 57 insertions(+), 12 deletions(-)
fs/fuse/dir.c +14 -7
index a04d93d5fc447..0ec3a600de107 100644
@@ -1496,7 +1496,8 @@ static int fuse_do_statx(const struct mnt_idmap *idmap, struct inode *inode,
 }
 
 static int fuse_do_getattr(const struct mnt_idmap *idmap, struct inode *inode,
-			   struct kstat *stat, struct file *file)
+			   struct kstat *stat, struct file *file,
+			   bool inode_locked)
 {
 	int err;
 	struct fuse_getattr_in inarg;
@@ -1531,9 +1532,15 @@ static int fuse_do_getattr(const struct mnt_idmap *idmap, struct inode *inode,
 			fuse_make_bad(inode);
 			err = -EIO;
 		} else {
-			fuse_change_attributes(inode, &outarg.attr, NULL,
-					       ATTR_TIMEOUT(&outarg),
-					       attr_version);
+			if (inode_locked)
+				fuse_change_attributes_locked(inode,
+							      &outarg.attr, NULL,
+							      ATTR_TIMEOUT(&outarg),
+							      attr_version);
+			else
+				fuse_change_attributes(inode, &outarg.attr, NULL,
+						       ATTR_TIMEOUT(&outarg),
+						       attr_version);
 			if (stat)
 				fuse_fillattr(idmap, inode, &outarg.attr, stat);
 		}
@@ -1581,7 +1588,7 @@ static int fuse_update_get_attr(const struct mnt_idmap *idmap, struct inode *ino
 				goto retry;
 			}
 		} else {
-			err = fuse_do_getattr(idmap, inode, stat, file);
+			err = fuse_do_getattr(idmap, inode, stat, file, false);
 		}
 	} else if (stat) {
 		generic_fillattr(idmap, request_mask, inode, stat);
@@ -1751,7 +1758,7 @@ static int fuse_perm_getattr(struct inode *inode, int mask)
 		return -ECHILD;
 
 	forget_all_cached_acls(inode);
-	return fuse_do_getattr(&nop_mnt_idmap, inode, NULL, NULL);
+	return fuse_do_getattr(&nop_mnt_idmap, inode, NULL, NULL, false);
 }
 
 /*
@@ -2360,7 +2367,7 @@ static int fuse_setattr(const struct mnt_idmap *idmap, struct dentry *entry,
 			 * ia_mode calculation may have used stale i_mode.
 			 * Refresh and recalculate.
 			 */
-			ret = fuse_do_getattr(idmap, inode, NULL, file);
+			ret = fuse_do_getattr(idmap, inode, NULL, file, true);
 			if (ret)
 				return ret;
 
fs/fuse/fuse_i.h +3 -0
index e0992fcbcc554..38a8fadfa3644 100644
@@ -1049,6 +1049,9 @@ void fuse_init_symlink(struct inode *inode);
 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
 			    struct fuse_statx *sx,
 			    u64 attr_valid, u64 attr_version);
+void fuse_change_attributes_locked(struct inode *inode, struct fuse_attr *attr,
+				   struct fuse_statx *sx,
+				   u64 attr_valid, u64 attr_version);
 
 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
 				   struct fuse_statx *sx,
fs/fuse/inode.c +40 -5

88 lines not on this page · raw message

Signature · 1 line
2.34.1