linux/fs/xfs/libxfs
Darrick J. Wong 7dfb218c82 xfs: fix exchange-range-to-eof file size exchange
Norbert Szetei posted a patch containing an incomplete description of a
bug in XFS_IOC_EXCHANGE_RANGE's TO_EOF flag.  The bug tricks
exchange-range into modifying a file so that it has shared blocks
starting beyond EOF, which is never allowed because files never have
written data beyond EOF, and you can only share written data blocks.
This is key to all the incorrect behavior that follows.

Eventually I got from Norbert a description of what's going wrong:

> Fair. Here it is with physical block numbers, 4k blocks. peer is the file
> that gets rewritten and file1 is the one that ends up with the flag
> cleared.
>
> Start, nothing shared:
>
>	peer size 4096   -> [100]
>	file1 size 12288 -> [200] [201] [202]
>	donor1 size 4096 -> [300]
>	donor2 size 4096 -> [400]
>
> 1. FICLONERANGE peer's block into file1's middle slot:
>
>	peer size 4096   -> [100]
>	file1 size 12288 -> [200] [100] [202] reflink flag set
>                             ^^^^^ both files own block 100
>
> 2. XFS_IOC_EXCHANGE_RANGE file1 against donor1, file1_offset 0,
> file2_offset 8192, length 0, TO_EOF. One block is exchanged, and the sizes
> are exchanged with it:
>
>	file1 size 4096   -> [200] [100] [300] reflink flag set
>                              ^^^^^^^^^^^ still owned, now past the size
>	donor1 size 12288 -> [202]
>
> file1 says it is one block long while it still owns three. Nothing was
> unmapped. file2_offset is block 2, so this exchange cannot clear a flag.
>
> 3. XFS_IOC_EXCHANGE_RANGE file1 against donor2, both offsets 0, length 4096.
> By i_disk_size this is two one-block files exchanging everything, so
> xmi_can_exchange_reflink_flags() agrees and the flag is cleared:
>
>	file1 size 4096   -> [400] [100] [300] reflink flag CLEARED
>	donor2 size 4096  -> [200]
>
> Block 100 is still shared with peer.
>
> 4. pwrite(file1, 4096, 4096), which is file1's second slot, block 100.
> xfs_is_cow_inode(file1) is false, so no CoW:
>
>	peer size 4096 -> [100] same block, new contents
>
> Block 100 is the only one that matters here. PoC below, it reads a file
> called peer in the current directory. The filesystem needs reflink and
> exchange-range, which mkfs.xfs 7.x turns on by default.

So yes, TO_EOF is screwing up the file size even if it's actually
exchanging the data block correctly.  The following is the intended
behavior of XFS_EXCHANGE_RANGE_TO_EOF, as described by its manpage:

"Ignore the length parameter.  All bytes in file1_fd from file1_offset
 to EOF are moved to file2_fd, and file2's size is set to
 (file2_offset+(file1_length-file1_offset)).  Meanwhile, all bytes in
 file2 from file2_offset to EOF are moved to file1 and file1's size is
 set to (file1_offset+(file2_length-file2_offset))."

In other words, if we only exchange a portion of the file data, then we
should only exchange the portion of the file size that relates to the
exchanged data.  Unfortunately, the code swaps the ondisk file size and
the incore file sizes, which is incorrect if the caller passes in a
nonzero offset.

Having confused the file sizes, the PoC uses a second exchange-range
call on what looks like a non-reflinked single-block file.  Because the
file size is set incorrectly, exchange-range thinks it's exchanging the
full contents of two files and clears the reflink flag on the broken
file.  That enables an extending write of the broken file to rewrite the
shared block that's just past EOF.  This has become known colloquially
as refluxfs.

Once the file sizes are set correctly, the second exchange-range no
longer thinks that it's doing a full-contents swap, so it won't clear
the reflink flag on either of its file arguments.

Norbert followed up to an earlier version of this patch with a second
complaint about us not handling the i_disk_size state correctly:

> The offsets are bounded against the incore i_size but subtracted from
> the ondisk i_disk_size, so a file that has not been written back gives
> xmi_isize1 = 0 + (0 - 12288).
>
> For instance, you can do:
>
> $ xfs_io -f -c "pwrite -q 0 4096" -c fsync file1
> $ xfs_io -f -c "pwrite -q 0 12288" \
>   -c "exchangerange -s 0 -d 12288 file1" dirty
> $ stat -c %s file1
> 0
> $ stat -c %i file1
> 131
> $ sudo xfs_io -r -c "bulkstat_single 131" . | grep bs_size
> bs_size = 18446744073709539328

That's wrong, and the new file size update logic depends on the ondisk
i_disk_size matching the incore i_size, so flush the pagecache as needed
to push it upwards.

