Skip to content

Commit 5b0db51

Browse files
cntianciMiklos Szeredi
authored andcommitted
ovl: Use ovl mounter's fsuid and fsgid in ovl_link()
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]>
1 parent cf8aa9b commit 5b0db51

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
@@ -592,28 +592,42 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
592592
goto out_revert_creds;
593593
}
594594

595-
err = -ENOMEM;
596-
override_cred = prepare_creds();
597-
if (override_cred) {
595+
if (!attr->hardlink) {
596+
err = -ENOMEM;
597+
override_cred = prepare_creds();
598+
if (!override_cred)
599+
goto out_revert_creds;
600+
/*
601+
* In the creation cases(create, mkdir, mknod, symlink),
602+
* ovl should transfer current's fs{u,g}id to underlying
603+
* fs. Because underlying fs want to initialize its new
604+
* inode owner using current's fs{u,g}id. And in this
605+
* case, the @inode is a new inode that is initialized
606+
* in inode_init_owner() to current's fs{u,g}id. So use
607+
* the inode's i_{u,g}id to override the cred's fs{u,g}id.
608+
*
609+
* But in the other hardlink case, ovl_link() does not
610+
* create a new inode, so just use the ovl mounter's
611+
* fs{u,g}id.
612+
*/
598613
override_cred->fsuid = inode->i_uid;
599614
override_cred->fsgid = inode->i_gid;
600-
if (!attr->hardlink) {
601-
err = security_dentry_create_files_as(dentry,
602-
attr->mode, &dentry->d_name, old_cred,
603-
override_cred);
604-
if (err) {
605-
put_cred(override_cred);
606-
goto out_revert_creds;
607-
}
615+
err = security_dentry_create_files_as(dentry,
616+
attr->mode, &dentry->d_name, old_cred,
617+
override_cred);
618+
if (err) {
619+
put_cred(override_cred);
620+
goto out_revert_creds;
608621
}
609622
put_cred(override_creds(override_cred));
610623
put_cred(override_cred);
611-
612-
if (!ovl_dentry_is_whiteout(dentry))
613-
err = ovl_create_upper(dentry, inode, attr);
614-
else
615-
err = ovl_create_over_whiteout(dentry, inode, attr);
616624
}
625+
626+
if (!ovl_dentry_is_whiteout(dentry))
627+
err = ovl_create_upper(dentry, inode, attr);
628+
else
629+
err = ovl_create_over_whiteout(dentry, inode, attr);
630+
617631
out_revert_creds:
618632
revert_creds(old_cred);
619633
return err;

0 commit comments

Comments
 (0)