[ARM] 4224/2: allow XIP kernel to boot again
[safe/jmp/linux-2.6] / security / selinux / selinuxfs.c
index ab7c993..93b3177 100644 (file)
@@ -9,7 +9,6 @@
  *     the Free Software Foundation, version 2.
  */
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/pagemap.h>
 #include <linux/slab.h>
@@ -22,6 +21,7 @@
 #include <linux/major.h>
 #include <linux/seq_file.h>
 #include <linux/percpu.h>
+#include <linux/audit.h>
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
 
 
 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
 
+#ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
+#define SELINUX_COMPAT_NET_VALUE 0
+#else
+#define SELINUX_COMPAT_NET_VALUE 1
+#endif
+
+int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
+
 static int __init checkreqprot_setup(char *str)
 {
        selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
@@ -44,6 +52,13 @@ static int __init checkreqprot_setup(char *str)
 }
 __setup("checkreqprot=", checkreqprot_setup);
 
+static int __init selinux_compat_net_setup(char *str)
+{
+       selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
+       return 1;
+}
+__setup("selinux_compat_net=", selinux_compat_net_setup);
+
 
 static DEFINE_MUTEX(sel_mutex);
 
@@ -84,6 +99,7 @@ enum sel_inos {
        SEL_AVC,        /* AVC management directory */
        SEL_MEMBER,     /* compute polyinstantiation membership decision */
        SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
+       SEL_COMPAT_NET, /* whether to use old compat network packet controls */
 };
 
 #define TMPBUFLEN      12
@@ -127,6 +143,10 @@ static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
                length = task_has_security(current, SECURITY__SETENFORCE);
                if (length)
                        goto out;
+               audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
+                       "enforcing=%d old_enforcing=%d auid=%u", new_value, 
+                       selinux_enforcing,
+                       audit_get_loginuid(current->audit_context));
                selinux_enforcing = new_value;
                if (selinux_enforcing)
                        avc_ss_reset(0);
@@ -141,7 +161,7 @@ out:
 #define sel_write_enforce NULL
 #endif
 
-static struct file_operations sel_enforce_ops = {
+static const struct file_operations sel_enforce_ops = {
        .read           = sel_read_enforce,
        .write          = sel_write_enforce,
 };
@@ -177,6 +197,9 @@ static ssize_t sel_write_disable(struct file * file, const char __user * buf,
                length = selinux_disable();
                if (length < 0)
                        goto out;
+               audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
+                       "selinux=0 auid=%u",
+                       audit_get_loginuid(current->audit_context));
        }
 
        length = count;
@@ -188,7 +211,7 @@ out:
 #define sel_write_disable NULL
 #endif
 
