73b41b3878a9bfa8abf3d2fd7d6fd33a1968a445
[xfstests-dev.git] / tests / generic / 397
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/397
6 #
7 # Test accessing encrypted files and directories, both with and without the
8 # encryption key.  Access with the encryption key is more of a sanity check and
9 # is not intended to fully test all the encrypted I/O paths; to do that you'd
10 # need to run all the xfstests with encryption enabled.  Access without the
11 # encryption key, on the other hand, should result in some particular behaviors.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31 . ./common/encrypt
32
33 # remove previous $seqres.full before test
34 rm -f $seqres.full
35
36 # real QA test starts here
37 _supported_fs generic
38 _require_symlinks
39 _require_scratch_encryption
40 _require_command "$KEYCTL_PROG" keyctl
41
42 _new_session_keyring
43
44 _scratch_mkfs_encrypted &>> $seqres.full
45 _scratch_mount
46
47 mkdir $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
48 keydesc=$(_generate_session_encryption_key)
49 _set_encpolicy $SCRATCH_MNT/edir $keydesc
50 for dir in $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir; do
51         touch $dir/empty > /dev/null
52         $XFS_IO_PROG -t -f -c "pwrite 0 4k" $dir/a > /dev/null
53         $XFS_IO_PROG -t -f -c "pwrite 0 33k" $dir/abcdefghijklmnopqrstuvwxyz > /dev/null
54         maxname=$(head -c 255 /dev/zero | tr '\0' y) # 255 character filename
55         $XFS_IO_PROG -t -f -c "pwrite 0 1k" $dir/$maxname > /dev/null
56         ln -s a $dir/symlink
57         ln -s abcdefghijklmnopqrstuvwxyz $dir/symlink2
58         ln -s $maxname $dir/symlink3
59         mkdir $dir/subdir
60         mkdir $dir/subdir/subsubdir
61 done
62 # Diff encrypted directory with unencrypted reference directory
63 diff -r $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
64 # Cycle mount and diff again
65 _scratch_cycle_mount
66 diff -r $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
67
68 #
69 # Now try accessing the files without the encryption key.  It should still be
70 # possible to list the directory and remove files.  But filenames should be
71 # encrypted, and it should not be possible to read regular files or to create
72 # new files or subdirectories.
73 #
74 # Note that we cannot simply use ls -R to verify the files because the encrypted
75 # filenames are unpredictable.  By design, the key used to encrypt a directory's
76 # filenames is derived from the master key (the key in the keyring) and a nonce
77 # generated by the kernel.  Hence, the encrypted filenames will be different
78 # every time this test is run, even if we were to put a fixed key into the
79 # keyring instead of a random one.  The same applies to symlink targets.
80 #
81 # TODO: there are some inconsistencies in which error codes are returned on
82 # different kernel versions and filesystems when trying to create a file or
83 # subdirectory without access to the parent directory's encryption key.  It's
84 # planned to consistently use ENOKEY, but for now make this test accept multiple
85 # error codes...
86 #
87
88 filter_create_errors()
89 {
90         sed -e 's/No such file or directory/Required key not available/' \
91             -e 's/Permission denied/Required key not available/' \
92             -e 's/Operation not permitted/Required key not available/'
93 }
94
95 _unlink_session_encryption_key $keydesc
96 _scratch_cycle_mount
97
98 # Check that unencrypted names aren't there
99 stat $SCRATCH_MNT/edir/empty |& _filter_scratch
100 stat $SCRATCH_MNT/edir/symlink |& _filter_scratch
101
102 # Check that the correct numbers of files and subdirectories are there
103 ls $SCRATCH_MNT/edir | wc -l
104 find $SCRATCH_MNT/edir -mindepth 2 -maxdepth 2 -type d | wc -l
105
106 # Try to read a nondirectory file (should fail with ENOKEY)
107 md5sum $(find $SCRATCH_MNT/edir -maxdepth 1 -type f | head -1) |& \
108                 cut -d ' ' -f3-
109
110 # Try to create new files, directories, and symlinks in the encrypted directory,
111 # both with and without using correctly base-64 encoded filenames.  These should
112 # all fail with ENOKEY.
113 $XFS_IO_PROG -f $SCRATCH_MNT/edir/newfile |& filter_create_errors | _filter_scratch
114 $XFS_IO_PROG -f $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
115 mkdir $SCRATCH_MNT/edir/newdir |& filter_create_errors | _filter_scratch
116 mkdir $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
117 ln -s foo $SCRATCH_MNT/edir/newlink |& filter_create_errors | _filter_scratch
118 ln -s foo $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
119
120 # Delete the encrypted directory (should succeed)
121 rm -r $SCRATCH_MNT/edir
122 stat $SCRATCH_MNT/edir |& _filter_scratch
123
124 # success, all done
125 status=0
126 exit