1
2
3
4
5
6
7
8
9
10#include <linux/audit.h>
11#include <linux/capability.h>
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
16#include <linux/pid_namespace.h>
17#include <asm/uaccess.h>
18#include "cred-internals.h"
19
20
21
22
23
24const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
25const kernel_cap_t __cap_full_set = CAP_FULL_SET;
26const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
27
28EXPORT_SYMBOL(__cap_empty_set);
29EXPORT_SYMBOL(__cap_full_set);
30EXPORT_SYMBOL(__cap_init_eff_set);
31
32#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
33int file_caps_enabled = 1;
34
35static int __init file_caps_disable(char *str)
36{
37 file_caps_enabled = 0;
38 return 1;
39}
40__setup("no_file_caps", file_caps_disable);
41#endif
42
43
44
45
46
47
48
49static void warn_legacy_capability_use(void)
50{
51 static int warned;
52 if (!warned) {
53 char name[sizeof(current->comm)];
54
55 printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
56 " (legacy support in use)\n",
57 get_task_comm(name, current));
58 warned = 1;
59 }
60}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78static void warn_deprecated_v2(void)
79{
80 static int warned;
81
82 if (!warned) {
83 char name[sizeof(current->comm)];
84
85 printk(KERN_INFO "warning: `%s' uses deprecated v2"
86 " capabilities in a way that may be insecure.\n",
87 get_task_comm(name, current));
88 warned = 1;
89 }
90}
91
92
93
94
95
96static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
97{
98 __u32 version;
99
100 if (get_user(version, &header->version))
101 return -EFAULT;
102
103 switch (version) {
104 case _LINUX_CAPABILITY_VERSION_1:
105 warn_legacy_capability_use();
106 *tocopy = _LINUX_CAPABILITY_U32S_1;
107 break;
108 case _LINUX_CAPABILITY_VERSION_2:
109 warn_deprecated_v2();
110
111
112
113 case _LINUX_CAPABILITY_VERSION_3:
114 *tocopy = _LINUX_CAPABILITY_U32S_3;
115 break;
116 default:
117 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
118 return -EFAULT;
119 return -EINVAL;
120 }
121
122 return 0;
123}
124
125
126
127
128
129
130
131
132static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
133 kernel_cap_t *pIp, kernel_cap_t *pPp)
134{
135 int ret;
136
137 if (pid && (pid != task_pid_vnr(current))) {
138 struct task_struct *target;
139
140 read_lock(&tasklist_lock);
141
142 target = find_task_by_vpid(pid);
143 if (!target)
144 ret = -ESRCH;
145 else
146 ret = security_capget(target, pEp, pIp, pPp);
147
148 read_unlock(&tasklist_lock);
149 } else
150 ret = security_capget(current, pEp, pIp, pPp);
151
152 return ret;
153}
154
155
156
157
158
159
160
161
162
163
164SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
165{
166 int ret = 0;
167 pid_t pid;
168 unsigned tocopy;
169 kernel_cap_t pE, pI, pP;
170
171 ret = cap_validate_magic(header, &tocopy);
172 if (ret != 0)
173 return ret;
174
175 if (get_user(pid, &header->pid))
176 return -EFAULT;
177
178 if (pid < 0)
179 return -EINVAL;
180
181 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
182 if (!ret) {
183 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
184 unsigned i;
185
186 for (i = 0; i < tocopy; i++) {
187 kdata[i].effective = pE.cap[i];
188 kdata[i].permitted = pP.cap[i];
189 kdata[i].inheritable = pI.cap[i];
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 if (copy_to_user(dataptr, kdata, tocopy
212 * sizeof(struct __user_cap_data_struct))) {
213 return -EFAULT;
214 }
215 }
216
217 return ret;
218}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
239{
240 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
241 unsigned i, tocopy;
242 kernel_cap_t inheritable, permitted, effective;
243 struct cred *new;
244 int ret;
245 pid_t pid;
246
247 ret = cap_validate_magic(header, &tocopy);
248 if (ret != 0)
249 return ret;
250
251 if (get_user(pid, &header->pid))
252 return -EFAULT;
253
254
255 if (pid != 0 && pid != task_pid_vnr(current))
256 return -EPERM;
257
258 if (copy_from_user(&kdata, data,
259 tocopy * sizeof(struct __user_cap_data_struct)))
260 return -EFAULT;
261
262 for (i = 0; i < tocopy; i++) {
263 effective.cap[i] = kdata[i].effective;
264 permitted.cap[i] = kdata[i].permitted;
265 inheritable.cap[i] = kdata[i].inheritable;
266 }
267 while (i < _KERNEL_CAPABILITY_U32S) {
268 effective.cap[i] = 0;
269 permitted.cap[i] = 0;
270 inheritable.cap[i] = 0;
271 i++;
272 }
273
274 new = prepare_creds();
275 if (!new)
276 return -ENOMEM;
277
278 ret = security_capset(new, current_cred(),
279 &effective, &inheritable, &permitted);
280 if (ret < 0)
281 goto error;
282
283 audit_log_capset(pid, new, current_cred());
284
285 return commit_creds(new);
286
287error:
288 abort_creds(new);
289 return ret;
290}
291
292
293
294
295
296
297
298
299
300
301
302int capable(int cap)
303{
304 if (unlikely(!cap_valid(cap))) {
305 printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
306 BUG();
307 }
308
309 if (security_capable(cap) == 0) {
310 current->flags |= PF_SUPERPRIV;
311 return 1;
312 }
313 return 0;
314}
315EXPORT_SYMBOL(capable);
316