Link: https://lore.kernel.org/linux-xfs/1E196589-DEBE-40AC-AFEA-D420DAAB067F@doyensec.com/
Reported-by: Norbert Szetei <norbert@doyensec.com>
Cc: stable@vger.kernel.org # v6.10
Fixes: 966ceafc7a ("xfs: create deferred log items for file mapping exchanges")
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
2026-10-11 08:23:43 +02:00
..
xfs_ag_resv.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_ag_resv.h xfs: get rid of xfs_ag_resv_rmapbt_alloc 2024-07-04 14:36:13 +05:30
xfs_ag.c xfs: cleanup xfs_growfs_compute_deltas 2026-06-10 12:21:52 +02:00
xfs_ag.h xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_alloc_btree.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_alloc_btree.h xfs: standardize the btree maxrecs function parameters 2024-09-01 08:58:20 -07:00
xfs_alloc.c xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_alloc.h xfs: support logging EFIs for realtime extents 2024-11-05 13:38:42 -08:00
xfs_attr_leaf.c xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_attr_leaf.h xfs: factor out xfs_attr3_leaf_init 2026-03-23 10:47:28 +01:00
xfs_attr_remote.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_attr_remote.h xfs: create a helper to compute the blockcount of a max sized remote value 2024-05-02 07:48:36 -07:00
xfs_attr_sf.h xfs: pass the attr value to put_listent when possible 2024-04-23 07:47:00 -07:00
xfs_attr.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_attr.h xfs: annotate struct xfs_attr_list_context with __counted_by_ptr 2026-03-18 09:54:39 +01:00
xfs_bit.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_bit.h
xfs_bmap_btree.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_bmap_btree.h xfs: fix pointer arithmetic error on 32-bit systems 2026-06-10 12:21:52 +02:00
xfs_bmap.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_bmap.h xfs: allow block allocator to take an alignment hint 2025-05-07 14:25:31 -07:00
xfs_btree_mem.c xfs: preserve owner on in-memory btree creation 2026-09-07 07:50:35 +02:00
xfs_btree_mem.h xfs: launder in-memory btree buffers before transaction commit 2024-02-22 12:43:36 -08:00
xfs_btree_staging.c xfs: don't leak new_bp if xfs_btree_bload_drop_buf fails 2026-09-07 07:50:35 +02:00
xfs_btree_staging.h xfs: don't override bc_ops for staging btrees 2024-02-22 12:37:35 -08:00
xfs_btree.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_btree.h xfs: refactor xfs_btree_diff_two_ptrs() to take advantage of cmp_int() 2025-07-24 17:30:13 +02:00
xfs_cksum.h
xfs_da_btree.c xfs: fix under-reservation of blocks when repairing sf directories 2026-09-11 08:37:30 +02:00
xfs_da_btree.h xfs: fix under-reservation of blocks when repairing sf directories 2026-09-11 08:37:30 +02:00
xfs_da_format.h xfs: refactor attr3 leaf table size computation 2026-01-23 09:27:31 -08:00
xfs_defer.c xfs: report the error that made deferred work shut down the fs 2026-09-07 07:50:34 +02:00
xfs_defer.h xfs: add a realtime flag to the refcount update log redo items 2024-12-23 13:06:11 -08:00
xfs_dir2_block.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_dir2_data.c xfs: zero directory data block padding on write verification 2026-04-27 10:36:36 +02:00
xfs_dir2_leaf.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_dir2_node.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_dir2_priv.h xfs: don't walk off the end of a directory data block 2024-07-01 09:32:29 +05:30
xfs_dir2_sf.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_dir2.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_dir2.h xfs: mark xfs_dir_isempty static 2025-01-13 14:55:06 +01:00
xfs_dquot_buf.c xfs: remove spurious XBF_DONE clearing on readahead validation failure 2026-08-10 08:34:43 +02:00
xfs_errortag.h xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_exchmaps.c xfs: fix exchange-range-to-eof file size exchange 2026-10-11 08:23:43 +02:00
xfs_exchmaps.h xfs: use atomic extent swapping to fix user file fork data 2024-04-15 14:58:53 -07:00
xfs_format.h xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_fs.h xfs: add write pointer to xfs_rtgroup_geometry 2026-03-04 09:58:24 +01:00
xfs_group.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_group.h xfs: add a xfs_groups_to_rfsbs helper 2025-11-11 11:38:05 +01:00
xfs_health.h xfs: convey metadata health events to the health monitor 2026-01-20 18:06:47 -08:00
xfs_ialloc_btree.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_ialloc_btree.h xfs: standardize the btree maxrecs function parameters 2024-09-01 08:58:20 -07:00
xfs_ialloc.c xfs: hide b_flags manipulation from code outside of xfs_buf.c 2026-08-10 08:34:43 +02:00
xfs_ialloc.h xfs: pass the icreate args object to xfs_dialloc 2024-09-01 08:58:19 -07:00
xfs_iext_tree.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_inode_buf.c xfs: fix typos and repeated words in comments 2026-09-15 10:47:28 +02:00
xfs_inode_buf.h xfs: mark struct xfs_imap as __packed 2026-06-09 09:14:42 +02:00
xfs_inode_fork.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_inode_fork.h xfs: make xfs_iroot_realloc a bmap btree function 2024-12-23 13:06:02 -08:00
xfs_inode_util.c xfs: new code for Linux 7.2 2026-06-16 11:50:40 +05:30
xfs_inode_util.h xfs: hoist inode free function to libxfs 2024-07-02 11:36:59 -07:00
xfs_log_format.h xfs: stop exchanging reflink flags when exchanging mappings 2026-10-11 08:23:38 +02:00
xfs_log_recover.h xfs: remove the xfs_trans_header_t typedef 2025-09-16 12:25:05 +02:00
xfs_log_rlimit.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_metadir.c xfs: mark internal metadir file creation helpers static 2026-07-22 15:12:11 +02:00
xfs_metadir.h xfs: mark internal metadir file creation helpers static 2026-07-22 15:12:11 +02:00
xfs_metafile.c xfs: don't let hidden_space go negative in xfs_metafile_resv_init 2026-09-17 11:27:41 +02:00
xfs_metafile.h xfs: make metabtree reservations global 2025-03-03 08:16:43 -07:00
xfs_ondisk.h xfs: add static size checks for ioctl UABI 2026-02-25 13:58:50 +01:00
xfs_parent.c xfs: initialise args->total for parent pointer updates 2026-09-07 07:50:34 +02:00
xfs_parent.h xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_quota_defs.h xfs: use a lockref for the xfs_dquot reference count 2025-11-11 11:45:57 +01:00
xfs_refcount_btree.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_refcount_btree.h xfs: standardize the btree maxrecs function parameters 2024-09-01 08:58:20 -07:00
xfs_refcount.c xfs: remove the meaningless XFS_ALLOC_FLAG_FREEING 2026-04-27 10:36:36 +02:00
xfs_refcount.h xfs: recover CoW leftovers in the realtime volume 2024-12-23 13:06:13 -08:00
xfs_rmap_btree.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_rmap_btree.h xfs: standardize the btree maxrecs function parameters 2024-09-01 08:58:20 -07:00
xfs_rmap.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_rmap.h xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_rtbitmap.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_rtbitmap.h xfs: online repair of realtime bitmaps for a realtime group 2024-12-23 13:06:08 -08:00
xfs_rtgroup.c xfs: create rtgroup metadir inodes using xfs_metadir_create_file 2026-07-22 15:12:10 +02:00
xfs_rtgroup.h xfs: don't hold buffer locks across sync transaction commit in xfs_sync_sb_buf 2026-08-21 09:09:53 +02:00
xfs_rtrefcount_btree.c xfs: fix wild memcpy access when formatting ondisk rtrefcount btree roots 2026-09-17 11:27:41 +02:00
xfs_rtrefcount_btree.h xfs: create routine to allocate and initialize a realtime refcount btree inode 2024-12-23 13:06:12 -08:00
xfs_rtrmap_btree.c xfs: fix xfs_rtrmapbt_mem_cursor for non-rmap filesystems 2026-09-07 07:50:35 +02:00
xfs_rtrmap_btree.h xfs: add a xfs_rtrmap_highest_rgbno helper 2025-03-03 08:16:45 -07:00
xfs_sb.c xfs: don't hold buffer locks across sync transaction commit in xfs_sync_sb_buf 2026-08-21 09:09:53 +02:00
xfs_sb.h xfs: make xfs_rtblock_t a segmented address like xfs_fsblock_t 2024-11-05 13:38:44 -08:00
xfs_shared.h xfs: introduce realtime refcount btree ondisk definitions 2024-12-23 13:06:10 -08:00
xfs_symlink_remote.c xfs: remove the i_ino field in struct xfs_inode 2026-06-09 09:14:41 +02:00
xfs_symlink_remote.h xfs: pass the owner to xfs_symlink_write_target 2024-04-15 14:58:57 -07:00
xfs_trans_inode.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_trans_resv.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_trans_resv.h xfs: allow sysadmins to specify a maximum atomic write limit at mount time 2025-05-07 14:25:33 -07:00
xfs_trans_space.c xfs: correct the parent pointer space reservation comment 2026-09-07 07:50:34 +02:00
xfs_trans_space.h xfs: realtime rmap btree transaction reservations 2024-12-23 13:06:04 -08:00
xfs_types.c xfs: directly include xfs_platform.h 2026-01-21 12:57:16 +01:00
xfs_types.h xfs: add support for zoned space reservations 2025-03-03 08:17:07 -07:00
xfs_zones.c xfs: split and refactor zone validation 2026-01-21 12:57:17 +01:00
xfs_zones.h xfs: split and refactor zone validation 2026-01-21 12:57:17 +01:00