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