1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_UIDGID_H
3#define _LINUX_UIDGID_H
4
5/*
6 * A set of types for the internal kernel types representing uids and gids.
7 *
8 * The types defined in this header allow distinguishing which uids and gids in
9 * the kernel are values used by userspace and which uid and gid values are
10 * the internal kernel values. With the addition of user namespaces the values
11 * can be different. Using the type system makes it possible for the compiler
12 * to detect when we overlook these differences.
13 *
14 */
15#include <linux/uidgid_types.h>
16#include <linux/highuid.h>
17
18struct user_namespace;
19extern struct user_namespace init_user_ns;
20struct uid_gid_map;
21
22#define KUIDT_INIT(value) (kuid_t){ value }
23#define KGIDT_INIT(value) (kgid_t){ value }
24
25#ifdef CONFIG_MULTIUSER
26static inline uid_t __kuid_val(kuid_t uid)
27{
28 return uid.val;
29}
30
31static inline gid_t __kgid_val(kgid_t gid)
32{
33 return gid.val;
34}
35#else
36static inline uid_t __kuid_val(kuid_t uid)
37{
38 return 0;
39}
40
41static inline gid_t __kgid_val(kgid_t gid)
42{
43 return 0;
44}
45#endif
46
47#define GLOBAL_ROOT_UID KUIDT_INIT(0)
48#define GLOBAL_ROOT_GID KGIDT_INIT(0)
49
50#define INVALID_UID KUIDT_INIT(-1)
51#define INVALID_GID KGIDT_INIT(-1)
52
53static inline bool uid_eq(kuid_t left, kuid_t right)
54{
55 return __kuid_val(uid: left) == __kuid_val(uid: right);
56}
57
58static inline bool gid_eq(kgid_t left, kgid_t right)
59{
60 return __kgid_val(gid: left) == __kgid_val(gid: right);
61}
62
63static inline bool uid_gt(kuid_t left, kuid_t right)
64{
65 return __kuid_val(uid: left) > __kuid_val(uid: right);
66}
67
68static inline bool gid_gt(kgid_t left, kgid_t right)
69{
70 return __kgid_val(gid: left) > __kgid_val(gid: right);
71}
72
73static inline bool uid_gte(kuid_t left, kuid_t right)
74{
75 return __kuid_val(uid: left) >= __kuid_val(uid: right);
76}
77
78static inline bool gid_gte(kgid_t left, kgid_t right)
79{
80 return __kgid_val(gid: left) >= __kgid_val(gid: right);
81}
82
83static inline bool uid_lt(kuid_t left, kuid_t right)
84{
85 return __kuid_val(uid: left) < __kuid_val(uid: right);
86}
87
88static inline bool gid_lt(kgid_t left, kgid_t right)
89{
90 return __kgid_val(gid: left) < __kgid_val(gid: right);
91}
92
93static inline bool uid_lte(kuid_t left, kuid_t right)
94{
95 return __kuid_val(uid: left) <= __kuid_val(uid: right);
96}
97
98static inline bool gid_lte(kgid_t left, kgid_t right)
99{
100 return __kgid_val(gid: left) <= __kgid_val(gid: right);
101}
102
103static inline bool uid_valid(kuid_t uid)
104{
105 return __kuid_val(uid) != (uid_t) -1;
106}
107
108static inline bool gid_valid(kgid_t gid)
109{
110 return __kgid_val(gid) != (gid_t) -1;
111}
112
113#ifdef CONFIG_USER_NS
114
115extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
116extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
117
118extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
119extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
120extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
121extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
122
123static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
124{
125 return from_kuid(to: ns, uid) != (uid_t) -1;
126}
127
128static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
129{
130 return from_kgid(to: ns, gid) != (gid_t) -1;
131}
132
133u32 map_id_down(struct uid_gid_map *map, u32 id);
134u32 map_id_up(struct uid_gid_map *map, u32 id);
135
136#else
137
138static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
139{
140 return KUIDT_INIT(uid);
141}
142
143static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
144{
145 return KGIDT_INIT(gid);
146}
147
148static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
149{
150 return __kuid_val(kuid);
151}
152
153static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
154{
155 return __kgid_val(kgid);
156}
157
158static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
159{
160 uid_t uid = from_kuid(to, kuid);
161 if (uid == (uid_t)-1)
162 uid = overflowuid;
163 return uid;
164}
165
166static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
167{
168 gid_t gid = from_kgid(to, kgid);
169 if (gid == (gid_t)-1)
170 gid = overflowgid;
171 return gid;
172}
173
174static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
175{
176 return uid_valid(uid);
177}
178
179static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
180{
181 return gid_valid(gid);
182}
183
184static inline u32 map_id_down(struct uid_gid_map *map, u32 id)
185{
186 return id;
187}
188
189static inline u32 map_id_up(struct uid_gid_map *map, u32 id)
190{
191 return id;
192}
193#endif /* CONFIG_USER_NS */
194
195#endif /* _LINUX_UIDGID_H */
196

source code of linux/include/linux/uidgid.h