Skip to content

Commit 34a456e

Browse files
committed
Merge tag 'fs.idmapped.helpers.v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux
Pull fs mapping helper updates from Christian Brauner: "This adds kernel-doc to all new idmapping helpers and improves their naming which was triggered by a discussion with some fs developers. Some of the names are based on suggestions by Vivek and Al. Also remove the open-coded permission checking in a few places with simple helpers. Overall this should lead to more clarity and make it easier to maintain" * tag 'fs.idmapped.helpers.v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux: fs: introduce two inode i_{u,g}id initialization helpers fs: introduce fsuidgid_has_mapping() helper fs: document and rename fsid helpers fs: document mapping helpers
2 parents cc15422 + db99855 commit 34a456e

File tree

6 files changed

+135
-20
lines changed

6 files changed

+135
-20
lines changed

fs/ext4/ialloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ struct inode *__ext4_new_inode(struct user_namespace *mnt_userns,
970970
i_gid_write(inode, owner[1]);
971971
} else if (test_opt(sb, GRPID)) {
972972
inode->i_mode = mode;
973-
inode->i_uid = fsuid_into_mnt(mnt_userns);
973+
inode_fsuid_set(inode, mnt_userns);
974974
inode->i_gid = dir->i_gid;
975975
} else
976976
inode_init_owner(mnt_userns, inode, dir, mode);

fs/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ EXPORT_SYMBOL(init_special_inode);
21472147
void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
21482148
const struct inode *dir, umode_t mode)
21492149
{
2150-
inode->i_uid = fsuid_into_mnt(mnt_userns);
2150+
inode_fsuid_set(inode, mnt_userns);
21512151
if (dir && dir->i_mode & S_ISGID) {
21522152
inode->i_gid = dir->i_gid;
21532153

@@ -2159,7 +2159,7 @@ void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
21592159
!capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
21602160
mode &= ~S_ISGID;
21612161
} else
2162-
inode->i_gid = fsgid_into_mnt(mnt_userns);
2162+
inode_fsgid_set(inode, mnt_userns);
21632163
inode->i_mode = mode;
21642164
}
21652165
EXPORT_SYMBOL(inode_init_owner);

fs/namei.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,16 +2824,14 @@ static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
28242824
static inline int may_create(struct user_namespace *mnt_userns,
28252825
struct inode *dir, struct dentry *child)
28262826
{
2827-
struct user_namespace *s_user_ns;
28282827
audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
28292828
if (child->d_inode)
28302829
return -EEXIST;
28312830
if (IS_DEADDIR(dir))
28322831
return -ENOENT;
2833-
s_user_ns = dir->i_sb->s_user_ns;
2834-
if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) ||
2835-
!kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns)))
2832+
if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
28362833
return -EOVERFLOW;
2834+
28372835
return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
28382836
}
28392837

@@ -3035,14 +3033,11 @@ static int may_o_create(struct user_namespace *mnt_userns,
30353033
const struct path *dir, struct dentry *dentry,
30363034
umode_t mode)
30373035
{
3038-
struct user_namespace *s_user_ns;
30393036
int error = security_path_mknod(dir, dentry, mode, 0);
30403037
if (error)
30413038
return error;
30423039

3043-
s_user_ns = dir->dentry->d_sb->s_user_ns;
3044-
if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) ||
3045-
!kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns)))
3040+
if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
30463041
return -EOVERFLOW;
30473042

