generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / generic / 572
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2018 Google LLC
4 #
5 # FS QA Test generic/572
6 #
7 # This is a basic fs-verity test which verifies:
8 #
9 # - conditions for enabling verity
10 # - verity files have correct contents and size
11 # - can't change contents of verity files, but can change metadata
12 # - can retrieve a verity file's measurement via FS_IOC_MEASURE_VERITY
13 #
14 . ./common/preamble
15 _begin_fstest auto quick verity
16
17 # Override the default cleanup function.
18 _cleanup()
19 {
20         cd /
21         _restore_fsverity_signatures
22         rm -f $tmp.*
23 }
24
25 # Import common functions.
26 . ./common/filter
27 . ./common/verity
28
29 # real QA test starts here
30 _supported_fs generic
31 _require_scratch_verity
32 _disable_fsverity_signatures
33
34 _scratch_mkfs_verity &>> $seqres.full
35 _scratch_mount
36 fsv_orig_file=$SCRATCH_MNT/file
37 fsv_file=$SCRATCH_MNT/file.fsv
38
39 verify_data_readable()
40 {
41         local file=$1
42
43         md5sum $file > /dev/null
44 }
45
46 verify_data_unreadable()
47 {
48         local file=$1
49
50         # try both reading just the first data block, and reading until EOF
51         head -c $FSV_BLOCK_SIZE $file 2>&1 >/dev/null | _filter_scratch
52         md5sum $file |& _filter_scratch
53 }
54
55 _fsv_scratch_begin_subtest "Enabling verity on file with verity already enabled fails with EEXIST"
56 _fsv_create_enable_file $fsv_file
57 echo "(trying again)"
58 _fsv_enable $fsv_file |& _filter_scratch
59
60 _fsv_scratch_begin_subtest "Enabling verity with invalid hash algorithm fails with EINVAL"
61 _fsv_create_enable_file $fsv_file --hash-alg=257 |& _filter_scratch
62 verify_data_readable $fsv_file
63
64 _fsv_scratch_begin_subtest "Enabling verity with invalid block size fails with EINVAL"
65 _fsv_create_enable_file $fsv_file --block-size=1 |& _filter_scratch
66 verify_data_readable $fsv_file
67
68 _fsv_scratch_begin_subtest "Enabling verity on directory fails with EISDIR"
69 mkdir $SCRATCH_MNT/dir
70 _fsv_enable $SCRATCH_MNT/dir |& _filter_scratch
71
72 _fsv_scratch_begin_subtest "Enabling verity with too-long salt fails with EMSGSIZE"
73 _fsv_create_enable_file $fsv_file --salt=$(perl -e 'print "A" x 1000') |& _filter_scratch
74 verify_data_readable $fsv_file
75
76 _fsv_scratch_begin_subtest "Enabling verity on file on read-only filesystem fails with EROFS"
77 echo foo > $fsv_file
78 _scratch_remount ro
79 _fsv_enable $fsv_file |& _filter_scratch
80 _scratch_remount rw
81
82 _fsv_scratch_begin_subtest "Enabling verity on file open for writing fails with ETXTBSY"
83 echo foo > $fsv_file
84 exec 3<> $fsv_file
85 _fsv_enable $fsv_file |& _filter_scratch
86 exec 3<&-
87 verify_data_readable $fsv_file
88
89 _fsv_scratch_begin_subtest "Enabling verity can be interrupted"
90 dd if=/dev/zero of=$fsv_file bs=1 count=0 seek=$((1 << 34)) status=none
91 start_time=$(date +%s)
92 $FSVERITY_PROG enable $fsv_file &
93 sleep 0.5
94 kill %1
95 wait
96 elapsed=$(( $(date +%s) - start_time ))
97 if (( elapsed > 5 )); then
98         echo "Failed to interrupt FS_IOC_ENABLE_VERITY ($elapsed seconds elapsed)"
99 fi
100
101 _fsv_scratch_begin_subtest "Enabling verity on file with verity already being enabled fails with EBUSY"
102 dd if=/dev/zero of=$fsv_file bs=1 count=0 seek=$((1 << 34)) status=none
103 start_time=$(date +%s)
104 $FSVERITY_PROG enable $fsv_file &
105 sleep 0.5
106 _fsv_enable $fsv_file |& _filter_scratch
107 kill %1
108 wait
109
110 _fsv_scratch_begin_subtest "verity file can't be opened for writing"
111 _fsv_create_enable_file $fsv_file >> $seqres.full
112 echo "* reading"
113 $XFS_IO_PROG -r $fsv_file -c ''
114 echo "* xfs_io writing, should be O_RDWR"
115 $XFS_IO_PROG $fsv_file -c '' |& _filter_scratch
116 echo "* bash >>, should be O_APPEND"
117 bash -c "echo >> $fsv_file" |& _filter_scratch
118 echo "* bash >, should be O_WRONLY|O_CREAT|O_TRUNC"
119 bash -c "echo > $fsv_file" |& _filter_scratch
120
121 _fsv_scratch_begin_subtest "verity file can be read"
122 _fsv_create_enable_file $fsv_file >> $seqres.full
123 verify_data_readable $fsv_file
124
125 _fsv_scratch_begin_subtest "verity file can be measured"
126 _fsv_create_enable_file $fsv_file >> $seqres.full
127 _fsv_measure $fsv_file
128
129 _fsv_scratch_begin_subtest "verity file can be renamed"
130 _fsv_create_enable_file $fsv_file
131 mv $fsv_file $fsv_file.newname
132
133 _fsv_scratch_begin_subtest "verity file can be unlinked"
134 _fsv_create_enable_file $fsv_file
135 rm $fsv_file
136
137 _fsv_scratch_begin_subtest "verity file can be linked to"
138 _fsv_create_enable_file $fsv_file
139 ln $fsv_file $fsv_file.newname
140
141 _fsv_scratch_begin_subtest "verity file can be chmodded"
142 _fsv_create_enable_file $fsv_file
143 chmod 777 $fsv_file
144 chmod 444 $fsv_file
145
146 _fsv_scratch_begin_subtest "verity file can be chowned"
147 _fsv_create_enable_file $fsv_file
148 chown 1:1 $fsv_file
149 chown 0:0 $fsv_file
150
151 _fsv_scratch_begin_subtest "verity file has correct contents and size"
152 head -c 100000 /dev/urandom > $fsv_orig_file
153 cp $fsv_orig_file $fsv_file
154 _fsv_enable $fsv_file >> $seqres.full
155 cmp $fsv_file $fsv_orig_file
156 _get_filesize $fsv_file
157 _scratch_cycle_mount
158 cmp $fsv_file $fsv_orig_file
159 _get_filesize $fsv_file
160
161 _fsv_scratch_begin_subtest "Trying to measure non-verity file fails with ENODATA"
162 echo foo > $fsv_file
163 _fsv_measure $fsv_file |& _filter_scratch
164 verify_data_readable $fsv_file
165
166 # Test files <= 1 block in size.  These are a bit of a special case since there
167 # are no hash blocks; the root hash is calculated directly over the data block.
168 for size in 1 $((FSV_BLOCK_SIZE - 1)) $FSV_BLOCK_SIZE; do
169         _fsv_scratch_begin_subtest "verity on $size-byte file"
170         head -c $size /dev/urandom > $fsv_orig_file
171         cp $fsv_orig_file $fsv_file
172         _fsv_enable $fsv_file
173         cmp $fsv_orig_file $fsv_file && echo "Files matched"
174         rm -f $fsv_file
175 done
176
177 _fsv_scratch_begin_subtest "verity on 100M file (multiple levels in hash tree)"
178 head -c 100000000 /dev/urandom > $fsv_orig_file
179 cp $fsv_orig_file $fsv_file
180 _fsv_enable $fsv_file
181 cmp $fsv_orig_file $fsv_file && echo "Files matched"
182
183 _fsv_scratch_begin_subtest "verity on sparse file"
184 dd if=/dev/zero of=$fsv_orig_file bs=1 count=1 seek=1000000 status=none
185 cp $fsv_orig_file $fsv_file
186 _fsv_enable $fsv_file
187 cmp $fsv_orig_file $fsv_file && echo "Files matched"
188
189 # success, all done
190 status=0
191 exit