51bc4100a1d5bb266bcd3cc950a7021231ad5628
[xfstests-dev.git] / tests / xfs / 520
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test 520
6 #
7 # Verify kernel doesn't hang when mounting a crafted image
8 # with bad agf.freeblks metadata due to CVE-2020-12655.
9 #
10 # Also, check if
11 # commit d0c7feaf8767 ("xfs: add agf freeblocks verify in xfs_agf_verify")
12 # is included in the current kernel.
13 #
14 seq=`basename $0`
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17
18 here=`pwd`
19 tmp=/tmp/$$
20 status=1        # failure is the default!
21 trap "_cleanup; exit \$status" 0 1 2 3 15
22
23 _cleanup()
24 {
25         cd /
26         rm -f $tmp.*
27         _scratch_unmount > /dev/null 2>&1
28 }
29
30 # get standard environment, filters and checks
31 . ./common/rc
32 . ./common/filter
33
34 # remove previous $seqres.full before test
35 rm -f $seqres.full
36
37 # real QA test starts here
38
39 _supported_fs xfs
40 _disable_dmesg_check
41 _require_check_dmesg
42 _require_scratch_nocheck
43
44 force_crafted_metadata() {
45         _scratch_mkfs_xfs -f $fsdsopt "$4" >> $seqres.full 2>&1 || _fail "mkfs failed"
46         _scratch_xfs_set_metadata_field "$1" "$2" "$3" >> $seqres.full 2>&1
47         local kmsg="xfs/$seq: testing $1=$2 at $(date +"%F %T")"
48         local mounted=0
49         local hasmsg=0
50
51         echo "${kmsg}" > /dev/kmsg
52         _try_scratch_mount >> $seqres.full 2>&1 && mounted=1
53
54         if [ $mounted -ne 0 ]; then
55                 dd if=/dev/zero of=$SCRATCH_MNT/test bs=65536 count=1 >> \
56                         $seqres.full 2>&1
57                 sync
58         fi
59
60         _dmesg_since_test_start | tac | sed -ne "0,\#${kmsg}#p" | tac | \
61                 egrep -q 'Metadata corruption detected at' && hasmsg=1
62
63         _scratch_unmount > /dev/null 2>&1
64         [ $mounted -eq 0 -o $hasmsg -eq 1 ] || \
65                 _fail "potential broken kernel (mounted=${mounted},hasmsg=${hasmsg})"
66 }
67
68 bigval=100000000
69 fsdsopt="-d agcount=1,size=64m"
70
71 force_crafted_metadata freeblks 0 "agf 0"
72 force_crafted_metadata longest $bigval "agf 0"
73 force_crafted_metadata length $bigval "agf 0"
74
75 _scratch_mkfs_xfs_supported -m reflink=1 >> $seqres.full 2>&1 && \
76         force_crafted_metadata refcntblocks $bigval "agf 0" "-m reflink=1"
77
78 _scratch_mkfs_xfs_supported -m rmapbt=1 >> $seqres.full 2>&1 && \
79         force_crafted_metadata rmapblocks $bigval "agf 0" "-m rmapbt=1"
80
81 echo "Silence is golden"
82
83 # success, all done
84 status=0
85 exit