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