greybus: use memdup_user()
authorAlex Elder <elder@linaro.org>
Thu, 14 Jul 2016 19:24:19 +0000 (14:24 -0500)
committerGreg Kroah-Hartman <gregkh@google.com>
Fri, 15 Jul 2016 00:14:27 +0000 (09:14 +0900)
Coccinelle reports that there are two opportunities to use memdup_user()
in "authentication.c".  This patch simplifies the code in cap_ioctl() by
taking advantage of that.  Make use of a local variable "size" to improve
readability.

Signed-off-by: Alex Elder <elder@linaro.org>
Reviewed-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
drivers/staging/greybus/authentication.c

index e469780..a4ac3bb 100644 (file)
@@ -209,6 +209,7 @@ static int cap_ioctl(struct gb_cap *cap, unsigned int cmd,
        struct cap_ioc_get_endpoint_uid endpoint_uid;
        struct cap_ioc_get_ims_certificate *ims_cert;
        struct cap_ioc_authenticate *authenticate;
+       size_t size;
        int ret;
 
        switch (cmd) {
@@ -222,38 +223,26 @@ static int cap_ioctl(struct gb_cap *cap, unsigned int cmd,
 
                return 0;
        case CAP_IOC_GET_IMS_CERTIFICATE:
-               ims_cert = kzalloc(sizeof(*ims_cert), GFP_KERNEL);
-               if (!ims_cert)
-                       return -ENOMEM;
-
-               if (copy_from_user(ims_cert, buf, sizeof(*ims_cert))) {
-                       ret = -EFAULT;
-                       goto free_ims_cert;
-               }
+               size = sizeof(*ims_cert);
+               ims_cert = memdup_user(buf, size);
+               if (IS_ERR(ims_cert))
+                       return PTR_ERR(ims_cert);
 
                ret = cap_get_ims_certificate(cap, ims_cert->certificate_class,
                                              ims_cert->certificate_id,
                                              ims_cert->certificate,
                                              &ims_cert->cert_size,
                                              &ims_cert->result_code);
-               if (ret)
-                       goto free_ims_cert;
-
-               if (copy_to_user(buf, ims_cert, sizeof(*ims_cert)))
+               if (!ret && copy_to_user(buf, ims_cert, size))
                        ret = -EFAULT;
-
-free_ims_cert:
                kfree(ims_cert);
+
                return ret;
        case CAP_IOC_AUTHENTICATE:
-               authenticate = kzalloc(sizeof(*authenticate), GFP_KERNEL);
-               if (!authenticate)
-                       return -ENOMEM;
-
-               if (copy_from_user(authenticate, buf, sizeof(*authenticate))) {
-                       ret = -EFAULT;
-                       goto free_authenticate;
-               }
+               size = sizeof(*authenticate);
+               authenticate = memdup_user(buf, size);
+               if (IS_ERR(authenticate))
+                       return PTR_ERR(authenticate);
 
                ret = cap_authenticate(cap, authenticate->auth_type,
                                       authenticate->uid,
@@ -262,13 +251,10 @@ free_ims_cert:
                                       authenticate->response,
                                       &authenticate->signature_size,
                                       authenticate->signature);
-               if (ret)
-                       goto free_authenticate;
-
-               if (copy_to_user(buf, authenticate, sizeof(*authenticate)))
+               if (!ret && copy_to_user(buf, authenticate, size))
                        ret = -EFAULT;
-free_authenticate:
                kfree(authenticate);
+
                return ret;
        default:
                return -ENOTTY;