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_ACCESS_TOKENID
59 #define NWEBSPAWN_UID KUIDT_INIT(3081)
60 #endif
61
62 #ifdef CONFIG_HYPERHOLD
63 #define GLOBAL_MEMMGR_UID KUIDT_INIT(1111)
64 #endif
65
66 #define INVALID_UID KUIDT_INIT(-1)
67 #define INVALID_GID KGIDT_INIT(-1)
68
uid_eq(kuid_t left,kuid_t right)69 static inline bool uid_eq(kuid_t left, kuid_t right)
70 {
71 return __kuid_val(left) == __kuid_val(right);
72 }
73
gid_eq(kgid_t left,kgid_t right)74 static inline bool gid_eq(kgid_t left, kgid_t right)
75 {
76 return __kgid_val(left) == __kgid_val(right);
77 }
78
uid_gt(kuid_t left,kuid_t right)79 static inline bool uid_gt(kuid_t left, kuid_t right)
80 {
81 return __kuid_val(left) > __kuid_val(right);
82 }
83
gid_gt(kgid_t left,kgid_t right)84 static inline bool gid_gt(kgid_t left, kgid_t right)
85 {
86 return __kgid_val(left) > __kgid_val(right);
87 }
88
uid_gte(kuid_t left,kuid_t right)89 static inline bool uid_gte(kuid_t left, kuid_t right)
90 {
91 return __kuid_val(left) >= __kuid_val(right);
92 }
93
gid_gte(kgid_t left,kgid_t right)94 static inline bool gid_gte(kgid_t left, kgid_t right)
95 {
96 return __kgid_val(left) >= __kgid_val(right);
97 }
98
uid_lt(kuid_t left,kuid_t right)99 static inline bool uid_lt(kuid_t left, kuid_t right)
100 {
101 return __kuid_val(left) < __kuid_val(right);
102 }
103
gid_lt(kgid_t left,kgid_t right)104 static inline bool gid_lt(kgid_t left, kgid_t right)
105 {
106 return __kgid_val(left) < __kgid_val(right);
107 }
108
uid_lte(kuid_t left,kuid_t right)109 static inline bool uid_lte(kuid_t left, kuid_t right)
110 {
111 return __kuid_val(left) <= __kuid_val(right);
112 }
113
gid_lte(kgid_t left,kgid_t right)114 static inline bool gid_lte(kgid_t left, kgid_t right)
115 {
116 return __kgid_val(left) <= __kgid_val(right);
117 }
118
uid_valid(kuid_t uid)119 static inline bool uid_valid(kuid_t uid)
120 {
121 return __kuid_val(uid) != (uid_t) -1;
122 }
123
gid_valid(kgid_t gid)124 static inline bool gid_valid(kgid_t gid)
125 {
126 return __kgid_val(gid) != (gid_t) -1;
127 }
128
129 #ifdef CONFIG_USER_NS
130
131 extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
132 extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
133
134 extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
135 extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
136 extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
137 extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
138
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)139 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
140 {
141 return from_kuid(ns, uid) != (uid_t) -1;
142 }
143
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)144 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
145 {
146 return from_kgid(ns, gid) != (gid_t) -1;
147 }
148
149 #else
150
make_kuid(struct user_namespace * from,uid_t uid)151 static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
152 {
153 return KUIDT_INIT(uid);
154 }
155
make_kgid(struct user_namespace * from,gid_t gid)156 static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
157 {
158 return KGIDT_INIT(gid);
159 }
160
from_kuid(struct user_namespace * to,kuid_t kuid)161 static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
162 {
163 return __kuid_val(kuid);
164 }
165
from_kgid(struct user_namespace * to,kgid_t kgid)166 static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
167 {
168 return __kgid_val(kgid);
169 }
170
from_kuid_munged(struct user_namespace * to,kuid_t kuid)171 static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
172 {
173 uid_t uid = from_kuid(to, kuid);
174 if (uid == (uid_t)-1)
175 uid = overflowuid;
176 return uid;
177 }
178
from_kgid_munged(struct user_namespace * to,kgid_t kgid)179 static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
180 {
181 gid_t gid = from_kgid(to, kgid);
182 if (gid == (gid_t)-1)
183 gid = overflowgid;
184 return gid;
185 }
186
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)187 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
188 {
189 return uid_valid(uid);
190 }
191
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)192 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
193 {
194 return gid_valid(gid);
195 }
196
197 #endif /* CONFIG_USER_NS */
198
199 #endif /* _LINUX_UIDGID_H */
200