1
2
3
4
5
6
7
8
9
10
11
12#ifndef _LINUX_CAPABILITY_H
13#define _LINUX_CAPABILITY_H
14
15#include <uapi/linux/capability.h>
16
17
18#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
19#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
20
21extern int file_caps_enabled;
22
23typedef struct kernel_cap_struct {
24 __u32 cap[_KERNEL_CAPABILITY_U32S];
25} kernel_cap_t;
26
27
28struct cpu_vfs_cap_data {
29 __u32 magic_etc;
30 kernel_cap_t permitted;
31 kernel_cap_t inheritable;
32};
33
34#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
35#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
36
37
38struct file;
39struct inode;
40struct dentry;
41struct task_struct;
42struct user_namespace;
43
44extern const kernel_cap_t __cap_empty_set;
45extern const kernel_cap_t __cap_init_eff_set;
46
47
48
49
50
51#define CAP_FOR_EACH_U32(__capi) \
52 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
68 | CAP_TO_MASK(CAP_MKNOD) \
69 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
70 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
71 | CAP_TO_MASK(CAP_FOWNER) \
72 | CAP_TO_MASK(CAP_FSETID))
73
74# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
75
76#if _KERNEL_CAPABILITY_U32S != 2
77# error Fix up hand-coded capability macro initializers
78#else
79
80#define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
81#define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
82
83# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
84# define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
85# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
86 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
87 CAP_FS_MASK_B1 } })
88# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
89 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
90 CAP_FS_MASK_B1 } })
91
92#endif
93
94# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
95
96#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
97#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
98#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
99
100#define CAP_BOP_ALL(c, a, b, OP) \
101do { \
102 unsigned __capi; \
103 CAP_FOR_EACH_U32(__capi) { \
104 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
105 } \
106} while (0)
107
108#define CAP_UOP_ALL(c, a, OP) \
109do { \
110 unsigned __capi; \
111 CAP_FOR_EACH_U32(__capi) { \
112 c.cap[__capi] = OP a.cap[__capi]; \
113 } \
114} while (0)
115
116static inline kernel_cap_t cap_combine(const kernel_cap_t a,
117 const kernel_cap_t b)
118{
119 kernel_cap_t dest;
120 CAP_BOP_ALL(dest, a, b, |);
121 return dest;
122}
123
124static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
125 const kernel_cap_t b)
126{
127 kernel_cap_t dest;
128 CAP_BOP_ALL(dest, a, b, &);
129 return dest;
130}
131
132static inline kernel_cap_t cap_drop(const kernel_cap_t a,
133 const kernel_cap_t drop)
134{
135 kernel_cap_t dest;
136 CAP_BOP_ALL(dest, a, drop, &~);
137 return dest;
138}
139
140static inline kernel_cap_t cap_invert(const kernel_cap_t c)
141{
142 kernel_cap_t dest;
143 CAP_UOP_ALL(dest, c, ~);
144 return dest;
145}
146
147static inline bool cap_isclear(const kernel_cap_t a)
148{
149 unsigned __capi;
150 CAP_FOR_EACH_U32(__capi) {
151 if (a.cap[__capi] != 0)
152 return false;
153 }
154 return true;
155}
156
157
158
159
160
161
162
163
164static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
165{
166 kernel_cap_t dest;
167 dest = cap_drop(a, set);
168 return cap_isclear(dest);
169}
170
171
172
173static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
174{
175 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
176 return cap_drop(a, __cap_fs_set);
177}
178
179static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
180 const kernel_cap_t permitted)
181{
182 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
183 return cap_combine(a,
184 cap_intersect(permitted, __cap_fs_set));
185}
186
187static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
188{
189 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
190 return cap_drop(a, __cap_fs_set);
191}
192
193static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
194 const kernel_cap_t permitted)
195{
196 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
197 return cap_combine(a,
198 cap_intersect(permitted, __cap_nfsd_set));
199}
200
201#ifdef CONFIG_MULTIUSER
202extern bool has_capability(struct task_struct *t, int cap);
203extern bool has_ns_capability(struct task_struct *t,
204 struct user_namespace *ns, int cap);
205extern bool has_capability_noaudit(struct task_struct *t, int cap);
206extern bool has_ns_capability_noaudit(struct task_struct *t,
207 struct user_namespace *ns, int cap);
208extern bool capable(int cap);
209extern bool ns_capable(struct user_namespace *ns, int cap);
210extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
211#else
212static inline bool has_capability(struct task_struct *t, int cap)
213{
214 return true;
215}
216static inline bool has_ns_capability(struct task_struct *t,
217 struct user_namespace *ns, int cap)
218{
219 return true;
220}
221static inline bool has_capability_noaudit(struct task_struct *t, int cap)
222{
223 return true;
224}
225static inline bool has_ns_capability_noaudit(struct task_struct *t,
226 struct user_namespace *ns, int cap)
227{
228 return true;
229}
230static inline bool capable(int cap)
231{
232 return true;
233}
234static inline bool ns_capable(struct user_namespace *ns, int cap)
235{
236 return true;
237}
238static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
239{
240 return true;
241}
242#endif
243extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
244extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
245extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
246extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
247
248
249extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
250
251#endif
252