ovl: Add check for missing lookup operation on inode
PATCH 1/1 ovl: Add check for missing lookup operation on inode
Ensure that the lookup operation is present for the inode in the overlay filesystem. If the operation is missing, log a warning and return an EIO error to prevent further issues in the lookup process.
--- fs/overlayfs/namei.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/overlayfs/namei.c +7 -0
@@ -1115,6 +1115,13 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,for (i =0 ; !d.stop && i <ovl_numlower (poe); i++) {struct ovl_path lower =ovl_lowerstack (poe)[i]; +if (!lower.dentry->d_inode->i_op->lookup) { + err = -EIO ; +pr_warn_ratelimited ("missing lookup operation for inode %p\n" , + lower.dentry->d_inode); + goto out_put; + } +if (!ovl_redirect_follow (ofs)) d.last = i ==ovl_numlower (poe) -1 ;else if (d.is_dir || !ofs->numdatalayer)
Signature · 1 line
2.33.8
Amir Goldstein
maintainerQuoted 17 lines · Vasiliy Kovalev
Ensure that the lookup operation is present for the inode in the overlay filesystem. If the operation is missing, log a warning and return an EIO error to prevent further issues in the lookup process.
Reported-by [email protected]Signed-off-by Vasiliy Kovalev <[email protected]>--- fs/overlayfs/namei.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/overlayfs/namei.c +0
-0
fs/overlayfs/namei.c struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, · line 1124
Quoted 10 lines
fs/overlayfs/namei.c +7
-0@@ -1115,6 +1115,13 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,for (i =0 ; !d.stop && i <ovl_numlower (poe); i++) {struct ovl_path lower =ovl_lowerstack (poe)[i]; +if (!lower.dentry->d_inode->i_op->lookup) { + err = -EIO ; +pr_warn_ratelimited ("missing lookup operation for inode %p\n" , + lower.dentry->d_inode); + goto out_put; + } +
This looks like it is papering over a bug.
The dentries in the poe lower stack are supposed to be d_can_lookup(), which means that they should have a ->lookup op.
See in ovl_lookup_single():
if (!d_can_lookup(this)) {
if (d->is_dir || !last_element) {
d->stop = true;
goto put_and_out;
}Can you analyse what went wrong with the reproducer? How did we get to a state where lowerstack of parent has a dentry which is !d_can_lookup?
Thanks,
Amir.
Miklos Szeredi
maintainerQuoted 3 lines · Amir Goldstein
Can you analyse what went wrong with the reproducer? How did we get to a state where lowerstack of parent has a dentry which is !d_can_lookup?
Theoretically we could still get a an S_ISDIR inode, because ovl_get_inode() doesn't look at the is_dir value that lookup found. I.e. lookup thinks it found a non-dir, but iget will create a dir because of the backing inode's type.
AFAICS this can only happen if i_op->lookup is not set on S_ISDIR for the backing inode, which shouldn't happen on normal filesystems. Reproducer seems to use bfs, which *should* be normal, and bfs_iget certainly doesn't do anything weird in that case, so I still don't understand what is happening.
In any case something like the following should filter out such weirdness:
bool ovl_dentry_weird(struct dentry *dentry)
{
+ if (!d_can_lookup(dentry) && !d_is_file(dentry) &&
!d_is_symlink(dentry))
+ return true;
+
return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
Thanks,
Miklos
Hi,
Quoted 11 lines · Miklos Szeredi
Quoted 3 lines · Amir Goldstein
Can you analyse what went wrong with the reproducer? How did we get to a state where lowerstack of parent has a dentry which is !d_can_lookup?
Theoretically we could still get a an
S_ISDIRinode, becauseovl_get_inode()doesn't look at theis_dirvalue that lookup found. I.e. lookup thinks it found a non-dir, but iget will create a dir because of the backing inode's type.
Quoted 5 lines
AFAICS this can only happen if
i_op->lookupis not set onS_ISDIRfor the backing inode, which shouldn't happen on normal filesystems. Reproducer seems to use bfs, which *should* be normal, andbfs_igetcertainly doesn't do anything weird in that case, so I still don't understand what is happening.
During testing, it was discovered that BFS can return a directory inode without a lookup operation. Adding the following check in bfs_iget:
struct inode *bfs_iget(struct super_block *sb, unsigned long ino) {
...
brelse(bh);
+ if (S_ISDIR(inode->i_mode) && !inode->i_op->lookup) {
+ printf("Directory inode missing lookup %s:%08lx\n",
inode->i_sb->s_id, ino); + goto error; + } + unlock_new_inode(inode); return inode;
error:
iget_failed(inode); return ERR_PTR(-EIO); }
prevents the error but exposes an invalid inode:
loop0: detected capacity change from 0 to 64
BFS-fs: bfs_iget(): Directory inode missing lookup loop0:00000002
overlayfs: overlapping lowerdir path
Would this be considered a valid workaround, or does BFS require further fixes?
Review
Quoted 9 lines
In any case something like the following should filter out such weirdness:
bool
ovl_dentry_weird(struct dentry *dentry)
{
+ if (!d_can_lookup(dentry) && !d_is_file(dentry) &&
!d_is_symlink(dentry))(quoted diff) +2
-0+ return true; + return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
I tested this addition successfully.
Additionally, fixes for BFS seem to be discussed reluctantly.
For instance, this patch set [1] has remained unanswered.
Perhaps it would be worth considering discarding invalid inodes at the overlayfs level?
Quoted 2 lines
Thanks,
Miklos
--
Thanks,
Vasiliy Kovalev
Amir Goldstein
maintainerQuoted 63 lines · Vasiliy Kovalev
Hi,
Quoted 11 lines · Miklos Szeredi
Quoted 3 lines · Amir Goldstein
Can you analyse what went wrong with the reproducer? How did we get to a state where lowerstack of parent has a dentry which is !d_can_lookup?
Theoretically we could still get a an
S_ISDIRinode, becauseovl_get_inode()doesn't look at theis_dirvalue that lookup found. I.e. lookup thinks it found a non-dir, but iget will create a dir because of the backing inode's type.Quoted 5 lines
AFAICS this can only happen if
i_op->lookupis not set onS_ISDIRfor the backing inode, which shouldn't happen on normal filesystems. Reproducer seems to use bfs, which *should* be normal, andbfs_igetcertainly doesn't do anything weird in that case, so I still don't understand what is happening.During testing, it was discovered that BFS can return a directory inode without a lookup operation. Adding the following check in
bfs_iget:struct inode *bfs_iget(struct
super_block*sb, unsigned long ino) {...
brelse(bh);(quoted diff) +5
-0+ if (S_ISDIR(inode->i_mode) && !inode->i_op->lookup) { + printf("Directory inode missing lookup %s:%08lx\n", inode->i_sb->s_id, ino); + goto error; + } + unlock_new_inode(inode); return inode;error:
iget_failed(inode); return ERR_PTR(-EIO); }prevents the error but exposes an invalid inode:
loop0: detected capacity change from 0 to 64
BFS-fs:bfs_iget(): Directory inode missing lookup loop0:00000002
overlayfs: overlapping lowerdir pathWould this be considered a valid workaround, or does BFS require further fixes?
Review
Quoted 9 lines
In any case something like the following should filter out such weirdness:
bool
ovl_dentry_weird(struct dentry *dentry)
{
+ if (!d_can_lookup(dentry) && !d_is_file(dentry) &&
!d_is_symlink(dentry))(quoted diff) +2
-0+ return true; + return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
Quoted 6 lines
I tested this addition successfully.
Additionally, fixes for BFS seem to be discussed reluctantly. For instance, this patch set [1] has remained unanswered. Perhaps it would be worth considering discarding invalid inodes at the overlayfs level?
Sure. please send proper patch following Miklos' suggestion
Thanks,
Amir.
PATCH v2 1/1 ovl: Filter invalid inodes with missing lookup function
Add a check to the ovl_dentry_weird() function to prevent the processing of directory inodes that lack the lookup function. This is important because such inodes can cause errors in overlayfs when passed to the lowerstack.
--- fs/overlayfs/util.c | 3 +++ 1 file changed, 3 insertions(+)
fs/overlayfs/util.c +3 -0
@@ -202,6 +202,9 @@ void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,bool ovl_dentry_weird (struct dentry *dentry) { +if (!d_can_lookup (dentry) && !d_is_file (dentry) && !d_is_symlink (dentry)) +return true; + return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | DCACHE_MANAGE_TRANSIT | DCACHE_OP_HASH |
Signature · 1 line
2.33.8
Re: [PATCH v2] ovl: Filter invalid inodes with missing lookup function
Quoted 30 lines · Vasiliy Kovalev
Add a check to the
ovl_dentry_weird()function to prevent the processing of directory inodes that lack the lookup function. This is important because such inodes can cause errors in overlayfs when passed to the lowerstack.Reported-by [email protected]Suggested-by Miklos Szeredi <[email protected]>Signed-off-by Vasiliy Kovalev <[email protected]>Cc <[email protected]>--- fs/overlayfs/util.c | 3 +++ 1 file changed, 3 insertions(+)fs/overlayfs/util.c +3
-1@@ -202,6 +202,9 @@ void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,bool ovl_dentry_weird (struct dentry *dentry) { +if (!d_can_lookup (dentry) && !d_is_file (dentry) && !d_is_symlink (dentry)) +return true; + return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | DCACHE_MANAGE_TRANSIT | DCACHE_OP_HASH | --
Quoted 2 lines
2.33.8
Applied to overlayfs-next. Will send along with 6.13 PR
Thanks,
Amir.
Quoted 22 lines · Vasiliy Kovalev
without a lookup operation. Adding the following check in
bfs_iget:struct inode *bfs_iget(struct
super_block*sb, unsigned long ino) {...
brelse(bh);(quoted diff) +2
-0+ if (S_ISDIR(inode->i_mode) && !inode->i_op->lookup) { + printf("Directory inode missing lookup %s:%08lx\n",inode->i_sb->s_id, ino);fs/overlayfs/namei.c +3
-0+ goto error; + } +unlock_new_inode(inode); return inode;error:
iget_failed(inode); return ERR_PTR(-EIO); }
Quoted 8 lines
prevents the error but exposes an invalid inode:
loop0: detected capacity change from 0 to 64
BFS-fs:bfs_iget(): Directory inode missing lookup loop0:00000002
overlayfs: overlapping lowerdir pathWould this be considered a valid workaround, or does BFS require further fixes?
Yes, it does. Note that this
inode->i_mode = 0x0000FFFF & le32_to_cpu(di->i_mode);
sets the bits 0..15, which includes not only the permissions (0..11), but the file type as well. And those |= are not going to be enough to prevent trouble - what we have there is
0x1000 => FIFO 0x2000 => CHR 0x4000 => DIR 0x6000 => BLK 0x8000 => REG 0xa000 => LNK 0xe000 => SOCK
So depending upon ->i_vtype you get one of
* ->i_op and ->i_fop set for directory, type bits - 0x4000 | junk
* ->i_op and ->i_fop set for regular file, type bits - 0x8000 | junk
* ->i_op and ->i_fop left empty, type bits - junk.
Frankly, I would rather ignore bits 12..15 (i.e.
inode->i_mode = 0x00000FFF & le32_to_cpu(di->i_mode);
instead of
inode->i_mode = 0x0000FFFF & le32_to_cpu(di->i_mode);) and complain (and fail) if ->i_vtype value is fucked up.