CRED: Use RCU to access another task's creds and to release a task's own creds
[safe/jmp/linux-2.6] / security / commoncap.c
1 /* Common capabilities, needed by capability.o and root_plug.o
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
10 #include <linux/capability.h>
11 #include <linux/audit.h>
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>
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>
26 #include <linux/mount.h>
27 #include <linux/sched.h>
28 #include <linux/prctl.h>
29 #include <linux/securebits.h>
30
31 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
32 {
33         NETLINK_CB(skb).eff_cap = current_cap();
34         return 0;
35 }
36
37 int cap_netlink_recv(struct sk_buff *skb, int cap)
38 {
39         if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
40                 return -EPERM;
41         return 0;
42 }
43
44 EXPORT_SYMBOL(cap_netlink_recv);
45
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  */
52 int cap_capable(struct task_struct *tsk, int cap, int audit)
53 {
54         __u32 cap_raised;
55
56         /* Derived from include/linux/sched.h:capable. */
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;
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
70 int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
71 {
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;
81 }
82
83 int cap_ptrace_traceme(struct task_struct *parent)
84 {
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;
94 }
95
96 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
97                 kernel_cap_t *inheritable, kernel_cap_t *permitted)
98 {
99         const struct cred *cred;
100
101         /* Derived from kernel/capability.c:sys_capget. */
102         rcu_read_lock();
103         cred = __task_cred(target);
104         *effective   = cred->cap_effective;
105         *inheritable = cred->cap_inheritable;
106         *permitted   = cred->cap_permitted;
107         rcu_read_unlock();
108         return 0;
109 }
110
111 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
112
113 static inline int cap_inh_is_capped(void)
114 {
115         /*
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.
119          */
120         return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
121 }
122
123 static inline int cap_limit_ptraced_target(void) { return 1; }
124
125 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
126
127 static inline int cap_inh_is_capped(void) { return 1; }
128 static inline int cap_limit_ptraced_target(void)
129 {
130         return !capable(CAP_SETPCAP);
131 }
132
133 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
134
135 int cap_capset_check(const kernel_cap_t *effective,
136                      const kernel_cap_t *inheritable,
137                      const kernel_cap_t *permitted)
138 {
139         const struct cred *cred = current->cred;
140
141         if (cap_inh_is_capped()
142             && !cap_issubset(*inheritable,
143                              cap_combine(cred->cap_inheritable,
144                                          cred->cap_permitted))) {
145                 /* incapable of using this inheritable set */
146                 return -EPERM;
147         }
148         if (!cap_issubset(*inheritable,
149                            cap_combine(cred->cap_inheritable,
150                                        cred->cap_bset))) {
151                 /* no new pI capabilities outside bounding set */
152                 return -EPERM;
153         }
154
155         /* verify restrictions on target's new Permitted set */
156         if (!cap_issubset (*permitted,
157                            cap_combine (cred->cap_permitted,
158                                         cred->cap_permitted))) {
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
170 void cap_capset_set(const kernel_cap_t *effective,
171                     const kernel_cap_t *inheritable,
172                     const kernel_cap_t *permitted)
173 {
174         struct cred *cred = current->cred;
175
176         cred->cap_effective   = *effective;
177         cred->cap_inheritable = *inheritable;
178         cred->cap_permitted   = *permitted;
179 }
180
181 static inline void bprm_clear_caps(struct linux_binprm *bprm)
182 {
183         cap_clear(bprm->cap_post_exec_permitted);
184         bprm->cap_effective = false;
185 }
186
187 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
188
189 int 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
203 int 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
213 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
214                                           struct linux_binprm *bprm)
215 {
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] =
232                         (current->cred->cap_bset.cap[i] & permitted) |
233                         (current->cred->cap_inheritable.cap[i] & inheritable);
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
251 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
252 {
253         struct inode *inode = dentry->d_inode;
254         __u32 magic_etc;
255         unsigned tocopy, i;
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;
272
273         if (size < sizeof(magic_etc))
274                 return -EINVAL;
275
276         cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
277
278         switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
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;
289         default:
290                 return -EINVAL;
291         }
292
293         CAP_FOR_EACH_U32(i) {
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);
298         }
299         return 0;
300 }
301
302 /* Locate any VFS capabilities: */
303 static int get_file_caps(struct linux_binprm *bprm)
304 {
305         struct dentry *dentry;
306         int rc = 0;
307         struct cpu_vfs_cap_data vcaps;
308
309         bprm_clear_caps(bprm);
310
311         if (!file_caps_enabled)
312                 return 0;
313
314         if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
315                 return 0;
316
317         dentry = dget(bprm->file->f_dentry);
318
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;
326                 goto out;
327         }
328
329         rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
330
331 out:
332         dput(dentry);
333         if (rc)
334                 bprm_clear_caps(bprm);
335
336         return rc;
337 }
338
339 #else
340 int cap_inode_need_killpriv(struct dentry *dentry)
341 {
342         return 0;
343 }
344
345 int cap_inode_killpriv(struct dentry *dentry)
346 {
347         return 0;
348 }
349
350 static inline int get_file_caps(struct linux_binprm *bprm)
351 {
352         bprm_clear_caps(bprm);
353         return 0;
354 }
355 #endif
356
357 int cap_bprm_set_security (struct linux_binprm *bprm)
358 {
359         int ret;
360
361         ret = get_file_caps(bprm);
362
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                  */
372                 if (bprm->e_uid == 0 || current_uid() == 0) {
373                         /* pP' = (cap_bset & ~0) | (pI & ~0) */
374                         bprm->cap_post_exec_permitted = cap_combine(
375                                 current->cred->cap_bset,
376                                 current->cred->cap_inheritable);
377                         bprm->cap_effective = (bprm->e_uid == 0);
378                         ret = 0;
379                 }
380         }
381
382         return ret;
383 }
384
385 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
386 {
387         struct cred *cred = current->cred;
388
389         if (bprm->e_uid != cred->uid || bprm->e_gid != cred->gid ||
390             !cap_issubset(bprm->cap_post_exec_permitted,
391                           cred->cap_permitted)) {
392                 set_dumpable(current->mm, suid_dumpable);
393                 current->pdeath_signal = 0;
394
395                 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
396                         if (!capable(CAP_SETUID)) {
397                                 bprm->e_uid = cred->uid;
398                                 bprm->e_gid = cred->gid;
399                         }
400                         if (cap_limit_ptraced_target()) {
401                                 bprm->cap_post_exec_permitted = cap_intersect(
402                                         bprm->cap_post_exec_permitted,
403                                         cred->cap_permitted);
404                         }
405                 }
406         }
407
408         cred->suid = cred->euid = cred->fsuid = bprm->e_uid;
409         cred->sgid = cred->egid = cred->fsgid = bprm->e_gid;
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 */
414         if (!is_global_init(current)) {
415                 cred->cap_permitted = bprm->cap_post_exec_permitted;
416                 if (bprm->cap_effective)
417                         cred->cap_effective = bprm->cap_post_exec_permitted;
418                 else
419                         cap_clear(cred->cap_effective);
420         }
421
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          */
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) ||
437                     issecure(SECURE_NOROOT))
438                         audit_log_bprm_fcaps(bprm, &cred->cap_permitted,
439                                              &cred->cap_effective);
440         }
441
442         cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
443 }
444
445 int cap_bprm_secureexec (struct linux_binprm *bprm)
446 {
447         const struct cred *cred = current_cred();
448
449         if (cred->uid != 0) {
450                 if (bprm->cap_effective)
451                         return 1;
452                 if (!cap_isclear(bprm->cap_post_exec_permitted))
453                         return 1;
454         }
455
456         return (cred->euid != cred->uid ||
457                 cred->egid != cred->gid);
458 }
459
460 int cap_inode_setxattr(struct dentry *dentry, const char *name,
461                        const void *value, size_t size, int flags)
462 {
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,
468                      sizeof(XATTR_SECURITY_PREFIX) - 1)  &&
469             !capable(CAP_SYS_ADMIN))
470                 return -EPERM;
471         return 0;
472 }
473
474 int cap_inode_removexattr(struct dentry *dentry, const char *name)
475 {
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,
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  */
517 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
518                                         int old_suid)
519 {
520         struct cred *cred = current->cred;
521
522         if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
523             (cred->uid != 0 && cred->euid != 0 && cred->suid != 0) &&
524             !issecure(SECURE_KEEP_CAPS)) {
525                 cap_clear(cred->cap_permitted);
526                 cap_clear(cred->cap_effective);
527         }
528         if (old_euid == 0 && cred->euid != 0) {
529                 cap_clear(cred->cap_effective);
530         }
531         if (old_euid != 0 && cred->euid == 0) {
532                 cred->cap_effective = cred->cap_permitted;
533         }
534 }
535
536 int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
537                           int flags)
538 {
539         struct cred *cred = current->cred;
540
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)) {
562                                 if (old_fsuid == 0 && cred->fsuid != 0) {
563                                         cred->cap_effective =
564                                                 cap_drop_fs_set(
565                                                         cred->cap_effective);
566                                 }
567                                 if (old_fsuid != 0 && cred->fsuid == 0) {
568                                         cred->cap_effective =
569                                                 cap_raise_fs_set(
570                                                     cred->cap_effective,
571                                                     cred->cap_permitted);
572                                 }
573                         }
574                         break;
575                 }
576         default:
577                 return -EINVAL;
578         }
579
580         return 0;
581 }
582
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  */
594 static int cap_safe_nice(struct task_struct *p)
595 {
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))
604                 return -EPERM;
605         return 0;
606 }
607
608 int cap_task_setscheduler (struct task_struct *p, int policy,
609                            struct sched_param *lp)
610 {
611         return cap_safe_nice(p);
612 }
613
614 int cap_task_setioprio (struct task_struct *p, int ioprio)
615 {
616         return cap_safe_nice(p);
617 }
618
619 int cap_task_setnice (struct task_struct *p, int nice)
620 {
621         return cap_safe_nice(p);
622 }
623
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  */
631 static long cap_prctl_drop(unsigned long cap)
632 {
633         if (!capable(CAP_SETPCAP))
634                 return -EPERM;
635         if (!cap_valid(cap))
636                 return -EINVAL;
637         cap_lower(current->cred->cap_bset, cap);
638         return 0;
639 }
640
641 #else
642 int cap_task_setscheduler (struct task_struct *p, int policy,
643                            struct sched_param *lp)
644 {
645         return 0;
646 }
647 int cap_task_setioprio (struct task_struct *p, int ioprio)
648 {
649         return 0;
650 }
651 int cap_task_setnice (struct task_struct *p, int nice)
652 {
653         return 0;
654 }
655 #endif
656
657 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
658                    unsigned long arg4, unsigned long arg5, long *rc_p)
659 {
660         struct cred *cred = current_cred();
661         long error = 0;
662
663         switch (option) {
664         case PR_CAPBSET_READ:
665                 if (!cap_valid(arg2))
666                         error = -EINVAL;
667                 else
668                         error = !!cap_raised(cred->cap_bset, arg2);
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:
695                 if ((((cred->securebits & SECURE_ALL_LOCKS) >> 1)
696                      & (cred->securebits ^ arg2))                  /*[1]*/
697                     || ((cred->securebits & SECURE_ALL_LOCKS
698                          & ~arg2))                                    /*[2]*/
699                     || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
700                     || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
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 {
710                         cred->securebits = arg2;
711                 }
712                 break;
713         case PR_GET_SECUREBITS:
714                 error = cred->securebits;
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)
729                         cred->securebits |= issecure_mask(SECURE_KEEP_CAPS);
730                 else
731                         cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
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
744 void cap_task_reparent_to_init (struct task_struct *p)
745 {
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;
752 }
753
754 int cap_syslog (int type)
755 {
756         if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
757                 return -EPERM;
758         return 0;
759 }
760
761 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
762 {
763         int cap_sys_admin = 0;
764
765         if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
766                 cap_sys_admin = 1;
767         return __vm_enough_memory(mm, pages, cap_sys_admin);
768 }
769