generic: convert tests to SPDX license tags
[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 _supported_os Linux
39 _require_scratch_encryption
40 _require_xfs_io_command "set_encpolicy"
41 _require_command "$KEYCTL_PROG" keyctl
42
43 _new_session_keyring
44
45 _scratch_mkfs_encrypted &>> $seqres.full
46 _scratch_mount
47
48 mkdir $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
49 keydesc=$(_generate_encryption_key)
50 $XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir
51 for dir in $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir; do
52         touch $dir/empty > /dev/null
53         $XFS_IO_PROG -t -f -c "pwrite 0 4k" $dir/a > /dev/null
54         $XFS_IO_PROG -t -f -c "pwrite 0 33k" $dir/abcdefghijklmnopqrstuvwxyz > /dev/null
55         maxname=$(head -c 255 /dev/zero | tr '\0' y) # 255 character filename
56         $XFS_IO_PROG -t -f -c "pwrite 0 1k" $dir/$maxname > /dev/null
57         ln -s a $dir/symlink
58         ln -s abcdefghijklmnopqrstuvwxyz $dir/symlink2
59         ln -s $maxname $dir/symlink3
60         mkdir $dir/subdir
61         mkdir $dir/subdir/subsubdir
62 done
63 # Diff encrypted directory with unencrypted reference directory
64 diff -r $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
65 # Cycle mount and diff again
66 _scratch_cycle_mount
67 diff -r $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
68
69 #
70 # Now try accessing the files without the encryption key.  It should still be
71 # possible to list the directory and remove files.  But filenames should be
72 # encrypted, and it should not be possible to read regular files or to create
73 # new files or subdirectories.
74 #
75 # Note that we cannot simply use ls -R to verify the files because the encrypted
76 # filenames are unpredictable.  By design, the key used to encrypt a directory's
77 # filenames is derived from the master key (the key in the keyring) and a nonce
78 # generated by the kernel.  Hence, the encrypted filenames will be different
79 # every time this test is run, even if we were to put a fixed key into the
80 # keyring instead of a random one.  The same applies to symlink targets.
81 #
82 # TODO: there are some inconsistencies in which error codes are returned on
83 # different kernel versions and filesystems when trying to create a file or
84 # subdirectory without access to the parent directory's encryption key.  It's
85 # planned to consistently use ENOKEY, but for now make this test accept multiple
86 # error codes...
87 #
88
89 filter_create_errors()
90 {
91         sed -e 's/No such file or directory/Required key not available/' \
92             -e 's/Permission denied/Required key not available/' \
93             -e 's/Operation not permitted/Required key not available/'
94 }
95
96 _unlink_encryption_key $keydesc
97 _scratch_cycle_mount
98
99 # Check that unencrypted names aren't there
100 stat $SCRATCH_MNT/edir/empty |& _filter_scratch
101 stat $SCRATCH_MNT/edir/symlink |& _filter_scratch
102
103 # Check that the correct numbers of files and subdirectories are there
104 ls $SCRATCH_MNT/edir | wc -l
105 find $SCRATCH_MNT/edir -mindepth 2 -maxdepth 2 -type d | wc -l
106
107 # Try to read a nondirectory file (should fail with ENOKEY)
108 md5sum $(find $SCRATCH_MNT/edir -maxdepth 1 -type f | head -1) |& \
109                 cut -d ' ' -f3-
110
111 # Try to create new files, directories, and symlinks in the encrypted directory,
112 # both with and without using correctly base-64 encoded filenames.  These should
113 # all fail with ENOKEY.
114 $XFS_IO_PROG -f $SCRATCH_MNT/edir/newfile |& filter_create_errors | _filter_scratch
115 $XFS_IO_PROG -f $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
116 mkdir $SCRATCH_MNT/edir/newdir |& filter_create_errors | _filter_scratch
117 mkdir $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
118 ln -s foo $SCRATCH_MNT/edir/newlink |& filter_create_errors | _filter_scratch
119 ln -s foo $SCRATCH_MNT/edir/0123456789abcdef |& filter_create_errors | _filter_scratch
120
121 # Delete the encrypted directory (should succeed)
122 rm -r $SCRATCH_MNT/edir
123 stat $SCRATCH_MNT/edir |& _filter_scratch
124
125 # success, all done
126 status=0
127 exit