Skip to contentSkip to conversation

Forbid truncate of fsverity files on btrfs

PATCH Applied Eric Biggers 3 participants 5 messages Oct 6 – Oct 7

Subsystems (3)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

cover Forbid truncate of fsverity files on btrfs

Backport the fix to forbid truncate(2) of fsverity files on btrfs (and also the fix for that fix).

Andrey Albershteyn (1):
fs,fsverity: remove check for fsverity being enabled in
setattr_prepare()

Christoph Hellwig (1):
fs,fsverity: reject size changes on fsverity files in setattr_prepare

 fs/attr.c                | 12 +++++++++++-
 fs/ext4/inode.c          |  4 ----
 fs/f2fs/file.c           |  4 ----
 fs/verity/open.c         |  8 --------
 include/linux/fsverity.h | 25 -------------------------
 5 files changed, 11 insertions(+), 42 deletions(-)

base-commit: a91a6aa9253c64936f0bd8273a2a3c11522ec9c3

Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH 1/2 fs,fsverity: reject size changes on fsverity files in setattr_prepare

From: Christoph Hellwig <[email protected]>

commit e9734653c523c744f03333ece6ae7a315187f05c upstream.

Add the check to reject truncates of fsverity files directly to setattr_prepare instead of requiring the file system to handle it. Besides removing boilerplate code, this also fixes the complete lack of such check in btrfs.

---
 fs/attr.c                | 12 +++++++++++-
 fs/ext4/inode.c          |  4 ----
 fs/f2fs/file.c           |  4 ----
 fs/verity/open.c         |  8 --------
 include/linux/fsverity.h | 25 -------------------------
 5 files changed, 11 insertions(+), 42 deletions(-)
fs/attr.c +11 -1
index a8ae5f6d9b16..c2302fc4b333 100644
@@ -173,7 +173,17 @@ int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
 	 * ATTR_FORCE.
 	 */
 	if (ia_valid & ATTR_SIZE) {
-		int error = inode_newsize_ok(inode, attr->ia_size);
+		int error;
+
+		/*
+		 * Verity files are immutable, so deny truncates.  This isn't
+		 * covered by the open-time check because sys_truncate() takes a
+		 * path, not an open file.
+		 */
+		if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
+			return -EPERM;
+
+		error = inode_newsize_ok(inode, attr->ia_size);
 		if (error)
 			return error;
 	}
fs/ext4/inode.c +0 -4
index dea6d27e5028..9ff2958509ed 100644
@@ -5484,10 +5484,6 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	if (error)
 		return error;
 
-	error = fsverity_prepare_setattr(dentry, attr);
-	if (error)
-		return error;
-
 	if (is_quota_modification(idmap, inode, attr)) {
 		error = dquot_initialize(inode);
 		if (error)
fs/f2fs/file.c +0 -4
index 7125117b6322..c1cd6dadabfe 100644
@@ -1065,10 +1065,6 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	if (err)
 		return err;
 
-	err = fsverity_prepare_setattr(dentry, attr);
-	if (err)
-		return err;
-
 	if (unlikely(IS_IMMUTABLE(inode)))
 		return -EPERM;
 
fs/verity/open.c +0 -8
index 6c31a871b84b..5342a8e1f781 100644
@@ -393,14 +393,6 @@ int __fsverity_file_open(struct inode *inode, struct file *filp)
 }
 EXPORT_SYMBOL_GPL(__fsverity_file_open);
 
