convert a bunch of open-coded instances of memdup_user_nul()
[cascardo/linux.git] / security / smack / smackfs.c
index 94bd9e4..e249a66 100644 (file)
@@ -497,14 +497,9 @@ static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
                }
        }
 
-       data = kmalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto out;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        /*
         * In case of parsing only part of user buf,
@@ -884,16 +879,10 @@ static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
            (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
                return -EINVAL;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto unlockedout;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
-       data[count] = '\0';
        rule = data;
        /*
         * Only allow one writer at a time. Writes should be
@@ -946,7 +935,6 @@ static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
 
 out:
        mutex_unlock(&smack_cipso_lock);
-unlockedout:
        kfree(data);
        return rc;
 }
@@ -1187,14 +1175,9 @@ static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
        if (count < SMK_NETLBLADDRMIN)
                return -EINVAL;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto free_data_out;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        smack = kzalloc(count + 1, GFP_KERNEL);
        if (smack == NULL) {
@@ -1202,8 +1185,6 @@ static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
                goto free_data_out;
        }
 
-       data[count] = '\0';
-
        rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
                &host[0], &host[1], &host[2], &host[3], &masks, smack);
        if (rc != 6) {
@@ -1454,14 +1435,9 @@ static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
        if (count < SMK_NETLBLADDRMIN)
                return -EINVAL;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto free_data_out;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        smack = kzalloc(count + 1, GFP_KERNEL);
        if (smack == NULL) {
@@ -1469,8 +1445,6 @@ static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
                goto free_data_out;
        }
 
-       data[count] = '\0';
-
        i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
                        &scanned[0], &scanned[1], &scanned[2], &scanned[3],
                        &scanned[4], &scanned[5], &scanned[6], &scanned[7],
@@ -1865,14 +1839,9 @@ static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
        if (!smack_privileged(CAP_MAC_ADMIN))
                return -EPERM;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto out;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        skp = smk_import_entry(data, count);
        if (IS_ERR(skp)) {
@@ -2041,14 +2010,9 @@ static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
        if (!smack_privileged(CAP_MAC_ADMIN))
                return -EPERM;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               kfree(data);
-               return -EFAULT;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        rc = smk_parse_label_list(data, &list_tmp);
        kfree(data);
@@ -2133,14 +2097,9 @@ static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
        if (!smack_privileged(CAP_MAC_ADMIN))
                return -EPERM;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               rc = -EFAULT;
-               goto freeout;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        /*
         * Clear the smack_unconfined on invalid label errors. This means
@@ -2696,19 +2655,15 @@ static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
        if (!smack_privileged(CAP_MAC_ADMIN))
                return -EPERM;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
-       if (copy_from_user(data, buf, count) != 0)
-               rc = -EFAULT;
-       else {
-               skp = smk_import_entry(data, count);
-               if (IS_ERR(skp))
-                       rc = PTR_ERR(skp);
-               else
-                       smack_syslog_label = skp;
-       }
+       skp = smk_import_entry(data, count);
+       if (IS_ERR(skp))
+               rc = PTR_ERR(skp);
+       else
+               smack_syslog_label = skp;
 
        kfree(data);
        return rc;
@@ -2798,14 +2753,9 @@ static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
        if (*ppos != 0)
                return -EINVAL;
 
-       data = kzalloc(count + 1, GFP_KERNEL);
-       if (data == NULL)
-               return -ENOMEM;
-
-       if (copy_from_user(data, buf, count) != 0) {
-               kfree(data);
-               return -EFAULT;
-       }
+       data = memdup_user_nul(buf, count);
+       if (IS_ERR(data))
+               return PTR_ERR(data);
 
        rc = smk_parse_label_list(data, &list_tmp);
        kfree(data);