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