generic: test for buggy fscrypt context consistency check
[xfstests-dev.git] / tests / generic / 440
1 #! /bin/bash
2 # FS QA Test No. 440
3 #
4 # Test that when the filesystem tries to enforce that all files in a directory
5 # tree use the same encryption policy, it doesn't get confused and incorrectly
6 # return EPERM in cases where the parent's key is cached but not the child's, or
7 # vice versa.  Such situations can arise following removal of the master key
8 # from the keyring.  Regression test for:
9 #       272f98f68462 ("fscrypt: fix context consistency check when key(s) unavailable")
10 #
11 #-----------------------------------------------------------------------
12 # Copyright (c) 2017 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
51 # remove previous $seqres.full before test
52 rm -f $seqres.full
53
54 # real QA test starts here
55 _supported_fs generic
56 _supported_os Linux
57 _require_scratch_encryption
58 _require_xfs_io_command "set_encpolicy"
59 _require_command "$KEYCTL_PROG" keyctl
60
61 # Set up an encryption-capable filesystem and an encryption key.
62 _new_session_keyring
63 _scratch_mkfs_encrypted &>> $seqres.full
64 _scratch_mount
65 keydesc=$(_generate_key_descriptor)
66 raw_key=$(_generate_raw_encryption_key)
67 _add_encryption_key $keydesc $raw_key
68
69 # Set up an encrypted directory containing a regular file, a subdirectory, and a
70 # symlink.
71 mkdir $SCRATCH_MNT/edir
72 $XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir
73 mkdir $SCRATCH_MNT/edir/subdir
74 ln -s target $SCRATCH_MNT/edir/symlink
75 echo contents > $SCRATCH_MNT/edir/file
76
77 # Starting from a fresh mount (no inodes have their encryption key cached),
78 # reproduce the situation where an encrypted file *without* its key cached is
79 # looked up or opened from within a directory *with* its key cached, and no key
80 # is in the keyring.  Try with a regular file, a directory, and a symlink.
81 _scratch_cycle_mount
82 echo
83 echo "***** Parent has key, but child doesn't *****"
84 exec 3< $SCRATCH_MNT/edir # pin inode with cached key in memory
85 ls $SCRATCH_MNT/edir | sort
86 _unlink_encryption_key $keydesc
87 cat $SCRATCH_MNT/edir/file |& _filter_scratch
88 ls $SCRATCH_MNT/edir/subdir
89 cat $SCRATCH_MNT/edir/symlink |& _filter_scratch
90 exec 3>&-
91
92 # Now, the inverse: an encrypted file *with* its key cached is looked up or
93 # opened from within a directory *without* its key cached, and no key is in the
94 # keyring.  This is most easily reproducible using a hard link.  Note: the
95 # expected behavior (at least, until we have a real API for revoking filesystem
96 # encryption keys) is that we should still be able to open the file and read its
97 # plaintext contents, even though its filename is shown in ciphertext!
98 echo
99 echo "***** Child has key, but parent doesn't *****"
100 _add_encryption_key $keydesc $raw_key
101 mkdir $SCRATCH_MNT/edir2
102 $XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir2
103 ln $SCRATCH_MNT/edir/file $SCRATCH_MNT/edir2/link
104 _scratch_cycle_mount
105 cat $SCRATCH_MNT/edir2/link
106 exec 3< $SCRATCH_MNT/edir2/link # pin inode with cached key in memory
107 _unlink_encryption_key $keydesc
108 stat $SCRATCH_MNT/edir/file |& _filter_scratch
109 cat "$(find $SCRATCH_MNT/edir/ -type f)"
110 exec 3>&-
111
112 # success, all done
113 status=0
114 exit