30483043
error = inode_permission(mnt_userns, dir->dentry->d_inode,

fs/xfs/xfs_inode.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ xfs_init_new_inode(
812812

813813
if (dir && !(dir->i_mode & S_ISGID) &&
814814
(mp->m_flags & XFS_MOUNT_GRPID)) {
815-
inode->i_uid = fsuid_into_mnt(mnt_userns);
815+
inode_fsuid_set(inode, mnt_userns);
816816
inode->i_gid = dir->i_gid;
817817
inode->i_mode = mode;
818818
} else {
@@ -1007,8 +1007,8 @@ xfs_create(
10071007
/*
10081008
* Make sure that we have allocated dquot(s) on disk.
10091009
*/
1010-
error = xfs_qm_vop_dqalloc(dp, fsuid_into_mnt(mnt_userns),
1011-
fsgid_into_mnt(mnt_userns), prid,
1010+
error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
1011+
mapped_fsgid(mnt_userns), prid,
10121012
XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
10131013
&udqp, &gdqp, &pdqp);
10141014
if (error)
@@ -1158,8 +1158,8 @@ xfs_create_tmpfile(
11581158
/*
11591159
* Make sure that we have allocated dquot(s) on disk.
11601160
*/
1161-
error = xfs_qm_vop_dqalloc(dp, fsuid_into_mnt(mnt_userns),
1162-
fsgid_into_mnt(mnt_userns), prid,
1161+
error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
1162+
mapped_fsgid(mnt_userns), prid,
11631163
XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
11641164
&udqp, &gdqp, &pdqp);
11651165
if (error)

fs/xfs/xfs_symlink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ xfs_symlink(
182182
/*
183183
* Make sure that we have allocated dquot(s) on disk.
184184
*/
185-
error = xfs_qm_vop_dqalloc(dp, fsuid_into_mnt(mnt_userns),
186-
fsgid_into_mnt(mnt_userns), prid,
185+
error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
186+
mapped_fsgid(mnt_userns), prid,
187187
XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
188188
&udqp, &gdqp, &pdqp);
189189
if (error)

include/linux/fs.h

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,52 +1575,172 @@ static inline void i_gid_write(struct inode *inode, gid_t gid)
15751575
inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid);
15761576
}
15771577

1578+
/**
1579+
* kuid_into_mnt - map a kuid down into a mnt_userns
1580+
* @mnt_userns: user namespace of the relevant mount
1581+
* @kuid: kuid to be mapped
1582+
*
1583+
* Return: @kuid mapped according to @mnt_userns.
1584+
* If @kuid has no mapping INVALID_UID is returned.
1585+
*/
15781586
static inline kuid_t kuid_into_mnt(struct user_namespace *mnt_userns,
15791587
kuid_t kuid)
15801588
{
15811589
return make_kuid(mnt_userns, __kuid_val(kuid));
15821590
}
15831591

1592+
/**
1593+
* kgid_into_mnt - map a kgid down into a mnt_userns
1594+
* @mnt_userns: user namespace of the relevant mount
1595+
* @kgid: kgid to be mapped
1596+
*
1597+
* Return: @kgid mapped according to @mnt_userns.
1598+
* If @kgid has no mapping INVALID_GID is returned.
1599+
*/
15841600
static inline kgid_t kgid_into_mnt(struct user_namespace *mnt_userns,
15851601
kgid_t kgid)
15861602
{
15871603
return make_kgid(mnt_userns, __kgid_val(kgid));
15881604
}
15891605

1606+
/**
1607+
* i_uid_into_mnt - map an inode's i_uid down into a mnt_userns
1608+
* @mnt_userns: user namespace of the mount the inode was found from
1609+
* @inode: inode to map
1610+
*
1611+
* Return: the inode's i_uid mapped down according to @mnt_userns.
1612+
* If the inode's i_uid has no mapping INVALID_UID is returned.
1613+
*/
15901614
static inline kuid_t i_uid_into_mnt(struct user_namespace *mnt_userns,
15911615
const struct inode *inode)
15921616
{
15931617
return kuid_into_mnt(mnt_userns, inode->i_uid);
15941618
}
15951619

1620+
/**
1621+
* i_gid_into_mnt - map an inode's i_gid down into a mnt_userns
1622+
* @mnt_userns: user namespace of the mount the inode was found from
1623+
* @inode: inode to map
1624+
*
1625+
* Return: the inode's i_gid mapped down according to @mnt_userns.
1626+
* If the inode's i_gid has no mapping INVALID_GID is returned.
1627+
*/
15961628
static inline kgid_t i_gid_into_mnt(struct user_namespace *mnt_userns,
15971629
const struct inode *inode)
15981630
{
15991631
return kgid_into_mnt(mnt_userns, inode->i_gid);
16001632
}
16011633

1634+
/**
1635+
* kuid_from_mnt - map a kuid up into a mnt_userns
1636+
* @mnt_userns: user namespace of the relevant mount
1637+
* @kuid: kuid to be mapped
1638+
*
1639+
* Return: @kuid mapped up according to @mnt_userns.
1640+
* If @kuid has no mapping INVALID_UID is returned.
1641+
*/
16021642
static inline kuid_t kuid_from_mnt(struct user_namespace *mnt_userns,
16031643
kuid_t kuid)
16041644
{
16051645
return KUIDT_INIT(from_kuid(mnt_userns, kuid));
16061646
}
16071647

1648+
/**
1649+
* kgid_from_mnt - map a kgid up into a mnt_userns
1650+
* @mnt_userns: user namespace of the relevant mount
1651+
* @kgid: kgid to be mapped
1652+
*
1653+
* Return: @kgid mapped up according to @mnt_userns.
1654+
* If @kgid has no mapping INVALID_GID is returned.
1655+
*/
16081656
static inline kgid_t kgid_from_mnt(struct user_namespace *mnt_userns,
16091657
kgid_t kgid)
16101658
{
16111659
return KGIDT_INIT(from_kgid(mnt_userns, kgid));
16121660
}
16131661

1614-
static inline kuid_t fsuid_into_mnt(struct user_namespace *mnt_userns)
1662+
/**
1663+
* mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
1664+
* @mnt_userns: user namespace of the relevant mount
1665+
*
1666+
* Use this helper to initialize a new vfs or filesystem object based on
1667+
* the caller's fsuid. A common example is initializing the i_uid field of
1668+
* a newly allocated inode triggered by a creation event such as mkdir or
1669+
* O_CREAT. Other examples include the allocation of quotas for a specific
1670+
* user.
1671+
*
1672+
* Return: the caller's current fsuid mapped up according to @mnt_userns.
1673+
*/
1674+
static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns)
16151675
{
16161676
return kuid_from_mnt(mnt_userns, current_fsuid());
16171677
}
16181678

1619-
static inline kgid_t fsgid_into_mnt(struct user_namespace *mnt_userns)
1679+
/**
1680+
* mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
1681+
* @mnt_userns: user namespace of the relevant mount
1682+
*
1683+
* Use this helper to initialize a new vfs or filesystem object based on
1684+
* the caller's fsgid. A common example is initializing the i_gid field of
1685+
* a newly allocated inode triggered by a creation event such as mkdir or
1686+
* O_CREAT. Other examples include the allocation of quotas for a specific
1687+
* user.
1688+
*
1689+
* Return: the caller's current fsgid mapped up according to @mnt_userns.
1690+
*/
1691+
static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns)
16201692
{
16211693
return kgid_from_mnt(mnt_userns, current_fsgid());
16221694
}
16231695

1696+
/**
1697+
* inode_fsuid_set - initialize inode's i_uid field with callers fsuid
1698+
* @inode: inode to initialize
1699+
* @mnt_userns: user namespace of the mount the inode was found from
1700+
*
1701+
* Initialize the i_uid field of @inode. If the inode was found/created via
1702+
* an idmapped mount map the caller's fsuid according to @mnt_users.
1703+
*/
1704+
static inline void inode_fsuid_set(struct inode *inode,
1705+
struct user_namespace *mnt_userns)
1706+
{
1707+
inode->i_uid = mapped_fsuid(mnt_userns);
1708+
}
1709+
1710+
/**
1711+
* inode_fsgid_set - initialize inode's i_gid field with callers fsgid
1712+
* @inode: inode to initialize
1713+
* @mnt_userns: user namespace of the mount the inode was found from
1714+
*
1715+
* Initialize the i_gid field of @inode. If the inode was found/created via
1716+
* an idmapped mount map the caller's fsgid according to @mnt_users.
1717+
*/
1718+
static inline void inode_fsgid_set(struct inode *inode,
1719+
struct user_namespace *mnt_userns)
1720+
{
1721+
inode->i_gid = mapped_fsgid(mnt_userns);
1722+
}
1723+
1724+
/**
1725+
* fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
1726+
* @sb: the superblock we want a mapping in
1727+
* @mnt_userns: user namespace of the relevant mount
1728+
*
1729+
* Check whether the caller's fsuid and fsgid have a valid mapping in the
1730+
* s_user_ns of the superblock @sb. If the caller is on an idmapped mount map
1731+
* the caller's fsuid and fsgid according to the @mnt_userns first.
1732+
*
1733+
* Return: true if fsuid and fsgid is mapped, false if not.
1734+
*/
1735+
static inline bool fsuidgid_has_mapping(struct super_block *sb,
1736+
struct user_namespace *mnt_userns)
1737+
{
1738+
struct user_namespace *s_user_ns = sb->s_user_ns;
1739+
1740+
return kuid_has_mapping(s_user_ns, mapped_fsuid(mnt_userns)) &&
1741+
kgid_has_mapping(s_user_ns, mapped_fsgid(mnt_userns));
1742+
}
1743+
16241744
extern struct timespec64 current_time(struct inode *inode);
16251745

16261746
/*

0 commit comments

Comments
 (0)