Skip to contentSkip to conversation

xfs_copy: don't use cached buffer reads until after libxfs_mount

PATCH In review Darrick J. Wong 4 participants 5 messages May 24, 2022 – May 27, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_copy: don't use cached buffer reads until after libxfs_mount

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

I accidentally tried to xfs_copy an ext4 filesystem, but instead of rejecting the filesystem, the program instead crashed. I figured out that zeroing the superblock was enough to trigger this:

# dd if=/dev/zero of=/dev/sda bs=1024k count=1
# xfs_copy  /dev/sda /dev/sdb
Floating point exception

The exact crash happens in this line from libxfs_getbuf_flags, which is called from the main() routine of xfs_copy:

	if (btp == btp->bt_mount->m_ddev_targp) {
		(*bpp)->b_pag = xfs_perag_get(btp->bt_mount,
				xfs_daddr_to_agno(btp->bt_mount, blkno));

The problem here is that the uncached read filled the incore superblock with zeroes, which means mbuf.sb_agblocks is zero. This causes a division by zero in xfs_daddr_to_agno, thereby crashing the program.

In commit f8b581d6, we made it so that xfs_buf structures contain a passive reference to the associated perag structure. That commit assumes that no program would try a cached buffer read until the buffer cache is fully set up, which is true throughout xfsprogs... except for the beginning of xfs_copy. For whatever reason, it attempts an uncached read of the superblock to figure out the real superblock size, then performs a *cached* read with the proper buffer length and verifier. The cached read crashes the program.

Fix the problem by changing the (second) cached read into an uncached read.

Fixes f8b581d6 ("libxfs: actually make buffers track the per-ag structures")
---
 copy/xfs_copy.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
copy/xfs_copy.c +1 -1
index 41f594bd..79f65946 100644
@@ -748,7 +748,7 @@ main(int argc, char **argv)
 	/* Do it again, now with proper length and verifier */
 	libxfs_buf_relse(sbp);
 
-	error = -libxfs_buf_read(mbuf.m_ddev_targp, XFS_SB_DADDR,
+	error = -libxfs_buf_read_uncached(mbuf.m_ddev_targp, XFS_SB_DADDR,
 			1 << (sb->sb_sectlog - BBSHIFT), 0, &sbp,
 			&xfs_sb_buf_ops);
 	if (error) {
#1 lore raw

PATCH 1/1 xfs: test xfs_copy doesn't do cached read before libxfs_mount

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

This is a regression test for an xfs_copy fix that ensures that it doesn't perform a cached read of an XFS filesystem prior to initializing libxfs, since the xfs_mount (and hence the buffer cache) isn't set up yet.

---
 tests/xfs/844     |   37 +++++++++++++++++++++++++++++++++++++
 tests/xfs/844.out |    3 +++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100755 tests/xfs/844
 create mode 100644 tests/xfs/844.out
tests/xfs/844 new file +37 -0
new file mode 100755
index 00000000..720f45bb
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2022 Oracle.  All Rights Reserved.
+#
+# FS QA Test 844
+#
+# Regression test for xfsprogs commit:
+#
+# XXXXXXXX ("xfs_copy: don't use cached buffer reads until after libxfs_mount")
+#
+. ./common/preamble
+_begin_fstest auto copy
+
+_cleanup()
+{
+	cd /
+	rm -r -f $tmp.* $TEST_DIR/$seq.*
+}
+
+# Import common functions.
+# . ./common/filter
+
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs generic
+_require_xfs_copy
+_require_test
+
+truncate -s 100m $TEST_DIR/$seq.a
+truncate -s 100m $TEST_DIR/$seq.b
+
+$XFS_COPY_PROG $TEST_DIR/$seq.a $TEST_DIR/$seq.b
+
+# success, all done
+status=0
+exit
tests/xfs/844.out new file +3 -0
new file mode 100644
index 00000000..dbefde1c
@@ -0,0 +1,3 @@
+QA output created by 844
+bad magic number
+xfs_copy: couldn't read superblock, error=22
#2 lore raw

Re: [PATCH] xfs: test xfs_copy doesn't do cached read before libxfs_mount

Quoted 29 lines · Darrick J. Wong

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

This is a regression test for an xfs_copy fix that ensures that it doesn't perform a cached read of an XFS filesystem prior to initializing libxfs, since the xfs_mount (and hence the buffer cache) isn't set up yet.

---
 tests/xfs/844     |   37 +++++++++++++++++++++++++++++++++++++
 tests/xfs/844.out |    3 +++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100755 tests/xfs/844
 create mode 100644 tests/xfs/844.out
tests/xfs/844 new file +8 -0
new file mode 100755
index 00000000..720f45bb
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2022 Oracle.  All Rights Reserved.
+#
+# FS QA Test 844
+#
+# Regression test for xfsprogs commit:
+#

tests/xfs/844 line 18

Quoted 10 lines
tests/xfs/844 new file +10 -0
+# XXXXXXXX ("xfs_copy: don't use cached buffer reads until after libxfs_mount")
+#
+. ./common/preamble
+_begin_fstest auto copy
+
+_cleanup()
+{
+	cd /
+	rm -r -f $tmp.* $TEST_DIR/$seq.*
+}

Not necessary - $TEST_DIR/$seq.* are sparse files that take up no space.

tests/xfs/844 line 29

Quoted 11 lines
tests/xfs/844 new file +11 -0
+
+# Import common functions.
+# . ./common/filter
+
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs generic
+_require_xfs_copy
+_require_test
+

rm -f $TEST_DIR/$seq.*

To set up known initial state prior to starting the test.

Otherwise looks OK.

Cheers,

Dave.

Quoted 18 lines
tests/xfs/844 new file +8 -0
+truncate -s 100m $TEST_DIR/$seq.a
+truncate -s 100m $TEST_DIR/$seq.b
+
+$XFS_COPY_PROG $TEST_DIR/$seq.a $TEST_DIR/$seq.b
+
+# success, all done
+status=0
+exit
tests/xfs/844.out new file +3 -0
new file mode 100644
index 00000000..dbefde1c
@@ -0,0 +1,3 @@
+QA output created by 844
+bad magic number
+xfs_copy: couldn't read superblock, error=22
Signature · 2 lines
Dave Chinner
[email protected]
#3 lore raw

Re: [PATCH] xfs: test xfs_copy doesn't do cached read before libxfs_mount

In reply to Darrick J. Wong
Quoted 21 lines · Darrick J. Wong

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

This is a regression test for an xfs_copy fix that ensures that it doesn't perform a cached read of an XFS filesystem prior to initializing libxfs, since the xfs_mount (and hence the buffer cache) isn't set up yet.

---
 tests/xfs/844     |   37 +++++++++++++++++++++++++++++++++++++
 tests/xfs/844.out |    3 +++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100755 tests/xfs/844
 create mode 100644 tests/xfs/844.out
tests/xfs/844 new file +0 -0
new file mode 100755
index 00000000..720f45bb

tests/xfs/844 line 9

Quoted 9 lines
tests/xfs/844 new file +9 -0
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2022 Oracle.  All Rights Reserved.
+#
+# FS QA Test 844
+#
+# Regression test for xfsprogs commit:
+#
+# XXXXXXXX ("xfs_copy: don't use cached buffer reads until after libxfs_mount")

^^^ I'd like to merge a test after it's fixed offically at first, anyway that doesn't prevent us from reviewing it at first :)

Quoted 6 lines
tests/xfs/844 new file +6 -0
+#
+. ./common/preamble
+_begin_fstest auto copy
+
+_cleanup()
+{

tests/xfs/844 line 25

Quoted 10 lines
tests/xfs/844 new file +10 -0
+	cd /
+	rm -r -f $tmp.* $TEST_DIR/$seq.*
+}
+
+# Import common functions.
+# . ./common/filter
+
+# real QA test starts here
+
+# Modify as appropriate.

"Modify as appropriate." can be removed.

tests/xfs/844 line 31

Quoted 6 lines
tests/xfs/844 new file +6 -0
+_supported_fs generic
+_require_xfs_copy
+_require_test
+
+truncate -s 100m $TEST_DIR/$seq.a
+truncate -s 100m $TEST_DIR/$seq.b

fstests recommends using "truncate" of XFS_IO_PROG, although use 'truncate' isn't wrong at here.

As TEST_DIR might have existed files, do we need to do

        rm -rf $TEST_DIR/$seq.a $TEST_DIR/$seq.b

at first?

Thanks,
Zorro

Quoted 16 lines
tests/xfs/844 new file +6 -0
+
+$XFS_COPY_PROG $TEST_DIR/$seq.a $TEST_DIR/$seq.b
+
+# success, all done
+status=0
+exit
tests/xfs/844.out new file +3 -0
new file mode 100644
index 00000000..dbefde1c
@@ -0,0 +1,3 @@
+QA output created by 844
+bad magic number
+xfs_copy: couldn't read superblock, error=22