fscrypt: Don't set a policy on immutable or append-only directories
Subsystems (2)
PATCH 1/1 fscrypt: Don't set a policy on immutable or append-only directories
From: Suyuan Wang <[email protected]>
FS_IOC_SET_ENCRYPTION_POLICY stores an encryption context in the directory (an xattr on ext4 and f2fs) and marks the inode as encrypted. fscrypt_ioctl_set_policy() checks inode_owner_or_capable(), but not the immutable or append-only inode flags, so the ioctl also succeeds on a directory with one of those flags set. Setting any other xattr on the same directory fails with -EPERM in may_write_xattr(), and FS_IOC_ENABLE_VERITY refuses such files as well.
For example, on ext4 with the encrypt feature:
mkdir d && chattr +i d setfattr -n user.x -v 1 d -> Operation not permitted FS_IOC_SET_ENCRYPTION_POLICY on d -> succeeds, d now has a policy
Return -EPERM for an immutable or append-only directory before setting the policy, as may_write_xattr() does. Calls that find the same policy already set still return 0, since they don't change the inode.
Found with a static analysis tool and checked by hand.
---
Tested in QEMU (x86_64, ext4 with -O encrypt, v1 policy). Before the patch the ioctl succeeds on both immutable and append-only empty directories; with it both get -EPERM, and an ordinary empty directory still gets its policy.
The Fixes: tag points to the commit that created fs/crypto/policy.c; the ext4 and f2fs encryption policy code before it lacked these checks too. I left out Cc: stable, as I'm not sure you'd want this backported.
--- fs/crypto/policy.c | 2 ++ 1 file changed, 2 insertions(+)
fs/crypto/policy.c +2 -0
@@ -553,6 +553,8 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) ret = -ENOTDIR ; else if (IS_DEADDIR(inode)) ret = -ENOENT ; + else if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) + ret = -EPERM ; else if (!inode->i_sb->s_cop->empty_dir(inode)) ret = -ENOTEMPTY ; else
Eric Biggers
maintainerQuoted 20 lines · On Thu, Nov 19, 2026 at 12:57:20PM +0000, Suyuan Wang via B4 Relay wrote:
From: Suyuan Wang <[email protected]>
FS_IOC_SET_ENCRYPTION_POLICYstores an encryption context in the directory (an xattr on ext4 and f2fs) and marks the inode as encrypted.fscrypt_ioctl_set_policy()checksinode_owner_or_capable(), but not the immutable or append-only inode flags, so the ioctl also succeeds on a directory with one of those flags set. Setting any other xattr on the same directory fails with-EPERMinmay_write_xattr(), andFS_IOC_ENABLE_VERITYrefuses such files as well.For example, on ext4 with the encrypt feature:
mkdir d && chattr +i d setfattr -n user.x -v 1 d -> Operation not permitted FS_IOC_SET_ENCRYPTION_POLICY on d -> succeeds, d now has a policyReturn
-EPERMfor an immutable or append-only directory before setting the policy, asmay_write_xattr()does. Calls that find the same policy already set still return 0, since they don't change the inode.
Quoted 6 lines
Found with a static analysis tool and checked by hand.
Fixes 0b81d0779072 ("fs crypto: move per-file encryption from f2fs tree to fs/crypto")Co-developed-by Kaixuan Li <[email protected]>Signed-off-by Kaixuan Li <[email protected]>Signed-off-by Suyuan Wang <[email protected]>
I guess so. Could you correct the Fixes tag and add 'Cc stable'? It should be:
Also, the documentation should be updated too, e.g.:
Documentation/filesystems/fscrypt.rst +1 -1
@@ -736,7 +736,7 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors: feature flag enabled using ``tune2fs -O encrypt`` or ``mkfs.ext4 -O encrypt``.) - ``EPERM``: this directory may not be encrypted, e.g. because it is - the root directory of an ext4 filesystem + the root directory of an ext4 filesystem or is immutable or append-only - ``EROFS``: the filesystem is readonly Getting an encryption policy