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