2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc. All Rights Reserved.
5 # FS QA Test generic/395
7 # Test setting and getting encryption policies.
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
15 status=1 # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
24 # get standard environment, filters and checks
29 # remove previous $seqres.full before test
32 # real QA test starts here
34 _require_scratch_encryption
35 _require_xfs_io_command "get_encpolicy"
38 _scratch_mkfs_encrypted &>> $seqres.full
43 # When a file is unencrypted, FS_IOC_GET_ENCRYPTION_POLICY currently
44 # fails with ENOENT on ext4 but with ENODATA on f2fs. TODO: it's
45 # planned to consistently use ENODATA. For now this test accepts both.
47 sed -e 's/No such file or directory/No data available/'
50 # Should be able to set an encryption policy on an empty directory
51 empty_dir=$SCRATCH_MNT/empty_dir
52 echo -e "\n*** Setting encryption policy on empty directory ***"
54 check_no_policy $empty_dir |& _filter_scratch
55 _set_encpolicy $empty_dir 0000111122223333
56 _get_encpolicy $empty_dir | _filter_scratch
58 # Should be able to set the same policy again, but not a different one.
59 # TODO: the error code for "already has a different policy" is planned to switch
60 # from EINVAL to EEXIST. For now this test accepts both.
61 echo -e "\n*** Setting encryption policy again ***"
62 _set_encpolicy $empty_dir 0000111122223333
63 _get_encpolicy $empty_dir | _filter_scratch
64 _set_encpolicy $empty_dir 4444555566667777 |& \
65 _filter_scratch | sed -e 's/Invalid argument/File exists/'
66 _get_encpolicy $empty_dir | _filter_scratch
68 # Should *not* be able to set an encryption policy on a nonempty directory
69 nonempty_dir=$SCRATCH_MNT/nonempty_dir
70 echo -e "\n*** Setting encryption policy on nonempty directory ***"
72 touch $nonempty_dir/file
73 _set_encpolicy $nonempty_dir |& _filter_scratch
74 check_no_policy $nonempty_dir |& _filter_scratch
76 # Should *not* be able to set an encryption policy on a nondirectory file, even
77 # an empty one. Regression test for 002ced4be642: "fscrypto: only allow setting
78 # encryption policy on directories".
79 # TODO: the error code for "not a directory" is planned to switch from EINVAL to
80 # ENOTDIR. For now this test accepts both.
81 nondirectory=$SCRATCH_MNT/nondirectory
82 echo -e "\n*** Setting encryption policy on nondirectory ***"
84 _set_encpolicy $nondirectory |& \
85 _filter_scratch | sed -e 's/Invalid argument/Not a directory/'
86 check_no_policy $nondirectory |& _filter_scratch
88 # Should *not* be able to set an encryption policy on another user's directory.
89 # Regression test for 163ae1c6ad62: "fscrypto: add authorization check for
90 # setting encryption policy".
91 unauthorized_dir=$SCRATCH_MNT/unauthorized_dir
92 echo -e "\n*** Setting encryption policy on another user's directory ***"
93 mkdir $unauthorized_dir
94 _user_do_set_encpolicy $unauthorized_dir |& _filter_scratch
95 check_no_policy $unauthorized_dir |& _filter_scratch
97 # Should *not* be able to set an encryption policy on a directory on a
98 # filesystem mounted readonly. Regression test for ba63f23d69a3: "fscrypto:
99 # require write access to mount to set encryption policy". Test both a regular
100 # readonly filesystem and a readonly bind mount of a read-write filesystem.
101 echo -e "\n*** Setting encryption policy on readonly filesystem ***"
102 mkdir $SCRATCH_MNT/ro_dir $SCRATCH_MNT/ro_bind_mnt
104 _set_encpolicy $SCRATCH_MNT/ro_dir |& _filter_scratch
105 check_no_policy $SCRATCH_MNT/ro_dir |& _filter_scratch
107 mount --bind $SCRATCH_MNT $SCRATCH_MNT/ro_bind_mnt
108 mount -o remount,ro,bind $SCRATCH_MNT/ro_bind_mnt
109 _set_encpolicy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
110 check_no_policy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
111 umount $SCRATCH_MNT/ro_bind_mnt