[PATCH] direct-io bio_add_page fix
[linux-2.6/history.git] / security / capability.c
blob6f9b25ba65bdd30673b263c5c50ac94efb81c080
1 /*
2 * Capabilities Linux Security Module
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 */
11 #include <linux/config.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/smp_lock.h>
19 #include <linux/skbuff.h>
20 #include <linux/netlink.h>
22 /* flag to keep track of how we were registered */
23 static int secondary;
25 static int cap_capable (struct task_struct *tsk, int cap)
27 /* Derived from include/linux/sched.h:capable. */
28 if (cap_raised (tsk->cap_effective, cap))
29 return 0;
30 else
31 return -EPERM;
34 static int cap_sys_security (unsigned int id, unsigned int call,
35 unsigned long *args)
37 return -ENOSYS;
40 static int cap_quotactl (int cmds, int type, int id, struct super_block *sb)
42 return 0;
45 static int cap_quota_on (struct file *f)
47 return 0;
50 static int cap_ptrace (struct task_struct *parent, struct task_struct *child)
52 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
53 if (!cap_issubset (child->cap_permitted, current->cap_permitted) &&
54 !capable (CAP_SYS_PTRACE))
55 return -EPERM;
56 else
57 return 0;
60 static int cap_capget (struct task_struct *target, kernel_cap_t * effective,
61 kernel_cap_t * inheritable, kernel_cap_t * permitted)
63 /* Derived from kernel/capability.c:sys_capget. */
64 *effective = cap_t (target->cap_effective);
65 *inheritable = cap_t (target->cap_inheritable);
66 *permitted = cap_t (target->cap_permitted);
67 return 0;
70 static int cap_capset_check (struct task_struct *target,
71 kernel_cap_t * effective,
72 kernel_cap_t * inheritable,
73 kernel_cap_t * permitted)
75 /* Derived from kernel/capability.c:sys_capset. */
76 /* verify restrictions on target's new Inheritable set */
77 if (!cap_issubset (*inheritable,
78 cap_combine (target->cap_inheritable,
79 current->cap_permitted))) {
80 return -EPERM;
83 /* verify restrictions on target's new Permitted set */
84 if (!cap_issubset (*permitted,
85 cap_combine (target->cap_permitted,
86 current->cap_permitted))) {
87 return -EPERM;
90 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
91 if (!cap_issubset (*effective, *permitted)) {
92 return -EPERM;
95 return 0;
98 static void cap_capset_set (struct task_struct *target,
99 kernel_cap_t * effective,
100 kernel_cap_t * inheritable,
101 kernel_cap_t * permitted)
103 target->cap_effective = *effective;
104 target->cap_inheritable = *inheritable;
105 target->cap_permitted = *permitted;
108 static int cap_acct (struct file *file)
110 return 0;
113 static int cap_bprm_alloc_security (struct linux_binprm *bprm)
115 return 0;
118 static int cap_bprm_set_security (struct linux_binprm *bprm)
120 /* Copied from fs/exec.c:prepare_binprm. */
122 /* We don't have VFS support for capabilities yet */
123 cap_clear (bprm->cap_inheritable);
124 cap_clear (bprm->cap_permitted);
125 cap_clear (bprm->cap_effective);
127 /* To support inheritance of root-permissions and suid-root
128 * executables under compatibility mode, we raise all three
129 * capability sets for the file.
131 * If only the real uid is 0, we only raise the inheritable
132 * and permitted sets of the executable file.
135 if (!issecure (SECURE_NOROOT)) {
136 if (bprm->e_uid == 0 || current->uid == 0) {
137 cap_set_full (bprm->cap_inheritable);
138 cap_set_full (bprm->cap_permitted);
140 if (bprm->e_uid == 0)
141 cap_set_full (bprm->cap_effective);
143 return 0;
146 static int cap_bprm_check_security (struct linux_binprm *bprm)
148 return 0;
151 static void cap_bprm_free_security (struct linux_binprm *bprm)
153 return;
156 /* Copied from fs/exec.c */
157 static inline int must_not_trace_exec (struct task_struct *p)
159 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
162 static void cap_bprm_compute_creds (struct linux_binprm *bprm)
164 /* Derived from fs/exec.c:compute_creds. */
165 kernel_cap_t new_permitted, working;
166 int do_unlock = 0;
168 new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
169 working = cap_intersect (bprm->cap_inheritable,
170 current->cap_inheritable);
171 new_permitted = cap_combine (new_permitted, working);
173 if (!cap_issubset (new_permitted, current->cap_permitted)) {
174 current->mm->dumpable = 0;
176 lock_kernel ();
177 if (must_not_trace_exec (current)
178 || atomic_read (&current->fs->count) > 1
179 || atomic_read (&current->files->count) > 1
180 || atomic_read (&current->sig->count) > 1) {
181 if (!capable (CAP_SETPCAP)) {
182 new_permitted = cap_intersect (new_permitted,
183 current->
184 cap_permitted);
187 do_unlock = 1;
190 /* For init, we want to retain the capabilities set
191 * in the init_task struct. Thus we skip the usual
192 * capability rules */
193 if (current->pid != 1) {
194 current->cap_permitted = new_permitted;
195 current->cap_effective =
196 cap_intersect (new_permitted, bprm->cap_effective);
199 /* AUD: Audit candidate if current->cap_effective is set */
201 if (do_unlock)
202 unlock_kernel ();
204 current->keep_capabilities = 0;
207 static int cap_sb_alloc_security (struct super_block *sb)
209 return 0;
212 static void cap_sb_free_security (struct super_block *sb)
214 return;
217 static int cap_sb_statfs (struct super_block *sb)
219 return 0;
222 static int cap_mount (char *dev_name, struct nameidata *nd, char *type,
223 unsigned long flags, void *data)
225 return 0;
228 static int cap_check_sb (struct vfsmount *mnt, struct nameidata *nd)
230 return 0;
233 static int cap_umount (struct vfsmount *mnt, int flags)
235 return 0;
238 static void cap_umount_close (struct vfsmount *mnt)
240 return;
243 static void cap_umount_busy (struct vfsmount *mnt)
245 return;
248 static void cap_post_remount (struct vfsmount *mnt, unsigned long flags,
249 void *data)
251 return;
254 static void cap_post_mountroot (void)
256 return;
259 static void cap_post_addmount (struct vfsmount *mnt, struct nameidata *nd)
261 return;
264 static int cap_pivotroot (struct nameidata *old_nd, struct nameidata *new_nd)
266 return 0;
269 static void cap_post_pivotroot (struct nameidata *old_nd, struct nameidata *new_nd)
271 return;
274 static int cap_inode_alloc_security (struct inode *inode)
276 return 0;
279 static void cap_inode_free_security (struct inode *inode)
281 return;
284 static int cap_inode_create (struct inode *inode, struct dentry *dentry,
285 int mask)
287 return 0;
290 static void cap_inode_post_create (struct inode *inode, struct dentry *dentry,
291 int mask)
293 return;
296 static int cap_inode_link (struct dentry *old_dentry, struct inode *inode,
297 struct dentry *new_dentry)
299 return 0;
302 static void cap_inode_post_link (struct dentry *old_dentry, struct inode *inode,
303 struct dentry *new_dentry)
305 return;
308 static int cap_inode_unlink (struct inode *inode, struct dentry *dentry)
310 return 0;
313 static int cap_inode_symlink (struct inode *inode, struct dentry *dentry,
314 const char *name)
316 return 0;
319 static void cap_inode_post_symlink (struct inode *inode, struct dentry *dentry,
320 const char *name)
322 return;
325 static int cap_inode_mkdir (struct inode *inode, struct dentry *dentry,
326 int mask)
328 return 0;
331 static void cap_inode_post_mkdir (struct inode *inode, struct dentry *dentry,
332 int mask)
334 return;
337 static int cap_inode_rmdir (struct inode *inode, struct dentry *dentry)
339 return 0;
342 static int cap_inode_mknod (struct inode *inode, struct dentry *dentry,
343 int major, dev_t minor)
345 return 0;
348 static void cap_inode_post_mknod (struct inode *inode, struct dentry *dentry,
349 int major, dev_t minor)
351 return;
354 static int cap_inode_rename (struct inode *old_inode, struct dentry *old_dentry,
355 struct inode *new_inode, struct dentry *new_dentry)
357 return 0;
360 static void cap_inode_post_rename (struct inode *old_inode,
361 struct dentry *old_dentry,
362 struct inode *new_inode,
363 struct dentry *new_dentry)
365 return;
368 static int cap_inode_readlink (struct dentry *dentry)
370 return 0;
373 static int cap_inode_follow_link (struct dentry *dentry,
374 struct nameidata *nameidata)
376 return 0;
379 static int cap_inode_permission (struct inode *inode, int mask)
381 return 0;
384 static int cap_inode_permission_lite (struct inode *inode, int mask)
386 return 0;
389 static int cap_inode_setattr (struct dentry *dentry, struct iattr *iattr)
391 return 0;
394 static int cap_inode_getattr (struct vfsmount *mnt, struct dentry *dentry)
396 return 0;
399 static void cap_post_lookup (struct inode *ino, struct dentry *d)
401 return;
404 static void cap_delete (struct inode *ino)
406 return;
409 static int cap_inode_setxattr (struct dentry *dentry, char *name, void *value,
410 size_t size, int flags)
412 return 0;
415 static int cap_inode_getxattr (struct dentry *dentry, char *name)
417 return 0;
420 static int cap_inode_listxattr (struct dentry *dentry)
422 return 0;
425 static int cap_inode_removexattr (struct dentry *dentry, char *name)
427 return 0;
430 static int cap_file_permission (struct file *file, int mask)
432 return 0;
435 static int cap_file_alloc_security (struct file *file)
437 return 0;
440 static void cap_file_free_security (struct file *file)
442 return;
445 static int cap_file_llseek (struct file *file)
447 return 0;
450 static int cap_file_ioctl (struct file *file, unsigned int command,
451 unsigned long arg)
453 return 0;
456 static int cap_file_mmap (struct file *file, unsigned long prot,
457 unsigned long flags)
459 return 0;
462 static int cap_file_mprotect (struct vm_area_struct *vma, unsigned long prot)
464 return 0;
467 static int cap_file_lock (struct file *file, unsigned int cmd)
469 return 0;
472 static int cap_file_fcntl (struct file *file, unsigned int cmd,
473 unsigned long arg)
475 return 0;
478 static int cap_file_set_fowner (struct file *file)
480 return 0;
483 static int cap_file_send_sigiotask (struct task_struct *tsk,
484 struct fown_struct *fown, int fd,
485 int reason)
487 return 0;
490 static int cap_file_receive (struct file *file)
492 return 0;
495 static int cap_task_create (unsigned long clone_flags)
497 return 0;
500 static int cap_task_alloc_security (struct task_struct *p)
502 return 0;
505 static void cap_task_free_security (struct task_struct *p)
507 return;
510 static int cap_task_setuid (uid_t id0, uid_t id1, uid_t id2, int flags)
512 return 0;
515 /* moved from kernel/sys.c. */
517 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
518 * a process after a call to setuid, setreuid, or setresuid.
520 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
521 * {r,e,s}uid != 0, the permitted and effective capabilities are
522 * cleared.
524 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
525 * capabilities of the process are cleared.
527 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
528 * capabilities are set to the permitted capabilities.
530 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
531 * never happen.
533 * -astor
535 * cevans - New behaviour, Oct '99
536 * A process may, via prctl(), elect to keep its capabilities when it
537 * calls setuid() and switches away from uid==0. Both permitted and
538 * effective sets will be retained.
539 * Without this change, it was impossible for a daemon to drop only some
540 * of its privilege. The call to setuid(!=0) would drop all privileges!
541 * Keeping uid 0 is not an option because uid 0 owns too many vital
542 * files..
543 * Thanks to Olaf Kirch and Peter Benie for spotting this.
545 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
546 int old_suid)
548 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
549 (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
550 !current->keep_capabilities) {
551 cap_clear (current->cap_permitted);
552 cap_clear (current->cap_effective);
554 if (old_euid == 0 && current->euid != 0) {
555 cap_clear (current->cap_effective);
557 if (old_euid != 0 && current->euid == 0) {
558 current->cap_effective = current->cap_permitted;
562 static int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
563 int flags)
565 switch (flags) {
566 case LSM_SETID_RE:
567 case LSM_SETID_ID:
568 case LSM_SETID_RES:
569 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
570 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
571 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
573 break;
574 case LSM_SETID_FS:
576 uid_t old_fsuid = old_ruid;
578 /* Copied from kernel/sys.c:setfsuid. */
581 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
582 * if not, we might be a bit too harsh here.
585 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
586 if (old_fsuid == 0 && current->fsuid != 0) {
587 cap_t (current->cap_effective) &=
588 ~CAP_FS_MASK;
590 if (old_fsuid != 0 && current->fsuid == 0) {
591 cap_t (current->cap_effective) |=
592 (cap_t (current->cap_permitted) &
593 CAP_FS_MASK);
596 break;
598 default:
599 return -EINVAL;
602 return 0;
605 static int cap_task_setgid (gid_t id0, gid_t id1, gid_t id2, int flags)
607 return 0;
610 static int cap_task_setpgid (struct task_struct *p, pid_t pgid)
612 return 0;
615 static int cap_task_getpgid (struct task_struct *p)
617 return 0;
620 static int cap_task_getsid (struct task_struct *p)
622 return 0;
625 static int cap_task_setgroups (int gidsetsize, gid_t * grouplist)
627 return 0;
630 static int cap_task_setnice (struct task_struct *p, int nice)
632 return 0;
635 static int cap_task_setrlimit (unsigned int resource, struct rlimit *new_rlim)
637 return 0;
640 static int cap_task_setscheduler (struct task_struct *p, int policy,
641 struct sched_param *lp)
643 return 0;
646 static int cap_task_getscheduler (struct task_struct *p)
648 return 0;
651 static int cap_task_wait (struct task_struct *p)
653 return 0;
656 static int cap_task_kill (struct task_struct *p, struct siginfo *info, int sig)
658 return 0;
661 static int cap_task_prctl (int option, unsigned long arg2, unsigned long arg3,
662 unsigned long arg4, unsigned long arg5)
664 return 0;
667 static void cap_task_kmod_set_label (void)
669 cap_set_full (current->cap_effective);
670 return;
673 static void cap_task_reparent_to_init (struct task_struct *p)
675 p->cap_effective = CAP_INIT_EFF_SET;
676 p->cap_inheritable = CAP_INIT_INH_SET;
677 p->cap_permitted = CAP_FULL_SET;
678 p->keep_capabilities = 0;
679 return;
682 static int cap_ipc_permission (struct kern_ipc_perm *ipcp, short flag)
684 return 0;
687 static int cap_msg_queue_alloc_security (struct msg_queue *msq)
689 return 0;
692 static void cap_msg_queue_free_security (struct msg_queue *msq)
694 return;
697 static int cap_shm_alloc_security (struct shmid_kernel *shp)
699 return 0;
702 static void cap_shm_free_security (struct shmid_kernel *shp)
704 return;
707 static int cap_sem_alloc_security (struct sem_array *sma)
709 return 0;
712 static void cap_sem_free_security (struct sem_array *sma)
714 return;
717 static int cap_register (const char *name, struct security_operations *ops)
719 return -EINVAL;
722 static int cap_unregister (const char *name, struct security_operations *ops)
724 return -EINVAL;
727 static struct security_operations capability_ops = {
728 .ptrace = cap_ptrace,
729 .capget = cap_capget,
730 .capset_check = cap_capset_check,
731 .capset_set = cap_capset_set,
732 .acct = cap_acct,
733 .capable = cap_capable,
734 .sys_security = cap_sys_security,
735 .quotactl = cap_quotactl,
736 .quota_on = cap_quota_on,
738 .bprm_alloc_security = cap_bprm_alloc_security,
739 .bprm_free_security = cap_bprm_free_security,
740 .bprm_compute_creds = cap_bprm_compute_creds,
741 .bprm_set_security = cap_bprm_set_security,
742 .bprm_check_security = cap_bprm_check_security,
744 .sb_alloc_security = cap_sb_alloc_security,
745 .sb_free_security = cap_sb_free_security,
746 .sb_statfs = cap_sb_statfs,
747 .sb_mount = cap_mount,
748 .sb_check_sb = cap_check_sb,
749 .sb_umount = cap_umount,
750 .sb_umount_close = cap_umount_close,
751 .sb_umount_busy = cap_umount_busy,
752 .sb_post_remount = cap_post_remount,
753 .sb_post_mountroot = cap_post_mountroot,
754 .sb_post_addmount = cap_post_addmount,
755 .sb_pivotroot = cap_pivotroot,
756 .sb_post_pivotroot = cap_post_pivotroot,
758 .inode_alloc_security = cap_inode_alloc_security,
759 .inode_free_security = cap_inode_free_security,
760 .inode_create = cap_inode_create,
761 .inode_post_create = cap_inode_post_create,
762 .inode_link = cap_inode_link,
763 .inode_post_link = cap_inode_post_link,
764 .inode_unlink = cap_inode_unlink,
765 .inode_symlink = cap_inode_symlink,
766 .inode_post_symlink = cap_inode_post_symlink,
767 .inode_mkdir = cap_inode_mkdir,
768 .inode_post_mkdir = cap_inode_post_mkdir,
769 .inode_rmdir = cap_inode_rmdir,
770 .inode_mknod = cap_inode_mknod,
771 .inode_post_mknod = cap_inode_post_mknod,
772 .inode_rename = cap_inode_rename,
773 .inode_post_rename = cap_inode_post_rename,
774 .inode_readlink = cap_inode_readlink,
775 .inode_follow_link = cap_inode_follow_link,
776 .inode_permission = cap_inode_permission,
777 .inode_permission_lite = cap_inode_permission_lite,
778 .inode_setattr = cap_inode_setattr,
779 .inode_getattr = cap_inode_getattr,
780 .inode_post_lookup = cap_post_lookup,
781 .inode_delete = cap_delete,
782 .inode_setxattr = cap_inode_setxattr,
783 .inode_getxattr = cap_inode_getxattr,
784 .inode_listxattr = cap_inode_listxattr,
785 .inode_removexattr = cap_inode_removexattr,
787 .file_permission = cap_file_permission,
788 .file_alloc_security = cap_file_alloc_security,
789 .file_free_security = cap_file_free_security,
790 .file_llseek = cap_file_llseek,
791 .file_ioctl = cap_file_ioctl,
792 .file_mmap = cap_file_mmap,
793 .file_mprotect = cap_file_mprotect,
794 .file_lock = cap_file_lock,
795 .file_fcntl = cap_file_fcntl,
796 .file_set_fowner = cap_file_set_fowner,
797 .file_send_sigiotask = cap_file_send_sigiotask,
798 .file_receive = cap_file_receive,
800 .task_create = cap_task_create,
801 .task_alloc_security = cap_task_alloc_security,
802 .task_free_security = cap_task_free_security,
803 .task_setuid = cap_task_setuid,
804 .task_post_setuid = cap_task_post_setuid,
805 .task_setgid = cap_task_setgid,
806 .task_setpgid = cap_task_setpgid,
807 .task_getpgid = cap_task_getpgid,
808 .task_getsid = cap_task_getsid,
809 .task_setgroups = cap_task_setgroups,
810 .task_setnice = cap_task_setnice,
811 .task_setrlimit = cap_task_setrlimit,
812 .task_setscheduler = cap_task_setscheduler,
813 .task_getscheduler = cap_task_getscheduler,
814 .task_wait = cap_task_wait,
815 .task_kill = cap_task_kill,
816 .task_prctl = cap_task_prctl,
817 .task_kmod_set_label = cap_task_kmod_set_label,
818 .task_reparent_to_init = cap_task_reparent_to_init,
820 .ipc_permission = cap_ipc_permission,
822 .msg_queue_alloc_security = cap_msg_queue_alloc_security,
823 .msg_queue_free_security = cap_msg_queue_free_security,
825 .shm_alloc_security = cap_shm_alloc_security,
826 .shm_free_security = cap_shm_free_security,
828 .sem_alloc_security = cap_sem_alloc_security,
829 .sem_free_security = cap_sem_free_security,
831 .register_security = cap_register,
832 .unregister_security = cap_unregister,
835 #if defined(CONFIG_SECURITY_CAPABILITIES_MODULE)
836 #define MY_NAME THIS_MODULE->name
837 #else
838 #define MY_NAME "capability"
839 #endif
841 static int __init capability_init (void)
843 /* register ourselves with the security framework */
844 if (register_security (&capability_ops)) {
845 printk (KERN_INFO
846 "Failure registering capabilities with the kernel\n");
847 /* try registering with primary module */
848 if (mod_reg_security (MY_NAME, &capability_ops)) {
849 printk (KERN_INFO "Failure registering capabilities "
850 "with primary security module.\n");
851 return -EINVAL;
853 secondary = 1;
855 printk (KERN_INFO "Capability LSM initialized\n");
856 return 0;
859 static void __exit capability_exit (void)
861 /* remove ourselves from the security framework */
862 if (secondary) {
863 if (mod_unreg_security (MY_NAME, &capability_ops))
864 printk (KERN_INFO "Failure unregistering capabilities "
865 "with primary module.\n");
866 return;
869 if (unregister_security (&capability_ops)) {
870 printk (KERN_INFO
871 "Failure unregistering capabilities with the kernel\n");
875 module_init (capability_init);
876 module_exit (capability_exit);
878 MODULE_DESCRIPTION("Standard Linux Capabilities Security Module");
879 MODULE_LICENSE("GPL");