-int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr)
-{
-	if (attr->ia_valid & ATTR_SIZE)
-		return -EPERM;
-	return 0;
-}
-EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr);
-
 void __fsverity_cleanup_inode(struct inode *inode)
 {
 	fsverity_free_info(inode->i_verity_info);
include/linux/fsverity.h +0 -25
index 1eb7eae580be..139a6f5c6217 100644
@@ -149,7 +149,6 @@ int fsverity_get_digest(struct inode *inode,
 /* open.c */
 
 int __fsverity_file_open(struct inode *inode, struct file *filp);
-int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr);
 void __fsverity_cleanup_inode(struct inode *inode);
 
 /**
@@ -214,12 +213,6 @@ static inline int __fsverity_file_open(struct inode *inode, struct file *filp)
 	return -EOPNOTSUPP;
 }
 
-static inline int __fsverity_prepare_setattr(struct dentry *dentry,
-					     struct iattr *attr)
-{
-	return -EOPNOTSUPP;
-}
-
 static inline void fsverity_cleanup_inode(struct inode *inode)
 {
 }
@@ -301,22 +294,4 @@ static inline int fsverity_file_open(struct inode *inode, struct file *filp)
 	return 0;
 }
 
-/**
- * fsverity_prepare_setattr() - prepare to change a verity inode's attributes
- * @dentry: dentry through which the inode is being changed
- * @attr: attributes to change
- *
- * Verity files are immutable, so deny truncates.  This isn't covered by the
- * open-time check because sys_truncate() takes a path, not a file descriptor.
- *
- * Return: 0 on success, -errno on failure
- */
-static inline int fsverity_prepare_setattr(struct dentry *dentry,
-					   struct iattr *attr)
-{
-	if (IS_VERITY(d_inode(dentry)))
-		return __fsverity_prepare_setattr(dentry, attr);
-	return 0;
-}
-
 #endif	/* _LINUX_FSVERITY_H */
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH 2/2 fs,fsverity: remove check for fsverity being enabled in setattr_prepare()

In reply to Eric Biggers

From: Andrey Albershteyn <[email protected]>

commit d2f96bcb89d36d488a10e3bcf819b98536968286 upstream.

The check that fs-verity is available in the kernel is not necessary here. Filesystems could have fsverity files even without fs-verity enabled. In that case, truncate on fsverity file will succeed, what this check is trying to prevent.

Fixes e9734653c523 ("fs,fsverity: reject size changes on fsverity files in setattr_prepare")
---
 fs/attr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/attr.c +1 -1
index c2302fc4b333..dd3b0b5115af 100644
@@ -180,7 +180,7 @@ int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
 		 * covered by the open-time check because sys_truncate() takes a
 		 * path, not an open file.
 		 */
-		if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
+		if (IS_VERITY(inode))
 			return -EPERM;
 
 		error = inode_newsize_ok(inode, attr->ia_size);
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 6.6 1/2] fs,fsverity: reject size changes on fsverity files in setattr_prepare

In reply to Eric Biggers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] fs: setattr_prepare bypasses fsverity immutability if CONFIG_FS_VERITY=n --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] fs: setattr_prepare bypasses fsverity immutability if CONFIG_FS_VERITY=n --

commit 124beac2c5f385e936a45c9aaf9bc8b5a46a9785
Author: Christoph Hellwig <[email protected]>

fs,fsverity: reject size changes on fsverity files in setattr_prepare

Add the check to reject truncates of fsverity files directly to setattr_prepare instead of requiring the file system to handle it. Besides removing boilerplate code, this also fixes the complete lack of such check in btrfs.

Quoted 8 lines
fs/attr.c +0 -0
index a8ae5f6d9b167..c2302fc4b3330 100644
@@ -173,7 +173,17 @@ int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,
 	 * ATTR_FORCE.
 	 */
 	if (ia_valid & ATTR_SIZE) {

fs/attr.c int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry, · line 184

Quoted 10 lines
fs/attr.c +9 -1
-		int error = inode_newsize_ok(inode, attr->ia_size);
+		int error;
+
+		/*
+		 * Verity files are immutable, so deny truncates.  This isn't
+		 * covered by the open-time check because sys_truncate() takes a
+		 * path, not an open file.
+		 */
+		if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
+			return -EPERM;

[Severity: High]
Is it possible that this IS_ENABLED() guard bypasses the immutability check when verity is disabled in the kernel?

Ext4 and F2FS previously relied on fsverity_prepare_setattr() which correctly returned -EOPNOTSUPP on non-verity kernels. By replacing those checks with this code, if the kernel is compiled without CONFIG_FS_VERITY, this check evaluates to false and the truncation is allowed to proceed.

Can an unprivileged user with write permissions to a verity file use sys_truncate() to destroy its contents and cryptographically sealed metadata on a kernel compiled without CONFIG_FS_VERITY support?

While I noticed this issue is addressed later in the series by commit 0129f7fd3696d1d9b07abe1ecccfcb575dde56d8 ("fs,fsverity: remove check for fsverity being enabled in setattr_prepare()"), shouldn't this patch be self-contained and not introduce new regressions?

Quoted 5 lines
fs/attr.c +2 -0
+
+		error = inode_newsize_ok(inode, attr->ia_size);
 		if (error)
 			return error;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#4 lore raw
In reply to Eric Biggers
Quoted 2 lines

Backport the fix to forbid truncate(2) of fsverity files on btrfs (and also the fix for that fix).

Queued the series for 6.6, thanks.

Signature · 2 lines
Thanks,
Sasha