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.
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>
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 */
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
))
34 static int cap_sys_security (unsigned int id
, unsigned int call
,
40 static int cap_quotactl (int cmds
, int type
, int id
, struct super_block
*sb
)
45 static int cap_quota_on (struct file
*f
)
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
))
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
);
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
))) {
83 /* verify restrictions on target's new Permitted set */
84 if (!cap_issubset (*permitted
,
85 cap_combine (target
->cap_permitted
,
86 current
->cap_permitted
))) {
90 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
91 if (!cap_issubset (*effective
, *permitted
)) {
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
)
113 static int cap_bprm_alloc_security (struct linux_binprm
*bprm
)
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
);
146 static int cap_bprm_check_security (struct linux_binprm
*bprm
)
151 static void cap_bprm_free_security (struct linux_binprm
*bprm
)
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
;
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;
177 if (must_not_trace_exec (current
)
178 || atomic_read (¤t
->fs
->count
) > 1
179 || atomic_read (¤t
->files
->count
) > 1
180 || atomic_read (¤t
->sig
->count
) > 1) {
181 if (!capable (CAP_SETPCAP
)) {
182 new_permitted
= cap_intersect (new_permitted
,
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 */
204 current
->keep_capabilities
= 0;
207 static int cap_sb_alloc_security (struct super_block
*sb
)
212 static void cap_sb_free_security (struct super_block
*sb
)
217 static int cap_sb_statfs (struct super_block
*sb
)
222 static int cap_mount (char *dev_name
, struct nameidata
*nd
, char *type
,
223 unsigned long flags
, void *data
)
228 static int cap_check_sb (struct vfsmount
*mnt
, struct nameidata
*nd
)
233 static int cap_umount (struct vfsmount
*mnt
, int flags
)
238 static void cap_umount_close (struct vfsmount
*mnt
)
243 static void cap_umount_busy (struct vfsmount
*mnt
)
248 static void cap_post_remount (struct vfsmount
*mnt
, unsigned long flags
,
254 static void cap_post_mountroot (void)
259 static void cap_post_addmount (struct vfsmount
*mnt
, struct nameidata
*nd
)
264 static int cap_pivotroot (struct nameidata
*old_nd
, struct nameidata
*new_nd
)
269 static void cap_post_pivotroot (struct nameidata
*old_nd
, struct nameidata
*new_nd
)
274 static int cap_inode_alloc_security (struct inode
*inode
)
279 static void cap_inode_free_security (struct inode
*inode
)
284 static int cap_inode_create (struct inode
*inode
, struct dentry
*dentry
,
290 static void cap_inode_post_create (struct inode
*inode
, struct dentry
*dentry
,
296 static int cap_inode_link (struct dentry
*old_dentry
, struct inode
*inode
,
297 struct dentry
*new_dentry
)
302 static void cap_inode_post_link (struct dentry
*old_dentry
, struct inode
*inode
,
303 struct dentry
*new_dentry
)
308 static int cap_inode_unlink (struct inode
*inode
, struct dentry
*dentry
)
313 static int cap_inode_symlink (struct inode
*inode
, struct dentry
*dentry
,
319 static void cap_inode_post_symlink (struct inode
*inode
, struct dentry
*dentry
,
325 static int cap_inode_mkdir (struct inode
*inode
, struct dentry
*dentry
,
331 static void cap_inode_post_mkdir (struct inode
*inode
, struct dentry
*dentry
,
337 static int cap_inode_rmdir (struct inode
*inode
, struct dentry
*dentry
)
342 static int cap_inode_mknod (struct inode
*inode
, struct dentry
*dentry
,
343 int major
, dev_t minor
)
348 static void cap_inode_post_mknod (struct inode
*inode
, struct dentry
*dentry
,
349 int major
, dev_t minor
)
354 static int cap_inode_rename (struct inode
*old_inode
, struct dentry
*old_dentry
,
355 struct inode
*new_inode
, struct dentry
*new_dentry
)
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
)
368 static int cap_inode_readlink (struct dentry
*dentry
)
373 static int cap_inode_follow_link (struct dentry
*dentry
,
374 struct nameidata
*nameidata
)
379 static int cap_inode_permission (struct inode
*inode
, int mask
)
384 static int cap_inode_permission_lite (struct inode
*inode
, int mask
)
389 static int cap_inode_setattr (struct dentry
*dentry
, struct iattr
*iattr
)
394 static int cap_inode_getattr (struct vfsmount
*mnt
, struct dentry
*dentry
)
399 static void cap_post_lookup (struct inode
*ino
, struct dentry
*d
)
404 static void cap_delete (struct inode
*ino
)
409 static int cap_inode_setxattr (struct dentry
*dentry
, char *name
, void *value
,
410 size_t size
, int flags
)
415 static int cap_inode_getxattr (struct dentry
*dentry
, char *name
)
420 static int cap_inode_listxattr (struct dentry
*dentry
)
425 static int cap_inode_removexattr (struct dentry
*dentry
, char *name
)
430 static int cap_file_permission (struct file
*file
, int mask
)
435 static int cap_file_alloc_security (struct file
*file
)
440 static void cap_file_free_security (struct file
*file
)
445 static int cap_file_llseek (struct file
*file
)
450 static int cap_file_ioctl (struct file
*file
, unsigned int command
,
456 static int cap_file_mmap (struct file
*file
, unsigned long prot
,
462 static int cap_file_mprotect (struct vm_area_struct
*vma
, unsigned long prot
)
467 static int cap_file_lock (struct file
*file
, unsigned int cmd
)
472 static int cap_file_fcntl (struct file
*file
, unsigned int cmd
,
478 static int cap_file_set_fowner (struct file
*file
)
483 static int cap_file_send_sigiotask (struct task_struct
*tsk
,
484 struct fown_struct
*fown
, int fd
,
490 static int cap_file_receive (struct file
*file
)
495 static int cap_task_create (unsigned long clone_flags
)
500 static int cap_task_alloc_security (struct task_struct
*p
)
505 static void cap_task_free_security (struct task_struct
*p
)
510 static int cap_task_setuid (uid_t id0
, uid_t id1
, uid_t id2
, int flags
)
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
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
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
543 * Thanks to Olaf Kirch and Peter Benie for spotting this.
545 static inline void cap_emulate_setxuid (int old_ruid
, int old_euid
,
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
,
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
);
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
) &=
590 if (old_fsuid
!= 0 && current
->fsuid
== 0) {
591 cap_t (current
->cap_effective
) |=
592 (cap_t (current
->cap_permitted
) &
605 static int cap_task_setgid (gid_t id0
, gid_t id1
, gid_t id2
, int flags
)
610 static int cap_task_setpgid (struct task_struct
*p
, pid_t pgid
)
615 static int cap_task_getpgid (struct task_struct
*p
)
620 static int cap_task_getsid (struct task_struct
*p
)
625 static int cap_task_setgroups (int gidsetsize
, gid_t
* grouplist
)
630 static int cap_task_setnice (struct task_struct
*p
, int nice
)
635 static int cap_task_setrlimit (unsigned int resource
, struct rlimit
*new_rlim
)
640 static int cap_task_setscheduler (struct task_struct
*p
, int policy
,
641 struct sched_param
*lp
)
646 static int cap_task_getscheduler (struct task_struct
*p
)
651 static int cap_task_wait (struct task_struct
*p
)
656 static int cap_task_kill (struct task_struct
*p
, struct siginfo
*info
, int sig
)
661 static int cap_task_prctl (int option
, unsigned long arg2
, unsigned long arg3
,
662 unsigned long arg4
, unsigned long arg5
)
667 static void cap_task_kmod_set_label (void)
669 cap_set_full (current
->cap_effective
);
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;
682 static int cap_ipc_permission (struct kern_ipc_perm
*ipcp
, short flag
)
687 static int cap_msg_queue_alloc_security (struct msg_queue
*msq
)
692 static void cap_msg_queue_free_security (struct msg_queue
*msq
)
697 static int cap_shm_alloc_security (struct shmid_kernel
*shp
)
702 static void cap_shm_free_security (struct shmid_kernel
*shp
)
707 static int cap_sem_alloc_security (struct sem_array
*sma
)
712 static void cap_sem_free_security (struct sem_array
*sma
)
717 static int cap_register (const char *name
, struct security_operations
*ops
)
722 static int cap_unregister (const char *name
, struct security_operations
*ops
)
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
,
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
838 #define MY_NAME "capability"
841 static int __init
capability_init (void)
843 /* register ourselves with the security framework */
844 if (register_security (&capability_ops
)) {
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");
855 printk (KERN_INFO
"Capability LSM initialized\n");
859 static void __exit
capability_exit (void)
861 /* remove ourselves from the security framework */
863 if (mod_unreg_security (MY_NAME
, &capability_ops
))
864 printk (KERN_INFO
"Failure unregistering capabilities "
865 "with primary module.\n");
869 if (unregister_security (&capability_ops
)) {
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");