Overlayfs casefold fixes
Subsystems (3)
Amir Goldstein
maintainercover Overlayfs casefold fixes
Hungyu,
You indicated that you are willing to review and test overlayfs casefold fixes.
Here are a bunch of fixes that I recived from Chris and added my edits.
They pass the basic sanity tests, but I fear that given the nature of those reports, the test coverage for casefold overlayfs is not so good and possibly there are no many users, so I am reluctant to merged the changes without a champion to verify them.
I am not really interested in adding test coverage for underlying change races, but the functional bugs that are not related to delicate timing deserve test coverage.
Please see fstest generic/783 and see if a similar test could be added to add test coverage.
Note that it is intentionally not an overlay/ fstest, but a generic test which only runs on filesystems that support casefold (e.g. ext4) and the test itself takes care of mounting overlayfs.
Thanks,
Amir.
Chris Mason (3):
ovl: fail the mount if the casefold flag has changed
ovl: do not let chattr change the casefold flag
ovl: fix whiteout creation in casefolded directories
fs/overlayfs/dir.c | 46 ++++++++++++++++++++++------------------ fs/overlayfs/inode.c | 8 +++++++ fs/overlayfs/overlayfs.h | 10 +-------- fs/overlayfs/readdir.c | 3 ++- fs/overlayfs/super.c | 12 ++++++++++- fs/overlayfs/util.c | 2 +- 6 files changed, 48 insertions(+), 33 deletions(-)
Signature · 1 line
2.55.0
Amir Goldstein
maintainerPATCH 1/3 ovl: fail the mount if the casefold flag has changed
From: Chris Mason <[email protected]>
An unprivileged user in a user namespace can repeatedly trigger a kernel warning with their own overlay mounts. They do so by changing the casefold flag of the topmost layer's empty root, the top directory of that layer, after the layers are set and before the mount is created.
The casefold flag of a directory makes name lookups in that directory case-insensitive. All layers of an overlay must agree on casefolding.
ovl_mount_dir_check() reads the casefold flag of a layer root when fsconfig() sets that layer, and saves the result in ofs->casefold. The mount is created later, by another fsconfig() call with FSCONFIG_CMD_CREATE.
The casefold flag of an empty directory can be changed.
The casefold flag of the overlay root must match ofs->casefold. ovl_get_root() copies the flag from the topmost layer root, as it is when the mount is created. On a mismatch, ovl_get_root() only does a WARN_ON(), and the mount continues. The check is not a WARN_ON_ONCE(), so each such mount warns again.
An unprivileged user needs CONFIG_UNICODE, and a tmpfs mounted with the casefold option in their namespace.
Reproduced by changing the casefold flag of the empty upper directory after the layers were set and before FSCONFIG_CMD_CREATE, with tmpfs and ext4 layers, and as uid 1000 in a user namespace.
Fix this by replacing the WARN_ON() with a check that fails the mount with -EINVAL.
--- fs/overlayfs/super.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
fs/overlayfs/super.c +11 -1
@@ -1372,7 +1372,6 @@ static struct dentry *ovl_get_root(struct super_block *sb,ovl_dentry_set_flag (OVL_E_CONNECTED , root);ovl_set_upperdata (d_inode (root));ovl_inode_init (d_inode (root), &oip, ino, fsid); -WARN_ON (!!IS_CASEFOLDED (d_inode (root)) != ofs->casefold);ovl_dentry_init_flags (root, upperdentry, oe,DCACHE_OP_WEAK_REVALIDATE );/* root keeps a reference of upperdentry */ dget (upperdentry);
@@ -1556,6 +1555,17 @@ static int ovl_fill_super_creds(struct fs_context *fc, struct super_block *sb)if (!sb->s_root) goto out_free_oe; +/* +* The casefold flag of an empty layer root may have changed since the +* mount options were parsed. The root inode owns oe by now. +*/ +if (!!IS_CASEFOLDED (d_inode (sb->s_root)) != ofs->casefold) { +pr_err ("casefold flag of the root directory has changed\n" ); +dput (sb->s_root); + sb->s_root =NULL ; +return -EINVAL ; + } +return 0 ; out_free_oe:
Signature · 1 line
2.55.0
Amir Goldstein
maintainerPATCH 2/3 ovl: do not let chattr change the casefold flag
In reply to Amir GoldsteinFrom: Chris Mason <[email protected]>
chattr +F or -F on an overlay directory can make the directory, or the whole mount, unusable. Lookups of the directory, and lookups in it, then fail. From code inspection, the caller only needs to own the directory. The upper filesystem must support casefolding: ext4 or f2fs with the casefold feature, or tmpfs with the casefold option.
chattr +F and -F set and clear FS_CASEFOLD_FL, the casefold flag of a directory. The flag enables casefolding (case-insensitive name lookup) for the directory. All directories of an overlay must match the casefold state that was recorded at mount time, and ovl_lookup_single() enforces this:
- A lookup in a directory that does not match fails with -ESTALE.
- A lookup of the directory itself fails with -EREMOTE.
ovl_fileattr_set() copies the directory up, which creates it in the upper layer if it only existed in a lower layer. It then passes FS_CASEFOLD_FL on to the upper filesystem. The upper filesystem accepts the change if the upper directory is empty, regardless of what the merged directory contains. The merged directory is the one that the user sees, with the names from the upper and the lower directories.
After the change the upper directory no longer matches the mount. If the directory is the root of the overlay, the whole mount is affected.
The flag can also change without chattr +F or -F. chattr reads all flags of a directory, changes one and writes all of them back. For a directory that only exists in a lower layer, overlayfs reads the flags from the lower filesystem. xfs with the ascii-ci feature reports FS_CASEFOLD_FL on every inode, but never sets S_CASEFOLD, the inode flag that overlayfs compares with the mount. So its directories count as not casefolded. With a lower layer on such an xfs and an upper layer on ext4 with the casefold feature, chattr +A on a lower-only directory therefore sets FS_CASEFOLD_FL on the new, empty upper directory.
Reproduced as root on tmpfs and on ext4 with chattr +F or -F on an overlay directory whose upper directory was empty. Afterwards lookups below it failed with ESTALE, and lookups of the directory itself failed with EREMOTE. The chattr +A case was seen once, on a kernel with an earlier form of this patch that still passed the caller's flag on:
lookups of the directory failed with EREMOTE.
Fix this by returning -EOPNOTSUPP if FS_IOC_SETFLAGS, the only call that can change the flag, asks for a casefold flag on a directory that does not agree with the casefold state of the mount. This also refuses the chattr +A case above.
[amir: check the flag against the casefold state of the mount instead of
reading back the flags]
--- fs/overlayfs/inode.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/overlayfs/inode.c +8 -0
@@ -658,6 +658,14 @@ int ovl_fileattr_set(struct mnt_idmap *idmap,unsigned int flags;int err; +/* +* Lookup refuses a directory that does not agree with the casefold +* state of the mount. Only FS_IOC_SETFLAGS can ask for a change. +*/ +if (S_ISDIR (inode->i_mode) && fa->flags_valid && + !!(fa->flags &FS_CASEFOLD_FL ) !=OVL_FS (inode->i_sb)->casefold) +return -EOPNOTSUPP ; + err =ovl_copy_up (dentry);if (!err) {ovl_path_real (dentry, &upperpath);
Signature · 1 line
2.55.0
Amir Goldstein
maintainerPATCH 3/3 ovl: fix whiteout creation in casefolded directories
In reply to Amir GoldsteinFrom: Chris Mason <[email protected]>
unlink and rmdir fail with EINVAL on an overlay with casefolded (case-insensitive) layers, if the name exists in a lower layer. Casefolded layers need CONFIG_UNICODE. Root can mount such an overlay, and so can an unprivileged user in a user namespace, with tmpfs layers.
Overlayfs does not change a lower layer. To remove a name that exists there, it hides the name with a marker file, a whiteout. It creates the whiteout in the work directory, a scratch directory on the upper filesystem, and renames it over the name in the upper directory:
- ovl_whiteout() creates the whiteout with mknod, or as a hard link to an earlier whiteout.
- ovl_cleanup_and_whiteout() then prepares the rename with start_renaming_two_dentries(), which returns -EINVAL if the source or the target dentry is unhashed, that is, not in the dentry hash table.
With casefolded directories, both dentries can be unhashed:
- The target. Lookup in a casefolded directory leaves a negative dentry unhashed, so the rename target is unhashed if the name does not exist in the upper directory.
- The source, which is the new whiteout. tmpfs hashes a dentry when mknod or link makes it positive, but ext4 does not, so on ext4 the source is unhashed as well.
The cleanup after the failure also refuses an unhashed dentry, so on ext4 the whiteout is left behind in the work directory.
Reproduced with rm and rmdir on casefolded tmpfs and ext4 layers, also as uid 1000 in a user namespace.
Fix this by creating the whiteout with ovl_create_temp(), which repeats the lookup if the new dentry is unhashed. Prepare the rename with start_renaming_dentry(), which looks up the target name with the directories locked. The caller looked the name up without a lock, so return -ESTALE if the name now refers to a different inode.
--- fs/overlayfs/dir.c | 46 ++++++++++++++++++++++------------------ fs/overlayfs/overlayfs.h | 10 +-------- fs/overlayfs/readdir.c | 3 ++- fs/overlayfs/util.c | 2 +- 4 files changed, 29 insertions(+), 32 deletions(-)
fs/overlayfs/dir.c +25 -21
@@ -74,39 +74,34 @@ static struct dentry *ovl_start_creating_temp(struct ovl_fs *ofs, &QSTR(name)); } +/* +* The whiteout is the source of a rename, so it has to be hashed. +* ovl_create_temp() takes care of that for a casefolded workdir. +*/ static struct dentry *ovl_whiteout (struct ovl_fs *ofs) {int err;struct dentry *whiteout, *link;struct dentry *workdir = ofs->workdir; -struct inode *wdir = workdir->d_inode; -char name[OVL_TEMPNAME_SIZE ];guard (mutex)(&ofs->whiteout_lock);if (!ofs->whiteout) { - whiteout =ovl_start_creating_temp (ofs, workdir, name); + whiteout =ovl_create_temp (ofs, workdir, &(struct ovl_cattr ) { + .mode =S_IFCHR |WHITEOUT_MODE , + .rdev =WHITEOUT_DEV });if (IS_ERR (whiteout))return whiteout; - err =ovl_do_whiteout (ofs, wdir, whiteout); -if (!err) - ofs->whiteout =dget (whiteout); -end_creating (whiteout); -if (err) -return ERR_PTR (err); + ofs->whiteout = whiteout; }if (!ofs->no_shared_whiteout) { - link =ovl_start_creating_temp (ofs, workdir, name); -if (IS_ERR (link)) + link =ovl_create_temp (ofs, workdir, &(struct ovl_cattr ) { + .hardlink = ofs->whiteout }); +if (!IS_ERR (link))return link; - err =ovl_do_link (ofs, ofs->whiteout, wdir, link); -if (!err) - whiteout =dget (link); -end_creating (link); -if (!err) -return whiteout; + err =PTR_ERR (link);if (err != -EMLINK ) { pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%u)\n", ofs->whiteout->d_inode->i_nlink,
@@ -120,9 +115,10 @@ static struct dentry *ovl_whiteout(struct ovl_fs *ofs) }int ovl_cleanup_and_whiteout (struct ovl_fs *ofs,struct dentry *dir, -struct dentry *dentry) +struct dentry *dentry,const struct qstr *name) {struct dentry *whiteout; +struct qstr last =QSTR_LEN (name->name, name->len);struct renamedata rd = {};int err;int flags =0 ;
@@ -139,9 +135,17 @@ int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct dentry *dir, rd.old_parent = ofs->workdir; rd.new_parent = dir; rd.flags = flags; - err =start_renaming_two_dentries (&rd, whiteout, dentry); +/* +* The caller looked up @dentry by @name without a lock on @dir, and a +* casefolded directory leaves a negative dentry unhashed. Nothing tells +* if such a dentry is still valid, so look up @name again with @dir locked. +*/ + err =start_renaming_dentry (&rd,0 , whiteout, &last);if (!err) { - err =ovl_do_rename_rd (&rd); +if (d_inode (rd.new_dentry) !=d_inode (dentry)) + err = -ESTALE ; +else + err =ovl_do_rename_rd (&rd);end_renaming (&rd); }if (err)
@@ -847,7 +851,7 @@ static int ovl_remove_and_whiteout(struct dentry *dentry, goto out_dput_upper; } - err =ovl_cleanup_and_whiteout (ofs, upperdir, upper); + err =ovl_cleanup_and_whiteout (ofs, upperdir, upper, &dentry->d_name);if (!err)ovl_dir_modified (dentry->d_parent, true);
fs/overlayfs/overlayfs.h +1 -9
@@ -394,14 +394,6 @@ static inline int ovl_do_rename(struct ovl_fs *ofs, struct dentry *olddir,return ovl_do_rename_rd (&rd); } -static inline int ovl_do_whiteout (struct ovl_fs *ofs, -struct inode *dir,struct dentry *dentry) -{ -int err =vfs_whiteout (ovl_upper_mnt_idmap (ofs), dir, dentry); -pr_debug ("whiteout(%pd2) = %i\n" , dentry, err); -return err; -} -static inline struct file *ovl_do_tmpfile (struct ovl_fs *ofs,struct dentry *dentry,umode_t mode) {
@@ -897,7 +889,7 @@ static inline void ovl_copyflags(struct inode *from, struct inode *to)/* dir.c */ extern const struct inode_operations ovl_dir_inode_operations;int ovl_cleanup_and_whiteout (struct ovl_fs *ofs,struct dentry *dir, -struct dentry *dentry); +struct dentry *dentry,const struct qstr *name);struct ovl_cattr {dev_t rdev;umode_t mode;
fs/overlayfs/readdir.c +2 -1
@@ -1343,7 +1343,8 @@ int ovl_indexdir_cleanup(struct ovl_fs *ofs)* Whiteout orphan index to block future open by * handle after overlay nlink dropped to zero. */ - err =ovl_cleanup_and_whiteout (ofs, indexdir, index); + err =ovl_cleanup_and_whiteout (ofs, indexdir, index, + &QSTR_LEN (p->name, p->len)); }else {/* Cleanup orphan index entries */ err =ovl_cleanup (ofs, indexdir, index);
fs/overlayfs/util.c +1 -1
@@ -1118,7 +1118,7 @@ static void ovl_cleanup_index(struct dentry *dentry) }else if (ovl_index_all (dentry->d_sb)) {/* Whiteout orphan index to block future open by handle */ err =ovl_cleanup_and_whiteout (OVL_FS (dentry->d_sb), - indexdir, index); + indexdir, index, &name); }else {/* Cleanup orphan index entries */ err =ovl_cleanup (ofs, indexdir, index);
Signature · 1 line
2.55.0
Quoted 2 lines
Please see fstest generic/783 and see if a similar test could be added to add test coverage.
I'll review and test the series, and work on functional fstests coverage following generic/783.
Thanks,
Hungyu
Hi Amir,
I've posted two tests to fstests, with you Cc'd:
https://lore.kernel.org/r/[email protected]
On arm64/QEMU with ext4 and tmpfs, both tests fail on ovl-fixes (1d27da7de3f7) and pass with the series applied.
For patch 1, changing the empty upper root's casefold flag between layer setup and FSCONFIG_CMD_CREATE triggers the WARN on the baseline. With the series, CREATE fails with EINVAL without the WARN. I tested both directions on ext4 and tmpfs.
I haven't run the full quick group on overlayfs. Feedback on the test coverage and structure would be appreciated.
Thanks,
Hungyu
Hungyu Lin
Tested-byQuoted 2 lines
On arm64/QEMU with ext4 and tmpfs, both tests fail on ovl-fixes (
1d27da7de3f7) and pass with the series applied.
Based on the two fstests and the fsconfig reproducer reported above:
Thanks,
Hungyu
Amir Goldstein
maintainerQuoted 8 lines · Hungyu Lin
Quoted 3 lines
On arm64/QEMU with ext4 and tmpfs, both tests fail on ovl-fixes (
1d27da7de3f7) and pass with the series applied.Based on the two fstests and the fsconfig reproducer reported above:
Tested-by Hungyu Lin <[email protected]>
Thanks!
I will send them with the next PR.
Amir.