summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2017-10-09 12:15:44 -0700
committerJaegeuk Kim <jaegeuk@kernel.org>2018-01-04 18:20:26 -0800
commit2286508d17c258719b7f1e37f8000cb4faebf51b (patch)
treeb004b850dc01fa6e2149f20aab58040822e65fb5
parent5cbdd42ad248df655c3a07c0be92078e6e4ebe84 (diff)
fscrypt: new helper function - fscrypt_prepare_setattr()
Introduce a helper function for filesystems to call when processing ->setattr() on a possibly-encrypted inode. It handles enforcing that an encrypted file can only be truncated if its encryption key is available. Acked-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r--include/linux/fscrypt.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 9f1050721ab1..8641e56b8f8a 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -262,4 +262,29 @@ static inline int fscrypt_prepare_lookup(struct inode *dir,
return 0;
}
+/**
+ * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
+ * @dentry: dentry through which the inode is being changed
+ * @attr: attributes to change
+ *
+ * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
+ * most attribute changes are allowed even without the encryption key. However,
+ * without the encryption key we do have to forbid truncates. This is needed
+ * because the size being truncated to may not be a multiple of the filesystem
+ * block size, and in that case we'd have to decrypt the final block, zero the
+ * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
+ * filesystem block boundary, but it's simpler to just forbid all truncates ---
+ * and we already forbid all other contents modifications without the key.)
+ *
+ * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
+ * if a problem occurred while setting up the encryption key.
+ */
+static inline int fscrypt_prepare_setattr(struct dentry *dentry,
+ struct iattr *attr)
+{
+ if (attr->ia_valid & ATTR_SIZE)
+ return fscrypt_require_key(d_inode(dentry));
+ return 0;
+}
+
#endif /* _LINUX_FSCRYPT_H */