1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_MNT_IDMAPPING_H 3 #define _LINUX_MNT_IDMAPPING_H 4 5 #include <linux/types.h> 6 #include <linux/uidgid.h> 7 8 struct mnt_idmap; 9 struct user_namespace; 10 11 extern struct mnt_idmap nop_mnt_idmap; 12 extern struct mnt_idmap invalid_mnt_idmap; 13 extern struct user_namespace init_user_ns; 14 15 typedef struct { 16 uid_t val; 17 } vfsuid_t; 18 19 typedef struct { 20 gid_t val; 21 } vfsgid_t; 22 23 static_assert(sizeof(vfsuid_t) == sizeof(kuid_t)); 24 static_assert(sizeof(vfsgid_t) == sizeof(kgid_t)); 25 static_assert(offsetof(vfsuid_t, val) == offsetof(kuid_t, val)); 26 static_assert(offsetof(vfsgid_t, val) == offsetof(kgid_t, val)); 27 28 static inline bool is_valid_mnt_idmap(const struct mnt_idmap *idmap) 29 { 30 return idmap != &nop_mnt_idmap && idmap != &invalid_mnt_idmap; 31 } 32 33 #ifdef CONFIG_MULTIUSER 34 static inline uid_t __vfsuid_val(vfsuid_t uid) 35 { 36 return uid.val; 37 } 38 39 static inline gid_t __vfsgid_val(vfsgid_t gid) 40 { 41 return gid.val; 42 } 43 #else 44 static inline uid_t __vfsuid_val(vfsuid_t uid) 45 { 46 return 0; 47 } 48 49 static inline gid_t __vfsgid_val(vfsgid_t gid) 50 { 51 return 0; 52 } 53 #endif 54 55 static inline bool vfsuid_valid(vfsuid_t uid) 56 { 57 return __vfsuid_val(uid) != (uid_t)-1; 58 } 59 60 static inline bool vfsgid_valid(vfsgid_t gid) 61 { 62 return __vfsgid_val(gid) != (gid_t)-1; 63 } 64 65 static inline bool vfsuid_eq(vfsuid_t left, vfsuid_t right) 66 { 67 return vfsuid_valid(left) && __vfsuid_val(left) == __vfsuid_val(right); 68 } 69 70 static inline bool vfsgid_eq(vfsgid_t left, vfsgid_t right) 71 { 72 return vfsgid_valid(left) && __vfsgid_val(left) == __vfsgid_val(right); 73 } 74 75 /** 76 * vfsuid_eq_kuid - check whether kuid and vfsuid have the same value 77 * @vfsuid: the vfsuid to compare 78 * @kuid: the kuid to compare 79 * 80 * Check whether @vfsuid and @kuid have the same values. 81 * 82 * Return: true if @vfsuid and @kuid have the same value, false if not. 83 * Comparison between two invalid uids returns false. 84 */ 85 static inline bool vfsuid_eq_kuid(vfsuid_t vfsuid, kuid_t kuid) 86 { 87 return vfsuid_valid(vfsuid) && __vfsuid_val(vfsuid) == __kuid_val(kuid); 88 } 89 90 /** 91 * vfsgid_eq_kgid - check whether kgid and vfsgid have the same value 92 * @vfsgid: the vfsgid to compare 93 * @kgid: the kgid to compare 94 * 95 * Check whether @vfsgid and @kgid have the same values. 96 * 97 * Return: true if @vfsgid and @kgid have the same value, false if not. 98 * Comparison between two invalid gids returns false. 99 */ 100 static inline bool vfsgid_eq_kgid(vfsgid_t vfsgid, kgid_t kgid) 101 { 102 return vfsgid_valid(vfsgid) && __vfsgid_val(vfsgid) == __kgid_val(kgid); 103 } 104 105 /* 106 * vfs{g,u}ids are created from k{g,u}ids. 107 * We don't allow them to be created from regular {u,g}id. 108 */ 109 #define VFSUIDT_INIT(val) (vfsuid_t){ __kuid_val(val) } 110 #define VFSGIDT_INIT(val) (vfsgid_t){ __kgid_val(val) } 111 112 #define INVALID_VFSUID VFSUIDT_INIT(INVALID_UID) 113 #define INVALID_VFSGID VFSGIDT_INIT(INVALID_GID) 114 115 /* 116 * Allow a vfs{g,u}id to be used as a k{g,u}id where we want to compare 117 * whether the mapped value is identical to value of a k{g,u}id. 118 */ 119 #define AS_KUIDT(val) (kuid_t){ __vfsuid_val(val) } 120 #define AS_KGIDT(val) (kgid_t){ __vfsgid_val(val) } 121 122 int vfsgid_in_group_p(vfsgid_t vfsgid); 123 124 struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap); 125 void mnt_idmap_put(struct mnt_idmap *idmap); 126 127 vfsuid_t make_vfsuid(struct mnt_idmap *idmap, 128 struct user_namespace *fs_userns, kuid_t kuid); 129 130 vfsgid_t make_vfsgid(struct mnt_idmap *idmap, 131 struct user_namespace *fs_userns, kgid_t kgid); 132 133 kuid_t from_vfsuid(struct mnt_idmap *idmap, 134 struct user_namespace *fs_userns, vfsuid_t vfsuid); 135 136 kgid_t from_vfsgid(struct mnt_idmap *idmap, 137 struct user_namespace *fs_userns, vfsgid_t vfsgid); 138 139 /** 140 * vfsuid_has_fsmapping - check whether a vfsuid maps into the filesystem 141 * @idmap: the mount's idmapping 142 * @fs_userns: the filesystem's idmapping 143 * @vfsuid: vfsuid to be mapped 144 * 145 * Check whether @vfsuid has a mapping in the filesystem idmapping. Use this 146 * function to check whether the filesystem idmapping has a mapping for 147 * @vfsuid. 148 * 149 * Return: true if @vfsuid has a mapping in the filesystem, false if not. 150 */ 151 static inline bool vfsuid_has_fsmapping(struct mnt_idmap *idmap, 152 struct user_namespace *fs_userns, 153 vfsuid_t vfsuid) 154 { 155 return uid_valid(from_vfsuid(idmap, fs_userns, vfsuid)); 156 } 157 158 static inline bool vfsuid_has_mapping(struct user_namespace *userns, 159 vfsuid_t vfsuid) 160 { 161 return from_kuid(userns, AS_KUIDT(vfsuid)) != (uid_t)-1; 162 } 163 164 /** 165 * vfsuid_into_kuid - convert vfsuid into kuid 166 * @vfsuid: the vfsuid to convert 167 * 168 * This can be used when a vfsuid is committed as a kuid. 169 * 170 * Return: a kuid with the value of @vfsuid 171 */ 172 static inline kuid_t vfsuid_into_kuid(vfsuid_t vfsuid) 173 { 174 return AS_KUIDT(vfsuid); 175 } 176 177 /** 178 * vfsgid_has_fsmapping - check whether a vfsgid maps into the filesystem 179 * @idmap: the mount's idmapping 180 * @fs_userns: the filesystem's idmapping 181 * @vfsgid: vfsgid to be mapped 182 * 183 * Check whether @vfsgid has a mapping in the filesystem idmapping. Use this 184 * function to check whether the filesystem idmapping has a mapping for 185 * @vfsgid. 186 * 187 * Return: true if @vfsgid has a mapping in the filesystem, false if not. 188 */ 189 static inline bool vfsgid_has_fsmapping(struct mnt_idmap *idmap, 190 struct user_namespace *fs_userns, 191 vfsgid_t vfsgid) 192 { 193 return gid_valid(from_vfsgid(idmap, fs_userns, vfsgid)); 194 } 195 196 static inline bool vfsgid_has_mapping(struct user_namespace *userns, 197 vfsgid_t vfsgid) 198 { 199 return from_kgid(userns, AS_KGIDT(vfsgid)) != (gid_t)-1; 200 } 201 202 /** 203 * vfsgid_into_kgid - convert vfsgid into kgid 204 * @vfsgid: the vfsgid to convert 205 * 206 * This can be used when a vfsgid is committed as a kgid. 207 * 208 * Return: a kgid with the value of @vfsgid 209 */ 210 static inline kgid_t vfsgid_into_kgid(vfsgid_t vfsgid) 211 { 212 return AS_KGIDT(vfsgid); 213 } 214 215 /** 216 * mapped_fsuid - return caller's fsuid mapped according to an idmapping 217 * @idmap: the mount's idmapping 218 * @fs_userns: the filesystem's idmapping 219 * 220 * Use this helper to initialize a new vfs or filesystem object based on 221 * the caller's fsuid. A common example is initializing the i_uid field of 222 * a newly allocated inode triggered by a creation event such as mkdir or 223 * O_CREAT. Other examples include the allocation of quotas for a specific 224 * user. 225 * 226 * Return: the caller's current fsuid mapped up according to @idmap. 227 */ 228 static inline kuid_t mapped_fsuid(struct mnt_idmap *idmap, 229 struct user_namespace *fs_userns) 230 { 231 return from_vfsuid(idmap, fs_userns, VFSUIDT_INIT(current_fsuid())); 232 } 233 234 /** 235 * mapped_fsgid - return caller's fsgid mapped according to an idmapping 236 * @idmap: the mount's idmapping 237 * @fs_userns: the filesystem's idmapping 238 * 239 * Use this helper to initialize a new vfs or filesystem object based on 240 * the caller's fsgid. A common example is initializing the i_gid field of 241 * a newly allocated inode triggered by a creation event such as mkdir or 242 * O_CREAT. Other examples include the allocation of quotas for a specific 243 * user. 244 * 245 * Return: the caller's current fsgid mapped up according to @idmap. 246 */ 247 static inline kgid_t mapped_fsgid(struct mnt_idmap *idmap, 248 struct user_namespace *fs_userns) 249 { 250 return from_vfsgid(idmap, fs_userns, VFSGIDT_INIT(current_fsgid())); 251 } 252 253 #endif /* _LINUX_MNT_IDMAPPING_H */ 254