2 * Capabilities Linux Security Module
4 * This is the default security module in case no other module is loaded.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
13 #include <linux/security.h>
15 static int cap_acct(struct file *file)
20 static int cap_sysctl(ctl_table *table, int op)
25 static int cap_quotactl(int cmds, int type, int id, struct super_block *sb)
30 static int cap_quota_on(struct dentry *dentry)
35 static int cap_bprm_alloc_security(struct linux_binprm *bprm)
40 static void cap_bprm_free_security(struct linux_binprm *bprm)
44 static void cap_bprm_post_apply_creds(struct linux_binprm *bprm)
48 static int cap_bprm_check_security(struct linux_binprm *bprm)
53 static int cap_sb_alloc_security(struct super_block *sb)
58 static void cap_sb_free_security(struct super_block *sb)
62 static int cap_sb_copy_data(char *orig, char *copy)
67 static int cap_sb_kern_mount(struct super_block *sb, void *data)
72 static int cap_sb_show_options(struct seq_file *m, struct super_block *sb)
77 static int cap_sb_statfs(struct dentry *dentry)
82 static int cap_sb_mount(char *dev_name, struct path *path, char *type,
83 unsigned long flags, void *data)
88 static int cap_sb_check_sb(struct vfsmount *mnt, struct path *path)
93 static int cap_sb_umount(struct vfsmount *mnt, int flags)
98 static void cap_sb_umount_close(struct vfsmount *mnt)
102 static void cap_sb_umount_busy(struct vfsmount *mnt)
106 static void cap_sb_post_remount(struct vfsmount *mnt, unsigned long flags,
111 static void cap_sb_post_addmount(struct vfsmount *mnt, struct path *path)
115 static int cap_sb_pivotroot(struct path *old_path, struct path *new_path)
120 static void cap_sb_post_pivotroot(struct path *old_path, struct path *new_path)
124 static int cap_sb_set_mnt_opts(struct super_block *sb,
125 struct security_mnt_opts *opts)
127 if (unlikely(opts->num_mnt_opts))
132 static void cap_sb_clone_mnt_opts(const struct super_block *oldsb,
133 struct super_block *newsb)
137 static int cap_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
142 static int cap_inode_alloc_security(struct inode *inode)
147 static void cap_inode_free_security(struct inode *inode)
151 static int cap_inode_init_security(struct inode *inode, struct inode *dir,
152 char **name, void **value, size_t *len)
157 static int cap_inode_create(struct inode *inode, struct dentry *dentry,
163 static int cap_inode_link(struct dentry *old_dentry, struct inode *inode,
164 struct dentry *new_dentry)
169 static int cap_inode_unlink(struct inode *inode, struct dentry *dentry)
174 static int cap_inode_symlink(struct inode *inode, struct dentry *dentry,
180 static int cap_inode_mkdir(struct inode *inode, struct dentry *dentry,
186 static int cap_inode_rmdir(struct inode *inode, struct dentry *dentry)
191 static int cap_inode_mknod(struct inode *inode, struct dentry *dentry,
197 static int cap_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
198 struct inode *new_inode, struct dentry *new_dentry)
203 static int cap_inode_readlink(struct dentry *dentry)
208 static int cap_inode_follow_link(struct dentry *dentry,
209 struct nameidata *nameidata)
214 static int cap_inode_permission(struct inode *inode, int mask)
219 static int cap_inode_setattr(struct dentry *dentry, struct iattr *iattr)
224 static int cap_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
229 static void cap_inode_delete(struct inode *ino)
233 static void cap_inode_post_setxattr(struct dentry *dentry, const char *name,
234 const void *value, size_t size, int flags)
238 static int cap_inode_getxattr(struct dentry *dentry, const char *name)
243 static int cap_inode_listxattr(struct dentry *dentry)
248 static int cap_inode_getsecurity(const struct inode *inode, const char *name,
249 void **buffer, bool alloc)
254 static int cap_inode_setsecurity(struct inode *inode, const char *name,
255 const void *value, size_t size, int flags)
260 static int cap_inode_listsecurity(struct inode *inode, char *buffer,
266 static void cap_inode_getsecid(const struct inode *inode, u32 *secid)
271 static int cap_file_permission(struct file *file, int mask)
276 static int cap_file_alloc_security(struct file *file)
281 static void cap_file_free_security(struct file *file)
285 static int cap_file_ioctl(struct file *file, unsigned int command,
291 static int cap_file_mmap(struct file *file, unsigned long reqprot,
292 unsigned long prot, unsigned long flags,
293 unsigned long addr, unsigned long addr_only)
295 if ((addr < mmap_min_addr) && !capable(CAP_SYS_RAWIO))
300 static int cap_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
306 static int cap_file_lock(struct file *file, unsigned int cmd)
311 static int cap_file_fcntl(struct file *file, unsigned int cmd,
317 static int cap_file_set_fowner(struct file *file)
322 static int cap_file_send_sigiotask(struct task_struct *tsk,
323 struct fown_struct *fown, int sig)
328 static int cap_file_receive(struct file *file)
333 static int cap_dentry_open(struct file *file)
338 static int cap_task_create(unsigned long clone_flags)
343 static int cap_task_alloc_security(struct task_struct *p)
348 static void cap_task_free_security(struct task_struct *p)
352 static int cap_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
357 static int cap_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
362 static int cap_task_setpgid(struct task_struct *p, pid_t pgid)
367 static int cap_task_getpgid(struct task_struct *p)
372 static int cap_task_getsid(struct task_struct *p)
377 static void cap_task_getsecid(struct task_struct *p, u32 *secid)
382 static int cap_task_setgroups(struct group_info *group_info)
387 static int cap_task_getioprio(struct task_struct *p)
392 static int cap_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
397 static int cap_task_getscheduler(struct task_struct *p)
402 static int cap_task_movememory(struct task_struct *p)
407 static int cap_task_wait(struct task_struct *p)
412 static int cap_task_kill(struct task_struct *p, struct siginfo *info,
418 static void cap_task_to_inode(struct task_struct *p, struct inode *inode)
422 static int cap_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
427 static void cap_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
432 static int cap_msg_msg_alloc_security(struct msg_msg *msg)
437 static void cap_msg_msg_free_security(struct msg_msg *msg)
441 static int cap_msg_queue_alloc_security(struct msg_queue *msq)
446 static void cap_msg_queue_free_security(struct msg_queue *msq)
450 static int cap_msg_queue_associate(struct msg_queue *msq, int msqflg)
455 static int cap_msg_queue_msgctl(struct msg_queue *msq, int cmd)
460 static int cap_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
466 static int cap_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
467 struct task_struct *target, long type, int mode)
472 static int cap_shm_alloc_security(struct shmid_kernel *shp)
477 static void cap_shm_free_security(struct shmid_kernel *shp)
481 static int cap_shm_associate(struct shmid_kernel *shp, int shmflg)
486 static int cap_shm_shmctl(struct shmid_kernel *shp, int cmd)
491 static int cap_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
497 static int cap_sem_alloc_security(struct sem_array *sma)
502 static void cap_sem_free_security(struct sem_array *sma)
506 static int cap_sem_associate(struct sem_array *sma, int semflg)
511 static int cap_sem_semctl(struct sem_array *sma, int cmd)
516 static int cap_sem_semop(struct sem_array *sma, struct sembuf *sops,
517 unsigned nsops, int alter)
522 #ifdef CONFIG_SECURITY_NETWORK
523 static int cap_unix_stream_connect(struct socket *sock, struct socket *other,
529 static int cap_unix_may_send(struct socket *sock, struct socket *other)
534 static int cap_socket_create(int family, int type, int protocol, int kern)
539 static int cap_socket_post_create(struct socket *sock, int family, int type,
540 int protocol, int kern)
545 static int cap_socket_bind(struct socket *sock, struct sockaddr *address,
551 static int cap_socket_connect(struct socket *sock, struct sockaddr *address,
557 static int cap_socket_listen(struct socket *sock, int backlog)
562 static int cap_socket_accept(struct socket *sock, struct socket *newsock)
567 static void cap_socket_post_accept(struct socket *sock, struct socket *newsock)
571 static int cap_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
576 static int cap_socket_recvmsg(struct socket *sock, struct msghdr *msg,
582 static int cap_socket_getsockname(struct socket *sock)
587 static int cap_socket_getpeername(struct socket *sock)
592 static int cap_socket_setsockopt(struct socket *sock, int level, int optname)
597 static int cap_socket_getsockopt(struct socket *sock, int level, int optname)
602 static int cap_socket_shutdown(struct socket *sock, int how)
607 static int cap_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
612 static int cap_socket_getpeersec_stream(struct socket *sock,
614 int __user *optlen, unsigned len)
619 static int cap_socket_getpeersec_dgram(struct socket *sock,
620 struct sk_buff *skb, u32 *secid)
625 static int cap_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
630 static void cap_sk_free_security(struct sock *sk)
634 static void cap_sk_clone_security(const struct sock *sk, struct sock *newsk)
638 static void cap_sk_getsecid(struct sock *sk, u32 *secid)
642 static void cap_sock_graft(struct sock *sk, struct socket *parent)
646 static int cap_inet_conn_request(struct sock *sk, struct sk_buff *skb,
647 struct request_sock *req)
652 static void cap_inet_csk_clone(struct sock *newsk,
653 const struct request_sock *req)
657 static void cap_inet_conn_established(struct sock *sk, struct sk_buff *skb)
661 static void cap_req_classify_flow(const struct request_sock *req,
665 #endif /* CONFIG_SECURITY_NETWORK */
667 #ifdef CONFIG_SECURITY_NETWORK_XFRM
668 static int cap_xfrm_policy_alloc_security(struct xfrm_sec_ctx **ctxp,
669 struct xfrm_user_sec_ctx *sec_ctx)
674 static int cap_xfrm_policy_clone_security(struct xfrm_sec_ctx *old_ctx,
675 struct xfrm_sec_ctx **new_ctxp)
680 static void cap_xfrm_policy_free_security(struct xfrm_sec_ctx *ctx)
684 static int cap_xfrm_policy_delete_security(struct xfrm_sec_ctx *ctx)
689 static int cap_xfrm_state_alloc_security(struct xfrm_state *x,
690 struct xfrm_user_sec_ctx *sec_ctx,
696 static void cap_xfrm_state_free_security(struct xfrm_state *x)
700 static int cap_xfrm_state_delete_security(struct xfrm_state *x)
705 static int cap_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 sk_sid, u8 dir)
710 static int cap_xfrm_state_pol_flow_match(struct xfrm_state *x,
711 struct xfrm_policy *xp,
717 static int cap_xfrm_decode_session(struct sk_buff *skb, u32 *fl, int ckall)
722 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
723 static void cap_d_instantiate(struct dentry *dentry, struct inode *inode)
727 static int cap_getprocattr(struct task_struct *p, char *name, char **value)
732 static int cap_setprocattr(struct task_struct *p, char *name, void *value,
738 static int cap_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
743 static int cap_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
748 static void cap_release_secctx(char *secdata, u32 seclen)
753 static int cap_key_alloc(struct key *key, struct task_struct *ctx,
759 static void cap_key_free(struct key *key)
763 static int cap_key_permission(key_ref_t key_ref, struct task_struct *context,
769 static int cap_key_getsecurity(struct key *key, char **_buffer)
775 #endif /* CONFIG_KEYS */
778 static int cap_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
783 static int cap_audit_rule_known(struct audit_krule *krule)
788 static int cap_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
789 struct audit_context *actx)
794 static void cap_audit_rule_free(void *lsmrule)
797 #endif /* CONFIG_AUDIT */
799 struct security_operations default_security_ops = {
803 #define set_to_cap_if_null(ops, function) \
805 if (!ops->function) { \
806 ops->function = cap_##function; \
807 pr_debug("Had to override the " #function \
808 " security operation with the default.\n");\
812 void security_fixup_ops(struct security_operations *ops)
814 set_to_cap_if_null(ops, ptrace_may_access);
815 set_to_cap_if_null(ops, ptrace_traceme);
816 set_to_cap_if_null(ops, capget);
817 set_to_cap_if_null(ops, capset_check);
818 set_to_cap_if_null(ops, capset_set);
819 set_to_cap_if_null(ops, acct);
820 set_to_cap_if_null(ops, capable);
821 set_to_cap_if_null(ops, quotactl);
822 set_to_cap_if_null(ops, quota_on);
823 set_to_cap_if_null(ops, sysctl);
824 set_to_cap_if_null(ops, syslog);
825 set_to_cap_if_null(ops, settime);
826 set_to_cap_if_null(ops, vm_enough_memory);
827 set_to_cap_if_null(ops, bprm_alloc_security);
828 set_to_cap_if_null(ops, bprm_free_security);
829 set_to_cap_if_null(ops, bprm_apply_creds);
830 set_to_cap_if_null(ops, bprm_post_apply_creds);
831 set_to_cap_if_null(ops, bprm_set_security);
832 set_to_cap_if_null(ops, bprm_check_security);
833 set_to_cap_if_null(ops, bprm_secureexec);
834 set_to_cap_if_null(ops, sb_alloc_security);
835 set_to_cap_if_null(ops, sb_free_security);
836 set_to_cap_if_null(ops, sb_copy_data);
837 set_to_cap_if_null(ops, sb_kern_mount);
838 set_to_cap_if_null(ops, sb_show_options);
839 set_to_cap_if_null(ops, sb_statfs);
840 set_to_cap_if_null(ops, sb_mount);
841 set_to_cap_if_null(ops, sb_check_sb);
842 set_to_cap_if_null(ops, sb_umount);
843 set_to_cap_if_null(ops, sb_umount_close);
844 set_to_cap_if_null(ops, sb_umount_busy);
845 set_to_cap_if_null(ops, sb_post_remount);
846 set_to_cap_if_null(ops, sb_post_addmount);
847 set_to_cap_if_null(ops, sb_pivotroot);
848 set_to_cap_if_null(ops, sb_post_pivotroot);
849 set_to_cap_if_null(ops, sb_set_mnt_opts);
850 set_to_cap_if_null(ops, sb_clone_mnt_opts);
851 set_to_cap_if_null(ops, sb_parse_opts_str);
852 set_to_cap_if_null(ops, inode_alloc_security);
853 set_to_cap_if_null(ops, inode_free_security);
854 set_to_cap_if_null(ops, inode_init_security);
855 set_to_cap_if_null(ops, inode_create);
856 set_to_cap_if_null(ops, inode_link);
857 set_to_cap_if_null(ops, inode_unlink);
858 set_to_cap_if_null(ops, inode_symlink);
859 set_to_cap_if_null(ops, inode_mkdir);
860 set_to_cap_if_null(ops, inode_rmdir);
861 set_to_cap_if_null(ops, inode_mknod);
862 set_to_cap_if_null(ops, inode_rename);
863 set_to_cap_if_null(ops, inode_readlink);
864 set_to_cap_if_null(ops, inode_follow_link);
865 set_to_cap_if_null(ops, inode_permission);
866 set_to_cap_if_null(ops, inode_setattr);
867 set_to_cap_if_null(ops, inode_getattr);
868 set_to_cap_if_null(ops, inode_delete);
869 set_to_cap_if_null(ops, inode_setxattr);
870 set_to_cap_if_null(ops, inode_post_setxattr);
871 set_to_cap_if_null(ops, inode_getxattr);
872 set_to_cap_if_null(ops, inode_listxattr);
873 set_to_cap_if_null(ops, inode_removexattr);
874 set_to_cap_if_null(ops, inode_need_killpriv);
875 set_to_cap_if_null(ops, inode_killpriv);
876 set_to_cap_if_null(ops, inode_getsecurity);
877 set_to_cap_if_null(ops, inode_setsecurity);
878 set_to_cap_if_null(ops, inode_listsecurity);
879 set_to_cap_if_null(ops, inode_getsecid);
880 set_to_cap_if_null(ops, file_permission);
881 set_to_cap_if_null(ops, file_alloc_security);
882 set_to_cap_if_null(ops, file_free_security);
883 set_to_cap_if_null(ops, file_ioctl);
884 set_to_cap_if_null(ops, file_mmap);
885 set_to_cap_if_null(ops, file_mprotect);
886 set_to_cap_if_null(ops, file_lock);
887 set_to_cap_if_null(ops, file_fcntl);
888 set_to_cap_if_null(ops, file_set_fowner);
889 set_to_cap_if_null(ops, file_send_sigiotask);
890 set_to_cap_if_null(ops, file_receive);
891 set_to_cap_if_null(ops, dentry_open);
892 set_to_cap_if_null(ops, task_create);
893 set_to_cap_if_null(ops, task_alloc_security);
894 set_to_cap_if_null(ops, task_free_security);
895 set_to_cap_if_null(ops, task_setuid);
896 set_to_cap_if_null(ops, task_post_setuid);
897 set_to_cap_if_null(ops, task_setgid);
898 set_to_cap_if_null(ops, task_setpgid);
899 set_to_cap_if_null(ops, task_getpgid);
900 set_to_cap_if_null(ops, task_getsid);
901 set_to_cap_if_null(ops, task_getsecid);
902 set_to_cap_if_null(ops, task_setgroups);
903 set_to_cap_if_null(ops, task_setnice);
904 set_to_cap_if_null(ops, task_setioprio);
905 set_to_cap_if_null(ops, task_getioprio);
906 set_to_cap_if_null(ops, task_setrlimit);
907 set_to_cap_if_null(ops, task_setscheduler);
908 set_to_cap_if_null(ops, task_getscheduler);
909 set_to_cap_if_null(ops, task_movememory);
910 set_to_cap_if_null(ops, task_wait);
911 set_to_cap_if_null(ops, task_kill);
912 set_to_cap_if_null(ops, task_prctl);
913 set_to_cap_if_null(ops, task_reparent_to_init);
914 set_to_cap_if_null(ops, task_to_inode);
915 set_to_cap_if_null(ops, ipc_permission);
916 set_to_cap_if_null(ops, ipc_getsecid);
917 set_to_cap_if_null(ops, msg_msg_alloc_security);
918 set_to_cap_if_null(ops, msg_msg_free_security);
919 set_to_cap_if_null(ops, msg_queue_alloc_security);
920 set_to_cap_if_null(ops, msg_queue_free_security);
921 set_to_cap_if_null(ops, msg_queue_associate);
922 set_to_cap_if_null(ops, msg_queue_msgctl);
923 set_to_cap_if_null(ops, msg_queue_msgsnd);
924 set_to_cap_if_null(ops, msg_queue_msgrcv);
925 set_to_cap_if_null(ops, shm_alloc_security);
926 set_to_cap_if_null(ops, shm_free_security);
927 set_to_cap_if_null(ops, shm_associate);
928 set_to_cap_if_null(ops, shm_shmctl);
929 set_to_cap_if_null(ops, shm_shmat);
930 set_to_cap_if_null(ops, sem_alloc_security);
931 set_to_cap_if_null(ops, sem_free_security);
932 set_to_cap_if_null(ops, sem_associate);
933 set_to_cap_if_null(ops, sem_semctl);
934 set_to_cap_if_null(ops, sem_semop);
935 set_to_cap_if_null(ops, netlink_send);
936 set_to_cap_if_null(ops, netlink_recv);
937 set_to_cap_if_null(ops, d_instantiate);
938 set_to_cap_if_null(ops, getprocattr);
939 set_to_cap_if_null(ops, setprocattr);
940 set_to_cap_if_null(ops, secid_to_secctx);
941 set_to_cap_if_null(ops, secctx_to_secid);
942 set_to_cap_if_null(ops, release_secctx);
943 #ifdef CONFIG_SECURITY_NETWORK
944 set_to_cap_if_null(ops, unix_stream_connect);
945 set_to_cap_if_null(ops, unix_may_send);
946 set_to_cap_if_null(ops, socket_create);
947 set_to_cap_if_null(ops, socket_post_create);
948 set_to_cap_if_null(ops, socket_bind);
949 set_to_cap_if_null(ops, socket_connect);
950 set_to_cap_if_null(ops, socket_listen);
951 set_to_cap_if_null(ops, socket_accept);
952 set_to_cap_if_null(ops, socket_post_accept);
953 set_to_cap_if_null(ops, socket_sendmsg);
954 set_to_cap_if_null(ops, socket_recvmsg);
955 set_to_cap_if_null(ops, socket_getsockname);
956 set_to_cap_if_null(ops, socket_getpeername);
957 set_to_cap_if_null(ops, socket_setsockopt);
958 set_to_cap_if_null(ops, socket_getsockopt);
959 set_to_cap_if_null(ops, socket_shutdown);
960 set_to_cap_if_null(ops, socket_sock_rcv_skb);
961 set_to_cap_if_null(ops, socket_getpeersec_stream);
962 set_to_cap_if_null(ops, socket_getpeersec_dgram);
963 set_to_cap_if_null(ops, sk_alloc_security);
964 set_to_cap_if_null(ops, sk_free_security);
965 set_to_cap_if_null(ops, sk_clone_security);
966 set_to_cap_if_null(ops, sk_getsecid);
967 set_to_cap_if_null(ops, sock_graft);
968 set_to_cap_if_null(ops, inet_conn_request);
969 set_to_cap_if_null(ops, inet_csk_clone);
970 set_to_cap_if_null(ops, inet_conn_established);
971 set_to_cap_if_null(ops, req_classify_flow);
972 #endif /* CONFIG_SECURITY_NETWORK */
973 #ifdef CONFIG_SECURITY_NETWORK_XFRM
974 set_to_cap_if_null(ops, xfrm_policy_alloc_security);
975 set_to_cap_if_null(ops, xfrm_policy_clone_security);
976 set_to_cap_if_null(ops, xfrm_policy_free_security);
977 set_to_cap_if_null(ops, xfrm_policy_delete_security);
978 set_to_cap_if_null(ops, xfrm_state_alloc_security);
979 set_to_cap_if_null(ops, xfrm_state_free_security);
980 set_to_cap_if_null(ops, xfrm_state_delete_security);
981 set_to_cap_if_null(ops, xfrm_policy_lookup);
982 set_to_cap_if_null(ops, xfrm_state_pol_flow_match);
983 set_to_cap_if_null(ops, xfrm_decode_session);
984 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
986 set_to_cap_if_null(ops, key_alloc);
987 set_to_cap_if_null(ops, key_free);
988 set_to_cap_if_null(ops, key_permission);
989 set_to_cap_if_null(ops, key_getsecurity);
990 #endif /* CONFIG_KEYS */
992 set_to_cap_if_null(ops, audit_rule_init);
993 set_to_cap_if_null(ops, audit_rule_known);
994 set_to_cap_if_null(ops, audit_rule_match);
995 set_to_cap_if_null(ops, audit_rule_free);