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
35 _require_scratch_encryption
36 _require_xfs_io_command "get_encpolicy"
39 _scratch_mkfs_encrypted &>> $seqres.full
44 # When a file is unencrypted, FS_IOC_GET_ENCRYPTION_POLICY currently
45 # fails with ENOENT on ext4 but with ENODATA on f2fs. TODO: it's
46 # planned to consistently use ENODATA. For now this test accepts both.
48 sed -e 's/No such file or directory/No data available/'
51 # Should be able to set an encryption policy on an empty directory
52 empty_dir=$SCRATCH_MNT/empty_dir
53 echo -e "\n*** Setting encryption policy on empty directory ***"
55 check_no_policy $empty_dir |& _filter_scratch
56 _set_encpolicy $empty_dir 0000111122223333
57 _get_encpolicy $empty_dir | _filter_scratch
59 # Should be able to set the same policy again, but not a different one.
60 # TODO: the error code for "already has a different policy" is planned to switch
61 # from EINVAL to EEXIST. For now this test accepts both.
62 echo -e "\n*** Setting encryption policy again ***"
63 _set_encpolicy $empty_dir 0000111122223333
64 _get_encpolicy $empty_dir | _filter_scratch
65 _set_encpolicy $empty_dir 4444555566667777 |& \
66 _filter_scratch | sed -e 's/Invalid argument/File exists/'
67 _get_encpolicy $empty_dir | _filter_scratch
69 # Should *not* be able to set an encryption policy on a nonempty directory
70 nonempty_dir=$SCRATCH_MNT/nonempty_dir
71 echo -e "\n*** Setting encryption policy on nonempty directory ***"
73 touch $nonempty_dir/file
74 _set_encpolicy $nonempty_dir |& _filter_scratch
75 check_no_policy $nonempty_dir |& _filter_scratch
77 # Should *not* be able to set an encryption policy on a nondirectory file, even
78 # an empty one. Regression test for 002ced4be642: "fscrypto: only allow setting
79 # encryption policy on directories".
80 # TODO: the error code for "not a directory" is planned to switch from EINVAL to
81 # ENOTDIR. For now this test accepts both.
82 nondirectory=$SCRATCH_MNT/nondirectory
83 echo -e "\n*** Setting encryption policy on nondirectory ***"
85 _set_encpolicy $nondirectory |& \
86 _filter_scratch | sed -e 's/Invalid argument/Not a directory/'
87 check_no_policy $nondirectory |& _filter_scratch
89 # Should *not* be able to set an encryption policy on another user's directory.
90 # Regression test for 163ae1c6ad62: "fscrypto: add authorization check for
91 # setting encryption policy".
92 unauthorized_dir=$SCRATCH_MNT/unauthorized_dir
93 echo -e "\n*** Setting encryption policy on another user's directory ***"
94 mkdir $unauthorized_dir
95 _user_do_set_encpolicy $unauthorized_dir |& _filter_scratch
96 check_no_policy $unauthorized_dir |& _filter_scratch
98 # Should *not* be able to set an encryption policy on a directory on a
99 # filesystem mounted readonly. Regression test for ba63f23d69a3: "fscrypto:
100 # require write access to mount to set encryption policy". Test both a regular
101 # readonly filesystem and a readonly bind mount of a read-write filesystem.
102 echo -e "\n*** Setting encryption policy on readonly filesystem ***"
103 mkdir $SCRATCH_MNT/ro_dir $SCRATCH_MNT/ro_bind_mnt
105 _set_encpolicy $SCRATCH_MNT/ro_dir |& _filter_scratch
106 check_no_policy $SCRATCH_MNT/ro_dir |& _filter_scratch
108 mount --bind $SCRATCH_MNT $SCRATCH_MNT/ro_bind_mnt
109 mount -o remount,ro,bind $SCRATCH_MNT/ro_bind_mnt
110 _set_encpolicy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
111 check_no_policy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch
112 umount $SCRATCH_MNT/ro_bind_mnt