On 11/16/26 17:19, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ovl: ovl_copyfile() leaks real_in/real_out refs when override
   fails
- [Medium] ve/overlayfs: backing-file I/O switches creds without the
   matching VE override
- [Low] ovl: ovl_lower_positive() returns false when
   ovl_override_creds() fails
- [Low] ovl: ovl_nlink_end() silently skips index cleanup on override
   failure
--

commit 9665af403c75ade64577a4c77c18a22fb272209d
Author: Vladimir Riabchun <[email protected]>

ve/overlayfs: Override VE together with creds

ovl_override_creds() now returns an error and fills a struct ovl_creds_desc 
holding both the old cred and the old VE. ovl_revert_creds() restores both. 
Every caller is converted, so overlayfs switches the VE together with the 
creator creds. Access to an overlayfs created in a different CT is rejected 
with -EPERM.

diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c
index 2531433f47bf..464699e51695 100644
--- a/fs/overlayfs/file.c
+++ b/fs/overlayfs/file.c

[ ... ]

@@ -520,14 +527,19 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t 
pos_in,
         * verify read access with the source mounter creds.
         */
        if (unlikely(inode_in->i_sb != inode_out->i_sb)) {
-               old_cred = ovl_override_creds(inode_in->i_sb);
+               ret = ovl_override_creds(inode_in->i_sb, &old_cred);
+               if (ret)
+                       goto out_unlock;
                ret = rw_verify_area(READ, fd_file(real_in), &pos_in, len);
-               revert_creds(old_cred);
+               ovl_revert_creds(&old_cred);
                if (unlikely(ret))
                        goto out_unlock;
        }

[Severity: Medium]
Does this new error exit leak the real_in and real_out file references?

Both were taken by ovl_real_fdget() earlier in ovl_copyfile(), but out_unlock 
only calls inode_unlock(inode_out), so fdput() is never reached. Before this 
patch the override could not fail, so the leak is a regression. A task in one 
CT doing copy_file_range() between its own overlayfs and one created by another 
CT gets -EPERM from override_VE() and leaks both files on every attempt. The 
second ovl_override_creds() failure below already uses skip_switch, which 
reaches the fdput() calls.


There is a leak and this is a rebase bug.
https://github.com/torvalds/linux/commit/a1e0eb8f55cfe09bb31a202a388babc411292656

We don't have with_ovl_creds helper.
I'll send a fix for this in v7.

[ ... ]

diff --git a/fs/overlayfs/namei.c b/fs/overlayfs/namei.c
index efa2a072aa69..edc11a7b27ee 100644
--- a/fs/overlayfs/namei.c
+++ b/fs/overlayfs/namei.c

[ ... ]

@@ -1426,7 +1436,10 @@ bool ovl_lower_positive(struct dentry *dentry)
        if (!ovl_dentry_upper(dentry))
                return true;
- old_cred = ovl_override_creds(dentry->d_sb);
+       /* Ugh, hard - we can't pass an error from here. */
+       if (ovl_override_creds(dentry->d_sb, &old_cred))
+               return false;
+

[Severity: Low]
Is false the safe answer when the override fails?

Returning false means "no lower entry exists", so ovl_do_remove() would choose 
ovl_remove_upper() instead of ovl_remove_and_whiteout(), and ovl_rename() would skip 
RENAME_WHITEOUT. A lower file could then reappear after unlink. In practice the later 
override calls in the same operation fail the same way, but correctness then depends on 
call ordering.

The lookup-error branch of this function already assumes "something is there" 
and sets positive = true. Would returning true here be consistent with that?

[ ... ]

diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c
index 817563215001..273b2e2e17eb 100644
--- a/fs/overlayfs/util.c
+++ b/fs/overlayfs/util.c
@@ -63,11 +63,38 @@ struct dentry *ovl_workdir(struct dentry *dentry)
        return ofs->workdir;
  }
-const struct cred *ovl_override_creds(struct super_block *sb)
+/* On error no revert is needed. */
+int ovl_override_creds(struct super_block *sb, struct ovl_creds_desc *desc)
  {
        struct ovl_fs *ofs = OVL_FS(sb);
- return override_creds(ofs->creator_cred);
+       /* Cleanup after previous users */
+       *desc = (struct ovl_creds_desc) {};
+
+       if (ve_is_super(ofs->creator_ve) && !ve_is_super(get_exec_env())) {
+               /* Host overlayfs used from a CT: host creds work there. */
+               desc->ve = NULL;
+               desc->ve_overridden = false;
+       } else {
+               desc->ve = override_ve(ofs->creator_ve);
+               if (IS_ERR(desc->ve))
+                       return PTR_ERR(desc->ve);
+               desc->ve_overridden = true;
+       }
+
+       desc->cred = override_creds(ofs->creator_cred);
+       return 0;
+}

[Severity: Medium]
As reported by Pavel Tikhomirov on v2 [1], the conversion is incomplete. Are 
the users of ovl_creds() still left with the cred/VE mismatch this patch 
describes?

Yes. https://virtuozzo.atlassian.net/browse/VSTOR-155347


ovl_read_iter(), ovl_write_iter(), ovl_splice_read(), ovl_splice_write() and 
ovl_mmap() set backing_file_ctx.cred = ovl_creds(sb). The helpers in 
fs/backing-file.c then call only override_creds(ctx->cred), with no override_ve(). 
ve_capable() uses current->overrided_ve ?: get_exec_env(), so on these paths the 
creator creds are still checked against the caller's VE. A host task reading a file 
on a CT overlayfs is one such case. Cross-CT access is also not rejected there with 
-EPERM, unlike the paths converted here. The commit message suggests all users of the 
old cred are covered, which is not the case.

+
+/* Accepts desc from a failed ovl_override_creds and does nothing. */
+void ovl_revert_creds(struct ovl_creds_desc *old_creds)
+{
+       if (IS_ERR(old_creds->ve))
+               return;
+
+       revert_creds(old_creds->cred);
+       if (old_creds->ve_overridden)
+               revert_ve(old_creds->ve);
  }

[ ... ]

@@ -1228,11 +1258,15 @@ void ovl_nlink_end(struct dentry *dentry)
        ovl_drop_write(dentry);
if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
-               const struct cred *old_cred;
+               struct ovl_creds_desc old_cred;
+               int err;
- old_cred = ovl_override_creds(dentry->d_sb);
-               ovl_cleanup_index(dentry);
-               revert_creds(old_cred);
+               err = ovl_override_creds(dentry->d_sb, &old_cred);
+               if (err)
+                       pr_err("%s: skipping ovl_cleanup_index.\n", __func__);
+               else
+                       ovl_cleanup_index(dentry);
+               ovl_revert_creds(&old_cred);
        }

[Severity: Low]
Is it acceptable to silently skip ovl_cleanup_index() here?

ovl_nlink_end() is void and runs after the upper unlink or rename has already 
succeeded. If the override fails, an orphan index entry is left behind with 
i_nlink at 0, and only a pr_err() records it. The old code could not fail at 
this point. ovl_nlink_start() would normally have failed first for the same 
task, sb and VE, so this looks mostly unreachable. But it is an error path that 
now leaves stale state.

Nothing we can do here. Everything is bad already. And we shouldn't be
able to get there anyway.


[1] 
https://lore.kernel.org/r/179318868950.3559520.6473416824609223980.b4-review@b4


--
Best regards, Riabchun Vladimir
Linux Kernel Developer, Virtuozzo

_______________________________________________
Devel mailing list
[email protected]
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to