diff mbox series

[-next,v2] crypto: hisilicon - switch to memdup_user_nul()

Message ID 1620804439-21392-1-git-send-email-zou_wei@huawei.com (mailing list archive)
State Changes Requested
Delegated to: Herbert Xu
Headers show
Series [-next,v2] crypto: hisilicon - switch to memdup_user_nul() | expand

Commit Message

Zou Wei May 12, 2021, 7:27 a.m. UTC
Use memdup_user_nul() helper instead of open-coding to
simplify the code.

v1-->v2:
   fixed patch title error

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zou Wei <zou_wei@huawei.com>
---
 drivers/crypto/hisilicon/qm.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

Comments

Herbert Xu May 21, 2021, 7:47 a.m. UTC | #1
On Wed, May 12, 2021 at 03:27:19PM +0800, Zou Wei wrote:
> diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c
> index ce439a0..83a5d30 100644
> --- a/drivers/crypto/hisilicon/qm.c
> +++ b/drivers/crypto/hisilicon/qm.c
> @@ -1570,17 +1570,10 @@ static ssize_t qm_cmd_write(struct file *filp, const char __user *buffer,
>  	if (count > QM_DBG_WRITE_LEN)
>  		return -ENOSPC;
>  
> -	cmd_buf = kzalloc(count + 1, GFP_KERNEL);
> -	if (!cmd_buf)
> +	cmd_buf = memdup_user_nul(buffer, count);
> +	if (IS_ERR(cmd_buf))
>  		return -ENOMEM;

You should return the actual error and not ENOMEM.

Thanks,
diff mbox series

Patch

diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c
index ce439a0..83a5d30 100644
--- a/drivers/crypto/hisilicon/qm.c
+++ b/drivers/crypto/hisilicon/qm.c
@@ -1570,17 +1570,10 @@  static ssize_t qm_cmd_write(struct file *filp, const char __user *buffer,
 	if (count > QM_DBG_WRITE_LEN)
 		return -ENOSPC;
 
-	cmd_buf = kzalloc(count + 1, GFP_KERNEL);
-	if (!cmd_buf)
+	cmd_buf = memdup_user_nul(buffer, count);
+	if (IS_ERR(cmd_buf))
 		return -ENOMEM;
 
-	if (copy_from_user(cmd_buf, buffer, count)) {
-		kfree(cmd_buf);
-		return -EFAULT;
-	}
-
-	cmd_buf[count] = '\0';
-
 	cmd_buf_tmp = strchr(cmd_buf, '\n');
 	if (cmd_buf_tmp) {
 		*cmd_buf_tmp = '\0';