linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user
@ 2021-10-22  2:55 Kai Song
  2021-10-22  9:07 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Kai Song @ 2021-10-22  2:55 UTC (permalink / raw)
  To: gregkh
  Cc: Larry.Finger, phil, straube.linux, linux-staging, linux-kernel, Kai Song

Use memdup_user helper instead of open-coding to simplify
the code.

Signed-off-by: Kai Song <songkai01@inspur.com>
---
 drivers/staging/r8188eu/os_dep/ioctl_linux.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
index 0201f6fbeb25..96a08cc5a1ed 100644
--- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
+++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
@@ -1984,14 +1984,10 @@ static int rtw_wx_read32(struct net_device *dev,
 	padapter = (struct adapter *)rtw_netdev_priv(dev);
 	p = &wrqu->data;
 	len = p->length;
-	ptmp = kmalloc(len, GFP_KERNEL);
-	if (!ptmp)
-		return -ENOMEM;
 
-	if (copy_from_user(ptmp, p->pointer, len)) {
-		kfree(ptmp);
-		return -EFAULT;
-	}
+	ptmp = memdup_user(p->pointer, len);
+	if (IS_ERR(ptmp))
+		return PTR_ERR(ptmp);
 
 	bytes = 0;
 	addr = 0;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user
  2021-10-22  2:55 [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user Kai Song
@ 2021-10-22  9:07 ` Greg KH
  0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2021-10-22  9:07 UTC (permalink / raw)
  To: Kai Song; +Cc: Larry.Finger, phil, straube.linux, linux-staging, linux-kernel

On Fri, Oct 22, 2021 at 10:55:55AM +0800, Kai Song wrote:
> Use memdup_user helper instead of open-coding to simplify
> the code.
> 
> Signed-off-by: Kai Song <songkai01@inspur.com>
> ---
>  drivers/staging/r8188eu/os_dep/ioctl_linux.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)

Someone sent this just before you did, sorry:
	https://lore.kernel.org/r/20211021122015.6974-1-wanjiabing@vivo.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user
  2021-12-10  3:38 Jiapeng Chong
@ 2021-12-10  8:30 ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2021-12-10  8:30 UTC (permalink / raw)
  To: Jiapeng Chong
  Cc: Larry.Finger, phil, gregkh, linux-staging, linux-kernel, Abaci Robot

On Fri, Dec 10, 2021 at 11:38:32AM +0800, Jiapeng Chong wrote:
> Fix following coccicheck warning:
> 
> ./drivers/staging/r8188eu/os_dep/ioctl_linux.c:4253:8-15: WARNING
> opportunity for memdup_user.
> 
> Reported-by: Abaci Robot <abaci@linux.alibaba.com>
> Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
> ---
>  drivers/staging/r8188eu/os_dep/ioctl_linux.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> index 56adfe4087a8..c6a2fbfe8f26 100644
> --- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> +++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> @@ -4250,17 +4250,12 @@ static int rtw_test(
>  	DBG_88E("+%s\n", __func__);
>  	len = wrqu->data.length;
>  
> -	pbuf = kzalloc(len, GFP_KERNEL);
> -	if (!pbuf) {
> -		DBG_88E("%s: no memory!\n", __func__);
> -		return -ENOMEM;
> -	}
> -
> -	if (copy_from_user(pbuf, wrqu->data.pointer, len)) {
> -		kfree(pbuf);
> +	pbuf = memdup_user(wrqu->data.pointer, len);

This code assumes that the user is going to give us a NUL terminated
string which is not necessarily true.  The original code was buggy too.
Anyway, please fix it.  Use strndup_user() and mention that it is a bug
fix in the commit message:

	pbuf = strndup_user(wrqu->data.pointer, len);

Add a Fixes tag.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user
@ 2021-12-10  3:38 Jiapeng Chong
  2021-12-10  8:30 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Jiapeng Chong @ 2021-12-10  3:38 UTC (permalink / raw)
  To: Larry.Finger
  Cc: phil, gregkh, linux-staging, linux-kernel, Jiapeng Chong, Abaci Robot

Fix following coccicheck warning:

./drivers/staging/r8188eu/os_dep/ioctl_linux.c:4253:8-15: WARNING
opportunity for memdup_user.

Reported-by: Abaci Robot <abaci@linux.alibaba.com>
Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
---
 drivers/staging/r8188eu/os_dep/ioctl_linux.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
index 56adfe4087a8..c6a2fbfe8f26 100644
--- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
+++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
@@ -4250,17 +4250,12 @@ static int rtw_test(
 	DBG_88E("+%s\n", __func__);
 	len = wrqu->data.length;
 
-	pbuf = kzalloc(len, GFP_KERNEL);
-	if (!pbuf) {
-		DBG_88E("%s: no memory!\n", __func__);
-		return -ENOMEM;
-	}
-
-	if (copy_from_user(pbuf, wrqu->data.pointer, len)) {
-		kfree(pbuf);
+	pbuf = memdup_user(wrqu->data.pointer, len);
+	if (IS_ERR(pbuf)) {
 		DBG_88E("%s: copy from user fail!\n", __func__);
-		return -EFAULT;
+		return PTR_ERR(pbuf);
 	}
+
 	DBG_88E("%s: string =\"%s\"\n", __func__, pbuf);
 
 	ptmp = (char *)pbuf;
-- 
2.34.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user
@ 2021-10-21 12:20 Wan Jiabing
  0 siblings, 0 replies; 5+ messages in thread
From: Wan Jiabing @ 2021-10-21 12:20 UTC (permalink / raw)
  To: Larry Finger, Phillip Potter, Greg Kroah-Hartman,
	Michael Straube, Martin Kaiser, linux-staging, linux-kernel
  Cc: kael_w, Wan Jiabing

Fix following coccicheck warning:
./drivers/staging/r8188eu/os_dep/ioctl_linux.c:1986:8-15: WARNING
opportunity for memdup_user.

Use memdup_user rather than duplicating its implementation, which
makes code simple and easy to understand.

Signed-off-by: Wan Jiabing <wanjiabing@vivo.com>
---
 drivers/staging/r8188eu/os_dep/ioctl_linux.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
index 4f0ae821d193..301a29984fad 100644
--- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
+++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
@@ -1983,14 +1983,9 @@ static int rtw_wx_read32(struct net_device *dev,
 	padapter = (struct adapter *)rtw_netdev_priv(dev);
 	p = &wrqu->data;
 	len = p->length;
-	ptmp = kmalloc(len, GFP_KERNEL);
-	if (!ptmp)
-		return -ENOMEM;
-
-	if (copy_from_user(ptmp, p->pointer, len)) {
-		kfree(ptmp);
-		return -EFAULT;
-	}
+	ptmp = memdup_user(p->pointer, len);
+	if (IS_ERR(ptmp))
+		return PTR_ERR(ptmp);
 
 	bytes = 0;
 	addr = 0;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-10  8:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-22  2:55 [PATCH] staging: r8188eu: Use memdup_user instead of kmalloc/copy_from_user Kai Song
2021-10-22  9:07 ` Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2021-12-10  3:38 Jiapeng Chong
2021-12-10  8:30 ` Dan Carpenter
2021-10-21 12:20 Wan Jiabing

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).