1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_UIDGID_H
3 #define _LINUX_UIDGID_H
6 * A set of types for the internal kernel types representing uids and gids.
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.
15 #include <linux/types.h>
16 #include <linux/highuid.h>
18 struct user_namespace;
19 extern struct user_namespace init_user_ns;
30 #define KUIDT_INIT(value) (kuid_t){ value }
31 #define KGIDT_INIT(value) (kgid_t){ value }
33 #ifdef CONFIG_MULTIUSER
34 static inline uid_t __kuid_val(kuid_t uid)
39 static inline gid_t __kgid_val(kgid_t gid)
44 static inline uid_t __kuid_val(kuid_t uid)
49 static inline gid_t __kgid_val(kgid_t gid)
55 #define GLOBAL_ROOT_UID KUIDT_INIT(0)
56 #define GLOBAL_ROOT_GID KGIDT_INIT(0)
58 #define INVALID_UID KUIDT_INIT(-1)
59 #define INVALID_GID KGIDT_INIT(-1)
61 static inline bool uid_eq(kuid_t left, kuid_t right)
63 return __kuid_val(left) == __kuid_val(right);
66 static inline bool gid_eq(kgid_t left, kgid_t right)
68 return __kgid_val(left) == __kgid_val(right);
71 static inline bool uid_gt(kuid_t left, kuid_t right)
73 return __kuid_val(left) > __kuid_val(right);
76 static inline bool gid_gt(kgid_t left, kgid_t right)
78 return __kgid_val(left) > __kgid_val(right);
81 static inline bool uid_gte(kuid_t left, kuid_t right)
83 return __kuid_val(left) >= __kuid_val(right);
86 static inline bool gid_gte(kgid_t left, kgid_t right)
88 return __kgid_val(left) >= __kgid_val(right);
91 static inline bool uid_lt(kuid_t left, kuid_t right)
93 return __kuid_val(left) < __kuid_val(right);
96 static inline bool gid_lt(kgid_t left, kgid_t right)
98 return __kgid_val(left) < __kgid_val(right);
101 static inline bool uid_lte(kuid_t left, kuid_t right)
103 return __kuid_val(left) <= __kuid_val(right);
106 static inline bool gid_lte(kgid_t left, kgid_t right)
108 return __kgid_val(left) <= __kgid_val(right);
111 static inline bool uid_valid(kuid_t uid)
113 return __kuid_val(uid) != (uid_t) -1;
116 static inline bool gid_valid(kgid_t gid)
118 return __kgid_val(gid) != (gid_t) -1;
121 #ifdef CONFIG_USER_NS
123 extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
124 extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
126 extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
127 extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
128 extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
129 extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
131 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
133 return from_kuid(ns, uid) != (uid_t) -1;
136 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
138 return from_kgid(ns, gid) != (gid_t) -1;
143 static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
145 return KUIDT_INIT(uid);
148 static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
150 return KGIDT_INIT(gid);
153 static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
155 return __kuid_val(kuid);
158 static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
160 return __kgid_val(kgid);
163 static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
165 uid_t uid = from_kuid(to, kuid);
166 if (uid == (uid_t)-1)
171 static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
173 gid_t gid = from_kgid(to, kgid);
174 if (gid == (gid_t)-1)
179 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
181 return uid_valid(uid);
184 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
186 return gid_valid(gid);
189 #endif /* CONFIG_USER_NS */
191 #endif /* _LINUX_UIDGID_H */