generic: test enforcement of one encryption policy per tree
[xfstests-dev.git] / tests / generic / 398
1 #! /bin/bash
2 # FS QA Test generic/398
3 #
4 # Filesystem encryption is designed to enforce that a consistent encryption
5 # policy is used within a given encrypted directory tree and that an encrypted
6 # directory tree does not contain any unencrypted files.  This test verifies
7 # that filesystem operations that would violate this constraint fail with EPERM.
8 # This does not test enforcement of this constraint on lookup, which is still
9 # needed to detect offline changes.
10 #
11 #-----------------------------------------------------------------------
12 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
13 #
14 # Author: Eric Biggers <ebiggers@google.com>
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License as
18 # published by the Free Software Foundation.
19 #
20 # This program is distributed in the hope that it would be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 #-----------------------------------------------------------------------
29 #
30
31 seq=`basename $0`
32 seqres=$RESULT_DIR/$seq
33 echo "QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42         cd /
43         rm -f $tmp.*
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_xfs_io_command "set_encpolicy"
60 _requires_renameat2
61
62 _new_session_keyring
63 _scratch_mkfs_encrypted &>> $seqres.full
64 _scratch_mount
65
66 # Set up two encrypted directories, with different encryption policies,
67 # and one unencrypted directory.
68 edir1=$SCRATCH_MNT/edir1
69 edir2=$SCRATCH_MNT/edir2
70 udir=$SCRATCH_MNT/udir
71 mkdir $edir1 $edir2 $udir
72 keydesc1=$(_generate_encryption_key)
73 keydesc2=$(_generate_encryption_key)
74 $XFS_IO_PROG -c "set_encpolicy $keydesc1" $edir1
75 $XFS_IO_PROG -c "set_encpolicy $keydesc2" $edir2
76 touch $edir1/efile1
77 touch $edir2/efile2
78 touch $udir/ufile
79
80
81 # Test linking and moving an encrypted file into an encrypted directory with a
82 # different encryption policy.  Should fail with EPERM.
83
84 echo -e "\n*** Link encrypted <= encrypted ***"
85 ln $edir1/efile1 $edir2/efile1 |& _filter_scratch
86
87 echo -e "\n*** Rename encrypted => encrypted ***"
88 mv $edir1/efile1 $edir2/efile1 |& _filter_scratch
89
90
91 # Test linking and moving an unencrypted file into an encrypted directory.
92 # Should fail with EPERM.
93
94 echo -e "\n\n*** Link unencrypted <= encrypted ***"
95 ln $udir/ufile $edir1/ufile |& _filter_scratch
96
97 echo -e "\n*** Rename unencrypted => encrypted ***"
98 mv $udir/ufile $edir1/ufile |& _filter_scratch
99
100
101 # Test linking and moving an encrypted file into an unencrypted directory.
102 # Should succeed.
103
104 echo -e "\n\n*** Link encrypted <= unencrypted ***"
105 ln -v $edir1/efile1 $udir/efile1 |& _filter_scratch
106 rm $udir/efile1 # undo
107
108 echo -e "\n*** Rename encrypted => unencrypted ***"
109 mv -v $edir1/efile1 $udir/efile1 |& _filter_scratch
110 mv $udir/efile1 $edir1/efile1 # undo
111
112
113 # Test moving a forbidden (unencrypted, or encrypted with a different encryption
114 # policy) file into an encrypted directory via an exchange (cross rename)
115 # operation.  Should fail with EPERM.
116
117 echo -e "\n\n*** Exchange encrypted <=> encrypted ***"
118 src/renameat2 -x $edir1/efile1 $edir2/efile2 |& _filter_scratch
119
120 echo -e "\n*** Exchange unencrypted <=> encrypted ***"
121 src/renameat2 -x $udir/ufile $edir1/efile1 |& _filter_scratch
122
123 echo -e "\n*** Exchange encrypted <=> unencrypted ***"
124 src/renameat2 -x $edir1/efile1 $udir/ufile |& _filter_scratch
125
126
127 # Test a file with a special type, i.e. not regular, directory, or symlink.
128 # Since such files are not subject to encryption, there should be no
129 # restrictions on linking or moving them into encrypted directories.
130
131 echo -e "\n\n*** Special file tests ***"
132 mkfifo $edir1/fifo
133 mv -v $edir1/fifo $edir2/fifo | _filter_scratch
134 mv -v $edir2/fifo $udir/fifo | _filter_scratch
135 mv -v $udir/fifo $edir1/fifo | _filter_scratch
136 mkfifo $udir/fifo
137 src/renameat2 -x $udir/fifo $edir1/fifo
138 ln -v $edir1/fifo $edir2/fifo | _filter_scratch
139 rm $edir1/fifo $edir2/fifo $udir/fifo
140
141
142 # Now test that *without* access to the encrypted key, we cannot use an exchange
143 # (cross rename) operation to move a forbidden file into an encrypted directory.
144
145 _unlink_encryption_key $keydesc1
146 _unlink_encryption_key $keydesc2
147 _scratch_cycle_mount
148 efile1=$(find $edir1 -type f)
149 efile2=$(find $edir2 -type f)
150
151 echo -e "\n\n*** Exchange encrypted <=> encrypted without key ***"
152 src/renameat2 -x $efile1 $efile2
153 echo -e "\n*** Exchange encrypted <=> unencrypted without key ***"
154 src/renameat2 -x $efile1 $udir/ufile
155
156 # success, all done
157 status=0
158 exit