@@ -1575,52 +1575,172 @@ static inline void i_gid_write(struct inode *inode, gid_t gid)
1575
1575
inode -> i_gid = make_kgid (inode -> i_sb -> s_user_ns , gid );
1576
1576
}
1577
1577
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
+ */
1578
1586
static inline kuid_t kuid_into_mnt (struct user_namespace * mnt_userns ,
1579
1587
kuid_t kuid )
1580
1588
{
1581
1589
return make_kuid (mnt_userns , __kuid_val (kuid ));
1582
1590
}
1583
1591
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
+ */
1584
1600
static inline kgid_t kgid_into_mnt (struct user_namespace * mnt_userns ,
1585
1601
kgid_t kgid )
1586
1602
{
1587
1603
return make_kgid (mnt_userns , __kgid_val (kgid ));
1588
1604
}
1589
1605
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
+ */
1590
1614
static inline kuid_t i_uid_into_mnt (struct user_namespace * mnt_userns ,
1591
1615
const struct inode * inode )
1592
1616
{
1593
1617
return kuid_into_mnt (mnt_userns , inode -> i_uid );
1594
1618
}
1595
1619
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
+ */
1596
1628
static inline kgid_t i_gid_into_mnt (struct user_namespace * mnt_userns ,
1597
1629
const struct inode * inode )
1598
1630
{
1599
1631
return kgid_into_mnt (mnt_userns , inode -> i_gid );
1600
1632
}
1601
1633
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
+ */
1602
1642
static inline kuid_t kuid_from_mnt (struct user_namespace * mnt_userns ,
1603
1643
kuid_t kuid )
1604
1644
{
1605
1645
return KUIDT_INIT (from_kuid (mnt_userns , kuid ));
1606
1646
}
1607
1647
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
+ */
1608
1656
static inline kgid_t kgid_from_mnt (struct user_namespace * mnt_userns ,
1609
1657
kgid_t kgid )
1610
1658
{
1611
1659
return KGIDT_INIT (from_kgid (mnt_userns , kgid ));
1612
1660
}
1613
1661
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 )
1615
1675
{
1616
1676
return kuid_from_mnt (mnt_userns , current_fsuid ());
1617
1677
}
1618
1678
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 )
1620
1692
{
1621
1693
return kgid_from_mnt (mnt_userns , current_fsgid ());
1622
1694
}
1623
1695
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
+
1624
1744
extern struct timespec64 current_time (struct inode * inode );
1625
1745
1626
1746
/*
0 commit comments