blob: 03835522dfcb68ab830a9d38768c374a99ec131d [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
Iulia Manda28138932015-04-15 16:16:41 -070032#ifdef CONFIG_MULTIUSER
Eric W. Biederman7a4e7402011-11-14 14:29:51 -080033static inline uid_t __kuid_val(kuid_t uid)
34{
35 return uid.val;
36}
37
38static inline gid_t __kgid_val(kgid_t gid)
39{
40 return gid.val;
41}
Iulia Manda28138932015-04-15 16:16:41 -070042#else
43static inline uid_t __kuid_val(kuid_t uid)
44{
45 return 0;
46}
47
48static inline gid_t __kgid_val(kgid_t gid)
49{
50 return 0;
51}
52#endif
Eric W. Biederman7a4e7402011-11-14 14:29:51 -080053
Eric W. Biederman7a4e7402011-11-14 14:29:51 -080054#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
60static inline bool uid_eq(kuid_t left, kuid_t right)
61{
62 return __kuid_val(left) == __kuid_val(right);
63}
64
65static inline bool gid_eq(kgid_t left, kgid_t right)
66{
67 return __kgid_val(left) == __kgid_val(right);
68}
69
70static inline bool uid_gt(kuid_t left, kuid_t right)
71{
72 return __kuid_val(left) > __kuid_val(right);
73}
74
75static inline bool gid_gt(kgid_t left, kgid_t right)
76{
77 return __kgid_val(left) > __kgid_val(right);
78}
79
80static inline bool uid_gte(kuid_t left, kuid_t right)
81{
82 return __kuid_val(left) >= __kuid_val(right);
83}
84
85static inline bool gid_gte(kgid_t left, kgid_t right)
86{
87 return __kgid_val(left) >= __kgid_val(right);
88}
89
90static inline bool uid_lt(kuid_t left, kuid_t right)
91{
92 return __kuid_val(left) < __kuid_val(right);
93}
94
95static inline bool gid_lt(kgid_t left, kgid_t right)
96{
97 return __kgid_val(left) < __kgid_val(right);
98}
99
100static inline bool uid_lte(kuid_t left, kuid_t right)
101{
102 return __kuid_val(left) <= __kuid_val(right);
103}
104
105static inline bool gid_lte(kgid_t left, kgid_t right)
106{
107 return __kgid_val(left) <= __kgid_val(right);
108}
109
110static inline bool uid_valid(kuid_t uid)
111{
Josh Triplett929aa5b2015-05-14 15:17:01 -0700112 return __kuid_val(uid) != (uid_t) -1;
Eric W. Biederman7a4e7402011-11-14 14:29:51 -0800113}
114
115static inline bool gid_valid(kgid_t gid)
116{
Josh Triplett929aa5b2015-05-14 15:17:01 -0700117 return __kgid_val(gid) != (gid_t) -1;
Eric W. Biederman7a4e7402011-11-14 14:29:51 -0800118}
119
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800120#ifdef CONFIG_USER_NS
121
122extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
123extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
124
125extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
126extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
127extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
128extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
129
130static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
131{
132 return from_kuid(ns, uid) != (uid_t) -1;
133}
134
135static 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
Eric W. Biederman7a4e7402011-11-14 14:29:51 -0800142static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
143{
144 return KUIDT_INIT(uid);
145}
146
147static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
148{
149 return KGIDT_INIT(gid);
150}
151
152static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
153{
154 return __kuid_val(kuid);
155}
156
157static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
158{
159 return __kgid_val(kgid);
160}
161
162static 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
170static 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
178static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
179{
180 return true;
181}
182
183static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
184{
185 return true;
186}
187
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800188#endif /* CONFIG_USER_NS */
189
Eric W. Biederman7a4e7402011-11-14 14:29:51 -0800190#endif /* _LINUX_UIDGID_H */