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