aboutsummaryrefslogtreecommitdiffstats
path: root/security/selinux/hooks.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/selinux/hooks.c')
-rw-r--r--security/selinux/hooks.c162
1 files changed, 58 insertions, 104 deletions
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 9a2ee845e9d4..5c9f25ba1c95 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -76,6 +76,7 @@
76#include <linux/selinux.h> 76#include <linux/selinux.h>
77#include <linux/mutex.h> 77#include <linux/mutex.h>
78#include <linux/posix-timers.h> 78#include <linux/posix-timers.h>
79#include <linux/syslog.h>
79 80
80#include "avc.h" 81#include "avc.h"
81#include "objsec.h" 82#include "objsec.h"
@@ -125,18 +126,6 @@ __setup("selinux=", selinux_enabled_setup);
125int selinux_enabled = 1; 126int selinux_enabled = 1;
126#endif 127#endif
127 128
128
129/*
130 * Minimal support for a secondary security module,
131 * just to allow the use of the capability module.
132 */
133static struct security_operations *secondary_ops;
134
135/* Lists of inode and superblock security structures initialized
136 before the policy was loaded. */
137static LIST_HEAD(superblock_security_head);
138static DEFINE_SPINLOCK(sb_security_lock);
139
140static struct kmem_cache *sel_inode_cache; 129static struct kmem_cache *sel_inode_cache;
141 130
142/** 131/**
@@ -272,7 +261,6 @@ static int superblock_alloc_security(struct super_block *sb)
272 return -ENOMEM; 261 return -ENOMEM;
273 262
274 mutex_init(&sbsec->lock); 263 mutex_init(&sbsec->lock);
275 INIT_LIST_HEAD(&sbsec->list);
276 INIT_LIST_HEAD(&sbsec->isec_head); 264 INIT_LIST_HEAD(&sbsec->isec_head);
277 spin_lock_init(&sbsec->isec_lock); 265 spin_lock_init(&sbsec->isec_lock);
278 sbsec->sb = sb; 266 sbsec->sb = sb;
@@ -287,40 +275,34 @@ static int superblock_alloc_security(struct super_block *sb)
287static void superblock_free_security(struct super_block *sb) 275static void superblock_free_security(struct super_block *sb)
288{ 276{
289 struct superblock_security_struct *sbsec = sb->s_security; 277 struct superblock_security_struct *sbsec = sb->s_security;
290
291 spin_lock(&sb_security_lock);
292 if (!list_empty(&sbsec->list))
293 list_del_init(&sbsec->list);
294 spin_unlock(&sb_security_lock);
295
296 sb->s_security = NULL; 278 sb->s_security = NULL;
297 kfree(sbsec); 279 kfree(sbsec);
298} 280}
299 281
300static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 282static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
301{ 283{
302 struct sk_security_struct *ssec; 284 struct sk_security_struct *sksec;
303 285
304 ssec = kzalloc(sizeof(*ssec), priority); 286 sksec = kzalloc(sizeof(*sksec), priority);
305 if (!ssec) 287 if (!sksec)
306 return -ENOMEM; 288 return -ENOMEM;
307 289
308 ssec->peer_sid = SECINITSID_UNLABELED; 290 sksec->peer_sid = SECINITSID_UNLABELED;
309 ssec->sid = SECINITSID_UNLABELED; 291 sksec->sid = SECINITSID_UNLABELED;
310 sk->sk_security = ssec; 292 sk->sk_security = sksec;
311 293
312 selinux_netlbl_sk_security_reset(ssec); 294 selinux_netlbl_sk_security_reset(sksec);
313 295
314 return 0; 296 return 0;
315} 297}
316 298
317static void sk_free_security(struct sock *sk) 299static void sk_free_security(struct sock *sk)
318{ 300{
319 struct sk_security_struct *ssec = sk->sk_security; 301 struct sk_security_struct *sksec = sk->sk_security;
320 302
321 sk->sk_security = NULL; 303 sk->sk_security = NULL;
322 selinux_netlbl_sk_security_free(ssec); 304 selinux_netlbl_sk_security_free(sksec);
323 kfree(ssec); 305 kfree(sksec);
324} 306}
325 307
326/* The security server must be initialized before 308/* The security server must be initialized before
@@ -329,7 +311,7 @@ extern int ss_initialized;
329 311
330/* The file system's label must be initialized prior to use. */ 312/* The file system's label must be initialized prior to use. */
331 313
332static char *labeling_behaviors[6] = { 314static const char *labeling_behaviors[6] = {
333 "uses xattr", 315 "uses xattr",
334 "uses transition SIDs", 316 "uses transition SIDs",
335 "uses task SIDs", 317 "uses task SIDs",
@@ -618,10 +600,6 @@ static int selinux_set_mnt_opts(struct super_block *sb,
618 /* Defer initialization until selinux_complete_init, 600 /* Defer initialization until selinux_complete_init,
619 after the initial policy is loaded and the security 601 after the initial policy is loaded and the security
620 server is ready to handle calls. */ 602 server is ready to handle calls. */
621 spin_lock(&sb_security_lock);
622 if (list_empty(&sbsec->list))
623 list_add(&sbsec->list, &superblock_security_head);
624 spin_unlock(&sb_security_lock);
625 goto out; 603 goto out;
626 } 604 }
627 rc = -EINVAL; 605 rc = -EINVAL;
@@ -812,16 +790,10 @@ static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
812 790
813 /* 791 /*
814 * if the parent was able to be mounted it clearly had no special lsm 792 * if the parent was able to be mounted it clearly had no special lsm
815 * mount options. thus we can safely put this sb on the list and deal 793 * mount options. thus we can safely deal with this superblock later
816 * with it later
817 */ 794 */
818 if (!ss_initialized) { 795 if (!ss_initialized)
819 spin_lock(&sb_security_lock);
820 if (list_empty(&newsbsec->list))
821 list_add(&newsbsec->list, &superblock_security_head);
822 spin_unlock(&sb_security_lock);
823 return; 796 return;
824 }
825 797
826 /* how can we clone if the old one wasn't set up?? */ 798 /* how can we clone if the old one wasn't set up?? */
827 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 799 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
@@ -2049,29 +2021,30 @@ static int selinux_quota_on(struct dentry *dentry)
2049 return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON); 2021 return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON);
2050} 2022}
2051 2023
2052static int selinux_syslog(int type) 2024static int selinux_syslog(int type, bool from_file)
2053{ 2025{
2054 int rc; 2026 int rc;
2055 2027
2056 rc = cap_syslog(type); 2028 rc = cap_syslog(type, from_file);
2057 if (rc) 2029 if (rc)
2058 return rc; 2030 return rc;
2059 2031
2060 switch (type) { 2032 switch (type) {
2061 case 3: /* Read last kernel messages */ 2033 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2062 case 10: /* Return size of the log buffer */ 2034 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2063 rc = task_has_system(current, SYSTEM__SYSLOG_READ); 2035 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2064 break; 2036 break;
2065 case 6: /* Disable logging to console */ 2037 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2066 case 7: /* Enable logging to console */ 2038 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2067 case 8: /* Set level of messages printed to console */ 2039 /* Set level of messages printed to console */
2040 case SYSLOG_ACTION_CONSOLE_LEVEL:
2068 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 2041 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2069 break; 2042 break;
2070 case 0: /* Close log */ 2043 case SYSLOG_ACTION_CLOSE: /* Close log */
2071 case 1: /* Open log */ 2044 case SYSLOG_ACTION_OPEN: /* Open log */
2072 case 2: /* Read from log */ 2045 case SYSLOG_ACTION_READ: /* Read from log */
2073 case 4: /* Read/clear last kernel messages */ 2046 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2074 case 5: /* Clear ring buffer */ 2047 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
2075 default: 2048 default:
2076 rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 2049 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2077 break; 2050 break;
@@ -3004,13 +2977,15 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3004 return file_has_perm(cred, file, av); 2977 return file_has_perm(cred, file, av);
3005} 2978}
3006 2979
2980static int default_noexec;
2981
3007static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 2982static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3008{ 2983{
3009 const struct cred *cred = current_cred(); 2984 const struct cred *cred = current_cred();
3010 int rc = 0; 2985 int rc = 0;
3011 2986
3012#ifndef CONFIG_PPC32 2987 if (default_noexec &&
3013 if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 2988 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
3014 /* 2989 /*
3015 * We are making executable an anonymous mapping or a 2990 * We are making executable an anonymous mapping or a
3016 * private file mapping that will also be writable. 2991 * private file mapping that will also be writable.
@@ -3020,7 +2995,6 @@ static int file_map_prot_check(struct file *file, unsigned long prot, int shared
3020 if (rc) 2995 if (rc)
3021 goto error; 2996 goto error;
3022 } 2997 }
3023#endif
3024 2998
3025 if (file) { 2999 if (file) {
3026 /* read access is always possible with a mapping */ 3000 /* read access is always possible with a mapping */
@@ -3081,8 +3055,8 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
3081 if (selinux_checkreqprot) 3055 if (selinux_checkreqprot)
3082 prot = reqprot; 3056 prot = reqprot;
3083 3057
3084#ifndef CONFIG_PPC32 3058 if (default_noexec &&
3085 if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3059 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3086 int rc = 0; 3060 int rc = 0;
3087 if (vma->vm_start >= vma->vm_mm->start_brk && 3061 if (vma->vm_start >= vma->vm_mm->start_brk &&
3088 vma->vm_end <= vma->vm_mm->brk) { 3062 vma->vm_end <= vma->vm_mm->brk) {
@@ -3104,7 +3078,6 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
3104 if (rc) 3078 if (rc)
3105 return rc; 3079 return rc;
3106 } 3080 }
3107#endif
3108 3081
3109 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 3082 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3110} 3083}
@@ -3334,7 +3307,7 @@ static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3334 3307
3335 if (ret == 0) 3308 if (ret == 0)
3336 tsec->create_sid = isec->sid; 3309 tsec->create_sid = isec->sid;
3337 return 0; 3310 return ret;
3338} 3311}
3339 3312
3340static int selinux_kernel_module_request(char *kmod_name) 3313static int selinux_kernel_module_request(char *kmod_name)
@@ -4007,7 +3980,7 @@ static int selinux_socket_unix_stream_connect(struct socket *sock,
4007 struct socket *other, 3980 struct socket *other,
4008 struct sock *newsk) 3981 struct sock *newsk)
4009{ 3982{
4010 struct sk_security_struct *ssec; 3983 struct sk_security_struct *sksec;
4011 struct inode_security_struct *isec; 3984 struct inode_security_struct *isec;
4012 struct inode_security_struct *other_isec; 3985 struct inode_security_struct *other_isec;
4013 struct common_audit_data ad; 3986 struct common_audit_data ad;
@@ -4026,13 +3999,13 @@ static int selinux_socket_unix_stream_connect(struct socket *sock,
4026 return err; 3999 return err;
4027 4000
4028 /* connecting socket */ 4001 /* connecting socket */
4029 ssec = sock->sk->sk_security; 4002 sksec = sock->sk->sk_security;
4030 ssec->peer_sid = other_isec->sid; 4003 sksec->peer_sid = other_isec->sid;
4031 4004
4032 /* server child socket */ 4005 /* server child socket */
4033 ssec = newsk->sk_security; 4006 sksec = newsk->sk_security;
4034 ssec->peer_sid = isec->sid; 4007 sksec->peer_sid = isec->sid;
4035 err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 4008 err = security_sid_mls_copy(other_isec->sid, sksec->peer_sid, &sksec->sid);
4036 4009
4037 return err; 4010 return err;
4038} 4011}
@@ -4195,7 +4168,7 @@ static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *op
4195 int err = 0; 4168 int err = 0;
4196 char *scontext; 4169 char *scontext;
4197 u32 scontext_len; 4170 u32 scontext_len;
4198 struct sk_security_struct *ssec; 4171 struct sk_security_struct *sksec;
4199 struct inode_security_struct *isec; 4172 struct inode_security_struct *isec;
4200 u32 peer_sid = SECSID_NULL; 4173 u32 peer_sid = SECSID_NULL;
4201 4174
@@ -4203,8 +4176,8 @@ static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *op
4203 4176
4204 if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4177 if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4205 isec->sclass == SECCLASS_TCP_SOCKET) { 4178 isec->sclass == SECCLASS_TCP_SOCKET) {
4206 ssec = sock->sk->sk_security; 4179 sksec = sock->sk->sk_security;
4207 peer_sid = ssec->peer_sid; 4180 peer_sid = sksec->peer_sid;
4208 } 4181 }
4209 if (peer_sid == SECSID_NULL) { 4182 if (peer_sid == SECSID_NULL) {
4210 err = -ENOPROTOOPT; 4183 err = -ENOPROTOOPT;
@@ -4271,14 +4244,14 @@ static void selinux_sk_free_security(struct sock *sk)
4271 4244
4272static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4245static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4273{ 4246{
4274 struct sk_security_struct *ssec = sk->sk_security; 4247 struct sk_security_struct *sksec = sk->sk_security;
4275 struct sk_security_struct *newssec = newsk->sk_security; 4248 struct sk_security_struct *newsksec = newsk->sk_security;
4276 4249
4277 newssec->sid = ssec->sid; 4250 newsksec->sid = sksec->sid;
4278 newssec->peer_sid = ssec->peer_sid; 4251 newsksec->peer_sid = sksec->peer_sid;
4279 newssec->sclass = ssec->sclass; 4252 newsksec->sclass = sksec->sclass;
4280 4253
4281 selinux_netlbl_sk_security_reset(newssec); 4254 selinux_netlbl_sk_security_reset(newsksec);
4282} 4255}
4283 4256
4284static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4257static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
@@ -5667,14 +5640,13 @@ static __init int selinux_init(void)
5667 /* Set the security state for the initial task. */ 5640 /* Set the security state for the initial task. */
5668 cred_init_security(); 5641 cred_init_security();
5669 5642
5643 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5644
5670 sel_inode_cache = kmem_cache_create("selinux_inode_security", 5645 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5671 sizeof(struct inode_security_struct), 5646 sizeof(struct inode_security_struct),
5672 0, SLAB_PANIC, NULL); 5647 0, SLAB_PANIC, NULL);
5673 avc_init(); 5648 avc_init();
5674 5649
5675 secondary_ops = security_ops;
5676 if (!secondary_ops)
5677 panic("SELinux: No initial security operations\n");
5678 if (register_security(&selinux_ops)) 5650 if (register_security(&selinux_ops))
5679 panic("SELinux: Unable to register with kernel.\n"); 5651 panic("SELinux: Unable to register with kernel.\n");
5680 5652
@@ -5686,35 +5658,18 @@ static __init int selinux_init(void)
5686 return 0; 5658 return 0;
5687} 5659}
5688 5660
5661static void delayed_superblock_init(struct super_block *sb, void *unused)
5662{
5663 superblock_doinit(sb, NULL);
5664}
5665
5689void selinux_complete_init(void) 5666void selinux_complete_init(void)
5690{ 5667{
5691 printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 5668 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
5692 5669
5693 /* Set up any superblocks initialized prior to the policy load. */ 5670 /* Set up any superblocks initialized prior to the policy load. */
5694 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5671 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
5695 spin_lock(&sb_lock); 5672 iterate_supers(delayed_superblock_init, NULL);
5696 spin_lock(&sb_security_lock);
5697next_sb:
5698 if (!list_empty(&superblock_security_head)) {
5699 struct superblock_security_struct *sbsec =
5700 list_entry(superblock_security_head.next,
5701 struct superblock_security_struct,
5702 list);
5703 struct super_block *sb = sbsec->sb;
5704 sb->s_count++;
5705 spin_unlock(&sb_security_lock);
5706 spin_unlock(&sb_lock);
5707 down_read(&sb->s_umount);
5708 if (sb->s_root)
5709 superblock_doinit(sb, NULL);
5710 drop_super(sb);
5711 spin_lock(&sb_lock);
5712 spin_lock(&sb_security_lock);
5713 list_del_init(&sbsec->list);
5714 goto next_sb;
5715 }
5716 spin_unlock(&sb_security_lock);
5717 spin_unlock(&sb_lock);
5718} 5673}
5719 5674
5720/* SELinux requires early initialization in order to label 5675/* SELinux requires early initialization in order to label
@@ -5835,8 +5790,7 @@ int selinux_disable(void)
5835 selinux_disabled = 1; 5790 selinux_disabled = 1;
5836 selinux_enabled = 0; 5791 selinux_enabled = 0;
5837 5792
5838 /* Reset security_ops to the secondary module, dummy or capability. */ 5793 reset_security_ops();
5839 security_ops = secondary_ops;
5840 5794
5841 /* Try to destroy the avc node cache */ 5795 /* Try to destroy the avc node cache */
5842 avc_disable(); 5796 avc_disable();