USB: support more Huawei data card product IDs
[linux-2.6/s3c2410-cpufreq.git] / security / commoncap.c
blob06d5c9469ba3bf260342c2cf80a3e256c6b16f18
1 /* Common capabilities, needed by capability.o and root_plug.o
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.
8 */
10 #include <linux/capability.h>
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>
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>
25 #include <linux/mount.h>
26 #include <linux/sched.h>
28 /* Global security state */
30 unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
31 EXPORT_SYMBOL(securebits);
33 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
35 NETLINK_CB(skb).eff_cap = current->cap_effective;
36 return 0;
39 int cap_netlink_recv(struct sk_buff *skb, int cap)
41 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
42 return -EPERM;
43 return 0;
46 EXPORT_SYMBOL(cap_netlink_recv);
49 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
50 * function. That is, it has the reverse semantics: cap_capable()
51 * returns 0 when a task has a capability, but the kernel's capable()
52 * returns 1 for this case.
54 int cap_capable (struct task_struct *tsk, int cap)
56 /* Derived from include/linux/sched.h:capable. */
57 if (cap_raised(tsk->cap_effective, cap))
58 return 0;
59 return -EPERM;
62 int cap_settime(struct timespec *ts, struct timezone *tz)
64 if (!capable(CAP_SYS_TIME))
65 return -EPERM;
66 return 0;
69 int cap_ptrace (struct task_struct *parent, struct task_struct *child)
71 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
72 if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
73 !__capable(parent, CAP_SYS_PTRACE))
74 return -EPERM;
75 return 0;
78 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
79 kernel_cap_t *inheritable, kernel_cap_t *permitted)
81 /* Derived from kernel/capability.c:sys_capget. */
82 *effective = target->cap_effective;
83 *inheritable = target->cap_inheritable;
84 *permitted = target->cap_permitted;
85 return 0;
88 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
90 static inline int cap_block_setpcap(struct task_struct *target)
93 * No support for remote process capability manipulation with
94 * filesystem capability support.
96 return (target != current);
99 static inline int cap_inh_is_capped(void)
102 * Return 1 if changes to the inheritable set are limited
103 * to the old permitted set. That is, if the current task
104 * does *not* possess the CAP_SETPCAP capability.
106 return (cap_capable(current, CAP_SETPCAP) != 0);
109 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
111 static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
112 static inline int cap_inh_is_capped(void) { return 1; }
114 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
116 int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
117 kernel_cap_t *inheritable, kernel_cap_t *permitted)
119 if (cap_block_setpcap(target)) {
120 return -EPERM;
122 if (cap_inh_is_capped()
123 && !cap_issubset(*inheritable,
124 cap_combine(target->cap_inheritable,
125 current->cap_permitted))) {
126 /* incapable of using this inheritable set */
127 return -EPERM;
129 if (!cap_issubset(*inheritable,
130 cap_combine(target->cap_inheritable,
131 current->cap_bset))) {
132 /* no new pI capabilities outside bounding set */
133 return -EPERM;
136 /* verify restrictions on target's new Permitted set */
137 if (!cap_issubset (*permitted,
138 cap_combine (target->cap_permitted,
139 current->cap_permitted))) {
140 return -EPERM;
143 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
144 if (!cap_issubset (*effective, *permitted)) {
145 return -EPERM;
148 return 0;
151 void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
152 kernel_cap_t *inheritable, kernel_cap_t *permitted)
154 target->cap_effective = *effective;
155 target->cap_inheritable = *inheritable;
156 target->cap_permitted = *permitted;
159 static inline void bprm_clear_caps(struct linux_binprm *bprm)
161 cap_clear(bprm->cap_inheritable);
162 cap_clear(bprm->cap_permitted);
163 bprm->cap_effective = false;
166 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
168 int cap_inode_need_killpriv(struct dentry *dentry)
170 struct inode *inode = dentry->d_inode;
171 int error;
173 if (!inode->i_op || !inode->i_op->getxattr)
174 return 0;
176 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
177 if (error <= 0)
178 return 0;
179 return 1;
182 int cap_inode_killpriv(struct dentry *dentry)
184 struct inode *inode = dentry->d_inode;
186 if (!inode->i_op || !inode->i_op->removexattr)
187 return 0;
189 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
192 static inline int cap_from_disk(struct vfs_cap_data *caps,
193 struct linux_binprm *bprm, unsigned size)
195 __u32 magic_etc;
196 unsigned tocopy, i;
198 if (size < sizeof(magic_etc))
199 return -EINVAL;
201 magic_etc = le32_to_cpu(caps->magic_etc);
203 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
204 case VFS_CAP_REVISION_1:
205 if (size != XATTR_CAPS_SZ_1)
206 return -EINVAL;
207 tocopy = VFS_CAP_U32_1;
208 break;
209 case VFS_CAP_REVISION_2:
210 if (size != XATTR_CAPS_SZ_2)
211 return -EINVAL;
212 tocopy = VFS_CAP_U32_2;
213 break;
214 default:
215 return -EINVAL;
218 if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE) {
219 bprm->cap_effective = true;
220 } else {
221 bprm->cap_effective = false;
224 for (i = 0; i < tocopy; ++i) {
225 bprm->cap_permitted.cap[i] =
226 le32_to_cpu(caps->data[i].permitted);
227 bprm->cap_inheritable.cap[i] =
228 le32_to_cpu(caps->data[i].inheritable);
230 while (i < VFS_CAP_U32) {
231 bprm->cap_permitted.cap[i] = 0;
232 bprm->cap_inheritable.cap[i] = 0;
233 i++;
236 return 0;
239 /* Locate any VFS capabilities: */
240 static int get_file_caps(struct linux_binprm *bprm)
242 struct dentry *dentry;
243 int rc = 0;
244 struct vfs_cap_data vcaps;
245 struct inode *inode;
247 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
248 bprm_clear_caps(bprm);
249 return 0;
252 dentry = dget(bprm->file->f_dentry);
253 inode = dentry->d_inode;
254 if (!inode->i_op || !inode->i_op->getxattr)
255 goto out;
257 rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &vcaps,
258 XATTR_CAPS_SZ);
259 if (rc == -ENODATA || rc == -EOPNOTSUPP) {
260 /* no data, that's ok */
261 rc = 0;
262 goto out;
264 if (rc < 0)
265 goto out;
267 rc = cap_from_disk(&vcaps, bprm, rc);
268 if (rc)
269 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
270 __FUNCTION__, rc, bprm->filename);
272 out:
273 dput(dentry);
274 if (rc)
275 bprm_clear_caps(bprm);
277 return rc;
280 #else
281 int cap_inode_need_killpriv(struct dentry *dentry)
283 return 0;
286 int cap_inode_killpriv(struct dentry *dentry)
288 return 0;
291 static inline int get_file_caps(struct linux_binprm *bprm)
293 bprm_clear_caps(bprm);
294 return 0;
296 #endif
298 int cap_bprm_set_security (struct linux_binprm *bprm)
300 int ret;
302 ret = get_file_caps(bprm);
303 if (ret)
304 printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
305 __FUNCTION__, ret, bprm->filename);
307 /* To support inheritance of root-permissions and suid-root
308 * executables under compatibility mode, we raise all three
309 * capability sets for the file.
311 * If only the real uid is 0, we only raise the inheritable
312 * and permitted sets of the executable file.
315 if (!issecure (SECURE_NOROOT)) {
316 if (bprm->e_uid == 0 || current->uid == 0) {
317 cap_set_full (bprm->cap_inheritable);
318 cap_set_full (bprm->cap_permitted);
320 if (bprm->e_uid == 0)
321 bprm->cap_effective = true;
324 return ret;
327 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
329 /* Derived from fs/exec.c:compute_creds. */
330 kernel_cap_t new_permitted, working;
332 new_permitted = cap_intersect(bprm->cap_permitted,
333 current->cap_bset);
334 working = cap_intersect(bprm->cap_inheritable,
335 current->cap_inheritable);
336 new_permitted = cap_combine(new_permitted, working);
338 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
339 !cap_issubset (new_permitted, current->cap_permitted)) {
340 set_dumpable(current->mm, suid_dumpable);
341 current->pdeath_signal = 0;
343 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
344 if (!capable(CAP_SETUID)) {
345 bprm->e_uid = current->uid;
346 bprm->e_gid = current->gid;
348 if (!capable (CAP_SETPCAP)) {
349 new_permitted = cap_intersect (new_permitted,
350 current->cap_permitted);
355 current->suid = current->euid = current->fsuid = bprm->e_uid;
356 current->sgid = current->egid = current->fsgid = bprm->e_gid;
358 /* For init, we want to retain the capabilities set
359 * in the init_task struct. Thus we skip the usual
360 * capability rules */
361 if (!is_global_init(current)) {
362 current->cap_permitted = new_permitted;
363 if (bprm->cap_effective)
364 current->cap_effective = new_permitted;
365 else
366 cap_clear(current->cap_effective);
369 /* AUD: Audit candidate if current->cap_effective is set */
371 current->keep_capabilities = 0;
374 int cap_bprm_secureexec (struct linux_binprm *bprm)
376 if (current->uid != 0) {
377 if (bprm->cap_effective)
378 return 1;
379 if (!cap_isclear(bprm->cap_permitted))
380 return 1;
381 if (!cap_isclear(bprm->cap_inheritable))
382 return 1;
385 return (current->euid != current->uid ||
386 current->egid != current->gid);
389 int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
390 size_t size, int flags)
392 if (!strcmp(name, XATTR_NAME_CAPS)) {
393 if (!capable(CAP_SETFCAP))
394 return -EPERM;
395 return 0;
396 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
397 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
398 !capable(CAP_SYS_ADMIN))
399 return -EPERM;
400 return 0;
403 int cap_inode_removexattr(struct dentry *dentry, char *name)
405 if (!strcmp(name, XATTR_NAME_CAPS)) {
406 if (!capable(CAP_SETFCAP))
407 return -EPERM;
408 return 0;
409 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
410 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
411 !capable(CAP_SYS_ADMIN))
412 return -EPERM;
413 return 0;
416 /* moved from kernel/sys.c. */
418 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
419 * a process after a call to setuid, setreuid, or setresuid.
421 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
422 * {r,e,s}uid != 0, the permitted and effective capabilities are
423 * cleared.
425 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
426 * capabilities of the process are cleared.
428 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
429 * capabilities are set to the permitted capabilities.
431 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
432 * never happen.
434 * -astor
436 * cevans - New behaviour, Oct '99
437 * A process may, via prctl(), elect to keep its capabilities when it
438 * calls setuid() and switches away from uid==0. Both permitted and
439 * effective sets will be retained.
440 * Without this change, it was impossible for a daemon to drop only some
441 * of its privilege. The call to setuid(!=0) would drop all privileges!
442 * Keeping uid 0 is not an option because uid 0 owns too many vital
443 * files..
444 * Thanks to Olaf Kirch and Peter Benie for spotting this.
446 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
447 int old_suid)
449 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
450 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
451 !current->keep_capabilities) {
452 cap_clear (current->cap_permitted);
453 cap_clear (current->cap_effective);
455 if (old_euid == 0 && current->euid != 0) {
456 cap_clear (current->cap_effective);
458 if (old_euid != 0 && current->euid == 0) {
459 current->cap_effective = current->cap_permitted;
463 int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
464 int flags)
466 switch (flags) {
467 case LSM_SETID_RE:
468 case LSM_SETID_ID:
469 case LSM_SETID_RES:
470 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
471 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
472 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
474 break;
475 case LSM_SETID_FS:
477 uid_t old_fsuid = old_ruid;
479 /* Copied from kernel/sys.c:setfsuid. */
482 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
483 * if not, we might be a bit too harsh here.
486 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
487 if (old_fsuid == 0 && current->fsuid != 0) {
488 current->cap_effective =
489 cap_drop_fs_set(
490 current->cap_effective);
492 if (old_fsuid != 0 && current->fsuid == 0) {
493 current->cap_effective =
494 cap_raise_fs_set(
495 current->cap_effective,
496 current->cap_permitted);
499 break;
501 default:
502 return -EINVAL;
505 return 0;
508 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
510 * Rationale: code calling task_setscheduler, task_setioprio, and
511 * task_setnice, assumes that
512 * . if capable(cap_sys_nice), then those actions should be allowed
513 * . if not capable(cap_sys_nice), but acting on your own processes,
514 * then those actions should be allowed
515 * This is insufficient now since you can call code without suid, but
516 * yet with increased caps.
517 * So we check for increased caps on the target process.
519 static inline int cap_safe_nice(struct task_struct *p)
521 if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
522 !__capable(current, CAP_SYS_NICE))
523 return -EPERM;
524 return 0;
527 int cap_task_setscheduler (struct task_struct *p, int policy,
528 struct sched_param *lp)
530 return cap_safe_nice(p);
533 int cap_task_setioprio (struct task_struct *p, int ioprio)
535 return cap_safe_nice(p);
538 int cap_task_setnice (struct task_struct *p, int nice)
540 return cap_safe_nice(p);
544 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
545 * done without task_capability_lock() because it introduces
546 * no new races - i.e. only another task doing capget() on
547 * this task could get inconsistent info. There can be no
548 * racing writer bc a task can only change its own caps.
550 long cap_prctl_drop(unsigned long cap)
552 if (!capable(CAP_SETPCAP))
553 return -EPERM;
554 if (!cap_valid(cap))
555 return -EINVAL;
556 cap_lower(current->cap_bset, cap);
557 return 0;
559 #else
560 int cap_task_setscheduler (struct task_struct *p, int policy,
561 struct sched_param *lp)
563 return 0;
565 int cap_task_setioprio (struct task_struct *p, int ioprio)
567 return 0;
569 int cap_task_setnice (struct task_struct *p, int nice)
571 return 0;
573 #endif
575 void cap_task_reparent_to_init (struct task_struct *p)
577 cap_set_init_eff(p->cap_effective);
578 cap_clear(p->cap_inheritable);
579 cap_set_full(p->cap_permitted);
580 p->keep_capabilities = 0;
581 return;
584 int cap_syslog (int type)
586 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
587 return -EPERM;
588 return 0;
591 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
593 int cap_sys_admin = 0;
595 if (cap_capable(current, CAP_SYS_ADMIN) == 0)
596 cap_sys_admin = 1;
597 return __vm_enough_memory(mm, pages, cap_sys_admin);