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