Commit | Line | Data |
---|---|---|
e338d263 | 1 | /* Common capabilities, needed by capability.o and root_plug.o |
1da177e4 LT |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2 of the License, or | |
6 | * (at your option) any later version. | |
7 | * | |
8 | */ | |
9 | ||
c59ede7b | 10 | #include <linux/capability.h> |
3fc689e9 | 11 | #include <linux/audit.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/security.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/mman.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/swap.h> | |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/netlink.h> | |
23 | #include <linux/ptrace.h> | |
24 | #include <linux/xattr.h> | |
25 | #include <linux/hugetlb.h> | |
b5376771 | 26 | #include <linux/mount.h> |
b460cbc5 | 27 | #include <linux/sched.h> |
3898b1b4 AM |
28 | #include <linux/prctl.h> |
29 | #include <linux/securebits.h> | |
72c2d582 | 30 | |
1da177e4 LT |
31 | int cap_netlink_send(struct sock *sk, struct sk_buff *skb) |
32 | { | |
b6dff3ec | 33 | NETLINK_CB(skb).eff_cap = current_cap(); |
1da177e4 LT |
34 | return 0; |
35 | } | |
36 | ||
c7bdb545 | 37 | int cap_netlink_recv(struct sk_buff *skb, int cap) |
1da177e4 | 38 | { |
c7bdb545 | 39 | if (!cap_raised(NETLINK_CB(skb).eff_cap, cap)) |
1da177e4 LT |
40 | return -EPERM; |
41 | return 0; | |
42 | } | |
43 | ||
44 | EXPORT_SYMBOL(cap_netlink_recv); | |
45 | ||
a6dbb1ef AM |
46 | /* |
47 | * NOTE WELL: cap_capable() cannot be used like the kernel's capable() | |
48 | * function. That is, it has the reverse semantics: cap_capable() | |
49 | * returns 0 when a task has a capability, but the kernel's capable() | |
50 | * returns 1 for this case. | |
51 | */ | |
06112163 | 52 | int cap_capable(struct task_struct *tsk, int cap, int audit) |
1da177e4 | 53 | { |
c69e8d9c DH |
54 | __u32 cap_raised; |
55 | ||
1da177e4 | 56 | /* Derived from include/linux/sched.h:capable. */ |
c69e8d9c DH |
57 | rcu_read_lock(); |
58 | cap_raised = cap_raised(__task_cred(tsk)->cap_effective, cap); | |
59 | rcu_read_unlock(); | |
60 | return cap_raised ? 0 : -EPERM; | |
1da177e4 LT |
61 | } |
62 | ||
63 | int cap_settime(struct timespec *ts, struct timezone *tz) | |
64 | { | |
65 | if (!capable(CAP_SYS_TIME)) | |
66 | return -EPERM; | |
67 | return 0; | |
68 | } | |
69 | ||
5cd9c58f | 70 | int cap_ptrace_may_access(struct task_struct *child, unsigned int mode) |
1da177e4 | 71 | { |
c69e8d9c DH |
72 | int ret = 0; |
73 | ||
74 | rcu_read_lock(); | |
d84f4f99 DH |
75 | if (!cap_issubset(__task_cred(child)->cap_permitted, |
76 | current_cred()->cap_permitted) && | |
c69e8d9c DH |
77 | !capable(CAP_SYS_PTRACE)) |
78 | ret = -EPERM; | |
79 | rcu_read_unlock(); | |
80 | return ret; | |
5cd9c58f DH |
81 | } |
82 | ||
83 | int cap_ptrace_traceme(struct task_struct *parent) | |
84 | { | |
c69e8d9c DH |
85 | int ret = 0; |
86 | ||
87 | rcu_read_lock(); | |
d84f4f99 DH |
88 | if (!cap_issubset(current_cred()->cap_permitted, |
89 | __task_cred(parent)->cap_permitted) && | |
c69e8d9c DH |
90 | !has_capability(parent, CAP_SYS_PTRACE)) |
91 | ret = -EPERM; | |
92 | rcu_read_unlock(); | |
93 | return ret; | |
1da177e4 LT |
94 | } |
95 | ||
96 | int cap_capget (struct task_struct *target, kernel_cap_t *effective, | |
97 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | |
98 | { | |
c69e8d9c | 99 | const struct cred *cred; |
b6dff3ec | 100 | |
1da177e4 | 101 | /* Derived from kernel/capability.c:sys_capget. */ |
c69e8d9c DH |
102 | rcu_read_lock(); |
103 | cred = __task_cred(target); | |
b6dff3ec DH |
104 | *effective = cred->cap_effective; |
105 | *inheritable = cred->cap_inheritable; | |
106 | *permitted = cred->cap_permitted; | |
c69e8d9c | 107 | rcu_read_unlock(); |
1da177e4 LT |
108 | return 0; |
109 | } | |
110 | ||
72c2d582 AM |
111 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
112 | ||
72c2d582 AM |
113 | static inline int cap_inh_is_capped(void) |
114 | { | |
115 | /* | |
a6dbb1ef AM |
116 | * Return 1 if changes to the inheritable set are limited |
117 | * to the old permitted set. That is, if the current task | |
118 | * does *not* possess the CAP_SETPCAP capability. | |
72c2d582 | 119 | */ |
d84f4f99 | 120 | return cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0; |
72c2d582 AM |
121 | } |
122 | ||
1209726c AM |
123 | static inline int cap_limit_ptraced_target(void) { return 1; } |
124 | ||
72c2d582 AM |
125 | #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */ |
126 | ||
72c2d582 | 127 | static inline int cap_inh_is_capped(void) { return 1; } |
1209726c AM |
128 | static inline int cap_limit_ptraced_target(void) |
129 | { | |
130 | return !capable(CAP_SETPCAP); | |
131 | } | |
72c2d582 AM |
132 | |
133 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ | |
134 | ||
d84f4f99 DH |
135 | int cap_capset(struct cred *new, |
136 | const struct cred *old, | |
137 | const kernel_cap_t *effective, | |
138 | const kernel_cap_t *inheritable, | |
139 | const kernel_cap_t *permitted) | |
1da177e4 | 140 | { |
d84f4f99 DH |
141 | if (cap_inh_is_capped() && |
142 | !cap_issubset(*inheritable, | |
143 | cap_combine(old->cap_inheritable, | |
144 | old->cap_permitted))) | |
72c2d582 | 145 | /* incapable of using this inheritable set */ |
1da177e4 | 146 | return -EPERM; |
d84f4f99 | 147 | |
3b7391de | 148 | if (!cap_issubset(*inheritable, |
d84f4f99 DH |
149 | cap_combine(old->cap_inheritable, |
150 | old->cap_bset))) | |
3b7391de SH |
151 | /* no new pI capabilities outside bounding set */ |
152 | return -EPERM; | |
1da177e4 LT |
153 | |
154 | /* verify restrictions on target's new Permitted set */ | |
d84f4f99 | 155 | if (!cap_issubset(*permitted, old->cap_permitted)) |
1da177e4 | 156 | return -EPERM; |
1da177e4 LT |
157 | |
158 | /* verify the _new_Effective_ is a subset of the _new_Permitted_ */ | |
d84f4f99 | 159 | if (!cap_issubset(*effective, *permitted)) |
1da177e4 | 160 | return -EPERM; |
1da177e4 | 161 | |
d84f4f99 DH |
162 | new->cap_effective = *effective; |
163 | new->cap_inheritable = *inheritable; | |
164 | new->cap_permitted = *permitted; | |
1da177e4 LT |
165 | return 0; |
166 | } | |
167 | ||
b5376771 SH |
168 | static inline void bprm_clear_caps(struct linux_binprm *bprm) |
169 | { | |
a6f76f23 | 170 | cap_clear(bprm->cred->cap_permitted); |
b5376771 SH |
171 | bprm->cap_effective = false; |
172 | } | |
173 | ||
174 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES | |
175 | ||
176 | int cap_inode_need_killpriv(struct dentry *dentry) | |
177 | { | |
178 | struct inode *inode = dentry->d_inode; | |
179 | int error; | |
180 | ||
181 | if (!inode->i_op || !inode->i_op->getxattr) | |
182 | return 0; | |
183 | ||
184 | error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0); | |
185 | if (error <= 0) | |
186 | return 0; | |
187 | return 1; | |
188 | } | |
189 | ||
190 | int cap_inode_killpriv(struct dentry *dentry) | |
191 | { | |
192 | struct inode *inode = dentry->d_inode; | |
193 | ||
194 | if (!inode->i_op || !inode->i_op->removexattr) | |
195 | return 0; | |
196 | ||
197 | return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS); | |
198 | } | |
199 | ||
c0b00441 | 200 | static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps, |
a6f76f23 DH |
201 | struct linux_binprm *bprm, |
202 | bool *effective) | |
b5376771 | 203 | { |
a6f76f23 | 204 | struct cred *new = bprm->cred; |
c0b00441 EP |
205 | unsigned i; |
206 | int ret = 0; | |
207 | ||
208 | if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE) | |
a6f76f23 | 209 | *effective = true; |
c0b00441 EP |
210 | |
211 | CAP_FOR_EACH_U32(i) { | |
212 | __u32 permitted = caps->permitted.cap[i]; | |
213 | __u32 inheritable = caps->inheritable.cap[i]; | |
214 | ||
215 | /* | |
216 | * pP' = (X & fP) | (pI & fI) | |
217 | */ | |
a6f76f23 DH |
218 | new->cap_permitted.cap[i] = |
219 | (new->cap_bset.cap[i] & permitted) | | |
220 | (new->cap_inheritable.cap[i] & inheritable); | |
c0b00441 | 221 | |
a6f76f23 DH |
222 | if (permitted & ~new->cap_permitted.cap[i]) |
223 | /* insufficient to execute correctly */ | |
c0b00441 | 224 | ret = -EPERM; |
c0b00441 EP |
225 | } |
226 | ||
227 | /* | |
228 | * For legacy apps, with no internal support for recognizing they | |
229 | * do not have enough capabilities, we return an error if they are | |
230 | * missing some "forced" (aka file-permitted) capabilities. | |
231 | */ | |
a6f76f23 | 232 | return *effective ? ret : 0; |
c0b00441 EP |
233 | } |
234 | ||
235 | int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps) | |
236 | { | |
237 | struct inode *inode = dentry->d_inode; | |
b5376771 | 238 | __u32 magic_etc; |
e338d263 | 239 | unsigned tocopy, i; |
c0b00441 EP |
240 | int size; |
241 | struct vfs_cap_data caps; | |
242 | ||
243 | memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data)); | |
244 | ||
245 | if (!inode || !inode->i_op || !inode->i_op->getxattr) | |
246 | return -ENODATA; | |
247 | ||
248 | size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps, | |
249 | XATTR_CAPS_SZ); | |
a6f76f23 | 250 | if (size == -ENODATA || size == -EOPNOTSUPP) |
c0b00441 EP |
251 | /* no data, that's ok */ |
252 | return -ENODATA; | |
c0b00441 EP |
253 | if (size < 0) |
254 | return size; | |
b5376771 | 255 | |
e338d263 | 256 | if (size < sizeof(magic_etc)) |
b5376771 SH |
257 | return -EINVAL; |
258 | ||
c0b00441 | 259 | cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc); |
b5376771 | 260 | |
a6f76f23 | 261 | switch (magic_etc & VFS_CAP_REVISION_MASK) { |
e338d263 AM |
262 | case VFS_CAP_REVISION_1: |
263 | if (size != XATTR_CAPS_SZ_1) | |
264 | return -EINVAL; | |
265 | tocopy = VFS_CAP_U32_1; | |
266 | break; | |
267 | case VFS_CAP_REVISION_2: | |
268 | if (size != XATTR_CAPS_SZ_2) | |
269 | return -EINVAL; | |
270 | tocopy = VFS_CAP_U32_2; | |
271 | break; | |
b5376771 SH |
272 | default: |
273 | return -EINVAL; | |
274 | } | |
e338d263 | 275 | |
5459c164 | 276 | CAP_FOR_EACH_U32(i) { |
c0b00441 EP |
277 | if (i >= tocopy) |
278 | break; | |
279 | cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted); | |
280 | cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable); | |
e338d263 | 281 | } |
a6f76f23 | 282 | |
c0b00441 | 283 | return 0; |
b5376771 SH |
284 | } |
285 | ||
286 | /* Locate any VFS capabilities: */ | |
a6f76f23 | 287 | static int get_file_caps(struct linux_binprm *bprm, bool *effective) |
b5376771 SH |
288 | { |
289 | struct dentry *dentry; | |
290 | int rc = 0; | |
c0b00441 | 291 | struct cpu_vfs_cap_data vcaps; |
b5376771 | 292 | |
3318a386 SH |
293 | bprm_clear_caps(bprm); |
294 | ||
1f29fae2 SH |
295 | if (!file_caps_enabled) |
296 | return 0; | |
297 | ||
3318a386 | 298 | if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) |
b5376771 | 299 | return 0; |
b5376771 SH |
300 | |
301 | dentry = dget(bprm->file->f_dentry); | |
b5376771 | 302 | |
c0b00441 EP |
303 | rc = get_vfs_caps_from_disk(dentry, &vcaps); |
304 | if (rc < 0) { | |
305 | if (rc == -EINVAL) | |
306 | printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n", | |
307 | __func__, rc, bprm->filename); | |
308 | else if (rc == -ENODATA) | |
309 | rc = 0; | |
b5376771 SH |
310 | goto out; |
311 | } | |
b5376771 | 312 | |
a6f76f23 DH |
313 | rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective); |
314 | if (rc == -EINVAL) | |
315 | printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n", | |
316 | __func__, rc, bprm->filename); | |
b5376771 SH |
317 | |
318 | out: | |
319 | dput(dentry); | |
320 | if (rc) | |
321 | bprm_clear_caps(bprm); | |
322 | ||
323 | return rc; | |
324 | } | |
325 | ||
326 | #else | |
327 | int cap_inode_need_killpriv(struct dentry *dentry) | |
328 | { | |
329 | return 0; | |
330 | } | |
331 | ||
332 | int cap_inode_killpriv(struct dentry *dentry) | |
333 | { | |
334 | return 0; | |
335 | } | |
336 | ||
a6f76f23 | 337 | static inline int get_file_caps(struct linux_binprm *bprm, bool *effective) |
b5376771 SH |
338 | { |
339 | bprm_clear_caps(bprm); | |
340 | return 0; | |
341 | } | |
342 | #endif | |
343 | ||
a6f76f23 DH |
344 | /* |
345 | * set up the new credentials for an exec'd task | |
346 | */ | |
347 | int cap_bprm_set_creds(struct linux_binprm *bprm) | |
1da177e4 | 348 | { |
a6f76f23 DH |
349 | const struct cred *old = current_cred(); |
350 | struct cred *new = bprm->cred; | |
351 | bool effective; | |
b5376771 | 352 | int ret; |
1da177e4 | 353 | |
a6f76f23 DH |
354 | effective = false; |
355 | ret = get_file_caps(bprm, &effective); | |
356 | if (ret < 0) | |
357 | return ret; | |
1da177e4 | 358 | |
5459c164 AM |
359 | if (!issecure(SECURE_NOROOT)) { |
360 | /* | |
361 | * To support inheritance of root-permissions and suid-root | |
362 | * executables under compatibility mode, we override the | |
363 | * capability sets for the file. | |
364 | * | |
a6f76f23 | 365 | * If only the real uid is 0, we do not set the effective bit. |
5459c164 | 366 | */ |
a6f76f23 | 367 | if (new->euid == 0 || new->uid == 0) { |
5459c164 | 368 | /* pP' = (cap_bset & ~0) | (pI & ~0) */ |
a6f76f23 DH |
369 | new->cap_permitted = cap_combine(old->cap_bset, |
370 | old->cap_inheritable); | |
1da177e4 | 371 | } |
a6f76f23 DH |
372 | if (new->euid == 0) |
373 | effective = true; | |
1da177e4 | 374 | } |
b5376771 | 375 | |
a6f76f23 DH |
376 | /* Don't let someone trace a set[ug]id/setpcap binary with the revised |
377 | * credentials unless they have the appropriate permit | |
378 | */ | |
379 | if ((new->euid != old->uid || | |
380 | new->egid != old->gid || | |
381 | !cap_issubset(new->cap_permitted, old->cap_permitted)) && | |
382 | bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) { | |
383 | /* downgrade; they get no more than they had, and maybe less */ | |
384 | if (!capable(CAP_SETUID)) { | |
385 | new->euid = new->uid; | |
386 | new->egid = new->gid; | |
1da177e4 | 387 | } |
a6f76f23 DH |
388 | if (cap_limit_ptraced_target()) |
389 | new->cap_permitted = cap_intersect(new->cap_permitted, | |
390 | old->cap_permitted); | |
1da177e4 LT |
391 | } |
392 | ||
a6f76f23 DH |
393 | new->suid = new->fsuid = new->euid; |
394 | new->sgid = new->fsgid = new->egid; | |
1da177e4 | 395 | |
a6f76f23 DH |
396 | /* For init, we want to retain the capabilities set in the initial |
397 | * task. Thus we skip the usual capability rules | |
398 | */ | |
b460cbc5 | 399 | if (!is_global_init(current)) { |
a6f76f23 DH |
400 | if (effective) |
401 | new->cap_effective = new->cap_permitted; | |
e338d263 | 402 | else |
d84f4f99 | 403 | cap_clear(new->cap_effective); |
1da177e4 | 404 | } |
a6f76f23 | 405 | bprm->cap_effective = effective; |
1da177e4 | 406 | |
3fc689e9 EP |
407 | /* |
408 | * Audit candidate if current->cap_effective is set | |
409 | * | |
410 | * We do not bother to audit if 3 things are true: | |
411 | * 1) cap_effective has all caps | |
412 | * 2) we are root | |
413 | * 3) root is supposed to have all caps (SECURE_NOROOT) | |
414 | * Since this is just a normal root execing a process. | |
415 | * | |
416 | * Number 1 above might fail if you don't have a full bset, but I think | |
417 | * that is interesting information to audit. | |
418 | */ | |
d84f4f99 DH |
419 | if (!cap_isclear(new->cap_effective)) { |
420 | if (!cap_issubset(CAP_FULL_SET, new->cap_effective) || | |
a6f76f23 DH |
421 | new->euid != 0 || new->uid != 0 || |
422 | issecure(SECURE_NOROOT)) { | |
423 | ret = audit_log_bprm_fcaps(bprm, new, old); | |
424 | if (ret < 0) | |
425 | return ret; | |
426 | } | |
3fc689e9 | 427 | } |
1da177e4 | 428 | |
d84f4f99 | 429 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
a6f76f23 | 430 | return 0; |
1da177e4 LT |
431 | } |
432 | ||
a6f76f23 DH |
433 | /* |
434 | * determine whether a secure execution is required | |
435 | * - the creds have been committed at this point, and are no longer available | |
436 | * through bprm | |
437 | */ | |
438 | int cap_bprm_secureexec(struct linux_binprm *bprm) | |
1da177e4 | 439 | { |
c69e8d9c | 440 | const struct cred *cred = current_cred(); |
b6dff3ec DH |
441 | |
442 | if (cred->uid != 0) { | |
b5376771 SH |
443 | if (bprm->cap_effective) |
444 | return 1; | |
a6f76f23 | 445 | if (!cap_isclear(cred->cap_permitted)) |
b5376771 SH |
446 | return 1; |
447 | } | |
448 | ||
b6dff3ec DH |
449 | return (cred->euid != cred->uid || |
450 | cred->egid != cred->gid); | |
1da177e4 LT |
451 | } |
452 | ||
8f0cfa52 DH |
453 | int cap_inode_setxattr(struct dentry *dentry, const char *name, |
454 | const void *value, size_t size, int flags) | |
1da177e4 | 455 | { |
b5376771 SH |
456 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
457 | if (!capable(CAP_SETFCAP)) | |
458 | return -EPERM; | |
459 | return 0; | |
460 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
1da177e4 LT |
461 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
462 | !capable(CAP_SYS_ADMIN)) | |
463 | return -EPERM; | |
464 | return 0; | |
465 | } | |
466 | ||
8f0cfa52 | 467 | int cap_inode_removexattr(struct dentry *dentry, const char *name) |
1da177e4 | 468 | { |
b5376771 SH |
469 | if (!strcmp(name, XATTR_NAME_CAPS)) { |
470 | if (!capable(CAP_SETFCAP)) | |
471 | return -EPERM; | |
472 | return 0; | |
473 | } else if (!strncmp(name, XATTR_SECURITY_PREFIX, | |
1da177e4 LT |
474 | sizeof(XATTR_SECURITY_PREFIX) - 1) && |
475 | !capable(CAP_SYS_ADMIN)) | |
476 | return -EPERM; | |
477 | return 0; | |
478 | } | |
479 | ||
480 | /* moved from kernel/sys.c. */ | |
a6f76f23 | 481 | /* |
1da177e4 LT |
482 | * cap_emulate_setxuid() fixes the effective / permitted capabilities of |
483 | * a process after a call to setuid, setreuid, or setresuid. | |
484 | * | |
485 | * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of | |
486 | * {r,e,s}uid != 0, the permitted and effective capabilities are | |
487 | * cleared. | |
488 | * | |
489 | * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective | |
490 | * capabilities of the process are cleared. | |
491 | * | |
492 | * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective | |
493 | * capabilities are set to the permitted capabilities. | |
494 | * | |
a6f76f23 | 495 | * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should |
1da177e4 LT |
496 | * never happen. |
497 | * | |
a6f76f23 | 498 | * -astor |
1da177e4 LT |
499 | * |
500 | * cevans - New behaviour, Oct '99 | |
501 | * A process may, via prctl(), elect to keep its capabilities when it | |
502 | * calls setuid() and switches away from uid==0. Both permitted and | |
503 | * effective sets will be retained. | |
504 | * Without this change, it was impossible for a daemon to drop only some | |
505 | * of its privilege. The call to setuid(!=0) would drop all privileges! | |
506 | * Keeping uid 0 is not an option because uid 0 owns too many vital | |
507 | * files.. | |
508 | * Thanks to Olaf Kirch and Peter Benie for spotting this. | |
509 | */ | |
d84f4f99 | 510 | static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old) |
1da177e4 | 511 | { |
d84f4f99 DH |
512 | if ((old->uid == 0 || old->euid == 0 || old->suid == 0) && |
513 | (new->uid != 0 && new->euid != 0 && new->suid != 0) && | |
3898b1b4 | 514 | !issecure(SECURE_KEEP_CAPS)) { |
d84f4f99 DH |
515 | cap_clear(new->cap_permitted); |
516 | cap_clear(new->cap_effective); | |
1da177e4 | 517 | } |
d84f4f99 DH |
518 | if (old->euid == 0 && new->euid != 0) |
519 | cap_clear(new->cap_effective); | |
520 | if (old->euid != 0 && new->euid == 0) | |
521 | new->cap_effective = new->cap_permitted; | |
1da177e4 LT |
522 | } |
523 | ||
d84f4f99 | 524 | int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags) |
1da177e4 LT |
525 | { |
526 | switch (flags) { | |
527 | case LSM_SETID_RE: | |
528 | case LSM_SETID_ID: | |
529 | case LSM_SETID_RES: | |
530 | /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */ | |
d84f4f99 DH |
531 | if (!issecure(SECURE_NO_SETUID_FIXUP)) |
532 | cap_emulate_setxuid(new, old); | |
1da177e4 LT |
533 | break; |
534 | case LSM_SETID_FS: | |
d84f4f99 | 535 | /* Copied from kernel/sys.c:setfsuid. */ |
1da177e4 | 536 | |
d84f4f99 DH |
537 | /* |
538 | * FIXME - is fsuser used for all CAP_FS_MASK capabilities? | |
539 | * if not, we might be a bit too harsh here. | |
540 | */ | |
541 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { | |
542 | if (old->fsuid == 0 && new->fsuid != 0) { | |
543 | new->cap_effective = | |
544 | cap_drop_fs_set(new->cap_effective); | |
545 | } | |
546 | if (old->fsuid != 0 && new->fsuid == 0) { | |
547 | new->cap_effective = | |
548 | cap_raise_fs_set(new->cap_effective, | |
549 | new->cap_permitted); | |
1da177e4 | 550 | } |
1da177e4 | 551 | } |
d84f4f99 | 552 | break; |
1da177e4 LT |
553 | default: |
554 | return -EINVAL; | |
555 | } | |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
b5376771 SH |
560 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
561 | /* | |
562 | * Rationale: code calling task_setscheduler, task_setioprio, and | |
563 | * task_setnice, assumes that | |
564 | * . if capable(cap_sys_nice), then those actions should be allowed | |
565 | * . if not capable(cap_sys_nice), but acting on your own processes, | |
566 | * then those actions should be allowed | |
567 | * This is insufficient now since you can call code without suid, but | |
568 | * yet with increased caps. | |
569 | * So we check for increased caps on the target process. | |
570 | */ | |
de45e806 | 571 | static int cap_safe_nice(struct task_struct *p) |
b5376771 | 572 | { |
c69e8d9c DH |
573 | int is_subset; |
574 | ||
575 | rcu_read_lock(); | |
576 | is_subset = cap_issubset(__task_cred(p)->cap_permitted, | |
577 | current_cred()->cap_permitted); | |
578 | rcu_read_unlock(); | |
579 | ||
580 | if (!is_subset && !capable(CAP_SYS_NICE)) | |
b5376771 SH |
581 | return -EPERM; |
582 | return 0; | |
583 | } | |
584 | ||
585 | int cap_task_setscheduler (struct task_struct *p, int policy, | |
586 | struct sched_param *lp) | |
587 | { | |
588 | return cap_safe_nice(p); | |
589 | } | |
590 | ||
591 | int cap_task_setioprio (struct task_struct *p, int ioprio) | |
592 | { | |
593 | return cap_safe_nice(p); | |
594 | } | |
595 | ||
596 | int cap_task_setnice (struct task_struct *p, int nice) | |
597 | { | |
598 | return cap_safe_nice(p); | |
599 | } | |
600 | ||
3b7391de SH |
601 | /* |
602 | * called from kernel/sys.c for prctl(PR_CABSET_DROP) | |
603 | * done without task_capability_lock() because it introduces | |
604 | * no new races - i.e. only another task doing capget() on | |
605 | * this task could get inconsistent info. There can be no | |
606 | * racing writer bc a task can only change its own caps. | |
607 | */ | |
d84f4f99 | 608 | static long cap_prctl_drop(struct cred *new, unsigned long cap) |
3b7391de SH |
609 | { |
610 | if (!capable(CAP_SETPCAP)) | |
611 | return -EPERM; | |
612 | if (!cap_valid(cap)) | |
613 | return -EINVAL; | |
d84f4f99 DH |
614 | |
615 | cap_lower(new->cap_bset, cap); | |
3b7391de SH |
616 | return 0; |
617 | } | |
3898b1b4 | 618 | |
b5376771 SH |
619 | #else |
620 | int cap_task_setscheduler (struct task_struct *p, int policy, | |
621 | struct sched_param *lp) | |
622 | { | |
623 | return 0; | |
624 | } | |
625 | int cap_task_setioprio (struct task_struct *p, int ioprio) | |
626 | { | |
627 | return 0; | |
628 | } | |
629 | int cap_task_setnice (struct task_struct *p, int nice) | |
630 | { | |
631 | return 0; | |
632 | } | |
b5376771 SH |
633 | #endif |
634 | ||
3898b1b4 | 635 | int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
d84f4f99 | 636 | unsigned long arg4, unsigned long arg5) |
3898b1b4 | 637 | { |
d84f4f99 | 638 | struct cred *new; |
3898b1b4 AM |
639 | long error = 0; |
640 | ||
d84f4f99 DH |
641 | new = prepare_creds(); |
642 | if (!new) | |
643 | return -ENOMEM; | |
644 | ||
3898b1b4 AM |
645 | switch (option) { |
646 | case PR_CAPBSET_READ: | |
d84f4f99 | 647 | error = -EINVAL; |
3898b1b4 | 648 | if (!cap_valid(arg2)) |
d84f4f99 DH |
649 | goto error; |
650 | error = !!cap_raised(new->cap_bset, arg2); | |
651 | goto no_change; | |
652 | ||
3898b1b4 AM |
653 | #ifdef CONFIG_SECURITY_FILE_CAPABILITIES |
654 | case PR_CAPBSET_DROP: | |
d84f4f99 DH |
655 | error = cap_prctl_drop(new, arg2); |
656 | if (error < 0) | |
657 | goto error; | |
658 | goto changed; | |
3898b1b4 AM |
659 | |
660 | /* | |
661 | * The next four prctl's remain to assist with transitioning a | |
662 | * system from legacy UID=0 based privilege (when filesystem | |
663 | * capabilities are not in use) to a system using filesystem | |
664 | * capabilities only - as the POSIX.1e draft intended. | |
665 | * | |
666 | * Note: | |
667 | * | |
668 | * PR_SET_SECUREBITS = | |
669 | * issecure_mask(SECURE_KEEP_CAPS_LOCKED) | |
670 | * | issecure_mask(SECURE_NOROOT) | |
671 | * | issecure_mask(SECURE_NOROOT_LOCKED) | |
672 | * | issecure_mask(SECURE_NO_SETUID_FIXUP) | |
673 | * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED) | |
674 | * | |
675 | * will ensure that the current process and all of its | |
676 | * children will be locked into a pure | |
677 | * capability-based-privilege environment. | |
678 | */ | |
679 | case PR_SET_SECUREBITS: | |
d84f4f99 DH |
680 | error = -EPERM; |
681 | if ((((new->securebits & SECURE_ALL_LOCKS) >> 1) | |
682 | & (new->securebits ^ arg2)) /*[1]*/ | |
683 | || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ | |
684 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ | |
685 | || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/ | |
3898b1b4 AM |
686 | /* |
687 | * [1] no changing of bits that are locked | |
688 | * [2] no unlocking of locks | |
689 | * [3] no setting of unsupported bits | |
690 | * [4] doing anything requires privilege (go read about | |
691 | * the "sendmail capabilities bug") | |
692 | */ | |
d84f4f99 DH |
693 | ) |
694 | /* cannot change a locked bit */ | |
695 | goto error; | |
696 | new->securebits = arg2; | |
697 | goto changed; | |
698 | ||
3898b1b4 | 699 | case PR_GET_SECUREBITS: |
d84f4f99 DH |
700 | error = new->securebits; |
701 | goto no_change; | |
3898b1b4 AM |
702 | |
703 | #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */ | |
704 | ||
705 | case PR_GET_KEEPCAPS: | |
706 | if (issecure(SECURE_KEEP_CAPS)) | |
707 | error = 1; | |
d84f4f99 DH |
708 | goto no_change; |
709 | ||
3898b1b4 | 710 | case PR_SET_KEEPCAPS: |
d84f4f99 | 711 | error = -EINVAL; |
3898b1b4 | 712 | if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */ |
d84f4f99 DH |
713 | goto error; |
714 | error = -EPERM; | |
715 | if (issecure(SECURE_KEEP_CAPS_LOCKED)) | |
716 | goto error; | |
717 | if (arg2) | |
718 | new->securebits |= issecure_mask(SECURE_KEEP_CAPS); | |
3898b1b4 | 719 | else |
d84f4f99 DH |
720 | new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS); |
721 | goto changed; | |
3898b1b4 AM |
722 | |
723 | default: | |
724 | /* No functionality available - continue with default */ | |
d84f4f99 DH |
725 | error = -ENOSYS; |
726 | goto error; | |
3898b1b4 AM |
727 | } |
728 | ||
729 | /* Functionality provided */ | |
d84f4f99 DH |
730 | changed: |
731 | return commit_creds(new); | |
732 | ||
733 | no_change: | |
734 | error = 0; | |
735 | error: | |
736 | abort_creds(new); | |
737 | return error; | |
1da177e4 LT |
738 | } |
739 | ||
740 | int cap_syslog (int type) | |
741 | { | |
742 | if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN)) | |
743 | return -EPERM; | |
744 | return 0; | |
745 | } | |
746 | ||
34b4e4aa | 747 | int cap_vm_enough_memory(struct mm_struct *mm, long pages) |
1da177e4 LT |
748 | { |
749 | int cap_sys_admin = 0; | |
750 | ||
06112163 | 751 | if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0) |
1da177e4 | 752 | cap_sys_admin = 1; |
34b4e4aa | 753 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
1da177e4 | 754 | } |