Skip to content

Commit 83c44f0

Browse files
cntiancigregkh
authored andcommitted
ovl: Use ovl mounter's fsuid and fsgid in ovl_link()
commit 5b0db51 upstream. There is a wrong case of link() on overlay: $ mkdir /lower /fuse /merge $ mount -t fuse /fuse $ mkdir /fuse/upper /fuse/work $ mount -t overlay /merge -o lowerdir=/lower,upperdir=/fuse/upper,\ workdir=work $ touch /merge/file $ chown bin.bin /merge/file // the file's caller becomes "bin" $ ln /merge/file /merge/lnkfile Then we will get an error(EACCES) because fuse daemon checks the link()'s caller is "bin", it denied this request. In the changing history of ovl_link(), there are two key commits: The first is commit bb0d2b8 ("ovl: fix sgid on directory") which overrides the cred's fsuid/fsgid using the new inode. The new inode's owner is initialized by inode_init_owner(), and inode->fsuid is assigned to the current user. So the override fsuid becomes the current user. We know link() is actually modifying the directory, so the caller must have the MAY_WRITE permission on the directory. The current caller may should have this permission. This is acceptable to use the caller's fsuid. The second is commit 51f7e52 ("ovl: share inode for hard link") which removed the inode creation in ovl_link(). This commit move inode_init_owner() into ovl_create_object(), so the ovl_link() just give the old inode to ovl_create_or_link(). Then the override fsuid becomes the old inode's fsuid, neither the caller nor the overlay's mounter! So this is incorrect. Fix this bug by using ovl mounter's fsuid/fsgid to do underlying fs's link(). Link: https://lore.kernel.org/all/20220817102952.xnvesg3a7rbv576x@wittgenstein/T Link: https://lore.kernel.org/lkml/[email protected]/t Signed-off-by: Zhang Tianci <[email protected]> Signed-off-by: Jiachen Zhang <[email protected]> Reviewed-by: Christian Brauner (Microsoft) <[email protected]> Fixes: 51f7e52 ("ovl: share inode for hard link") Cc: <[email protected]> # v4.8 Signed-off-by: Miklos Szeredi <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent fcb9428 commit 83c44f0

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

fs/overlayfs/dir.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -589,28 +589,42 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
589589
goto out_revert_creds;
590590
}
591591

592-
err = -ENOMEM;
593-
override_cred = prepare_creds();
594-
if (override_cred) {
592+
if (!attr->hardlink) {
593+
err = -ENOMEM;
594+
override_cred = prepare_creds();
595+
if (!override_cred)
596+
goto out_revert_creds;
597+
/*
598+
* In the creation cases(create, mkdir, mknod, symlink),
599+
* ovl should transfer current's fs{u,g}id to underlying
600+
* fs. Because underlying fs want to initialize its new
601+
* inode owner using current's fs{u,g}id. And in this
602+
* case, the @inode is a new inode that is initialized
603+
* in inode_init_owner() to current's fs{u,g}id. So use
604+
* the inode's i_{u,g}id to override the cred's fs{u,g}id.
605+
*
606+
* But in the other hardlink case, ovl_link() does not
607+
* create a new inode, so just use the ovl mounter's
608+
* fs{u,g}id.
609+
*/
595610
override_cred->fsuid = inode->i_uid;
596611
override_cred->fsgid = inode->i_gid;
597-
if (!attr->hardlink) {
598-
err = security_dentry_create_files_as(dentry,
599-
attr->mode, &dentry->d_name, old_cred,
600-
override_cred);
601-
if (err) {
602-
put_cred(override_cred);
603-
goto out_revert_creds;
604-
}
612+
err = security_dentry_create_files_as(dentry,
613+
attr->mode, &dentry->d_name, old_cred,
614+
override_cred);
615+
if (err) {
616+
put_cred(override_cred);
617+
goto out_revert_creds;
605618
}
606619
put_cred(override_creds(override_cred));
607620
put_cred(override_cred);
608-
609-
if (!ovl_dentry_is_whiteout(dentry))
610-
err = ovl_create_upper(dentry, inode, attr);
611-
else
612-
err = ovl_create_over_whiteout(dentry, inode, attr);
613621
}
622+
623+
if (!ovl_dentry_is_whiteout(dentry))
624+
err = ovl_create_upper(dentry, inode, attr);
625+
else
626+
err = ovl_create_over_whiteout(dentry, inode, attr);
627+
614628
out_revert_creds:
615629
revert_creds(old_cred);
616630
return err;

0 commit comments

Comments
 (0)