common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / generic / 398
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
4 #
5 # FS QA Test generic/398
6 #
7 # Filesystem encryption is designed to enforce that a consistent encryption
8 # policy is used within a given encrypted directory tree and that an encrypted
9 # directory tree does not contain any unencrypted files.  This test verifies
10 # that filesystem operations that would violate this constraint fail.  This does
11 # not test enforcement of this constraint on lookup, which is still needed to
12 # detect offline changes.
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 }
28
29 # The error code for incompatible rename or link into an encrypted directory was
30 # changed from EPERM to EXDEV in Linux v5.1, to allow tools like 'mv' to work.
31 # See kernel commit f5e55e777cc9 ("fscrypt: return -EXDEV for incompatible
32 # rename or link into encrypted dir").  Accept both errors for now.
33 filter_eperm_to_exdev()
34 {
35         sed -e 's/Operation not permitted/Invalid cross-device link/'
36 }
37
38 # The error code for incompatible cross-rename without the key has been ENOKEY
39 # on all filesystems since Linux v4.16.  Previously it was EPERM on some
40 # filesystems.  Accept both errors for now.
41 filter_eperm_to_enokey()
42 {
43         sed -e 's/Operation not permitted/Required key not available/'
44 }
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49 . ./common/encrypt
50 . ./common/renameat2
51
52 # remove previous $seqres.full before test
53 rm -f $seqres.full
54
55 # real QA test starts here
56 _supported_fs generic
57 _supported_os Linux
58 _require_scratch_encryption
59 _require_renameat2 exchange
60
61 _new_session_keyring
62 _scratch_mkfs_encrypted &>> $seqres.full
63 _scratch_mount
64
65 # Set up two encrypted directories, with different encryption policies,
66 # and one unencrypted directory.
67 edir1=$SCRATCH_MNT/edir1
68 edir2=$SCRATCH_MNT/edir2
69 udir=$SCRATCH_MNT/udir
70 mkdir $edir1 $edir2 $udir
71 keydesc1=$(_generate_session_encryption_key)
72 keydesc2=$(_generate_session_encryption_key)
73 _set_encpolicy $edir1 $keydesc1
74 _set_encpolicy $edir2 $keydesc2
75 touch $edir1/efile1
76 touch $edir2/efile2
77 touch $udir/ufile
78
79
80 # Test linking and renaming an encrypted file into an encrypted directory with a
81 # different encryption policy.  Should fail with EXDEV.
82
83 echo -e "\n*** Link encrypted <= encrypted ***"
84 ln $edir1/efile1 $edir2/efile1 |& _filter_scratch | filter_eperm_to_exdev
85
86 echo -e "\n*** Rename encrypted => encrypted ***"
87 $here/src/renameat2 $edir1/efile1 $edir2/efile1 |& filter_eperm_to_exdev
88
89
90 # Test linking and renaming an unencrypted file into an encrypted directory.
91 # Should fail with EXDEV.
92
93 echo -e "\n\n*** Link unencrypted <= encrypted ***"
94 ln $udir/ufile $edir1/ufile |& _filter_scratch | filter_eperm_to_exdev
95
96 echo -e "\n*** Rename unencrypted => encrypted ***"
97 $here/src/renameat2 $udir/ufile $edir1/ufile |& filter_eperm_to_exdev
98
99
100 # Test linking and renaming an encrypted file into an unencrypted directory.
101 # Should succeed.
102
103 echo -e "\n\n*** Link encrypted <= unencrypted ***"
104 ln -v $edir1/efile1 $udir/efile1 |& _filter_scratch
105 rm $udir/efile1 # undo
106
107 echo -e "\n*** Rename encrypted => unencrypted ***"
108 $here/src/renameat2 $edir1/efile1 $udir/efile1
109 $here/src/renameat2 $udir/efile1 $edir1/efile1 # undo
110
111
112 # Test renaming a forbidden (unencrypted, or encrypted with a different
113 # encryption policy) file into an encrypted directory via an exchange (cross
114 # rename) operation.  Should fail with EXDEV.
115
116 echo -e "\n\n*** Exchange encrypted <=> encrypted ***"
117 $here/src/renameat2 -x $edir1/efile1 $edir2/efile2 |& filter_eperm_to_exdev
118
119 echo -e "\n*** Exchange unencrypted <=> encrypted ***"
120 $here/src/renameat2 -x $udir/ufile $edir1/efile1 |& filter_eperm_to_exdev
121
122 echo -e "\n*** Exchange encrypted <=> unencrypted ***"
123 $here/src/renameat2 -x $edir1/efile1 $udir/ufile |& filter_eperm_to_exdev
124
125
126 # Test a file with a special type, i.e. not regular, directory, or symlink.
127 # Since such files are not subject to encryption, there should be no
128 # restrictions on linking or renaming them into encrypted directories.
129
130 echo -e "\n\n*** Special file tests ***"
131 mkfifo $edir1/fifo
132 $here/src/renameat2 $edir1/fifo $edir2/fifo
133 $here/src/renameat2 $edir2/fifo $udir/fifo
134 $here/src/renameat2 $udir/fifo $edir1/fifo
135 mkfifo $udir/fifo
136 $here/src/renameat2 -x $udir/fifo $edir1/fifo
137 ln -v $edir1/fifo $edir2/fifo | _filter_scratch
138 rm $edir1/fifo $edir2/fifo $udir/fifo
139
140
141 # Now test that *without* access to the encrypted key, we cannot use an exchange
142 # (cross rename) operation to move a forbidden file into an encrypted directory.
143
144 _unlink_session_encryption_key $keydesc1
145 _unlink_session_encryption_key $keydesc2
146 _scratch_cycle_mount
147 efile1=$(find $edir1 -type f)
148 efile2=$(find $edir2 -type f)
149
150 echo -e "\n\n*** Exchange encrypted <=> encrypted without key ***"
151 $here/src/renameat2 -x $efile1 $efile2 |& filter_eperm_to_enokey
152 echo -e "\n*** Exchange encrypted <=> unencrypted without key ***"
153 $here/src/renameat2 -x $efile1 $udir/ufile |& filter_eperm_to_enokey
154
155 # success, all done
156 status=0
157 exit