Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[pandora-kernel.git] / net / netlabel / netlabel_unlabeled.c
index 785f496..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 = {
@@ -55,9 +57,48 @@ static struct genl_family netlbl_unlabel_gnl_family = {
        .hdrsize = 0,
        .name = NETLBL_NLTYPE_UNLABELED_NAME,
        .version = NETLBL_PROTO_VERSION,
-       .maxattr = 0,
+       .maxattr = NLBL_UNLABEL_A_MAX,
 };
 
+/* NetLabel Netlink attribute policy */
+static struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
+       [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
+};
+
+/*
+ * Helper Functions
+ */
+
+/**
+ * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
+ * @value: desired value
+ * @audit_info: NetLabel audit information
+ *
+ * Description:
+ * Set the value of the unlabeled accept flag to @value.
+ *
+ */
+static void netlbl_unlabel_acceptflg_set(u8 value,
+                                        struct netlbl_audit *audit_info)
+{
+       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);
+       }
+}
 
 /*
  * NetLabel Command Handlers
@@ -75,30 +116,18 @@ static struct genl_family netlbl_unlabel_gnl_family = {
  */
 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
 {
-       int ret_val;
-       struct nlattr *data = netlbl_netlink_payload_data(skb);
-       u32 value;
+       u8 value;
+       struct netlbl_audit audit_info;
 
-       ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
-       if (ret_val != 0)
-               return ret_val;
-
-       if (netlbl_netlink_payload_len(skb) == NETLBL_LEN_U32) {
-               value = nla_get_u32(data);
+       if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
+               value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
                if (value == 1 || value == 0) {
-                       atomic_set(&netlabel_unlabel_accept_flg, value);
-                       netlbl_netlink_send_ack(info,
-                                               netlbl_unlabel_gnl_family.id,
-                                               NLBL_UNLABEL_C_ACK,
-                                               NETLBL_E_OK);
+                       netlbl_netlink_auditinfo(skb, &audit_info);
+                       netlbl_unlabel_acceptflg_set(value, &audit_info);
                        return 0;
                }
        }
 
-       netlbl_netlink_send_ack(info,
-                               netlbl_unlabel_gnl_family.id,
-                               NLBL_UNLABEL_C_ACK,
-                               EINVAL);
        return -EINVAL;
 }
 
@@ -114,39 +143,37 @@ static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  */
 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
 {
-       int ret_val = -ENOMEM;
+       int ret_val = -EINVAL;
        struct sk_buff *ans_skb;
+       void *data;
 
-       ans_skb = netlbl_netlink_alloc_skb(0,
-                                          GENL_HDRLEN + NETLBL_LEN_U32,
-                                          GFP_KERNEL);
+       ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
        if (ans_skb == NULL)
                goto list_failure;
-
-       if (netlbl_netlink_hdr_put(ans_skb,
-                                  info->snd_pid,
-                                  0,
-                                  netlbl_unlabel_gnl_family.id,
-                                  NLBL_UNLABEL_C_LIST) == NULL)
+       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;
+       }
 
-       ret_val = nla_put_u32(ans_skb,
-                             NLA_U32,
-                             atomic_read(&netlabel_unlabel_accept_flg));
+       rcu_read_lock();
+       ret_val = nla_put_u8(ans_skb,
+                            NLBL_UNLABEL_A_ACPTFLG,
+                            netlabel_unlabel_acceptflg);
+       rcu_read_unlock();
        if (ret_val != 0)
                goto list_failure;
 
-       ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
+       genlmsg_end(ans_skb, data);
+
+       ret_val = genlmsg_reply(ans_skb, info);
        if (ret_val != 0)
                goto list_failure;
-
        return 0;
 
 list_failure:
-       netlbl_netlink_send_ack(info,
-                               netlbl_unlabel_gnl_family.id,
-                               NLBL_UNLABEL_C_ACK,
-                               -ret_val);
+       kfree(ans_skb);
        return ret_val;
 }
 
@@ -157,7 +184,8 @@ list_failure:
 
 static struct genl_ops netlbl_unlabel_genl_c_accept = {
        .cmd = NLBL_UNLABEL_C_ACCEPT,
-       .flags = 0,
+       .flags = GENL_ADMIN_PERM,
+       .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_accept,
        .dumpit = NULL,
 };
@@ -165,6 +193,7 @@ static struct genl_ops netlbl_unlabel_genl_c_accept = {
 static struct genl_ops netlbl_unlabel_genl_c_list = {
        .cmd = NLBL_UNLABEL_C_LIST,
        .flags = 0,
+       .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_list,
        .dumpit = NULL,
 };
@@ -218,12 +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) {
-               memset(secattr, 0, sizeof(*secattr));
-               return 0;
-       }
+       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;
 }
 
 /**
@@ -238,16 +272,23 @@ int netlbl_unlabel_defconf(void)
 {
        int ret_val;
        struct netlbl_dom_map *entry;
+       struct netlbl_audit audit_info;
+
+       /* 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);
+       ret_val = netlbl_domhsh_add_default(entry, &audit_info);
        if (ret_val != 0)
                return ret_val;
 
-       atomic_set(&netlabel_unlabel_accept_flg, 1);
+       netlbl_unlabel_acceptflg_set(1, &audit_info);
 
        return 0;
 }