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