xref: /linux/include/linux/uidgid.h (revision af6da56a)
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 
18 struct user_namespace;
19 extern struct user_namespace init_user_ns;
20 struct 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
__kuid_val(kuid_t uid)26 static inline uid_t __kuid_val(kuid_t uid)
27 {
28 	return uid.val;
29 }
30 
__kgid_val(kgid_t gid)31 static inline gid_t __kgid_val(kgid_t gid)
32 {
33 	return gid.val;
34 }
35 #else
__kuid_val(kuid_t uid)36 static inline uid_t __kuid_val(kuid_t uid)
37 {
38 	return 0;
39 }
40 
__kgid_val(kgid_t gid)41 static 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 
uid_eq(kuid_t left,kuid_t right)53 static inline bool uid_eq(kuid_t left, kuid_t right)
54 {
55 	return __kuid_val(left) == __kuid_val(right);
56 }
57 
gid_eq(kgid_t left,kgid_t right)58 static inline bool gid_eq(kgid_t left, kgid_t right)
59 {
60 	return __kgid_val(left) == __kgid_val(right);
61 }
62 
uid_gt(kuid_t left,kuid_t right)63 static inline bool uid_gt(kuid_t left, kuid_t right)
64 {
65 	return __kuid_val(left) > __kuid_val(right);
66 }
67 
gid_gt(kgid_t left,kgid_t right)68 static inline bool gid_gt(kgid_t left, kgid_t right)
69 {
70 	return __kgid_val(left) > __kgid_val(right);
71 }
72 
uid_gte(kuid_t left,kuid_t right)73 static inline bool uid_gte(kuid_t left, kuid_t right)
74 {
75 	return __kuid_val(left) >= __kuid_val(right);
76 }
77 
gid_gte(kgid_t left,kgid_t right)78 static inline bool gid_gte(kgid_t left, kgid_t right)
79 {
80 	return __kgid_val(left) >= __kgid_val(right);
81 }
82 
uid_lt(kuid_t left,kuid_t right)83 static inline bool uid_lt(kuid_t left, kuid_t right)
84 {
85 	return __kuid_val(left) < __kuid_val(right);
86 }
87 
gid_lt(kgid_t left,kgid_t right)88 static inline bool gid_lt(kgid_t left, kgid_t right)
89 {
90 	return __kgid_val(left) < __kgid_val(right);
91 }
92 
uid_lte(kuid_t left,kuid_t right)93 static inline bool uid_lte(kuid_t left, kuid_t right)
94 {
95 	return __kuid_val(left) <= __kuid_val(right);
96 }
97 
gid_lte(kgid_t left,kgid_t right)98 static inline bool gid_lte(kgid_t left, kgid_t right)
99 {
100 	return __kgid_val(left) <= __kgid_val(right);
101 }
102 
uid_valid(kuid_t uid)103 static inline bool uid_valid(kuid_t uid)
104 {
105 	return __kuid_val(uid) != (uid_t) -1;
106 }
107 
gid_valid(kgid_t gid)108 static inline bool gid_valid(kgid_t gid)
109 {
110 	return __kgid_val(gid) != (gid_t) -1;
111 }
112 
113 #ifdef CONFIG_USER_NS
114 
115 extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
116 extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
117 
118 extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
119 extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
120 extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
121 extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
122 
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)123 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
124 {
125 	return from_kuid(ns, uid) != (uid_t) -1;
126 }
127 
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)128 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
129 {
130 	return from_kgid(ns, gid) != (gid_t) -1;
131 }
132 
133 u32 map_id_down(struct uid_gid_map *map, u32 id);
134 u32 map_id_up(struct uid_gid_map *map, u32 id);
135 
136 #else
137 
make_kuid(struct user_namespace * from,uid_t uid)138 static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
139 {
140 	return KUIDT_INIT(uid);
141 }
142 
make_kgid(struct user_namespace * from,gid_t gid)143 static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
144 {
145 	return KGIDT_INIT(gid);
146 }
147 
from_kuid(struct user_namespace * to,kuid_t kuid)148 static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
149 {
150 	return __kuid_val(kuid);
151 }
152 
from_kgid(struct user_namespace * to,kgid_t kgid)153 static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
154 {
155 	return __kgid_val(kgid);
156 }
157 
from_kuid_munged(struct user_namespace * to,kuid_t kuid)158 static 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 
from_kgid_munged(struct user_namespace * to,kgid_t kgid)166 static 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 
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)174 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
175 {
176 	return uid_valid(uid);
177 }
178 
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)179 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
180 {
181 	return gid_valid(gid);
182 }
183 
map_id_down(struct uid_gid_map * map,u32 id)184 static inline u32 map_id_down(struct uid_gid_map *map, u32 id)
185 {
186 	return id;
187 }
188 
map_id_up(struct uid_gid_map * map,u32 id)189 static 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