CRED: Inaugurate COW credentials
[deliverable/linux.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();
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
83int 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
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 */
d84f4f99 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
d84f4f99
DH
135int 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
168static inline void bprm_clear_caps(struct linux_binprm *bprm)
169{
5459c164 170 cap_clear(bprm->cap_post_exec_permitted);
b5376771
SH
171 bprm->cap_effective = false;
172}
173
174#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
175
176int 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
190int 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
EP
200static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
201 struct linux_binprm *bprm)
b5376771 202{
c0b00441
EP
203 unsigned i;
204 int ret = 0;
205
206 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
207 bprm->cap_effective = true;
208 else
209 bprm->cap_effective = false;
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 */
218 bprm->cap_post_exec_permitted.cap[i] =
b6dff3ec
DH
219 (current->cred->cap_bset.cap[i] & permitted) |
220 (current->cred->cap_inheritable.cap[i] & inheritable);
c0b00441
EP
221
222 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
223 /*
224 * insufficient to execute correctly
225 */
226 ret = -EPERM;
227 }
228 }
229
230 /*
231 * For legacy apps, with no internal support for recognizing they
232 * do not have enough capabilities, we return an error if they are
233 * missing some "forced" (aka file-permitted) capabilities.
234 */
235 return bprm->cap_effective ? ret : 0;
236}
237
238int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
239{
240 struct inode *inode = dentry->d_inode;
b5376771 241 __u32 magic_etc;
e338d263 242 unsigned tocopy, i;
c0b00441
EP
243 int size;
244 struct vfs_cap_data caps;
245
246 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
247
248 if (!inode || !inode->i_op || !inode->i_op->getxattr)
249 return -ENODATA;
250
251 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
252 XATTR_CAPS_SZ);
253 if (size == -ENODATA || size == -EOPNOTSUPP) {
254 /* no data, that's ok */
255 return -ENODATA;
256 }
257 if (size < 0)
258 return size;
b5376771 259
e338d263 260 if (size < sizeof(magic_etc))
b5376771
SH
261 return -EINVAL;
262
c0b00441 263 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
b5376771
SH
264
265 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
e338d263
AM
266 case VFS_CAP_REVISION_1:
267 if (size != XATTR_CAPS_SZ_1)
268 return -EINVAL;
269 tocopy = VFS_CAP_U32_1;
270 break;
271 case VFS_CAP_REVISION_2:
272 if (size != XATTR_CAPS_SZ_2)
273 return -EINVAL;
274 tocopy = VFS_CAP_U32_2;
275 break;
b5376771
SH
276 default:
277 return -EINVAL;
278 }
e338d263 279
5459c164 280 CAP_FOR_EACH_U32(i) {
c0b00441
EP
281 if (i >= tocopy)
282 break;
283 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
284 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
e338d263 285 }
c0b00441 286 return 0;
b5376771
SH
287}
288
289/* Locate any VFS capabilities: */
290static int get_file_caps(struct linux_binprm *bprm)
291{
292 struct dentry *dentry;
293 int rc = 0;
c0b00441 294 struct cpu_vfs_cap_data vcaps;
b5376771 295
3318a386
SH
296 bprm_clear_caps(bprm);
297
1f29fae2
SH
298 if (!file_caps_enabled)
299 return 0;
300
3318a386 301 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
b5376771 302 return 0;
b5376771
SH
303
304 dentry = dget(bprm->file->f_dentry);
b5376771 305
c0b00441
EP
306 rc = get_vfs_caps_from_disk(dentry, &vcaps);
307 if (rc < 0) {
308 if (rc == -EINVAL)
309 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
310 __func__, rc, bprm->filename);
311 else if (rc == -ENODATA)
312 rc = 0;
b5376771
SH
313 goto out;
314 }
b5376771 315
c0b00441 316 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
b5376771
SH
317
318out:
319 dput(dentry);
320 if (rc)
321 bprm_clear_caps(bprm);
322
323 return rc;
324}
325
326#else
327int cap_inode_need_killpriv(struct dentry *dentry)
328{
329 return 0;
330}
331
332int cap_inode_killpriv(struct dentry *dentry)
333{
334 return 0;
335}
336
337static inline int get_file_caps(struct linux_binprm *bprm)
338{
339 bprm_clear_caps(bprm);
340 return 0;
341}
342#endif
343
1da177e4
LT
344int cap_bprm_set_security (struct linux_binprm *bprm)
345{
b5376771 346 int ret;
1da177e4 347
b5376771 348 ret = get_file_caps(bprm);
1da177e4 349
5459c164
AM
350 if (!issecure(SECURE_NOROOT)) {
351 /*
352 * To support inheritance of root-permissions and suid-root
353 * executables under compatibility mode, we override the
354 * capability sets for the file.
355 *
356 * If only the real uid is 0, we do not set the effective
357 * bit.
358 */
b103c598 359 if (bprm->e_uid == 0 || current_uid() == 0) {
5459c164
AM
360 /* pP' = (cap_bset & ~0) | (pI & ~0) */
361 bprm->cap_post_exec_permitted = cap_combine(
b6dff3ec
DH
362 current->cred->cap_bset,
363 current->cred->cap_inheritable);
5459c164
AM
364 bprm->cap_effective = (bprm->e_uid == 0);
365 ret = 0;
1da177e4 366 }
1da177e4 367 }
b5376771
SH
368
369 return ret;
1da177e4
LT
370}
371
d84f4f99 372int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
1da177e4 373{
d84f4f99
DH
374 const struct cred *old = current_cred();
375 struct cred *new;
376
377 new = prepare_creds();
378 if (!new)
379 return -ENOMEM;
3fc689e9 380
d84f4f99 381 if (bprm->e_uid != old->uid || bprm->e_gid != old->gid ||
5459c164 382 !cap_issubset(bprm->cap_post_exec_permitted,
d84f4f99 383 old->cap_permitted)) {
6c5d5238 384 set_dumpable(current->mm, suid_dumpable);
b5376771 385 current->pdeath_signal = 0;
1da177e4
LT
386
387 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
388 if (!capable(CAP_SETUID)) {
d84f4f99
DH
389 bprm->e_uid = old->uid;
390 bprm->e_gid = old->gid;
1da177e4 391 }
1209726c 392 if (cap_limit_ptraced_target()) {
5459c164
AM
393 bprm->cap_post_exec_permitted = cap_intersect(
394 bprm->cap_post_exec_permitted,
d84f4f99 395 new->cap_permitted);
1da177e4
LT
396 }
397 }
398 }
399
d84f4f99
DH
400 new->suid = new->euid = new->fsuid = bprm->e_uid;
401 new->sgid = new->egid = new->fsgid = bprm->e_gid;
1da177e4
LT
402
403 /* For init, we want to retain the capabilities set
404 * in the init_task struct. Thus we skip the usual
405 * capability rules */
b460cbc5 406 if (!is_global_init(current)) {
d84f4f99 407 new->cap_permitted = bprm->cap_post_exec_permitted;
e338d263 408 if (bprm->cap_effective)
d84f4f99 409 new->cap_effective = bprm->cap_post_exec_permitted;
e338d263 410 else
d84f4f99 411 cap_clear(new->cap_effective);
1da177e4
LT
412 }
413
3fc689e9
EP
414 /*
415 * Audit candidate if current->cap_effective is set
416 *
417 * We do not bother to audit if 3 things are true:
418 * 1) cap_effective has all caps
419 * 2) we are root
420 * 3) root is supposed to have all caps (SECURE_NOROOT)
421 * Since this is just a normal root execing a process.
422 *
423 * Number 1 above might fail if you don't have a full bset, but I think
424 * that is interesting information to audit.
425 */
d84f4f99
DH
426 if (!cap_isclear(new->cap_effective)) {
427 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
428 bprm->e_uid != 0 || new->uid != 0 ||
3fc689e9 429 issecure(SECURE_NOROOT))
d84f4f99 430 audit_log_bprm_fcaps(bprm, new, old);
3fc689e9 431 }
1da177e4 432
d84f4f99
DH
433 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
434 return commit_creds(new);
1da177e4
LT
435}
436
437int cap_bprm_secureexec (struct linux_binprm *bprm)
438{
c69e8d9c 439 const struct cred *cred = current_cred();
b6dff3ec
DH
440
441 if (cred->uid != 0) {
b5376771
SH
442 if (bprm->cap_effective)
443 return 1;
5459c164 444 if (!cap_isclear(bprm->cap_post_exec_permitted))
b5376771
SH
445 return 1;
446 }
447
b6dff3ec
DH
448 return (cred->euid != cred->uid ||
449 cred->egid != cred->gid);
1da177e4
LT
450}
451
8f0cfa52
DH
452int cap_inode_setxattr(struct dentry *dentry, const char *name,
453 const void *value, size_t size, int flags)
1da177e4 454{
b5376771
SH
455 if (!strcmp(name, XATTR_NAME_CAPS)) {
456 if (!capable(CAP_SETFCAP))
457 return -EPERM;
458 return 0;
459 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
1da177e4
LT
460 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
461 !capable(CAP_SYS_ADMIN))
462 return -EPERM;
463 return 0;
464}
465
8f0cfa52 466int cap_inode_removexattr(struct dentry *dentry, const char *name)
1da177e4 467{
b5376771
SH
468 if (!strcmp(name, XATTR_NAME_CAPS)) {
469 if (!capable(CAP_SETFCAP))
470 return -EPERM;
471 return 0;
472 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
1da177e4
LT
473 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
474 !capable(CAP_SYS_ADMIN))
475 return -EPERM;
476 return 0;
477}
478
479/* moved from kernel/sys.c. */
480/*
481 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
482 * a process after a call to setuid, setreuid, or setresuid.
483 *
484 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
485 * {r,e,s}uid != 0, the permitted and effective capabilities are
486 * cleared.
487 *
488 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
489 * capabilities of the process are cleared.
490 *
491 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
492 * capabilities are set to the permitted capabilities.
493 *
494 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
495 * never happen.
496 *
497 * -astor
498 *
499 * cevans - New behaviour, Oct '99
500 * A process may, via prctl(), elect to keep its capabilities when it
501 * calls setuid() and switches away from uid==0. Both permitted and
502 * effective sets will be retained.
503 * Without this change, it was impossible for a daemon to drop only some
504 * of its privilege. The call to setuid(!=0) would drop all privileges!
505 * Keeping uid 0 is not an option because uid 0 owns too many vital
506 * files..
507 * Thanks to Olaf Kirch and Peter Benie for spotting this.
508 */
d84f4f99 509static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
1da177e4 510{
d84f4f99
DH
511 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
512 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
3898b1b4 513 !issecure(SECURE_KEEP_CAPS)) {
d84f4f99
DH
514 cap_clear(new->cap_permitted);
515 cap_clear(new->cap_effective);
1da177e4 516 }
d84f4f99
DH
517 if (old->euid == 0 && new->euid != 0)
518 cap_clear(new->cap_effective);
519 if (old->euid != 0 && new->euid == 0)
520 new->cap_effective = new->cap_permitted;
1da177e4
LT
521}
522
d84f4f99 523int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
1da177e4
LT
524{
525 switch (flags) {
526 case LSM_SETID_RE:
527 case LSM_SETID_ID:
528 case LSM_SETID_RES:
529 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
d84f4f99
DH
530 if (!issecure(SECURE_NO_SETUID_FIXUP))
531 cap_emulate_setxuid(new, old);
1da177e4
LT
532 break;
533 case LSM_SETID_FS:
d84f4f99 534 /* Copied from kernel/sys.c:setfsuid. */
1da177e4 535
d84f4f99
DH
536 /*
537 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
538 * if not, we might be a bit too harsh here.
539 */
540 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
541 if (old->fsuid == 0 && new->fsuid != 0) {
542 new->cap_effective =
543 cap_drop_fs_set(new->cap_effective);
544 }
545 if (old->fsuid != 0 && new->fsuid == 0) {
546 new->cap_effective =
547 cap_raise_fs_set(new->cap_effective,
548 new->cap_permitted);
1da177e4 549 }
1da177e4 550 }
d84f4f99 551 break;
1da177e4
LT
552 default:
553 return -EINVAL;
554 }
555
556 return 0;
557}
558
b5376771
SH
559#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
560/*
561 * Rationale: code calling task_setscheduler, task_setioprio, and
562 * task_setnice, assumes that
563 * . if capable(cap_sys_nice), then those actions should be allowed
564 * . if not capable(cap_sys_nice), but acting on your own processes,
565 * then those actions should be allowed
566 * This is insufficient now since you can call code without suid, but
567 * yet with increased caps.
568 * So we check for increased caps on the target process.
569 */
de45e806 570static int cap_safe_nice(struct task_struct *p)
b5376771 571{
c69e8d9c
DH
572 int is_subset;
573
574 rcu_read_lock();
575 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
576 current_cred()->cap_permitted);
577 rcu_read_unlock();
578
579 if (!is_subset && !capable(CAP_SYS_NICE))
b5376771
SH
580 return -EPERM;
581 return 0;
582}
583
584int cap_task_setscheduler (struct task_struct *p, int policy,
585 struct sched_param *lp)
586{
587 return cap_safe_nice(p);
588}
589
590int cap_task_setioprio (struct task_struct *p, int ioprio)
591{
592 return cap_safe_nice(p);
593}
594
595int cap_task_setnice (struct task_struct *p, int nice)
596{
597 return cap_safe_nice(p);
598}
599
3b7391de
SH
600/*
601 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
602 * done without task_capability_lock() because it introduces
603 * no new races - i.e. only another task doing capget() on
604 * this task could get inconsistent info. There can be no
605 * racing writer bc a task can only change its own caps.
606 */
d84f4f99 607static long cap_prctl_drop(struct cred *new, unsigned long cap)
3b7391de
SH
608{
609 if (!capable(CAP_SETPCAP))
610 return -EPERM;
611 if (!cap_valid(cap))
612 return -EINVAL;
d84f4f99
DH
613
614 cap_lower(new->cap_bset, cap);
3b7391de
SH
615 return 0;
616}
3898b1b4 617
b5376771
SH
618#else
619int cap_task_setscheduler (struct task_struct *p, int policy,
620 struct sched_param *lp)
621{
622 return 0;
623}
624int cap_task_setioprio (struct task_struct *p, int ioprio)
625{
626 return 0;
627}
628int cap_task_setnice (struct task_struct *p, int nice)
629{
630 return 0;
631}
b5376771
SH
632#endif
633
3898b1b4 634int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
d84f4f99 635 unsigned long arg4, unsigned long arg5)
3898b1b4 636{
d84f4f99 637 struct cred *new;
3898b1b4
AM
638 long error = 0;
639
d84f4f99
DH
640 new = prepare_creds();
641 if (!new)
642 return -ENOMEM;
643
3898b1b4
AM
644 switch (option) {
645 case PR_CAPBSET_READ:
d84f4f99 646 error = -EINVAL;
3898b1b4 647 if (!cap_valid(arg2))
d84f4f99
DH
648 goto error;
649 error = !!cap_raised(new->cap_bset, arg2);
650 goto no_change;
651
3898b1b4
AM
652#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
653 case PR_CAPBSET_DROP:
d84f4f99
DH
654 error = cap_prctl_drop(new, arg2);
655 if (error < 0)
656 goto error;
657 goto changed;
3898b1b4
AM
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:
d84f4f99
DH
679 error = -EPERM;
680 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
681 & (new->securebits ^ arg2)) /*[1]*/
682 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
683 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
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 */
d84f4f99
DH
692 )
693 /* cannot change a locked bit */
694 goto error;
695 new->securebits = arg2;
696 goto changed;
697
3898b1b4 698 case PR_GET_SECUREBITS:
d84f4f99
DH
699 error = new->securebits;
700 goto no_change;
3898b1b4
AM
701
702#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
703
704 case PR_GET_KEEPCAPS:
705 if (issecure(SECURE_KEEP_CAPS))
706 error = 1;
d84f4f99
DH
707 goto no_change;
708
3898b1b4 709 case PR_SET_KEEPCAPS:
d84f4f99 710 error = -EINVAL;
3898b1b4 711 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
d84f4f99
DH
712 goto error;
713 error = -EPERM;
714 if (issecure(SECURE_KEEP_CAPS_LOCKED))
715 goto error;
716 if (arg2)
717 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
3898b1b4 718 else
d84f4f99
DH
719 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
720 goto changed;
3898b1b4
AM
721
722 default:
723 /* No functionality available - continue with default */
d84f4f99
DH
724 error = -ENOSYS;
725 goto error;
3898b1b4
AM
726 }
727
728 /* Functionality provided */
d84f4f99
DH
729changed:
730 return commit_creds(new);
731
732no_change:
733 error = 0;
734error:
735 abort_creds(new);
736 return error;
1da177e4
LT
737}
738
739int cap_syslog (int type)
740{
741 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
742 return -EPERM;
743 return 0;
744}
745
34b4e4aa 746int cap_vm_enough_memory(struct mm_struct *mm, long pages)
1da177e4
LT
747{
748 int cap_sys_admin = 0;
749
06112163 750 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
1da177e4 751 cap_sys_admin = 1;
34b4e4aa 752 return __vm_enough_memory(mm, pages, cap_sys_admin);
1da177e4
LT
753}
754
This page took 0.397822 seconds and 5 git commands to generate.