netdevsim: switch to memdup_user_nul()
authorWei Yongjun <weiyongjun1@huawei.com>
Wed, 24 Mar 2021 14:42:20 +0000 (14:42 +0000)
committerDavid S. Miller <davem@davemloft.net>
Wed, 24 Mar 2021 23:26:58 +0000 (16:26 -0700)
Use memdup_user_nul() helper instead of open-coding to
simplify the code.

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Reviewed-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/netdevsim/health.c

index 21e2974..04aebdf 100644 (file)
@@ -235,15 +235,10 @@ static ssize_t nsim_dev_health_break_write(struct file *file,
        char *break_msg;
        int err;
 
-       break_msg = kmalloc(count + 1, GFP_KERNEL);
-       if (!break_msg)
-               return -ENOMEM;
+       break_msg = memdup_user_nul(data, count);
+       if (IS_ERR(break_msg))
+               return PTR_ERR(break_msg);
 
-       if (copy_from_user(break_msg, data, count)) {
-               err = -EFAULT;
-               goto out;
-       }
-       break_msg[count] = '\0';
        if (break_msg[count - 1] == '\n')
                break_msg[count - 1] = '\0';