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