From: Eric Biggers <ebigg...@google.com>

[ Upstream commit 63136858aecbe86dbd3c3289a3f46ba1b5f92239 ]

Currently, filesystems allow truncate(2) on an encrypted file without
the encryption key.  However, it's impossible to correctly handle the
case where the size being truncated to is not a multiple of the
filesystem block size, because that would require decrypting the final
block, zeroing the part beyond i_size, then encrypting the block.

As other modifications to encrypted file contents are prohibited without
the key, just prohibit truncate(2) as well, making it fail with ENOKEY.

Signed-off-by: Eric Biggers <ebigg...@google.com>
Signed-off-by: Theodore Ts'o <ty...@mit.edu>
Signed-off-by: Sasha Levin <alexander.le...@microsoft.com>
---
 fs/ext4/inode.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 5cccec68a0a5..58d57c56ec62 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5166,6 +5166,14 @@ int ext4_setattr(struct dentry *dentry, struct iattr 
*attr)
                loff_t oldsize = inode->i_size;
                int shrink = (attr->ia_size <= inode->i_size);
 
+               if (ext4_encrypted_inode(inode)) {
+                       error = fscrypt_get_encryption_info(inode);
+                       if (error)
+                               return error;
+                       if (!fscrypt_has_encryption_key(inode))
+                               return -ENOKEY;
+               }
+
                if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
                        struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
 
-- 
2.15.1

Reply via email to