1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MNT_IDMAPPING_H
3 #define _LINUX_MNT_IDMAPPING_H
5 #include <linux/types.h>
6 #include <linux/uidgid.h>
10 * Carries the initial idmapping of 0:0:4294967295 which is an identity
11 * mapping. This means that {g,u}id 0 is mapped to {g,u}id 0, {g,u}id 1 is
12 * mapped to {g,u}id 1, [...], {g,u}id 1000 to {g,u}id 1000, [...].
14 extern struct user_namespace init_user_ns;
24 static_assert(sizeof(vfsuid_t) == sizeof(kuid_t));
25 static_assert(sizeof(vfsgid_t) == sizeof(kgid_t));
26 static_assert(offsetof(vfsuid_t, val) == offsetof(kuid_t, val));
27 static_assert(offsetof(vfsgid_t, val) == offsetof(kgid_t, val));
29 #ifdef CONFIG_MULTIUSER
30 static inline uid_t __vfsuid_val(vfsuid_t uid)
35 static inline gid_t __vfsgid_val(vfsgid_t gid)
40 static inline uid_t __vfsuid_val(vfsuid_t uid)
45 static inline gid_t __vfsgid_val(vfsgid_t gid)
51 static inline bool vfsuid_valid(vfsuid_t uid)
53 return __vfsuid_val(uid) != (uid_t)-1;
56 static inline bool vfsgid_valid(vfsgid_t gid)
58 return __vfsgid_val(gid) != (gid_t)-1;
61 static inline bool vfsuid_eq(vfsuid_t left, vfsuid_t right)
63 return vfsuid_valid(left) && __vfsuid_val(left) == __vfsuid_val(right);
66 static inline bool vfsgid_eq(vfsgid_t left, vfsgid_t right)
68 return vfsgid_valid(left) && __vfsgid_val(left) == __vfsgid_val(right);
72 * vfsuid_eq_kuid - check whether kuid and vfsuid have the same value
73 * @vfsuid: the vfsuid to compare
74 * @kuid: the kuid to compare
76 * Check whether @vfsuid and @kuid have the same values.
78 * Return: true if @vfsuid and @kuid have the same value, false if not.
79 * Comparison between two invalid uids returns false.
81 static inline bool vfsuid_eq_kuid(vfsuid_t vfsuid, kuid_t kuid)
83 return vfsuid_valid(vfsuid) && __vfsuid_val(vfsuid) == __kuid_val(kuid);
87 * vfsgid_eq_kgid - check whether kgid and vfsgid have the same value
88 * @vfsgid: the vfsgid to compare
89 * @kgid: the kgid to compare
91 * Check whether @vfsgid and @kgid have the same values.
93 * Return: true if @vfsgid and @kgid have the same value, false if not.
94 * Comparison between two invalid gids returns false.
96 static inline bool vfsgid_eq_kgid(vfsgid_t vfsgid, kgid_t kgid)
98 return vfsgid_valid(vfsgid) && __vfsgid_val(vfsgid) == __kgid_val(kgid);
101 static inline bool vfsuid_gt_kuid(vfsuid_t vfsuid, kuid_t kuid)
103 return __vfsuid_val(vfsuid) > __kuid_val(kuid);
106 static inline bool vfsgid_gt_kgid(vfsgid_t vfsgid, kgid_t kgid)
108 return __vfsgid_val(vfsgid) > __kgid_val(kgid);
111 static inline bool vfsuid_lt_kuid(vfsuid_t vfsuid, kuid_t kuid)
113 return __vfsuid_val(vfsuid) < __kuid_val(kuid);
116 static inline bool vfsgid_lt_kgid(vfsgid_t vfsgid, kgid_t kgid)
118 return __vfsgid_val(vfsgid) < __kgid_val(kgid);
122 * vfs{g,u}ids are created from k{g,u}ids.
123 * We don't allow them to be created from regular {u,g}id.
125 #define VFSUIDT_INIT(val) (vfsuid_t){ __kuid_val(val) }
126 #define VFSGIDT_INIT(val) (vfsgid_t){ __kgid_val(val) }
128 #define INVALID_VFSUID VFSUIDT_INIT(INVALID_UID)
129 #define INVALID_VFSGID VFSGIDT_INIT(INVALID_GID)
132 * Allow a vfs{g,u}id to be used as a k{g,u}id where we want to compare
133 * whether the mapped value is identical to value of a k{g,u}id.
135 #define AS_KUIDT(val) (kuid_t){ __vfsuid_val(val) }
136 #define AS_KGIDT(val) (kgid_t){ __vfsgid_val(val) }
138 #ifdef CONFIG_MULTIUSER
140 * vfsgid_in_group_p() - check whether a vfsuid matches the caller's groups
141 * @vfsgid: the mnt gid to match
143 * This function can be used to determine whether @vfsuid matches any of the
146 * Return: 1 if vfsuid matches caller's groups, 0 if not.
148 static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
150 return in_group_p(AS_KGIDT(vfsgid));
153 static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
160 * initial_idmapping - check whether this is the initial mapping
161 * @ns: idmapping to check
163 * Check whether this is the initial mapping, mapping 0 to 0, 1 to 1,
164 * [...], 1000 to 1000 [...].
166 * Return: true if this is the initial mapping, false if not.
168 static inline bool initial_idmapping(const struct user_namespace *ns)
170 return ns == &init_user_ns;
174 * no_idmapping - check whether we can skip remapping a kuid/gid
175 * @mnt_userns: the mount's idmapping
176 * @fs_userns: the filesystem's idmapping
178 * This function can be used to check whether a remapping between two
179 * idmappings is required.
180 * An idmapped mount is a mount that has an idmapping attached to it that
181 * is different from the filsystem's idmapping and the initial idmapping.
182 * If the initial mapping is used or the idmapping of the mount and the
183 * filesystem are identical no remapping is required.
185 * Return: true if remapping can be skipped, false if not.
187 static inline bool no_idmapping(const struct user_namespace *mnt_userns,
188 const struct user_namespace *fs_userns)
190 return initial_idmapping(mnt_userns) || mnt_userns == fs_userns;
194 * make_vfsuid - map a filesystem kuid into a mnt_userns
195 * @mnt_userns: the mount's idmapping
196 * @fs_userns: the filesystem's idmapping
197 * @kuid : kuid to be mapped
199 * Take a @kuid and remap it from @fs_userns into @mnt_userns. Use this
200 * function when preparing a @kuid to be reported to userspace.
202 * If no_idmapping() determines that this is not an idmapped mount we can
203 * simply return @kuid unchanged.
204 * If initial_idmapping() tells us that the filesystem is not mounted with an
205 * idmapping we know the value of @kuid won't change when calling
206 * from_kuid() so we can simply retrieve the value via __kuid_val()
209 * Return: @kuid mapped according to @mnt_userns.
210 * If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is
214 static inline vfsuid_t make_vfsuid(struct user_namespace *mnt_userns,
215 struct user_namespace *fs_userns,
220 if (no_idmapping(mnt_userns, fs_userns))
221 return VFSUIDT_INIT(kuid);
222 if (initial_idmapping(fs_userns))
223 uid = __kuid_val(kuid);
225 uid = from_kuid(fs_userns, kuid);
226 if (uid == (uid_t)-1)
227 return INVALID_VFSUID;
228 return VFSUIDT_INIT(make_kuid(mnt_userns, uid));
231 static inline kuid_t mapped_kuid_fs(struct user_namespace *mnt_userns,
232 struct user_namespace *fs_userns,
235 return AS_KUIDT(make_vfsuid(mnt_userns, fs_userns, kuid));
239 * make_vfsgid - map a filesystem kgid into a mnt_userns
240 * @mnt_userns: the mount's idmapping
241 * @fs_userns: the filesystem's idmapping
242 * @kgid : kgid to be mapped
244 * Take a @kgid and remap it from @fs_userns into @mnt_userns. Use this
245 * function when preparing a @kgid to be reported to userspace.
247 * If no_idmapping() determines that this is not an idmapped mount we can
248 * simply return @kgid unchanged.
249 * If initial_idmapping() tells us that the filesystem is not mounted with an
250 * idmapping we know the value of @kgid won't change when calling
251 * from_kgid() so we can simply retrieve the value via __kgid_val()
254 * Return: @kgid mapped according to @mnt_userns.
255 * If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is
259 static inline vfsgid_t make_vfsgid(struct user_namespace *mnt_userns,
260 struct user_namespace *fs_userns,
265 if (no_idmapping(mnt_userns, fs_userns))
266 return VFSGIDT_INIT(kgid);
267 if (initial_idmapping(fs_userns))
268 gid = __kgid_val(kgid);
270 gid = from_kgid(fs_userns, kgid);
271 if (gid == (gid_t)-1)
272 return INVALID_VFSGID;
273 return VFSGIDT_INIT(make_kgid(mnt_userns, gid));
276 static inline kgid_t mapped_kgid_fs(struct user_namespace *mnt_userns,
277 struct user_namespace *fs_userns,
280 return AS_KGIDT(make_vfsgid(mnt_userns, fs_userns, kgid));
284 * from_vfsuid - map a vfsuid into the filesystem idmapping
285 * @mnt_userns: the mount's idmapping
286 * @fs_userns: the filesystem's idmapping
287 * @vfsuid : vfsuid to be mapped
289 * Map @vfsuid into the filesystem idmapping. This function has to be used in
290 * order to e.g. write @vfsuid to inode->i_uid.
292 * Return: @vfsuid mapped into the filesystem idmapping
294 static inline kuid_t from_vfsuid(struct user_namespace *mnt_userns,
295 struct user_namespace *fs_userns,
300 if (no_idmapping(mnt_userns, fs_userns))
301 return AS_KUIDT(vfsuid);
302 uid = from_kuid(mnt_userns, AS_KUIDT(vfsuid));
303 if (uid == (uid_t)-1)
305 if (initial_idmapping(fs_userns))
306 return KUIDT_INIT(uid);
307 return make_kuid(fs_userns, uid);
311 * mapped_kuid_user - map a user kuid into a mnt_userns
312 * @mnt_userns: the mount's idmapping
313 * @fs_userns: the filesystem's idmapping
314 * @kuid : kuid to be mapped
316 * Use the idmapping of @mnt_userns to remap a @kuid into @fs_userns. Use this
317 * function when preparing a @kuid to be written to disk or inode.
319 * If no_idmapping() determines that this is not an idmapped mount we can
320 * simply return @kuid unchanged.
321 * If initial_idmapping() tells us that the filesystem is not mounted with an
322 * idmapping we know the value of @kuid won't change when calling
323 * make_kuid() so we can simply retrieve the value via KUIDT_INIT()
326 * Return: @kuid mapped according to @mnt_userns.
327 * If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is
330 static inline kuid_t mapped_kuid_user(struct user_namespace *mnt_userns,
331 struct user_namespace *fs_userns,
334 return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
338 * vfsuid_has_fsmapping - check whether a vfsuid maps into the filesystem
339 * @mnt_userns: the mount's idmapping
340 * @fs_userns: the filesystem's idmapping
341 * @vfsuid: vfsuid to be mapped
343 * Check whether @vfsuid has a mapping in the filesystem idmapping. Use this
344 * function to check whether the filesystem idmapping has a mapping for
347 * Return: true if @vfsuid has a mapping in the filesystem, false if not.
349 static inline bool vfsuid_has_fsmapping(struct user_namespace *mnt_userns,
350 struct user_namespace *fs_userns,
353 return uid_valid(from_vfsuid(mnt_userns, fs_userns, vfsuid));
356 static inline bool vfsuid_has_mapping(struct user_namespace *userns,
359 return from_kuid(userns, AS_KUIDT(vfsuid)) != (uid_t)-1;
363 * vfsuid_into_kuid - convert vfsuid into kuid
364 * @vfsuid: the vfsuid to convert
366 * This can be used when a vfsuid is committed as a kuid.
368 * Return: a kuid with the value of @vfsuid
370 static inline kuid_t vfsuid_into_kuid(vfsuid_t vfsuid)
372 return AS_KUIDT(vfsuid);
376 * from_vfsgid - map a vfsgid into the filesystem idmapping
377 * @mnt_userns: the mount's idmapping
378 * @fs_userns: the filesystem's idmapping
379 * @vfsgid : vfsgid to be mapped
381 * Map @vfsgid into the filesystem idmapping. This function has to be used in
382 * order to e.g. write @vfsgid to inode->i_gid.
384 * Return: @vfsgid mapped into the filesystem idmapping
386 static inline kgid_t from_vfsgid(struct user_namespace *mnt_userns,
387 struct user_namespace *fs_userns,
392 if (no_idmapping(mnt_userns, fs_userns))
393 return AS_KGIDT(vfsgid);
394 gid = from_kgid(mnt_userns, AS_KGIDT(vfsgid));
395 if (gid == (gid_t)-1)
397 if (initial_idmapping(fs_userns))
398 return KGIDT_INIT(gid);
399 return make_kgid(fs_userns, gid);
403 * mapped_kgid_user - map a user kgid into a mnt_userns
404 * @mnt_userns: the mount's idmapping
405 * @fs_userns: the filesystem's idmapping
406 * @kgid : kgid to be mapped
408 * Use the idmapping of @mnt_userns to remap a @kgid into @fs_userns. Use this
409 * function when preparing a @kgid to be written to disk or inode.
411 * If no_idmapping() determines that this is not an idmapped mount we can
412 * simply return @kgid unchanged.
413 * If initial_idmapping() tells us that the filesystem is not mounted with an
414 * idmapping we know the value of @kgid won't change when calling
415 * make_kgid() so we can simply retrieve the value via KGIDT_INIT()
418 * Return: @kgid mapped according to @mnt_userns.
419 * If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is
422 static inline kgid_t mapped_kgid_user(struct user_namespace *mnt_userns,
423 struct user_namespace *fs_userns,
426 return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
430 * vfsgid_has_fsmapping - check whether a vfsgid maps into the filesystem
431 * @mnt_userns: the mount's idmapping
432 * @fs_userns: the filesystem's idmapping
433 * @vfsgid: vfsgid to be mapped
435 * Check whether @vfsgid has a mapping in the filesystem idmapping. Use this
436 * function to check whether the filesystem idmapping has a mapping for
439 * Return: true if @vfsgid has a mapping in the filesystem, false if not.
441 static inline bool vfsgid_has_fsmapping(struct user_namespace *mnt_userns,
442 struct user_namespace *fs_userns,
445 return gid_valid(from_vfsgid(mnt_userns, fs_userns, vfsgid));
448 static inline bool vfsgid_has_mapping(struct user_namespace *userns,
451 return from_kgid(userns, AS_KGIDT(vfsgid)) != (gid_t)-1;
455 * vfsgid_into_kgid - convert vfsgid into kgid
456 * @vfsgid: the vfsgid to convert
458 * This can be used when a vfsgid is committed as a kgid.
460 * Return: a kgid with the value of @vfsgid
462 static inline kgid_t vfsgid_into_kgid(vfsgid_t vfsgid)
464 return AS_KGIDT(vfsgid);
468 * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
469 * @mnt_userns: the mount's idmapping
470 * @fs_userns: the filesystem's idmapping
472 * Use this helper to initialize a new vfs or filesystem object based on
473 * the caller's fsuid. A common example is initializing the i_uid field of
474 * a newly allocated inode triggered by a creation event such as mkdir or
475 * O_CREAT. Other examples include the allocation of quotas for a specific
478 * Return: the caller's current fsuid mapped up according to @mnt_userns.
480 static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns,
481 struct user_namespace *fs_userns)
483 return from_vfsuid(mnt_userns, fs_userns,
484 VFSUIDT_INIT(current_fsuid()));
488 * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
489 * @mnt_userns: the mount's idmapping
490 * @fs_userns: the filesystem's idmapping
492 * Use this helper to initialize a new vfs or filesystem object based on
493 * the caller's fsgid. A common example is initializing the i_gid field of
494 * a newly allocated inode triggered by a creation event such as mkdir or
495 * O_CREAT. Other examples include the allocation of quotas for a specific
498 * Return: the caller's current fsgid mapped up according to @mnt_userns.
500 static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns,
501 struct user_namespace *fs_userns)
503 return from_vfsgid(mnt_userns, fs_userns,
504 VFSGIDT_INIT(current_fsgid()));
507 #endif /* _LINUX_MNT_IDMAPPING_H */