fs-verity rejects enabling fsverity on append only files. However, there are two cases when FS_IOC_SETFLAGS can turn inode append only. The first one is right before the lock is taken in the enable_verity(), making original check useless. The second one is after lock in enable_verity() is unlocked, therefore, when merkle tree is being build and after fs-verity inode is sealed. This one is not critical, it just sets incompatible flags on inode but append-only inode will not get any writes anyway. Fix it by checking S_APPEND again under the lock, serializing this with vfs_fileattr_set() and reject S_APPEND on fsverity inodes. Fixes: 3fda4c617e84 ("fs-verity: implement FS_IOC_ENABLE_VERITY ioctl") Signed-off-by: Andrey Albershteyn --- fs/file_attr.c | 13 +++++++++++++ fs/verity/enable.c | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/fs/file_attr.c b/fs/file_attr.c index 81bf55f9b255..a5e436711008 100644 --- a/fs/file_attr.c +++ b/fs/file_attr.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -209,6 +210,18 @@ static int fileattr_set_prepare(struct inode *inode, fa->flags &= ~FS_VERITY_FL; fa->fsx_xflags &= ~FS_XFLAG_VERITY; + /* + * FS_APPEND_FL is incompatible with fs-verity. FS_IOC_ENABLE_VERITY + * rejects append-only files, also reject turning a verity file + * append-only. + * + * Reject in both cases, after it has been sealed and while its Merkle + * tree is being built. + */ + if ((fa->flags & FS_APPEND_FL) && + ((old_ma->flags & FS_VERITY_FL) || fsverity_is_building(inode))) + return -EPERM; + /* * Project Quota ID state is only allowed to change from within the init * namespace. Enforce that restriction only if we are trying to change diff --git a/fs/verity/enable.c b/fs/verity/enable.c index d820c8d348ca..8a469ddf76ba 100644 --- a/fs/verity/enable.c +++ b/fs/verity/enable.c @@ -238,6 +238,14 @@ static int enable_verity(struct file *filp, err = -EEXIST; } else if (fsverity_is_building(inode)) { err = -EBUSY; + } else if (IS_APPEND(inode)) { + /* + * The check in fsverity_ioctl_enable() is done without the + * inode lock, so a concurrent FS_IOC_SETFLAGS could have set + * S_APPEND in the meantime. Recheck it here, serialized with + * ->fileattr_set(). + */ + err = -EPERM; } else { err = vops->begin_enable_verity(filp); if (!err) { -- 2.54.0