[PATCH] ecryptfs: freeze protection fixes for ecryptfs
From: Drif Abdelmalek Mohamed Said
Date: Fri Nov 27 2026 - 14:55:14 EST
The VFS only takes freeze protection on the eCryptfs superblock.
Operations like mkdir, rename and chmod can still change the lower
filesystem while it is frozen. On ext4 this can trigger a warning in
ext4_journal_check_start().
Add helpers to take and drop write access on the lower mount. Take it
before locking the lower inodes and drop it after unlocking.
Use one helper for the lower notify_change() calls in setattr and
truncate.
Use mnt_want_write_file() when writing the inode size xattr. The lower
file is already open.
Keep the new holds separate from kernel_write(). It takes its own freeze
protection, and taking it twice on the same superblock can deadlock
if a freeze is pending.
Fixes: eb04c28288bb ("fs: Add freezing handling to mnt_want_write() / mnt_drop_write()")
Signed-off-by: Drif Abdelmalek Mohamed Said <drifabdelmalekmohamedsaid@xxxxxxxxx>
---
fs/ecryptfs/crypto.c | 4 ++
fs/ecryptfs/ecryptfs_kernel.h | 15 +++++
fs/ecryptfs/inode.c | 121 +++++++++++++++++++++++++++-------
fs/ecryptfs/mmap.c | 9 ++-
4 files changed, 124 insertions(+), 25 deletions(-)
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
index e67119b6029c..52fa02ed0fba 100644
--- a/fs/ecryptfs/crypto.c
+++ b/fs/ecryptfs/crypto.c
@@ -995,12 +995,16 @@ ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
goto out;
}
+ rc = ecryptfs_want_write(ecryptfs_dentry);
+ if (rc)
+ goto out;
inode_lock(lower_inode);
rc = __vfs_setxattr(&nop_mnt_idmap, lower_dentry, lower_inode,
ECRYPTFS_XATTR_NAME, page_virt, size, 0);
if (!rc && ecryptfs_inode)
fsstack_copy_attr_all(ecryptfs_inode, lower_inode);
inode_unlock(lower_inode);
+ ecryptfs_drop_write(ecryptfs_dentry);
out:
return rc;
}
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index 58165928ed1e..aae51cb465c0 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -22,6 +22,7 @@
#include <linux/fs.h>
#include <linux/fs_stack.h>
#include <linux/hex.h>
+#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/scatterlist.h>
#include <linux/hash.h>
@@ -463,6 +464,20 @@ ecryptfs_superblock_to_private(struct super_block *sb)
return (struct ecryptfs_sb_info *)sb->s_fs_info;
}
+static inline int ecryptfs_want_write(struct dentry *dentry)
+{
+ struct ecryptfs_sb_info *sbi = ecryptfs_superblock_to_private(dentry->d_sb);
+
+ return mnt_want_write(sbi->lower_mnt);
+}
+
+static inline void ecryptfs_drop_write(struct dentry *dentry)
+{
+ struct ecryptfs_sb_info *sbi = ecryptfs_superblock_to_private(dentry->d_sb);
+
+ mnt_drop_write(sbi->lower_mnt);
+}
+
static inline void
ecryptfs_set_superblock_private(struct super_block *sb,
struct ecryptfs_sb_info *sb_info)
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index 48e520960d66..4bd767b4bd95 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -149,9 +149,14 @@ static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
struct inode *lower_dir;
int rc;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
lower_dentry = ecryptfs_start_removing_dentry(dentry);
- if (IS_ERR(lower_dentry))
- return PTR_ERR(lower_dentry);
+ if (IS_ERR(lower_dentry)) {
+ rc = PTR_ERR(lower_dentry);
+ goto out_drop_write;
+ }
lower_dir = lower_dentry->d_parent->d_inode;
rc = vfs_unlink(&nop_mnt_idmap, lower_dir, lower_dentry, NULL);
@@ -166,6 +171,8 @@ static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
end_removing(lower_dentry);
if (!rc)
d_drop(dentry);
+out_drop_write:
+ ecryptfs_drop_write(dentry);
return rc;
}
@@ -273,8 +280,12 @@ ecryptfs_create(const struct mnt_idmap *idmap,
struct inode *ecryptfs_inode;
int rc;
+ rc = ecryptfs_want_write(ecryptfs_dentry);
+ if (rc)
+ return rc;
ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
mode);
+ ecryptfs_drop_write(ecryptfs_dentry);
if (IS_ERR(ecryptfs_inode)) {
ecryptfs_printk(KERN_WARNING, "Failed to create file in"
"lower filesystem\n");
@@ -436,9 +447,14 @@ static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
file_size_save = i_size_read(d_inode(old_dentry));
lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
+ rc = ecryptfs_want_write(new_dentry);
+ if (rc)
+ return rc;
lower_new_dentry = ecryptfs_start_creating_dentry(new_dentry);
- if (IS_ERR(lower_new_dentry))
- return PTR_ERR(lower_new_dentry);
+ if (IS_ERR(lower_new_dentry)) {
+ rc = PTR_ERR(lower_new_dentry);
+ goto out_drop_write;
+ }
lower_dir = lower_new_dentry->d_parent->d_inode;
rc = vfs_link(lower_old_dentry, &nop_mnt_idmap, lower_dir,
lower_new_dentry, NULL);
@@ -454,6 +470,8 @@ static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
i_size_write(d_inode(new_dentry), file_size_save);
out_lock:
end_creating(lower_new_dentry);
+out_drop_write:
+ ecryptfs_drop_write(new_dentry);
return rc;
}
@@ -473,9 +491,14 @@ static int ecryptfs_symlink(const struct mnt_idmap *idmap,
size_t encoded_symlen;
struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
lower_dentry = ecryptfs_start_creating_dentry(dentry);
- if (IS_ERR(lower_dentry))
- return PTR_ERR(lower_dentry);
+ if (IS_ERR(lower_dentry)) {
+ rc = PTR_ERR(lower_dentry);
+ goto out_drop_write;
+ }
lower_dir = lower_dentry->d_parent->d_inode;
mount_crypt_stat = &ecryptfs_superblock_to_private(
@@ -500,6 +523,8 @@ static int ecryptfs_symlink(const struct mnt_idmap *idmap,
end_creating(lower_dentry);
if (d_really_is_negative(dentry))
d_drop(dentry);
+out_drop_write:
+ ecryptfs_drop_write(dentry);
return rc;
}
@@ -511,9 +536,14 @@ static struct dentry *ecryptfs_mkdir(const struct mnt_idmap *idmap, struct inode
struct dentry *lower_dir_dentry;
struct inode *lower_dir;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return ERR_PTR(rc);
lower_dentry = ecryptfs_start_creating_dentry(dentry);
- if (IS_ERR(lower_dentry))
- return lower_dentry;
+ if (IS_ERR(lower_dentry)) {
+ rc = PTR_ERR(lower_dentry);
+ goto out_drop_write;
+ }
lower_dir_dentry = dget(lower_dentry->d_parent);
lower_dir = lower_dir_dentry->d_inode;
lower_dentry = vfs_mkdir(&nop_mnt_idmap, lower_dir,
@@ -535,6 +565,8 @@ static struct dentry *ecryptfs_mkdir(const struct mnt_idmap *idmap, struct inode
end_creating(lower_dentry);
if (d_really_is_negative(dentry))
d_drop(dentry);
+out_drop_write:
+ ecryptfs_drop_write(dentry);
return ERR_PTR(rc);
}
@@ -544,9 +576,14 @@ static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
struct inode *lower_dir;
int rc;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
lower_dentry = ecryptfs_start_removing_dentry(dentry);
- if (IS_ERR(lower_dentry))
- return PTR_ERR(lower_dentry);
+ if (IS_ERR(lower_dentry)) {
+ rc = PTR_ERR(lower_dentry);
+ goto out_drop_write;
+ }
lower_dir = lower_dentry->d_parent->d_inode;
rc = vfs_rmdir(&nop_mnt_idmap, lower_dir, lower_dentry, NULL);
@@ -558,6 +595,8 @@ static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
end_removing(lower_dentry);
if (!rc)
d_drop(dentry);
+out_drop_write:
+ ecryptfs_drop_write(dentry);
return rc;
}
@@ -569,9 +608,14 @@ ecryptfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,
struct dentry *lower_dentry;
struct inode *lower_dir;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
lower_dentry = ecryptfs_start_creating_dentry(dentry);
- if (IS_ERR(lower_dentry))
- return PTR_ERR(lower_dentry);
+ if (IS_ERR(lower_dentry)) {
+ rc = PTR_ERR(lower_dentry);
+ goto out_drop_write;
+ }
lower_dir = lower_dentry->d_parent->d_inode;
rc = vfs_mknod(&nop_mnt_idmap, lower_dir, lower_dentry, mode, dev, NULL);
@@ -586,6 +630,8 @@ ecryptfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,
end_creating(lower_dentry);
if (d_really_is_negative(dentry))
d_drop(dentry);
+out_drop_write:
+ ecryptfs_drop_write(dentry);
return rc;
}
@@ -616,9 +662,12 @@ ecryptfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
rd.mnt_idmap = &nop_mnt_idmap;
rd.old_parent = lower_old_dir_dentry;
rd.new_parent = lower_new_dir_dentry;
- rc = start_renaming_two_dentries(&rd, lower_old_dentry, lower_new_dentry);
+ rc = ecryptfs_want_write(old_dentry);
if (rc)
return rc;
+ rc = start_renaming_two_dentries(&rd, lower_old_dentry, lower_new_dentry);
+ if (rc)
+ goto out_drop_write;
rc = vfs_rename(&rd);
if (rc)
@@ -631,6 +680,8 @@ ecryptfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
out_lock:
end_renaming(&rd);
+out_drop_write:
+ ecryptfs_drop_write(old_dentry);
return rc;
}
@@ -718,6 +769,22 @@ upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
return lower_size;
}
+static int ecryptfs_notify_change(struct dentry *dentry, struct iattr *lower_ia)
+{
+ struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
+ struct inode *lower_inode = d_inode(lower_dentry);
+ int rc;
+
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
+ inode_lock(lower_inode);
+ rc = notify_change(&nop_mnt_idmap, lower_dentry, lower_ia, NULL);
+ inode_unlock(lower_inode);
+ ecryptfs_drop_write(dentry);
+ return rc;
+}
+
/**
* __ecryptfs_truncate
* @dentry: The ecryptfs layer dentry
@@ -732,7 +799,6 @@ upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
*/
static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)
{
- struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
struct inode *inode = d_inode(dentry);
struct ecryptfs_crypt_stat *crypt_stat;
loff_t i_size = i_size_read(inode);
@@ -820,9 +886,7 @@ static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)
lower_ia.ia_size = lower_size_after_truncate;
set_size:
lower_ia.ia_valid |= ATTR_SIZE;
- inode_lock(d_inode(lower_dentry));
- rc = notify_change(&nop_mnt_idmap, lower_dentry, &lower_ia, NULL);
- inode_unlock(d_inode(lower_dentry));
+ rc = ecryptfs_notify_change(dentry, &lower_ia);
out:
ecryptfs_put_lower_file(inode);
return rc;
@@ -873,7 +937,6 @@ static int ecryptfs_setattr(const struct mnt_idmap *idmap,
struct dentry *dentry, struct iattr *ia)
{
struct inode *inode = d_inode(dentry);
- struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
struct ecryptfs_crypt_stat *crypt_stat;
int rc;
@@ -928,11 +991,7 @@ static int ecryptfs_setattr(const struct mnt_idmap *idmap,
struct iattr lower_ia;
ecryptfs_iattr_to_lower(&lower_ia, ia);
-
- inode_lock(d_inode(lower_dentry));
- rc = notify_change(&nop_mnt_idmap, lower_dentry, &lower_ia,
- NULL);
- inode_unlock(d_inode(lower_dentry));
+ rc = ecryptfs_notify_change(dentry, &lower_ia);
}
out:
fsstack_copy_attr_all(inode, lower_inode);
@@ -1000,9 +1059,13 @@ ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
rc = -EOPNOTSUPP;
goto out;
}
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ goto out;
inode_lock(lower_inode);
rc = __vfs_setxattr_locked(&nop_mnt_idmap, lower_dentry, name, value, size, flags, NULL);
inode_unlock(lower_inode);
+ ecryptfs_drop_write(dentry);
if (!rc && inode)
fsstack_copy_attr_all(inode, lower_inode);
out:
@@ -1066,9 +1129,13 @@ static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
rc = -EOPNOTSUPP;
goto out;
}
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ goto out;
inode_lock(lower_inode);
rc = __vfs_removexattr(&nop_mnt_idmap, lower_dentry, name);
inode_unlock(lower_inode);
+ ecryptfs_drop_write(dentry);
out:
return rc;
}
@@ -1084,7 +1151,11 @@ static int ecryptfs_fileattr_set(const struct mnt_idmap *idmap,
struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
int rc;
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
rc = vfs_fileattr_set(&nop_mnt_idmap, lower_dentry, fa);
+ ecryptfs_drop_write(dentry);
fsstack_copy_attr_all(d_inode(dentry), d_inode(lower_dentry));
return rc;
@@ -1105,8 +1176,12 @@ static int ecryptfs_set_acl(const struct mnt_idmap *idmap,
struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
struct inode *lower_inode = d_inode(lower_dentry);
+ rc = ecryptfs_want_write(dentry);
+ if (rc)
+ return rc;
rc = vfs_set_acl(&nop_mnt_idmap, lower_dentry,
posix_acl_xattr_name(type), acl);
+ ecryptfs_drop_write(dentry);
if (!rc)
fsstack_copy_attr_all(d_inode(dentry), lower_inode);
return rc;
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c
index 07a6c8448100..8daba46907fd 100644
--- a/fs/ecryptfs/mmap.c
+++ b/fs/ecryptfs/mmap.c
@@ -375,8 +375,8 @@ static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
{
ssize_t size;
void *xattr_virt;
- struct dentry *lower_dentry =
- ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
+ struct file *lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
+ struct dentry *lower_dentry = lower_file->f_path.dentry;
struct inode *lower_inode = d_inode(lower_dentry);
int rc;
@@ -391,6 +391,9 @@ static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
rc = -ENOMEM;
goto out;
}
+ rc = mnt_want_write_file(lower_file);
+ if (rc)
+ goto out_free;
inode_lock(lower_inode);
size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
xattr_virt, PAGE_SIZE);
@@ -400,9 +403,11 @@ static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
rc = __vfs_setxattr(&nop_mnt_idmap, lower_dentry, lower_inode,
ECRYPTFS_XATTR_NAME, xattr_virt, size, 0);
inode_unlock(lower_inode);
+ mnt_drop_write_file(lower_file);
if (rc)
printk(KERN_ERR "Error whilst attempting to write inode size "
"to lower file xattr; rc = [%d]\n", rc);
+out_free:
kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
out:
return rc;
--
2.47.3