1a793d79eSChristian Brauner /* SPDX-License-Identifier: GPL-2.0 */ 2a793d79eSChristian Brauner #ifndef _LINUX_MNT_IDMAPPING_H 3a793d79eSChristian Brauner #define _LINUX_MNT_IDMAPPING_H 4a793d79eSChristian Brauner 5a793d79eSChristian Brauner #include <linux/types.h> 6a793d79eSChristian Brauner #include <linux/uidgid.h> 7a793d79eSChristian Brauner 8*256c8aedSChristian Brauner struct mnt_idmap; 9a793d79eSChristian Brauner struct user_namespace; 10*256c8aedSChristian Brauner 11*256c8aedSChristian Brauner extern struct mnt_idmap nop_mnt_idmap; 12a793d79eSChristian Brauner extern struct user_namespace init_user_ns; 13a793d79eSChristian Brauner 141e5267cdSChristian Brauner typedef struct { 151e5267cdSChristian Brauner uid_t val; 161e5267cdSChristian Brauner } vfsuid_t; 171e5267cdSChristian Brauner 181e5267cdSChristian Brauner typedef struct { 191e5267cdSChristian Brauner gid_t val; 201e5267cdSChristian Brauner } vfsgid_t; 211e5267cdSChristian Brauner 2245c31150SChristian Brauner static_assert(sizeof(vfsuid_t) == sizeof(kuid_t)); 2345c31150SChristian Brauner static_assert(sizeof(vfsgid_t) == sizeof(kgid_t)); 2445c31150SChristian Brauner static_assert(offsetof(vfsuid_t, val) == offsetof(kuid_t, val)); 2545c31150SChristian Brauner static_assert(offsetof(vfsgid_t, val) == offsetof(kgid_t, val)); 2645c31150SChristian Brauner 271e5267cdSChristian Brauner #ifdef CONFIG_MULTIUSER 281e5267cdSChristian Brauner static inline uid_t __vfsuid_val(vfsuid_t uid) 291e5267cdSChristian Brauner { 301e5267cdSChristian Brauner return uid.val; 311e5267cdSChristian Brauner } 321e5267cdSChristian Brauner 331e5267cdSChristian Brauner static inline gid_t __vfsgid_val(vfsgid_t gid) 341e5267cdSChristian Brauner { 351e5267cdSChristian Brauner return gid.val; 361e5267cdSChristian Brauner } 371e5267cdSChristian Brauner #else 381e5267cdSChristian Brauner static inline uid_t __vfsuid_val(vfsuid_t uid) 391e5267cdSChristian Brauner { 401e5267cdSChristian Brauner return 0; 411e5267cdSChristian Brauner } 421e5267cdSChristian Brauner 431e5267cdSChristian Brauner static inline gid_t __vfsgid_val(vfsgid_t gid) 441e5267cdSChristian Brauner { 451e5267cdSChristian Brauner return 0; 461e5267cdSChristian Brauner } 471e5267cdSChristian Brauner #endif 481e5267cdSChristian Brauner 491e5267cdSChristian Brauner static inline bool vfsuid_valid(vfsuid_t uid) 501e5267cdSChristian Brauner { 511e5267cdSChristian Brauner return __vfsuid_val(uid) != (uid_t)-1; 521e5267cdSChristian Brauner } 531e5267cdSChristian Brauner 541e5267cdSChristian Brauner static inline bool vfsgid_valid(vfsgid_t gid) 551e5267cdSChristian Brauner { 561e5267cdSChristian Brauner return __vfsgid_val(gid) != (gid_t)-1; 571e5267cdSChristian Brauner } 581e5267cdSChristian Brauner 591e5267cdSChristian Brauner static inline bool vfsuid_eq(vfsuid_t left, vfsuid_t right) 601e5267cdSChristian Brauner { 614d0548a7SSeth Forshee return vfsuid_valid(left) && __vfsuid_val(left) == __vfsuid_val(right); 621e5267cdSChristian Brauner } 631e5267cdSChristian Brauner 641e5267cdSChristian Brauner static inline bool vfsgid_eq(vfsgid_t left, vfsgid_t right) 651e5267cdSChristian Brauner { 664d0548a7SSeth Forshee return vfsgid_valid(left) && __vfsgid_val(left) == __vfsgid_val(right); 671e5267cdSChristian Brauner } 681e5267cdSChristian Brauner 691e5267cdSChristian Brauner /** 701e5267cdSChristian Brauner * vfsuid_eq_kuid - check whether kuid and vfsuid have the same value 711e5267cdSChristian Brauner * @vfsuid: the vfsuid to compare 7277940f0dSChristian Brauner * @kuid: the kuid to compare 731e5267cdSChristian Brauner * 7477940f0dSChristian Brauner * Check whether @vfsuid and @kuid have the same values. 751e5267cdSChristian Brauner * 7677940f0dSChristian Brauner * Return: true if @vfsuid and @kuid have the same value, false if not. 774d0548a7SSeth Forshee * Comparison between two invalid uids returns false. 781e5267cdSChristian Brauner */ 791e5267cdSChristian Brauner static inline bool vfsuid_eq_kuid(vfsuid_t vfsuid, kuid_t kuid) 801e5267cdSChristian Brauner { 814d0548a7SSeth Forshee return vfsuid_valid(vfsuid) && __vfsuid_val(vfsuid) == __kuid_val(kuid); 821e5267cdSChristian Brauner } 831e5267cdSChristian Brauner 841e5267cdSChristian Brauner /** 851e5267cdSChristian Brauner * vfsgid_eq_kgid - check whether kgid and vfsgid have the same value 861e5267cdSChristian Brauner * @vfsgid: the vfsgid to compare 8777940f0dSChristian Brauner * @kgid: the kgid to compare 881e5267cdSChristian Brauner * 8977940f0dSChristian Brauner * Check whether @vfsgid and @kgid have the same values. 901e5267cdSChristian Brauner * 9177940f0dSChristian Brauner * Return: true if @vfsgid and @kgid have the same value, false if not. 924d0548a7SSeth Forshee * Comparison between two invalid gids returns false. 931e5267cdSChristian Brauner */ 9477940f0dSChristian Brauner static inline bool vfsgid_eq_kgid(vfsgid_t vfsgid, kgid_t kgid) 951e5267cdSChristian Brauner { 964d0548a7SSeth Forshee return vfsgid_valid(vfsgid) && __vfsgid_val(vfsgid) == __kgid_val(kgid); 971e5267cdSChristian Brauner } 981e5267cdSChristian Brauner 999c4f28ddSChristian Brauner static inline bool vfsuid_gt_kuid(vfsuid_t vfsuid, kuid_t kuid) 1009c4f28ddSChristian Brauner { 1019c4f28ddSChristian Brauner return __vfsuid_val(vfsuid) > __kuid_val(kuid); 1029c4f28ddSChristian Brauner } 1039c4f28ddSChristian Brauner 1049c4f28ddSChristian Brauner static inline bool vfsgid_gt_kgid(vfsgid_t vfsgid, kgid_t kgid) 1059c4f28ddSChristian Brauner { 1069c4f28ddSChristian Brauner return __vfsgid_val(vfsgid) > __kgid_val(kgid); 1079c4f28ddSChristian Brauner } 1089c4f28ddSChristian Brauner 1099c4f28ddSChristian Brauner static inline bool vfsuid_lt_kuid(vfsuid_t vfsuid, kuid_t kuid) 1109c4f28ddSChristian Brauner { 1119c4f28ddSChristian Brauner return __vfsuid_val(vfsuid) < __kuid_val(kuid); 1129c4f28ddSChristian Brauner } 1139c4f28ddSChristian Brauner 1149c4f28ddSChristian Brauner static inline bool vfsgid_lt_kgid(vfsgid_t vfsgid, kgid_t kgid) 1159c4f28ddSChristian Brauner { 1169c4f28ddSChristian Brauner return __vfsgid_val(vfsgid) < __kgid_val(kgid); 1179c4f28ddSChristian Brauner } 1189c4f28ddSChristian Brauner 1191e5267cdSChristian Brauner /* 1201e5267cdSChristian Brauner * vfs{g,u}ids are created from k{g,u}ids. 1211e5267cdSChristian Brauner * We don't allow them to be created from regular {u,g}id. 1221e5267cdSChristian Brauner */ 1231e5267cdSChristian Brauner #define VFSUIDT_INIT(val) (vfsuid_t){ __kuid_val(val) } 1241e5267cdSChristian Brauner #define VFSGIDT_INIT(val) (vfsgid_t){ __kgid_val(val) } 1251e5267cdSChristian Brauner 1261e5267cdSChristian Brauner #define INVALID_VFSUID VFSUIDT_INIT(INVALID_UID) 1271e5267cdSChristian Brauner #define INVALID_VFSGID VFSGIDT_INIT(INVALID_GID) 1281e5267cdSChristian Brauner 1291e5267cdSChristian Brauner /* 1301e5267cdSChristian Brauner * Allow a vfs{g,u}id to be used as a k{g,u}id where we want to compare 1311e5267cdSChristian Brauner * whether the mapped value is identical to value of a k{g,u}id. 1321e5267cdSChristian Brauner */ 1331e5267cdSChristian Brauner #define AS_KUIDT(val) (kuid_t){ __vfsuid_val(val) } 1341e5267cdSChristian Brauner #define AS_KGIDT(val) (kgid_t){ __vfsgid_val(val) } 1351e5267cdSChristian Brauner 1361e5267cdSChristian Brauner #ifdef CONFIG_MULTIUSER 1371e5267cdSChristian Brauner /** 1381e5267cdSChristian Brauner * vfsgid_in_group_p() - check whether a vfsuid matches the caller's groups 1391e5267cdSChristian Brauner * @vfsgid: the mnt gid to match 1401e5267cdSChristian Brauner * 1411e5267cdSChristian Brauner * This function can be used to determine whether @vfsuid matches any of the 1421e5267cdSChristian Brauner * caller's groups. 1431e5267cdSChristian Brauner * 1441e5267cdSChristian Brauner * Return: 1 if vfsuid matches caller's groups, 0 if not. 1451e5267cdSChristian Brauner */ 1461e5267cdSChristian Brauner static inline int vfsgid_in_group_p(vfsgid_t vfsgid) 1471e5267cdSChristian Brauner { 1481e5267cdSChristian Brauner return in_group_p(AS_KGIDT(vfsgid)); 1491e5267cdSChristian Brauner } 1501e5267cdSChristian Brauner #else 1511e5267cdSChristian Brauner static inline int vfsgid_in_group_p(vfsgid_t vfsgid) 1521e5267cdSChristian Brauner { 1531e5267cdSChristian Brauner return 1; 1541e5267cdSChristian Brauner } 1551e5267cdSChristian Brauner #endif 1561e5267cdSChristian Brauner 157a793d79eSChristian Brauner /** 1581ac2a410SChristian Brauner * initial_idmapping - check whether this is the initial mapping 1591ac2a410SChristian Brauner * @ns: idmapping to check 1601ac2a410SChristian Brauner * 1611ac2a410SChristian Brauner * Check whether this is the initial mapping, mapping 0 to 0, 1 to 1, 1621ac2a410SChristian Brauner * [...], 1000 to 1000 [...]. 1631ac2a410SChristian Brauner * 1641ac2a410SChristian Brauner * Return: true if this is the initial mapping, false if not. 1651ac2a410SChristian Brauner */ 1661ac2a410SChristian Brauner static inline bool initial_idmapping(const struct user_namespace *ns) 1671ac2a410SChristian Brauner { 1681ac2a410SChristian Brauner return ns == &init_user_ns; 1691ac2a410SChristian Brauner } 1701ac2a410SChristian Brauner 1711ac2a410SChristian Brauner /** 1721ac2a410SChristian Brauner * no_idmapping - check whether we can skip remapping a kuid/gid 1731ac2a410SChristian Brauner * @mnt_userns: the mount's idmapping 1741ac2a410SChristian Brauner * @fs_userns: the filesystem's idmapping 1751ac2a410SChristian Brauner * 1761ac2a410SChristian Brauner * This function can be used to check whether a remapping between two 1771ac2a410SChristian Brauner * idmappings is required. 1781ac2a410SChristian Brauner * An idmapped mount is a mount that has an idmapping attached to it that 1791ac2a410SChristian Brauner * is different from the filsystem's idmapping and the initial idmapping. 1801ac2a410SChristian Brauner * If the initial mapping is used or the idmapping of the mount and the 1811ac2a410SChristian Brauner * filesystem are identical no remapping is required. 1821ac2a410SChristian Brauner * 1831ac2a410SChristian Brauner * Return: true if remapping can be skipped, false if not. 1841ac2a410SChristian Brauner */ 1851ac2a410SChristian Brauner static inline bool no_idmapping(const struct user_namespace *mnt_userns, 1861ac2a410SChristian Brauner const struct user_namespace *fs_userns) 1871ac2a410SChristian Brauner { 1881ac2a410SChristian Brauner return initial_idmapping(mnt_userns) || mnt_userns == fs_userns; 1891ac2a410SChristian Brauner } 1901ac2a410SChristian Brauner 1911ac2a410SChristian Brauner /** 19277940f0dSChristian Brauner * make_vfsuid - map a filesystem kuid into a mnt_userns 1931ac2a410SChristian Brauner * @mnt_userns: the mount's idmapping 1941ac2a410SChristian Brauner * @fs_userns: the filesystem's idmapping 1951ac2a410SChristian Brauner * @kuid : kuid to be mapped 1961ac2a410SChristian Brauner * 1971ac2a410SChristian Brauner * Take a @kuid and remap it from @fs_userns into @mnt_userns. Use this 1981ac2a410SChristian Brauner * function when preparing a @kuid to be reported to userspace. 1991ac2a410SChristian Brauner * 2001ac2a410SChristian Brauner * If no_idmapping() determines that this is not an idmapped mount we can 2011ac2a410SChristian Brauner * simply return @kuid unchanged. 2021ac2a410SChristian Brauner * If initial_idmapping() tells us that the filesystem is not mounted with an 2031ac2a410SChristian Brauner * idmapping we know the value of @kuid won't change when calling 2041ac2a410SChristian Brauner * from_kuid() so we can simply retrieve the value via __kuid_val() 2051ac2a410SChristian Brauner * directly. 2061ac2a410SChristian Brauner * 2071ac2a410SChristian Brauner * Return: @kuid mapped according to @mnt_userns. 2081ac2a410SChristian Brauner * If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is 2091ac2a410SChristian Brauner * returned. 2101ac2a410SChristian Brauner */ 2111e5267cdSChristian Brauner 2121e5267cdSChristian Brauner static inline vfsuid_t make_vfsuid(struct user_namespace *mnt_userns, 2131ac2a410SChristian Brauner struct user_namespace *fs_userns, 2141ac2a410SChristian Brauner kuid_t kuid) 2151ac2a410SChristian Brauner { 2161ac2a410SChristian Brauner uid_t uid; 2171ac2a410SChristian Brauner 2181ac2a410SChristian Brauner if (no_idmapping(mnt_userns, fs_userns)) 2191e5267cdSChristian Brauner return VFSUIDT_INIT(kuid); 2201ac2a410SChristian Brauner if (initial_idmapping(fs_userns)) 2211ac2a410SChristian Brauner uid = __kuid_val(kuid); 2221ac2a410SChristian Brauner else 2231ac2a410SChristian Brauner uid = from_kuid(fs_userns, kuid); 2241ac2a410SChristian Brauner if (uid == (uid_t)-1) 2251e5267cdSChristian Brauner return INVALID_VFSUID; 2261e5267cdSChristian Brauner return VFSUIDT_INIT(make_kuid(mnt_userns, uid)); 2271e5267cdSChristian Brauner } 2281e5267cdSChristian Brauner 2291ac2a410SChristian Brauner /** 23077940f0dSChristian Brauner * make_vfsgid - map a filesystem kgid into a mnt_userns 2311ac2a410SChristian Brauner * @mnt_userns: the mount's idmapping 2321ac2a410SChristian Brauner * @fs_userns: the filesystem's idmapping 2331ac2a410SChristian Brauner * @kgid : kgid to be mapped 2341ac2a410SChristian Brauner * 2351ac2a410SChristian Brauner * Take a @kgid and remap it from @fs_userns into @mnt_userns. Use this 2361ac2a410SChristian Brauner * function when preparing a @kgid to be reported to userspace. 2371ac2a410SChristian Brauner * 2381ac2a410SChristian Brauner * If no_idmapping() determines that this is not an idmapped mount we can 2391ac2a410SChristian Brauner * simply return @kgid unchanged. 2401ac2a410SChristian Brauner * If initial_idmapping() tells us that the filesystem is not mounted with an 2411ac2a410SChristian Brauner * idmapping we know the value of @kgid won't change when calling 2421ac2a410SChristian Brauner * from_kgid() so we can simply retrieve the value via __kgid_val() 2431ac2a410SChristian Brauner * directly. 2441ac2a410SChristian Brauner * 2451ac2a410SChristian Brauner * Return: @kgid mapped according to @mnt_userns. 2461ac2a410SChristian Brauner * If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is 2471ac2a410SChristian Brauner * returned. 2481ac2a410SChristian Brauner */ 2491e5267cdSChristian Brauner 2501e5267cdSChristian Brauner static inline vfsgid_t make_vfsgid(struct user_namespace *mnt_userns, 2511ac2a410SChristian Brauner struct user_namespace *fs_userns, 2521ac2a410SChristian Brauner kgid_t kgid) 2531ac2a410SChristian Brauner { 2541ac2a410SChristian Brauner gid_t gid; 2551ac2a410SChristian Brauner 2561ac2a410SChristian Brauner if (no_idmapping(mnt_userns, fs_userns)) 2571e5267cdSChristian Brauner return VFSGIDT_INIT(kgid); 2581ac2a410SChristian Brauner if (initial_idmapping(fs_userns)) 2591ac2a410SChristian Brauner gid = __kgid_val(kgid); 2601ac2a410SChristian Brauner else 2611ac2a410SChristian Brauner gid = from_kgid(fs_userns, kgid); 2621ac2a410SChristian Brauner if (gid == (gid_t)-1) 2631e5267cdSChristian Brauner return INVALID_VFSGID; 2641e5267cdSChristian Brauner return VFSGIDT_INIT(make_kgid(mnt_userns, gid)); 2651e5267cdSChristian Brauner } 2661e5267cdSChristian Brauner 2671e5267cdSChristian Brauner /** 2681e5267cdSChristian Brauner * from_vfsuid - map a vfsuid into the filesystem idmapping 2691e5267cdSChristian Brauner * @mnt_userns: the mount's idmapping 2701e5267cdSChristian Brauner * @fs_userns: the filesystem's idmapping 2711e5267cdSChristian Brauner * @vfsuid : vfsuid to be mapped 2721e5267cdSChristian Brauner * 2731e5267cdSChristian Brauner * Map @vfsuid into the filesystem idmapping. This function has to be used in 2741e5267cdSChristian Brauner * order to e.g. write @vfsuid to inode->i_uid. 2751e5267cdSChristian Brauner * 2761e5267cdSChristian Brauner * Return: @vfsuid mapped into the filesystem idmapping 2771e5267cdSChristian Brauner */ 2781e5267cdSChristian Brauner static inline kuid_t from_vfsuid(struct user_namespace *mnt_userns, 2791e5267cdSChristian Brauner struct user_namespace *fs_userns, 2801e5267cdSChristian Brauner vfsuid_t vfsuid) 2811e5267cdSChristian Brauner { 2821e5267cdSChristian Brauner uid_t uid; 2831e5267cdSChristian Brauner 2841e5267cdSChristian Brauner if (no_idmapping(mnt_userns, fs_userns)) 2851e5267cdSChristian Brauner return AS_KUIDT(vfsuid); 2861e5267cdSChristian Brauner uid = from_kuid(mnt_userns, AS_KUIDT(vfsuid)); 2871e5267cdSChristian Brauner if (uid == (uid_t)-1) 2881e5267cdSChristian Brauner return INVALID_UID; 2891e5267cdSChristian Brauner if (initial_idmapping(fs_userns)) 2901e5267cdSChristian Brauner return KUIDT_INIT(uid); 2911e5267cdSChristian Brauner return make_kuid(fs_userns, uid); 2921ac2a410SChristian Brauner } 2931ac2a410SChristian Brauner 2941ac2a410SChristian Brauner /** 2951e5267cdSChristian Brauner * vfsuid_has_fsmapping - check whether a vfsuid maps into the filesystem 2961e5267cdSChristian Brauner * @mnt_userns: the mount's idmapping 2971e5267cdSChristian Brauner * @fs_userns: the filesystem's idmapping 2981e5267cdSChristian Brauner * @vfsuid: vfsuid to be mapped 2991e5267cdSChristian Brauner * 3001e5267cdSChristian Brauner * Check whether @vfsuid has a mapping in the filesystem idmapping. Use this 3011e5267cdSChristian Brauner * function to check whether the filesystem idmapping has a mapping for 3021e5267cdSChristian Brauner * @vfsuid. 3031e5267cdSChristian Brauner * 3041e5267cdSChristian Brauner * Return: true if @vfsuid has a mapping in the filesystem, false if not. 3051e5267cdSChristian Brauner */ 3061e5267cdSChristian Brauner static inline bool vfsuid_has_fsmapping(struct user_namespace *mnt_userns, 3071e5267cdSChristian Brauner struct user_namespace *fs_userns, 3081e5267cdSChristian Brauner vfsuid_t vfsuid) 3091e5267cdSChristian Brauner { 3101e5267cdSChristian Brauner return uid_valid(from_vfsuid(mnt_userns, fs_userns, vfsuid)); 3111e5267cdSChristian Brauner } 3121e5267cdSChristian Brauner 3139c4f28ddSChristian Brauner static inline bool vfsuid_has_mapping(struct user_namespace *userns, 3149c4f28ddSChristian Brauner vfsuid_t vfsuid) 3159c4f28ddSChristian Brauner { 3169c4f28ddSChristian Brauner return from_kuid(userns, AS_KUIDT(vfsuid)) != (uid_t)-1; 3179c4f28ddSChristian Brauner } 3189c4f28ddSChristian Brauner 3191e5267cdSChristian Brauner /** 320c9fa2b07SChristian Brauner * vfsuid_into_kuid - convert vfsuid into kuid 321c9fa2b07SChristian Brauner * @vfsuid: the vfsuid to convert 322c9fa2b07SChristian Brauner * 323c9fa2b07SChristian Brauner * This can be used when a vfsuid is committed as a kuid. 324c9fa2b07SChristian Brauner * 325c9fa2b07SChristian Brauner * Return: a kuid with the value of @vfsuid 326c9fa2b07SChristian Brauner */ 327c9fa2b07SChristian Brauner static inline kuid_t vfsuid_into_kuid(vfsuid_t vfsuid) 328c9fa2b07SChristian Brauner { 329c9fa2b07SChristian Brauner return AS_KUIDT(vfsuid); 330c9fa2b07SChristian Brauner } 331c9fa2b07SChristian Brauner 332c9fa2b07SChristian Brauner /** 3331e5267cdSChristian Brauner * from_vfsgid - map a vfsgid into the filesystem idmapping 3341e5267cdSChristian Brauner * @mnt_userns: the mount's idmapping 3351e5267cdSChristian Brauner * @fs_userns: the filesystem's idmapping 3361e5267cdSChristian Brauner * @vfsgid : vfsgid to be mapped 3371e5267cdSChristian Brauner * 3381e5267cdSChristian Brauner * Map @vfsgid into the filesystem idmapping. This function has to be used in 3391e5267cdSChristian Brauner * order to e.g. write @vfsgid to inode->i_gid. 3401e5267cdSChristian Brauner * 3411e5267cdSChristian Brauner * Return: @vfsgid mapped into the filesystem idmapping 3421e5267cdSChristian Brauner */ 3431e5267cdSChristian Brauner static inline kgid_t from_vfsgid(struct user_namespace *mnt_userns, 3441e5267cdSChristian Brauner struct user_namespace *fs_userns, 3451e5267cdSChristian Brauner vfsgid_t vfsgid) 3461e5267cdSChristian Brauner { 3471e5267cdSChristian Brauner gid_t gid; 3481ac2a410SChristian Brauner 3491ac2a410SChristian Brauner if (no_idmapping(mnt_userns, fs_userns)) 3501e5267cdSChristian Brauner return AS_KGIDT(vfsgid); 3511e5267cdSChristian Brauner gid = from_kgid(mnt_userns, AS_KGIDT(vfsgid)); 3521e5267cdSChristian Brauner if (gid == (gid_t)-1) 3531e5267cdSChristian Brauner return INVALID_GID; 3541ac2a410SChristian Brauner if (initial_idmapping(fs_userns)) 3551e5267cdSChristian Brauner return KGIDT_INIT(gid); 3561e5267cdSChristian Brauner return make_kgid(fs_userns, gid); 3571ac2a410SChristian Brauner } 3581ac2a410SChristian Brauner 3591ac2a410SChristian Brauner /** 3601e5267cdSChristian Brauner * vfsgid_has_fsmapping - check whether a vfsgid maps into the filesystem 3611e5267cdSChristian Brauner * @mnt_userns: the mount's idmapping 3621e5267cdSChristian Brauner * @fs_userns: the filesystem's idmapping 3631e5267cdSChristian Brauner * @vfsgid: vfsgid to be mapped 3641e5267cdSChristian Brauner * 3651e5267cdSChristian Brauner * Check whether @vfsgid has a mapping in the filesystem idmapping. Use this 3661e5267cdSChristian Brauner * function to check whether the filesystem idmapping has a mapping for 3671e5267cdSChristian Brauner * @vfsgid. 3681e5267cdSChristian Brauner * 3691e5267cdSChristian Brauner * Return: true if @vfsgid has a mapping in the filesystem, false if not. 3701e5267cdSChristian Brauner */ 3711e5267cdSChristian Brauner static inline bool vfsgid_has_fsmapping(struct user_namespace *mnt_userns, 3721e5267cdSChristian Brauner struct user_namespace *fs_userns, 3731e5267cdSChristian Brauner vfsgid_t vfsgid) 3741e5267cdSChristian Brauner { 3751e5267cdSChristian Brauner return gid_valid(from_vfsgid(mnt_userns, fs_userns, vfsgid)); 3761ac2a410SChristian Brauner } 3771ac2a410SChristian Brauner 3789c4f28ddSChristian Brauner static inline bool vfsgid_has_mapping(struct user_namespace *userns, 3799c4f28ddSChristian Brauner vfsgid_t vfsgid) 3809c4f28ddSChristian Brauner { 3819c4f28ddSChristian Brauner return from_kgid(userns, AS_KGIDT(vfsgid)) != (gid_t)-1; 3829c4f28ddSChristian Brauner } 3839c4f28ddSChristian Brauner 3841ac2a410SChristian Brauner /** 385c9fa2b07SChristian Brauner * vfsgid_into_kgid - convert vfsgid into kgid 386c9fa2b07SChristian Brauner * @vfsgid: the vfsgid to convert 387c9fa2b07SChristian Brauner * 388c9fa2b07SChristian Brauner * This can be used when a vfsgid is committed as a kgid. 389c9fa2b07SChristian Brauner * 390c9fa2b07SChristian Brauner * Return: a kgid with the value of @vfsgid 391c9fa2b07SChristian Brauner */ 392c9fa2b07SChristian Brauner static inline kgid_t vfsgid_into_kgid(vfsgid_t vfsgid) 393c9fa2b07SChristian Brauner { 394c9fa2b07SChristian Brauner return AS_KGIDT(vfsgid); 395c9fa2b07SChristian Brauner } 396c9fa2b07SChristian Brauner 397c9fa2b07SChristian Brauner /** 398a793d79eSChristian Brauner * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns 3991ac2a410SChristian Brauner * @mnt_userns: the mount's idmapping 400209188ceSChristian Brauner * @fs_userns: the filesystem's idmapping 401a793d79eSChristian Brauner * 402a793d79eSChristian Brauner * Use this helper to initialize a new vfs or filesystem object based on 403a793d79eSChristian Brauner * the caller's fsuid. A common example is initializing the i_uid field of 404a793d79eSChristian Brauner * a newly allocated inode triggered by a creation event such as mkdir or 405a793d79eSChristian Brauner * O_CREAT. Other examples include the allocation of quotas for a specific 406a793d79eSChristian Brauner * user. 407a793d79eSChristian Brauner * 408a793d79eSChristian Brauner * Return: the caller's current fsuid mapped up according to @mnt_userns. 409a793d79eSChristian Brauner */ 410209188ceSChristian Brauner static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns, 411209188ceSChristian Brauner struct user_namespace *fs_userns) 412a793d79eSChristian Brauner { 413fc04dafdSChristian Brauner return from_vfsuid(mnt_userns, fs_userns, 414fc04dafdSChristian Brauner VFSUIDT_INIT(current_fsuid())); 415a793d79eSChristian Brauner } 416a793d79eSChristian Brauner 417a793d79eSChristian Brauner /** 418a793d79eSChristian Brauner * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns 4191ac2a410SChristian Brauner * @mnt_userns: the mount's idmapping 420209188ceSChristian Brauner * @fs_userns: the filesystem's idmapping 421a793d79eSChristian Brauner * 422a793d79eSChristian Brauner * Use this helper to initialize a new vfs or filesystem object based on 423a793d79eSChristian Brauner * the caller's fsgid. A common example is initializing the i_gid field of 424a793d79eSChristian Brauner * a newly allocated inode triggered by a creation event such as mkdir or 425a793d79eSChristian Brauner * O_CREAT. Other examples include the allocation of quotas for a specific 426a793d79eSChristian Brauner * user. 427a793d79eSChristian Brauner * 428a793d79eSChristian Brauner * Return: the caller's current fsgid mapped up according to @mnt_userns. 429a793d79eSChristian Brauner */ 430209188ceSChristian Brauner static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns, 431209188ceSChristian Brauner struct user_namespace *fs_userns) 432a793d79eSChristian Brauner { 433fc04dafdSChristian Brauner return from_vfsgid(mnt_userns, fs_userns, 434fc04dafdSChristian Brauner VFSGIDT_INIT(current_fsgid())); 435a793d79eSChristian Brauner } 436a793d79eSChristian Brauner 437a793d79eSChristian Brauner #endif /* _LINUX_MNT_IDMAPPING_H */ 438