revert "capabilities: clean up file capability reading"
[linux-2.6-block.git] / security / commoncap.c
CommitLineData
1da177e4
LT
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
c59ede7b 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/security.h>
15#include <linux/file.h>
16#include <linux/mm.h>
17#include <linux/mman.h>
18#include <linux/pagemap.h>
19#include <linux/swap.h>
1da177e4
LT
20#include <linux/skbuff.h>
21#include <linux/netlink.h>
22#include <linux/ptrace.h>
23#include <linux/xattr.h>
24#include <linux/hugetlb.h>
b5376771 25#include <linux/mount.h>
b460cbc5 26#include <linux/sched.h>
1da177e4 27
72c2d582
AM
28#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
29/*
30 * Because of the reduced scope of CAP_SETPCAP when filesystem
31 * capabilities are in effect, it is safe to allow this capability to
32 * be available in the default configuration.
33 */
34# define CAP_INIT_BSET CAP_FULL_SET
35#else /* ie. ndef CONFIG_SECURITY_FILE_CAPABILITIES */
36# define CAP_INIT_BSET CAP_INIT_EFF_SET
37#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
38
39kernel_cap_t cap_bset = CAP_INIT_BSET; /* systemwide capability bound */
40EXPORT_SYMBOL(cap_bset);
41
42/* Global security state */
43
44unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
45EXPORT_SYMBOL(securebits);
46
1da177e4
LT
47int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
48{
49 NETLINK_CB(skb).eff_cap = current->cap_effective;
50 return 0;
51}
52
c7bdb545 53int cap_netlink_recv(struct sk_buff *skb, int cap)
1da177e4 54{
c7bdb545 55 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
1da177e4
LT
56 return -EPERM;
57 return 0;
58}
59
60EXPORT_SYMBOL(cap_netlink_recv);
61
a6dbb1ef
AM
62/*
63 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
64 * function. That is, it has the reverse semantics: cap_capable()
65 * returns 0 when a task has a capability, but the kernel's capable()
66 * returns 1 for this case.
67 */
1da177e4
LT
68int cap_capable (struct task_struct *tsk, int cap)
69{
70 /* Derived from include/linux/sched.h:capable. */
71 if (cap_raised(tsk->cap_effective, cap))
72 return 0;
73 return -EPERM;
74}
75
76int cap_settime(struct timespec *ts, struct timezone *tz)
77{
78 if (!capable(CAP_SYS_TIME))
79 return -EPERM;
80 return 0;
81}
82
83int cap_ptrace (struct task_struct *parent, struct task_struct *child)
84{
85 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
d4eb82c7
CW
86 if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
87 !__capable(parent, CAP_SYS_PTRACE))
1da177e4
LT
88 return -EPERM;
89 return 0;
90}
91
92int cap_capget (struct task_struct *target, kernel_cap_t *effective,
93 kernel_cap_t *inheritable, kernel_cap_t *permitted)
94{
95 /* Derived from kernel/capability.c:sys_capget. */
96 *effective = cap_t (target->cap_effective);
97 *inheritable = cap_t (target->cap_inheritable);
98 *permitted = cap_t (target->cap_permitted);
99 return 0;
100}
101
72c2d582
AM
102#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
103
104static inline int cap_block_setpcap(struct task_struct *target)
105{
106 /*
107 * No support for remote process capability manipulation with
108 * filesystem capability support.
109 */
110 return (target != current);
111}
112
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 */
a6dbb1ef 120 return (cap_capable(current, CAP_SETPCAP) != 0);
72c2d582
AM
121}
122
123#else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
124
125static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
126static inline int cap_inh_is_capped(void) { return 1; }
127
128#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
129
1da177e4
LT
130int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
131 kernel_cap_t *inheritable, kernel_cap_t *permitted)
132{
72c2d582
AM
133 if (cap_block_setpcap(target)) {
134 return -EPERM;
135 }
136 if (cap_inh_is_capped()
137 && !cap_issubset(*inheritable,
138 cap_combine(target->cap_inheritable,
139 current->cap_permitted))) {
140 /* incapable of using this inheritable set */
1da177e4
LT
141 return -EPERM;
142 }
143
144 /* verify restrictions on target's new Permitted set */
145 if (!cap_issubset (*permitted,
146 cap_combine (target->cap_permitted,
147 current->cap_permitted))) {
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
159void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
160 kernel_cap_t *inheritable, kernel_cap_t *permitted)
161{
162 target->cap_effective = *effective;
163 target->cap_inheritable = *inheritable;
164 target->cap_permitted = *permitted;
165}
166
b5376771
SH
167static inline void bprm_clear_caps(struct linux_binprm *bprm)
168{
169 cap_clear(bprm->cap_inheritable);
170 cap_clear(bprm->cap_permitted);
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
8f6936f4 200static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
b5376771
SH
201 int size)
202{
203 __u32 magic_etc;
204
205 if (size != XATTR_CAPS_SZ)
206 return -EINVAL;
207
8f6936f4 208 magic_etc = le32_to_cpu(caps[0]);
b5376771
SH
209
210 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
211 case VFS_CAP_REVISION:
212 if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
213 bprm->cap_effective = true;
214 else
215 bprm->cap_effective = false;
8f6936f4
AM
216 bprm->cap_permitted = to_cap_t(le32_to_cpu(caps[1]));
217 bprm->cap_inheritable = to_cap_t(le32_to_cpu(caps[2]));
b5376771
SH
218 return 0;
219 default:
220 return -EINVAL;
221 }
222}
223
224/* Locate any VFS capabilities: */
225static int get_file_caps(struct linux_binprm *bprm)
226{
227 struct dentry *dentry;
228 int rc = 0;
8f6936f4 229 __le32 v1caps[XATTR_CAPS_SZ];
b5376771
SH
230 struct inode *inode;
231
232 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
233 bprm_clear_caps(bprm);
234 return 0;
235 }
236
237 dentry = dget(bprm->file->f_dentry);
238 inode = dentry->d_inode;
239 if (!inode->i_op || !inode->i_op->getxattr)
240 goto out;
241
8f6936f4
AM
242 rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
243 XATTR_CAPS_SZ);
b5376771
SH
244 if (rc == -ENODATA || rc == -EOPNOTSUPP) {
245 /* no data, that's ok */
246 rc = 0;
247 goto out;
248 }
249 if (rc < 0)
250 goto out;
251
8f6936f4 252 rc = cap_from_disk(v1caps, bprm, rc);
b5376771
SH
253 if (rc)
254 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
255 __FUNCTION__, rc, bprm->filename);
256
257out:
258 dput(dentry);
259 if (rc)
260 bprm_clear_caps(bprm);
261
262 return rc;
263}
264
265#else
266int cap_inode_need_killpriv(struct dentry *dentry)
267{
268 return 0;
269}
270
271int cap_inode_killpriv(struct dentry *dentry)
272{
273 return 0;
274}
275
276static inline int get_file_caps(struct linux_binprm *bprm)
277{
278 bprm_clear_caps(bprm);
279 return 0;
280}
281#endif
282
1da177e4
LT
283int cap_bprm_set_security (struct linux_binprm *bprm)
284{
b5376771 285 int ret;
1da177e4 286
b5376771
SH
287 ret = get_file_caps(bprm);
288 if (ret)
289 printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
290 __FUNCTION__, ret, bprm->filename);
1da177e4
LT
291
292 /* To support inheritance of root-permissions and suid-root
293 * executables under compatibility mode, we raise all three
294 * capability sets for the file.
295 *
296 * If only the real uid is 0, we only raise the inheritable
297 * and permitted sets of the executable file.
298 */
299
300 if (!issecure (SECURE_NOROOT)) {
301 if (bprm->e_uid == 0 || current->uid == 0) {
302 cap_set_full (bprm->cap_inheritable);
303 cap_set_full (bprm->cap_permitted);
304 }
305 if (bprm->e_uid == 0)
b5376771 306 bprm->cap_effective = true;
1da177e4 307 }
b5376771
SH
308
309 return ret;
1da177e4
LT
310}
311
312void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
313{
314 /* Derived from fs/exec.c:compute_creds. */
315 kernel_cap_t new_permitted, working;
316
317 new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
318 working = cap_intersect (bprm->cap_inheritable,
319 current->cap_inheritable);
320 new_permitted = cap_combine (new_permitted, working);
321
322 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
323 !cap_issubset (new_permitted, current->cap_permitted)) {
6c5d5238 324 set_dumpable(current->mm, suid_dumpable);
b5376771 325 current->pdeath_signal = 0;
1da177e4
LT
326
327 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
328 if (!capable(CAP_SETUID)) {
329 bprm->e_uid = current->uid;
330 bprm->e_gid = current->gid;
331 }
332 if (!capable (CAP_SETPCAP)) {
333 new_permitted = cap_intersect (new_permitted,
334 current->cap_permitted);
335 }
336 }
337 }
338
339 current->suid = current->euid = current->fsuid = bprm->e_uid;
340 current->sgid = current->egid = current->fsgid = bprm->e_gid;
341
342 /* For init, we want to retain the capabilities set
343 * in the init_task struct. Thus we skip the usual
344 * capability rules */
b460cbc5 345 if (!is_global_init(current)) {
1da177e4 346 current->cap_permitted = new_permitted;
b5376771
SH
347 current->cap_effective = bprm->cap_effective ?
348 new_permitted : 0;
1da177e4
LT
349 }
350
351 /* AUD: Audit candidate if current->cap_effective is set */
352
353 current->keep_capabilities = 0;
354}
355
356int cap_bprm_secureexec (struct linux_binprm *bprm)
357{
b5376771
SH
358 if (current->uid != 0) {
359 if (bprm->cap_effective)
360 return 1;
361 if (!cap_isclear(bprm->cap_permitted))
362 return 1;
363 if (!cap_isclear(bprm->cap_inheritable))
364 return 1;
365 }
366
1da177e4
LT
367 return (current->euid != current->uid ||
368 current->egid != current->gid);
369}
370
371int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
372 size_t size, int flags)
373{
b5376771
SH
374 if (!strcmp(name, XATTR_NAME_CAPS)) {
375 if (!capable(CAP_SETFCAP))
376 return -EPERM;
377 return 0;
378 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
1da177e4
LT
379 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
380 !capable(CAP_SYS_ADMIN))
381 return -EPERM;
382 return 0;
383}
384
385int cap_inode_removexattr(struct dentry *dentry, char *name)
386{
b5376771
SH
387 if (!strcmp(name, XATTR_NAME_CAPS)) {
388 if (!capable(CAP_SETFCAP))
389 return -EPERM;
390 return 0;
391 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
1da177e4
LT
392 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
393 !capable(CAP_SYS_ADMIN))
394 return -EPERM;
395 return 0;
396}
397
398/* moved from kernel/sys.c. */
399/*
400 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
401 * a process after a call to setuid, setreuid, or setresuid.
402 *
403 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
404 * {r,e,s}uid != 0, the permitted and effective capabilities are
405 * cleared.
406 *
407 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
408 * capabilities of the process are cleared.
409 *
410 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
411 * capabilities are set to the permitted capabilities.
412 *
413 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
414 * never happen.
415 *
416 * -astor
417 *
418 * cevans - New behaviour, Oct '99
419 * A process may, via prctl(), elect to keep its capabilities when it
420 * calls setuid() and switches away from uid==0. Both permitted and
421 * effective sets will be retained.
422 * Without this change, it was impossible for a daemon to drop only some
423 * of its privilege. The call to setuid(!=0) would drop all privileges!
424 * Keeping uid 0 is not an option because uid 0 owns too many vital
425 * files..
426 * Thanks to Olaf Kirch and Peter Benie for spotting this.
427 */
428static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
429 int old_suid)
430{
431 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
432 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
433 !current->keep_capabilities) {
434 cap_clear (current->cap_permitted);
435 cap_clear (current->cap_effective);
436 }
437 if (old_euid == 0 && current->euid != 0) {
438 cap_clear (current->cap_effective);
439 }
440 if (old_euid != 0 && current->euid == 0) {
441 current->cap_effective = current->cap_permitted;
442 }
443}
444
445int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
446 int flags)
447{
448 switch (flags) {
449 case LSM_SETID_RE:
450 case LSM_SETID_ID:
451 case LSM_SETID_RES:
452 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
453 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
454 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
455 }
456 break;
457 case LSM_SETID_FS:
458 {
459 uid_t old_fsuid = old_ruid;
460
461 /* Copied from kernel/sys.c:setfsuid. */
462
463 /*
464 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
465 * if not, we might be a bit too harsh here.
466 */
467
468 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
469 if (old_fsuid == 0 && current->fsuid != 0) {
470 cap_t (current->cap_effective) &=
471 ~CAP_FS_MASK;
472 }
473 if (old_fsuid != 0 && current->fsuid == 0) {
474 cap_t (current->cap_effective) |=
475 (cap_t (current->cap_permitted) &
476 CAP_FS_MASK);
477 }
478 }
479 break;
480 }
481 default:
482 return -EINVAL;
483 }
484
485 return 0;
486}
487
b5376771
SH
488#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
489/*
490 * Rationale: code calling task_setscheduler, task_setioprio, and
491 * task_setnice, assumes that
492 * . if capable(cap_sys_nice), then those actions should be allowed
493 * . if not capable(cap_sys_nice), but acting on your own processes,
494 * then those actions should be allowed
495 * This is insufficient now since you can call code without suid, but
496 * yet with increased caps.
497 * So we check for increased caps on the target process.
498 */
499static inline int cap_safe_nice(struct task_struct *p)
500{
501 if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
502 !__capable(current, CAP_SYS_NICE))
503 return -EPERM;
504 return 0;
505}
506
507int cap_task_setscheduler (struct task_struct *p, int policy,
508 struct sched_param *lp)
509{
510 return cap_safe_nice(p);
511}
512
513int cap_task_setioprio (struct task_struct *p, int ioprio)
514{
515 return cap_safe_nice(p);
516}
517
518int cap_task_setnice (struct task_struct *p, int nice)
519{
520 return cap_safe_nice(p);
521}
522
523int cap_task_kill(struct task_struct *p, struct siginfo *info,
524 int sig, u32 secid)
525{
526 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
527 return 0;
528
8ec2328f
SH
529 /*
530 * Running a setuid root program raises your capabilities.
531 * Killing your own setuid root processes was previously
532 * allowed.
533 * We must preserve legacy signal behavior in this case.
534 */
535 if (p->euid == 0 && p->uid == current->uid)
536 return 0;
537
91ad997a
SH
538 /* sigcont is permitted within same session */
539 if (sig == SIGCONT && (task_session_nr(current) == task_session_nr(p)))
540 return 0;
541
b5376771
SH
542 if (secid)
543 /*
544 * Signal sent as a particular user.
545 * Capabilities are ignored. May be wrong, but it's the
546 * only thing we can do at the moment.
547 * Used only by usb drivers?
548 */
549 return 0;
550 if (cap_issubset(p->cap_permitted, current->cap_permitted))
551 return 0;
552 if (capable(CAP_KILL))
553 return 0;
554
555 return -EPERM;
556}
557#else
558int cap_task_setscheduler (struct task_struct *p, int policy,
559 struct sched_param *lp)
560{
561 return 0;
562}
563int cap_task_setioprio (struct task_struct *p, int ioprio)
564{
565 return 0;
566}
567int cap_task_setnice (struct task_struct *p, int nice)
568{
569 return 0;
570}
571int cap_task_kill(struct task_struct *p, struct siginfo *info,
572 int sig, u32 secid)
573{
574 return 0;
575}
576#endif
577
1da177e4
LT
578void cap_task_reparent_to_init (struct task_struct *p)
579{
580 p->cap_effective = CAP_INIT_EFF_SET;
581 p->cap_inheritable = CAP_INIT_INH_SET;
582 p->cap_permitted = CAP_FULL_SET;
583 p->keep_capabilities = 0;
584 return;
585}
586
587int cap_syslog (int type)
588{
589 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
590 return -EPERM;
591 return 0;
592}
593
34b4e4aa 594int cap_vm_enough_memory(struct mm_struct *mm, long pages)
1da177e4
LT
595{
596 int cap_sys_admin = 0;
597
598 if (cap_capable(current, CAP_SYS_ADMIN) == 0)
599 cap_sys_admin = 1;
34b4e4aa 600 return __vm_enough_memory(mm, pages, cap_sys_admin);
1da177e4
LT
601}
602