-static struct file_operations sel_disable_ops = {
+static const struct file_operations sel_disable_ops = {
        .write          = sel_write_disable,
 };
 
@@ -202,7 +225,7 @@ static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
        return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
 
-static struct file_operations sel_policyvers_ops = {
+static const struct file_operations sel_policyvers_ops = {
        .read           = sel_read_policyvers,
 };
 
@@ -219,7 +242,7 @@ static ssize_t sel_read_mls(struct file *filp, char __user *buf,
        return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
 
-static struct file_operations sel_mls_ops = {
+static const struct file_operations sel_mls_ops = {
        .read           = sel_read_mls,
 };
 
@@ -262,13 +285,16 @@ static ssize_t sel_write_load(struct file * file, const char __user * buf,
                length = ret;
        else
                length = count;
+       audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
+               "policy loaded auid=%u",
+               audit_get_loginuid(current->audit_context));
 out:
        mutex_unlock(&sel_mutex);
        vfree(data);
        return length;
 }
 
-static struct file_operations sel_load_ops = {
+static const struct file_operations sel_load_ops = {
        .write          = sel_write_load,
 };
 
@@ -348,11 +374,60 @@ out:
        free_page((unsigned long) page);
        return length;
 }
-static struct file_operations sel_checkreqprot_ops = {
+static const struct file_operations sel_checkreqprot_ops = {
        .read           = sel_read_checkreqprot,
        .write          = sel_write_checkreqprot,
 };
 
+static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
+                                  size_t count, loff_t *ppos)
+{
+       char tmpbuf[TMPBUFLEN];
+       ssize_t length;
+
+       length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
+       return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
+}
+
+static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
+                                   size_t count, loff_t *ppos)
+{
+       char *page;
+       ssize_t length;
+       int new_value;
+
+       length = task_has_security(current, SECURITY__LOAD_POLICY);
+       if (length)
+               return length;
+
+       if (count >= PAGE_SIZE)
+               return -ENOMEM;
+       if (*ppos != 0) {
+               /* No partial writes. */
+               return -EINVAL;
+       }
+       page = (char*)get_zeroed_page(GFP_KERNEL);
+       if (!page)
+               return -ENOMEM;
+       length = -EFAULT;
+       if (copy_from_user(page, buf, count))
+               goto out;
+
+       length = -EINVAL;
+       if (sscanf(page, "%d", &new_value) != 1)
+               goto out;
+
+       selinux_compat_net = new_value ? 1 : 0;
+       length = count;
+out:
+       free_page((unsigned long) page);
+       return length;
+}
+static const struct file_operations sel_compat_net_ops = {
+       .read           = sel_read_compat_net,
+       .write          = sel_write_compat_net,
+};
+
 /*
  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
  */
@@ -373,7 +448,7 @@ static ssize_t (*write_op[])(struct file *, char *, size_t) = {
 
 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
 {
-       ino_t ino =  file->f_dentry->d_inode->i_ino;
+       ino_t ino =  file->f_path.dentry->d_inode->i_ino;
        char *data;
        ssize_t rv;
 
@@ -392,7 +467,7 @@ static ssize_t selinux_transaction_write(struct file *file, const char __user *b
        return rv;
 }
 
-static struct file_operations transaction_ops = {
+static const struct file_operations transaction_ops = {
        .write          = selinux_transaction_write,
        .read           = simple_transaction_read,
        .release        = simple_transaction_release,
@@ -696,7 +771,6 @@ static struct inode *sel_make_inode(struct super_block *sb, int mode)
        if (ret) {
                ret->i_mode = mode;
                ret->i_uid = ret->i_gid = 0;
-               ret->i_blksize = PAGE_CACHE_SIZE;
                ret->i_blocks = 0;
                ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
        }
@@ -731,7 +805,7 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,
                goto out;
        }
 
-       inode = filep->f_dentry->d_inode;
+       inode = filep->f_path.dentry->d_inode;
        cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
        if (cur_enforcing < 0) {
                ret = cur_enforcing;
@@ -790,7 +864,7 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
        if (new_value)
                new_value = 1;
 
-       inode = filep->f_dentry->d_inode;
+       inode = filep->f_path.dentry->d_inode;
        bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
        length = count;
 
@@ -801,7 +875,7 @@ out:
        return length;
 }
 
-static struct file_operations sel_bool_ops = {
+static const struct file_operations sel_bool_ops = {
        .read           = sel_read_bool,
        .write          = sel_write_bool,
 };
@@ -858,7 +932,7 @@ out:
        return length;
 }
 
-static struct file_operations sel_commit_bools_ops = {
+static const struct file_operations sel_commit_bools_ops = {
        .write          = sel_commit_bools_write,
 };
 
@@ -891,7 +965,7 @@ static void sel_remove_bools(struct dentry *de)
        file_list_lock();
        list_for_each(p, &sb->s_files) {
                struct file * filp = list_entry(p, struct file, f_u.fu_list);
-               struct dentry * dentry = filp->f_dentry;
+               struct dentry * dentry = filp->f_path.dentry;
 
                if (dentry->d_parent != de) {
                        continue;
@@ -1057,12 +1131,12 @@ out:
        return ret;
 }
 
-static struct file_operations sel_avc_cache_threshold_ops = {
+static const struct file_operations sel_avc_cache_threshold_ops = {
        .read           = sel_read_avc_cache_threshold,
        .write          = sel_write_avc_cache_threshold,
 };
 
-static struct file_operations sel_avc_hash_stats_ops = {
+static const struct file_operations sel_avc_hash_stats_ops = {
        .read           = sel_read_avc_hash_stats,
 };
 
@@ -1124,7 +1198,7 @@ static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
        return seq_open(file, &sel_avc_cache_stats_seq_ops);
 }
 
-static struct file_operations sel_avc_cache_stats_ops = {
+static const struct file_operations sel_avc_cache_stats_ops = {
        .open           = sel_open_avc_cache_stats,
        .read           = seq_read,
        .llseek         = seq_lseek,
@@ -1151,30 +1225,27 @@ static int sel_make_avc_files(struct dentry *dir)
                dentry = d_alloc_name(dir, files[i].name);
                if (!dentry) {
                        ret = -ENOMEM;
-                       goto err;
+                       goto out;
                }
 
                inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
                if (!inode) {
                        ret = -ENOMEM;
-                       goto err;
+                       goto out;
                }
                inode->i_fop = files[i].ops;
                d_add(dentry, inode);
        }
 out:
        return ret;
-err:
-       d_genocide(dir);
-       goto out;
 }
 
-static int sel_make_dir(struct super_block *sb, struct dentry *dentry)
+static int sel_make_dir(struct inode *dir, struct dentry *dentry)
 {
        int ret = 0;
        struct inode *inode;
 
-       inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
+       inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
        if (!inode) {
                ret = -ENOMEM;
                goto out;
@@ -1182,8 +1253,10 @@ static int sel_make_dir(struct super_block *sb, struct dentry *dentry)
        inode->i_op = &simple_dir_inode_operations;
        inode->i_fop = &simple_dir_operations;
        /* directory inodes start off with i_nlink == 2 (for "." entry) */
-       inode->i_nlink++;
+       inc_nlink(inode);
        d_add(dentry, inode);
+       /* bump link count on parent directory, too */
+       inc_nlink(dir);
 out:
        return ret;
 }
@@ -1192,7 +1265,7 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent)
 {
        int ret;
        struct dentry *dentry;
-       struct inode *inode;
+       struct inode *inode, *root_inode;
        struct inode_security_struct *isec;
 
        static struct tree_descr selinux_files[] = {
@@ -1209,19 +1282,22 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent)
                [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
                [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
                [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
+               [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
                /* last one */ {""}
        };
        ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
        if (ret)
                goto err;
 
+       root_inode = sb->s_root->d_inode;
+
        dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
        if (!dentry) {
                ret = -ENOMEM;
                goto err;
        }
 
-       ret = sel_make_dir(sb, dentry);
+       ret = sel_make_dir(root_inode, dentry);
        if (ret)
                goto err;
 
@@ -1253,7 +1329,7 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent)
                goto err;
        }
 
-       ret = sel_make_dir(sb, dentry);
+       ret = sel_make_dir(root_inode, dentry);
        if (ret)
                goto err;
 
@@ -1267,10 +1343,11 @@ err:
        goto out;
 }
 
-static struct super_block *sel_get_sb(struct file_system_type *fs_type,
-                                     int flags, const char *dev_name, void *data)
+static int sel_get_sb(struct file_system_type *fs_type,
+                     int flags, const char *dev_name, void *data,
+                     struct vfsmount *mnt)
 {
-       return get_sb_single(fs_type, flags, data, sel_fill_super);
+       return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
 }
 
 static struct file_system_type sel_fs_type = {