Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[pandora-kernel.git] / net / netlabel / netlabel_unlabeled.c
index ab36675..5bc3718 100644 (file)
@@ -35,6 +35,7 @@
 #include <linux/socket.h>
 #include <linux/string.h>
 #include <linux/skbuff.h>
+#include <linux/audit.h>
 #include <net/sock.h>
 #include <net/netlink.h>
 #include <net/genetlink.h>
@@ -47,7 +48,8 @@
 #include "netlabel_unlabeled.h"
 
 /* Accept unlabeled packets flag */
-static atomic_t netlabel_unlabel_accept_flg = ATOMIC_INIT(0);
+static DEFINE_SPINLOCK(netlabel_unlabel_acceptflg_lock);
+static u8 netlabel_unlabel_acceptflg = 0;
 
 /* NetLabel Generic NETLINK CIPSOv4 family */
 static struct genl_family netlbl_unlabel_gnl_family = {
@@ -70,18 +72,32 @@ static struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
 /**
  * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  * @value: desired value
- * @audit_secid: the LSM secid to use in the audit message
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Set the value of the unlabeled accept flag to @value.
  *
  */
-static void netlbl_unlabel_acceptflg_set(u8 value, u32 audit_secid)
+static void netlbl_unlabel_acceptflg_set(u8 value,
+                                        struct netlbl_audit *audit_info)
 {
-       atomic_set(&netlabel_unlabel_accept_flg, value);
-       netlbl_audit_nomsg((value ?
-                           AUDIT_MAC_UNLBL_ACCEPT : AUDIT_MAC_UNLBL_DENY),
-                          audit_secid);
+       struct audit_buffer *audit_buf;
+       u8 old_val;
+
+       rcu_read_lock();
+       old_val = netlabel_unlabel_acceptflg;
+       spin_lock(&netlabel_unlabel_acceptflg_lock);
+       netlabel_unlabel_acceptflg = value;
+       spin_unlock(&netlabel_unlabel_acceptflg_lock);
+       rcu_read_unlock();
+
+       audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
+                                             audit_info);
+       if (audit_buf != NULL) {
+               audit_log_format(audit_buf,
+                                " unlbl_accept=%u old=%u", value, old_val);
+               audit_log_end(audit_buf);
+       }
 }
 
 /*
@@ -101,12 +117,13 @@ static void netlbl_unlabel_acceptflg_set(u8 value, u32 audit_secid)
 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
 {
        u8 value;
+       struct netlbl_audit audit_info;
 
        if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
                value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
                if (value == 1 || value == 0) {
-                       netlbl_unlabel_acceptflg_set(value,
-                                                    NETLINK_CB(skb).sid);
+                       netlbl_netlink_auditinfo(skb, &audit_info);
+                       netlbl_unlabel_acceptflg_set(value, &audit_info);
                        return 0;
                }
        }
@@ -130,29 +147,27 @@ static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
        struct sk_buff *ans_skb;
        void *data;
 
-       ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
        if (ans_skb == NULL)
                goto list_failure;
-       data = netlbl_netlink_hdr_put(ans_skb,
-                                     info->snd_pid,
-                                     info->snd_seq,
-                                     netlbl_unlabel_gnl_family.id,
-                                     0,
-                                     NLBL_UNLABEL_C_LIST);
+       data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
+                                0, NLBL_UNLABEL_C_LIST);
        if (data == NULL) {
                ret_val = -ENOMEM;
                goto list_failure;
        }
 
+       rcu_read_lock();
        ret_val = nla_put_u8(ans_skb,
                             NLBL_UNLABEL_A_ACPTFLG,
-                            atomic_read(&netlabel_unlabel_accept_flg));
+                            netlabel_unlabel_acceptflg);
+       rcu_read_unlock();
        if (ret_val != 0)
                goto list_failure;
 
        genlmsg_end(ans_skb, data);
 
-       ret_val = genlmsg_unicast(ans_skb, info->snd_pid);
+       ret_val = genlmsg_reply(ans_skb, info);
        if (ret_val != 0)
                goto list_failure;
        return 0;
@@ -232,10 +247,17 @@ int netlbl_unlabel_genl_init(void)
  */
 int netlbl_unlabel_getattr(struct netlbl_lsm_secattr *secattr)
 {
-       if (atomic_read(&netlabel_unlabel_accept_flg) == 1)
-               return netlbl_secattr_init(secattr);
+       int ret_val;
 
-       return -ENOMSG;
+       rcu_read_lock();
+       if (netlabel_unlabel_acceptflg == 1) {
+               netlbl_secattr_init(secattr);
+               ret_val = 0;
+       } else
+               ret_val = -ENOMSG;
+       rcu_read_unlock();
+
+       return ret_val;
 }
 
 /**
@@ -250,19 +272,23 @@ int netlbl_unlabel_defconf(void)
 {
        int ret_val;
        struct netlbl_dom_map *entry;
-       u32 secid;
+       struct netlbl_audit audit_info;
 
-       security_task_getsecid(current, &secid);
+       /* Only the kernel is allowed to call this function and the only time
+        * it is called is at bootup before the audit subsystem is reporting
+        * messages so don't worry to much about these values. */
+       security_task_getsecid(current, &audit_info.secid);
+       audit_info.loginuid = 0;
 
        entry = kzalloc(sizeof(*entry), GFP_KERNEL);
        if (entry == NULL)
                return -ENOMEM;
        entry->type = NETLBL_NLTYPE_UNLABELED;
-       ret_val = netlbl_domhsh_add_default(entry, secid);
+       ret_val = netlbl_domhsh_add_default(entry, &audit_info);
        if (ret_val != 0)
                return ret_val;
 
-       netlbl_unlabel_acceptflg_set(1, secid);
+       netlbl_unlabel_acceptflg_set(1, &audit_info);
 
        return 0;
 }