Blame view

include/linux/uidgid.h 4.07 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #ifndef _LINUX_UIDGID_H
  #define _LINUX_UIDGID_H
  
  /*
   * A set of types for the internal kernel types representing uids and gids.
   *
   * The types defined in this header allow distinguishing which uids and gids in
   * the kernel are values used by userspace and which uid and gid values are
   * the internal kernel values.  With the addition of user namespaces the values
   * can be different.  Using the type system makes it possible for the compiler
   * to detect when we overlook these differences.
   *
   */
  #include <linux/types.h>
  #include <linux/highuid.h>
  
  struct user_namespace;
  extern struct user_namespace init_user_ns;
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
20
21
22
23
24
25
26
27
28
29
30
  typedef struct {
  	uid_t val;
  } kuid_t;
  
  
  typedef struct {
  	gid_t val;
  } kgid_t;
  
  #define KUIDT_INIT(value) (kuid_t){ value }
  #define KGIDT_INIT(value) (kgid_t){ value }
2813893f8   Iulia Manda   kernel: condition...
31
  #ifdef CONFIG_MULTIUSER
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
32
33
34
35
36
37
38
39
40
  static inline uid_t __kuid_val(kuid_t uid)
  {
  	return uid.val;
  }
  
  static inline gid_t __kgid_val(kgid_t gid)
  {
  	return gid.val;
  }
2813893f8   Iulia Manda   kernel: condition...
41
42
43
44
45
46
47
48
49
50
51
  #else
  static inline uid_t __kuid_val(kuid_t uid)
  {
  	return 0;
  }
  
  static inline gid_t __kgid_val(kgid_t gid)
  {
  	return 0;
  }
  #endif
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
52

7a4e7408c   Eric W. Biederman   userns: Add kuid_...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  #define GLOBAL_ROOT_UID KUIDT_INIT(0)
  #define GLOBAL_ROOT_GID KGIDT_INIT(0)
  
  #define INVALID_UID KUIDT_INIT(-1)
  #define INVALID_GID KGIDT_INIT(-1)
  
  static inline bool uid_eq(kuid_t left, kuid_t right)
  {
  	return __kuid_val(left) == __kuid_val(right);
  }
  
  static inline bool gid_eq(kgid_t left, kgid_t right)
  {
  	return __kgid_val(left) == __kgid_val(right);
  }
  
  static inline bool uid_gt(kuid_t left, kuid_t right)
  {
  	return __kuid_val(left) > __kuid_val(right);
  }
  
  static inline bool gid_gt(kgid_t left, kgid_t right)
  {
  	return __kgid_val(left) > __kgid_val(right);
  }
  
  static inline bool uid_gte(kuid_t left, kuid_t right)
  {
  	return __kuid_val(left) >= __kuid_val(right);
  }
  
  static inline bool gid_gte(kgid_t left, kgid_t right)
  {
  	return __kgid_val(left) >= __kgid_val(right);
  }
  
  static inline bool uid_lt(kuid_t left, kuid_t right)
  {
  	return __kuid_val(left) < __kuid_val(right);
  }
  
  static inline bool gid_lt(kgid_t left, kgid_t right)
  {
  	return __kgid_val(left) < __kgid_val(right);
  }
  
  static inline bool uid_lte(kuid_t left, kuid_t right)
  {
  	return __kuid_val(left) <= __kuid_val(right);
  }
  
  static inline bool gid_lte(kgid_t left, kgid_t right)
  {
  	return __kgid_val(left) <= __kgid_val(right);
  }
  
  static inline bool uid_valid(kuid_t uid)
  {
929aa5b25   Josh Triplett   uidgid: make uid_...
111
  	return __kuid_val(uid) != (uid_t) -1;
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
112
113
114
115
  }
  
  static inline bool gid_valid(kgid_t gid)
  {
929aa5b25   Josh Triplett   uidgid: make uid_...
116
  	return __kgid_val(gid) != (gid_t) -1;
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
117
  }
22d917d80   Eric W. Biederman   userns: Rework th...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  #ifdef CONFIG_USER_NS
  
  extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
  extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
  
  extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
  extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
  extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
  extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
  
  static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
  {
  	return from_kuid(ns, uid) != (uid_t) -1;
  }
  
  static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
  {
  	return from_kgid(ns, gid) != (gid_t) -1;
  }
  
  #else
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
  {
  	return KUIDT_INIT(uid);
  }
  
  static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
  {
  	return KGIDT_INIT(gid);
  }
  
  static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
  {
  	return __kuid_val(kuid);
  }
  
  static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
  {
  	return __kgid_val(kgid);
  }
  
  static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
  {
  	uid_t uid = from_kuid(to, kuid);
  	if (uid == (uid_t)-1)
  		uid = overflowuid;
  	return uid;
  }
  
  static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
  {
  	gid_t gid = from_kgid(to, kgid);
  	if (gid == (gid_t)-1)
  		gid = overflowgid;
  	return gid;
  }
  
  static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
  {
37b11804e   Eric W. Biederman   userns: Handle -1...
177
  	return uid_valid(uid);
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
178
179
180
181
  }
  
  static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
  {
37b11804e   Eric W. Biederman   userns: Handle -1...
182
  	return gid_valid(gid);
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
183
  }
22d917d80   Eric W. Biederman   userns: Rework th...
184
  #endif /* CONFIG_USER_NS */
7a4e7408c   Eric W. Biederman   userns: Add kuid_...
185
  #endif /* _LINUX_UIDGID_H */