xref: /openbmc/linux/include/linux/mnt_idmapping.h (revision 1e5267cd0895183e09c5bb76da85c674014285d0)
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 user_namespace;
9 /*
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, [...].
13  */
14 extern struct user_namespace init_user_ns;
15 
16 typedef struct {
17 	uid_t val;
18 } vfsuid_t;
19 
20 typedef struct {
21 	gid_t val;
22 } vfsgid_t;
23 
24 #ifdef CONFIG_MULTIUSER
25 static inline uid_t __vfsuid_val(vfsuid_t uid)
26 {
27 	return uid.val;
28 }
29 
30 static inline gid_t __vfsgid_val(vfsgid_t gid)
31 {
32 	return gid.val;
33 }
34 #else
35 static inline uid_t __vfsuid_val(vfsuid_t uid)
36 {
37 	return 0;
38 }
39 
40 static inline gid_t __vfsgid_val(vfsgid_t gid)
41 {
42 	return 0;
43 }
44 #endif
45 
46 static inline bool vfsuid_valid(vfsuid_t uid)
47 {
48 	return __vfsuid_val(uid) != (uid_t)-1;
49 }
50 
51 static inline bool vfsgid_valid(vfsgid_t gid)
52 {
53 	return __vfsgid_val(gid) != (gid_t)-1;
54 }
55 
56 static inline bool vfsuid_eq(vfsuid_t left, vfsuid_t right)
57 {
58 	return __vfsuid_val(left) == __vfsuid_val(right);
59 }
60 
61 static inline bool vfsgid_eq(vfsgid_t left, vfsgid_t right)
62 {
63 	return __vfsgid_val(left) == __vfsgid_val(right);
64 }
65 
66 /**
67  * vfsuid_eq_kuid - check whether kuid and vfsuid have the same value
68  * @kuid: the kuid to compare
69  * @vfsuid: the vfsuid to compare
70  *
71  * Check whether @kuid and @vfsuid have the same values.
72  *
73  * Return: true if @kuid and @vfsuid have the same value, false if not.
74  */
75 static inline bool vfsuid_eq_kuid(vfsuid_t vfsuid, kuid_t kuid)
76 {
77 	return __vfsuid_val(vfsuid) == __kuid_val(kuid);
78 }
79 
80 /**
81  * vfsgid_eq_kgid - check whether kgid and vfsgid have the same value
82  * @kgid: the kgid to compare
83  * @vfsgid: the vfsgid to compare
84  *
85  * Check whether @kgid and @vfsgid have the same values.
86  *
87  * Return: true if @kgid and @vfsgid have the same value, false if not.
88  */
89 static inline bool vfsgid_eq_kgid(kgid_t kgid, vfsgid_t vfsgid)
90 {
91 	return __vfsgid_val(vfsgid) == __kgid_val(kgid);
92 }
93 
94 /*
95  * vfs{g,u}ids are created from k{g,u}ids.
96  * We don't allow them to be created from regular {u,g}id.
97  */
98 #define VFSUIDT_INIT(val) (vfsuid_t){ __kuid_val(val) }
99 #define VFSGIDT_INIT(val) (vfsgid_t){ __kgid_val(val) }
100 
101 #define INVALID_VFSUID VFSUIDT_INIT(INVALID_UID)
102 #define INVALID_VFSGID VFSGIDT_INIT(INVALID_GID)
103 
104 /*
105  * Allow a vfs{g,u}id to be used as a k{g,u}id where we want to compare
106  * whether the mapped value is identical to value of a k{g,u}id.
107  */
108 #define AS_KUIDT(val) (kuid_t){ __vfsuid_val(val) }
109 #define AS_KGIDT(val) (kgid_t){ __vfsgid_val(val) }
110 
111 #ifdef CONFIG_MULTIUSER
112 /**
113  * vfsgid_in_group_p() - check whether a vfsuid matches the caller's groups
114  * @vfsgid: the mnt gid to match
115  *
116  * This function can be used to determine whether @vfsuid matches any of the
117  * caller's groups.
118  *
119  * Return: 1 if vfsuid matches caller's groups, 0 if not.
120  */
121 static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
122 {
123 	return in_group_p(AS_KGIDT(vfsgid));
124 }
125 #else
126 static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
127 {
128 	return 1;
129 }
130 #endif
131 
132 /**
133  * initial_idmapping - check whether this is the initial mapping
134  * @ns: idmapping to check
135  *
136  * Check whether this is the initial mapping, mapping 0 to 0, 1 to 1,
137  * [...], 1000 to 1000 [...].
138  *
139  * Return: true if this is the initial mapping, false if not.
140  */
141 static inline bool initial_idmapping(const struct user_namespace *ns)
142 {
143 	return ns == &init_user_ns;
144 }
145 
146 /**
147  * no_idmapping - check whether we can skip remapping a kuid/gid
148  * @mnt_userns: the mount's idmapping
149  * @fs_userns: the filesystem's idmapping
150  *
151  * This function can be used to check whether a remapping between two
152  * idmappings is required.
153  * An idmapped mount is a mount that has an idmapping attached to it that
154  * is different from the filsystem's idmapping and the initial idmapping.
155  * If the initial mapping is used or the idmapping of the mount and the
156  * filesystem are identical no remapping is required.
157  *
158  * Return: true if remapping can be skipped, false if not.
159  */
160 static inline bool no_idmapping(const struct user_namespace *mnt_userns,
161 				const struct user_namespace *fs_userns)
162 {
163 	return initial_idmapping(mnt_userns) || mnt_userns == fs_userns;
164 }
165 
166 /**
167  * mapped_kuid_fs - map a filesystem kuid into a mnt_userns
168  * @mnt_userns: the mount's idmapping
169  * @fs_userns: the filesystem's idmapping
170  * @kuid : kuid to be mapped
171  *
172  * Take a @kuid and remap it from @fs_userns into @mnt_userns. Use this
173  * function when preparing a @kuid to be reported to userspace.
174  *
175  * If no_idmapping() determines that this is not an idmapped mount we can
176  * simply return @kuid unchanged.
177  * If initial_idmapping() tells us that the filesystem is not mounted with an
178  * idmapping we know the value of @kuid won't change when calling
179  * from_kuid() so we can simply retrieve the value via __kuid_val()
180  * directly.
181  *
182  * Return: @kuid mapped according to @mnt_userns.
183  * If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is
184  * returned.
185  */
186 
187 static inline vfsuid_t make_vfsuid(struct user_namespace *mnt_userns,
188 				   struct user_namespace *fs_userns,
189 				   kuid_t kuid)
190 {
191 	uid_t uid;
192 
193 	if (no_idmapping(mnt_userns, fs_userns))
194 		return VFSUIDT_INIT(kuid);
195 	if (initial_idmapping(fs_userns))
196 		uid = __kuid_val(kuid);
197 	else
198 		uid = from_kuid(fs_userns, kuid);
199 	if (uid == (uid_t)-1)
200 		return INVALID_VFSUID;
201 	return VFSUIDT_INIT(make_kuid(mnt_userns, uid));
202 }
203 
204 static inline kuid_t mapped_kuid_fs(struct user_namespace *mnt_userns,
205 				    struct user_namespace *fs_userns,
206 				    kuid_t kuid)
207 {
208 	return AS_KUIDT(make_vfsuid(mnt_userns, fs_userns, kuid));
209 }
210 
211 /**
212  * mapped_kgid_fs - map a filesystem kgid into a mnt_userns
213  * @mnt_userns: the mount's idmapping
214  * @fs_userns: the filesystem's idmapping
215  * @kgid : kgid to be mapped
216  *
217  * Take a @kgid and remap it from @fs_userns into @mnt_userns. Use this
218  * function when preparing a @kgid to be reported to userspace.
219  *
220  * If no_idmapping() determines that this is not an idmapped mount we can
221  * simply return @kgid unchanged.
222  * If initial_idmapping() tells us that the filesystem is not mounted with an
223  * idmapping we know the value of @kgid won't change when calling
224  * from_kgid() so we can simply retrieve the value via __kgid_val()
225  * directly.
226  *
227  * Return: @kgid mapped according to @mnt_userns.
228  * If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is
229  * returned.
230  */
231 
232 static inline vfsgid_t make_vfsgid(struct user_namespace *mnt_userns,
233 				   struct user_namespace *fs_userns,
234 				   kgid_t kgid)
235 {
236 	gid_t gid;
237 
238 	if (no_idmapping(mnt_userns, fs_userns))
239 		return VFSGIDT_INIT(kgid);
240 	if (initial_idmapping(fs_userns))
241 		gid = __kgid_val(kgid);
242 	else
243 		gid = from_kgid(fs_userns, kgid);
244 	if (gid == (gid_t)-1)
245 		return INVALID_VFSGID;
246 	return VFSGIDT_INIT(make_kgid(mnt_userns, gid));
247 }
248 
249 static inline kgid_t mapped_kgid_fs(struct user_namespace *mnt_userns,
250 				    struct user_namespace *fs_userns,
251 				    kgid_t kgid)
252 {
253 	return AS_KGIDT(make_vfsgid(mnt_userns, fs_userns, kgid));
254 }
255 
256 /**
257  * from_vfsuid - map a vfsuid into the filesystem idmapping
258  * @mnt_userns: the mount's idmapping
259  * @fs_userns: the filesystem's idmapping
260  * @vfsuid : vfsuid to be mapped
261  *
262  * Map @vfsuid into the filesystem idmapping. This function has to be used in
263  * order to e.g. write @vfsuid to inode->i_uid.
264  *
265  * Return: @vfsuid mapped into the filesystem idmapping
266  */
267 static inline kuid_t from_vfsuid(struct user_namespace *mnt_userns,
268 				 struct user_namespace *fs_userns,
269 				 vfsuid_t vfsuid)
270 {
271 	uid_t uid;
272 
273 	if (no_idmapping(mnt_userns, fs_userns))
274 		return AS_KUIDT(vfsuid);
275 	uid = from_kuid(mnt_userns, AS_KUIDT(vfsuid));
276 	if (uid == (uid_t)-1)
277 		return INVALID_UID;
278 	if (initial_idmapping(fs_userns))
279 		return KUIDT_INIT(uid);
280 	return make_kuid(fs_userns, uid);
281 }
282 
283 /**
284  * mapped_kuid_user - map a user kuid into a mnt_userns
285  * @mnt_userns: the mount's idmapping
286  * @fs_userns: the filesystem's idmapping
287  * @kuid : kuid to be mapped
288  *
289  * Use the idmapping of @mnt_userns to remap a @kuid into @fs_userns. Use this
290  * function when preparing a @kuid to be written to disk or inode.
291  *
292  * If no_idmapping() determines that this is not an idmapped mount we can
293  * simply return @kuid unchanged.
294  * If initial_idmapping() tells us that the filesystem is not mounted with an
295  * idmapping we know the value of @kuid won't change when calling
296  * make_kuid() so we can simply retrieve the value via KUIDT_INIT()
297  * directly.
298  *
299  * Return: @kuid mapped according to @mnt_userns.
300  * If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is
301  * returned.
302  */
303 static inline kuid_t mapped_kuid_user(struct user_namespace *mnt_userns,
304 				      struct user_namespace *fs_userns,
305 				      kuid_t kuid)
306 {
307 	return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
308 }
309 
310 /**
311  * vfsuid_has_fsmapping - check whether a vfsuid maps into the filesystem
312  * @mnt_userns: the mount's idmapping
313  * @fs_userns: the filesystem's idmapping
314  * @vfsuid: vfsuid to be mapped
315  *
316  * Check whether @vfsuid has a mapping in the filesystem idmapping. Use this
317  * function to check whether the filesystem idmapping has a mapping for
318  * @vfsuid.
319  *
320  * Return: true if @vfsuid has a mapping in the filesystem, false if not.
321  */
322 static inline bool vfsuid_has_fsmapping(struct user_namespace *mnt_userns,
323 					struct user_namespace *fs_userns,
324 					vfsuid_t vfsuid)
325 {
326 	return uid_valid(from_vfsuid(mnt_userns, fs_userns, vfsuid));
327 }
328 
329 /**
330  * from_vfsgid - map a vfsgid into the filesystem idmapping
331  * @mnt_userns: the mount's idmapping
332  * @fs_userns: the filesystem's idmapping
333  * @vfsgid : vfsgid to be mapped
334  *
335  * Map @vfsgid into the filesystem idmapping. This function has to be used in
336  * order to e.g. write @vfsgid to inode->i_gid.
337  *
338  * Return: @vfsgid mapped into the filesystem idmapping
339  */
340 static inline kgid_t from_vfsgid(struct user_namespace *mnt_userns,
341 				 struct user_namespace *fs_userns,
342 				 vfsgid_t vfsgid)
343 {
344 	gid_t gid;
345 
346 	if (no_idmapping(mnt_userns, fs_userns))
347 		return AS_KGIDT(vfsgid);
348 	gid = from_kgid(mnt_userns, AS_KGIDT(vfsgid));
349 	if (gid == (gid_t)-1)
350 		return INVALID_GID;
351 	if (initial_idmapping(fs_userns))
352 		return KGIDT_INIT(gid);
353 	return make_kgid(fs_userns, gid);
354 }
355 
356 /**
357  * mapped_kgid_user - map a user kgid into a mnt_userns
358  * @mnt_userns: the mount's idmapping
359  * @fs_userns: the filesystem's idmapping
360  * @kgid : kgid to be mapped
361  *
362  * Use the idmapping of @mnt_userns to remap a @kgid into @fs_userns. Use this
363  * function when preparing a @kgid to be written to disk or inode.
364  *
365  * If no_idmapping() determines that this is not an idmapped mount we can
366  * simply return @kgid unchanged.
367  * If initial_idmapping() tells us that the filesystem is not mounted with an
368  * idmapping we know the value of @kgid won't change when calling
369  * make_kgid() so we can simply retrieve the value via KGIDT_INIT()
370  * directly.
371  *
372  * Return: @kgid mapped according to @mnt_userns.
373  * If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is
374  * returned.
375  */
376 static inline kgid_t mapped_kgid_user(struct user_namespace *mnt_userns,
377 				      struct user_namespace *fs_userns,
378 				      kgid_t kgid)
379 {
380 	return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
381 }
382 
383 /**
384  * vfsgid_has_fsmapping - check whether a vfsgid maps into the filesystem
385  * @mnt_userns: the mount's idmapping
386  * @fs_userns: the filesystem's idmapping
387  * @vfsgid: vfsgid to be mapped
388  *
389  * Check whether @vfsgid has a mapping in the filesystem idmapping. Use this
390  * function to check whether the filesystem idmapping has a mapping for
391  * @vfsgid.
392  *
393  * Return: true if @vfsgid has a mapping in the filesystem, false if not.
394  */
395 static inline bool vfsgid_has_fsmapping(struct user_namespace *mnt_userns,
396 					struct user_namespace *fs_userns,
397 					vfsgid_t vfsgid)
398 {
399 	return gid_valid(from_vfsgid(mnt_userns, fs_userns, vfsgid));
400 }
401 
402 /**
403  * mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
404  * @mnt_userns: the mount's idmapping
405  * @fs_userns: the filesystem's idmapping
406  *
407  * Use this helper to initialize a new vfs or filesystem object based on
408  * the caller's fsuid. A common example is initializing the i_uid field of
409  * a newly allocated inode triggered by a creation event such as mkdir or
410  * O_CREAT. Other examples include the allocation of quotas for a specific
411  * user.
412  *
413  * Return: the caller's current fsuid mapped up according to @mnt_userns.
414  */
415 static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns,
416 				  struct user_namespace *fs_userns)
417 {
418 	return mapped_kuid_user(mnt_userns, fs_userns, current_fsuid());
419 }
420 
421 /**
422  * mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
423  * @mnt_userns: the mount's idmapping
424  * @fs_userns: the filesystem's idmapping
425  *
426  * Use this helper to initialize a new vfs or filesystem object based on
427  * the caller's fsgid. A common example is initializing the i_gid field of
428  * a newly allocated inode triggered by a creation event such as mkdir or
429  * O_CREAT. Other examples include the allocation of quotas for a specific
430  * user.
431  *
432  * Return: the caller's current fsgid mapped up according to @mnt_userns.
433  */
434 static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns,
435 				  struct user_namespace *fs_userns)
436 {
437 	return mapped_kgid_user(mnt_userns, fs_userns, current_fsgid());
438 }
439 
440 #endif /* _LINUX_MNT_IDMAPPING_H */
441