• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/types.h>
16 #include <linux/highuid.h>
17 
18 struct user_namespace;
19 extern struct user_namespace init_user_ns;
20 
21 typedef struct {
22 	uid_t val;
23 } kuid_t;
24 
25 
26 typedef struct {
27 	gid_t val;
28 } kgid_t;
29 
30 #define KUIDT_INIT(value) (kuid_t){ value }
31 #define KGIDT_INIT(value) (kgid_t){ value }
32 
33 #ifdef CONFIG_MULTIUSER
__kuid_val(kuid_t uid)34 static inline uid_t __kuid_val(kuid_t uid)
35 {
36 	return uid.val;
37 }
38 
__kgid_val(kgid_t gid)39 static inline gid_t __kgid_val(kgid_t gid)
40 {
41 	return gid.val;
42 }
43 #else
__kuid_val(kuid_t uid)44 static inline uid_t __kuid_val(kuid_t uid)
45 {
46 	return 0;
47 }
48 
__kgid_val(kgid_t gid)49 static inline gid_t __kgid_val(kgid_t gid)
50 {
51 	return 0;
52 }
53 #endif
54 
55 #define GLOBAL_ROOT_UID KUIDT_INIT(0)
56 #define GLOBAL_ROOT_GID KGIDT_INIT(0)
57 
58 #ifdef CONFIG_HYPERHOLD
59 #define GLOBAL_MEMMGR_UID KUIDT_INIT(1111)
60 #endif
61 
62 #define INVALID_UID KUIDT_INIT(-1)
63 #define INVALID_GID KGIDT_INIT(-1)
64 
uid_eq(kuid_t left,kuid_t right)65 static inline bool uid_eq(kuid_t left, kuid_t right)
66 {
67 	return __kuid_val(left) == __kuid_val(right);
68 }
69 
gid_eq(kgid_t left,kgid_t right)70 static inline bool gid_eq(kgid_t left, kgid_t right)
71 {
72 	return __kgid_val(left) == __kgid_val(right);
73 }
74 
uid_gt(kuid_t left,kuid_t right)75 static inline bool uid_gt(kuid_t left, kuid_t right)
76 {
77 	return __kuid_val(left) > __kuid_val(right);
78 }
79 
gid_gt(kgid_t left,kgid_t right)80 static inline bool gid_gt(kgid_t left, kgid_t right)
81 {
82 	return __kgid_val(left) > __kgid_val(right);
83 }
84 
uid_gte(kuid_t left,kuid_t right)85 static inline bool uid_gte(kuid_t left, kuid_t right)
86 {
87 	return __kuid_val(left) >= __kuid_val(right);
88 }
89 
gid_gte(kgid_t left,kgid_t right)90 static inline bool gid_gte(kgid_t left, kgid_t right)
91 {
92 	return __kgid_val(left) >= __kgid_val(right);
93 }
94 
uid_lt(kuid_t left,kuid_t right)95 static inline bool uid_lt(kuid_t left, kuid_t right)
96 {
97 	return __kuid_val(left) < __kuid_val(right);
98 }
99 
gid_lt(kgid_t left,kgid_t right)100 static inline bool gid_lt(kgid_t left, kgid_t right)
101 {
102 	return __kgid_val(left) < __kgid_val(right);
103 }
104 
uid_lte(kuid_t left,kuid_t right)105 static inline bool uid_lte(kuid_t left, kuid_t right)
106 {
107 	return __kuid_val(left) <= __kuid_val(right);
108 }
109 
gid_lte(kgid_t left,kgid_t right)110 static inline bool gid_lte(kgid_t left, kgid_t right)
111 {
112 	return __kgid_val(left) <= __kgid_val(right);
113 }
114 
uid_valid(kuid_t uid)115 static inline bool uid_valid(kuid_t uid)
116 {
117 	return __kuid_val(uid) != (uid_t) -1;
118 }
119 
gid_valid(kgid_t gid)120 static inline bool gid_valid(kgid_t gid)
121 {
122 	return __kgid_val(gid) != (gid_t) -1;
123 }
124 
125 #ifdef CONFIG_USER_NS
126 
127 extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
128 extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
129 
130 extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
131 extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
132 extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
133 extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
134 
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)135 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
136 {
137 	return from_kuid(ns, uid) != (uid_t) -1;
138 }
139 
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)140 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
141 {
142 	return from_kgid(ns, gid) != (gid_t) -1;
143 }
144 
145 #else
146 
make_kuid(struct user_namespace * from,uid_t uid)147 static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
148 {
149 	return KUIDT_INIT(uid);
150 }
151 
make_kgid(struct user_namespace * from,gid_t gid)152 static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
153 {
154 	return KGIDT_INIT(gid);
155 }
156 
from_kuid(struct user_namespace * to,kuid_t kuid)157 static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
158 {
159 	return __kuid_val(kuid);
160 }
161 
from_kgid(struct user_namespace * to,kgid_t kgid)162 static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
163 {
164 	return __kgid_val(kgid);
165 }
166 
from_kuid_munged(struct user_namespace * to,kuid_t kuid)167 static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
168 {
169 	uid_t uid = from_kuid(to, kuid);
170 	if (uid == (uid_t)-1)
171 		uid = overflowuid;
172 	return uid;
173 }
174 
from_kgid_munged(struct user_namespace * to,kgid_t kgid)175 static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
176 {
177 	gid_t gid = from_kgid(to, kgid);
178 	if (gid == (gid_t)-1)
179 		gid = overflowgid;
180 	return gid;
181 }
182 
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)183 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
184 {
185 	return uid_valid(uid);
186 }
187 
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)188 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
189 {
190 	return gid_valid(gid);
191 }
192 
193 #endif /* CONFIG_USER_NS */
194 
195 #endif /* _LINUX_UIDGID_H */
196