Skip to contentSkip to conversation

staging: r8188eu: Use strndup_user instead of kmalloc/copy_from_user

PATCH Superseded Jiapeng Chong 2 participants 2 messages Dec 10, 2021 Staging

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: r8188eu: Use strndup_user instead of kmalloc/copy_from_user

This code assumes that the user is going to give us a NULL terminated string which is not necessarily true. The original code was buggy too.

Fix following coccicheck warning:

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

Fixes 7bdedfef085b ("staging: r8188eu: Remove mp, a.k.a. manufacturing process, code")

---
Changes in v2:
-For the follow advice:https://lore.kernel.org/lkml/20211210083010.GM1956@kadam/

 drivers/staging/r8188eu/os_dep/ioctl_linux.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/r8188eu/os_dep/ioctl_linux.c +4 -9
index 56adfe4087a8..5d26dffe5c0d 100644
@@ -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 = strndup_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;
Signature · 1 line
2.20.1.7.g153144c
#1 lore raw
Quoted 1 line · Jiapeng Chong

This code assumes that the user is going to give us a NULL terminated

I wouldn't comment on this except that the Fixes tag is wrong so it needs to be resent anyway. NULL is a pointer. When it's a character '\0'; then it's just one L "NUL terminated".

Quoted 1 line

string which is not necessarily true. The original code was buggy too.

                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Leave this out.

Quoted 8 lines

Fix following coccicheck warning:

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

Fixes 7bdedfef085b ("staging: r8188eu: Remove mp, a.k.a. manufacturing process, code")

The fixes tag should be:

Fixes 2b42bd58b321 ("staging: r8188eu: introduce new os_dep dir for RTL8188eu driver")
Quoted 4 lines

---
Changes in v2:
-For the follow advice:https://lore.kernel.org/lkml/20211210083010.GM1956@kadam/

Don't put a link in the email. Just say what the difference is. No one wants to have to click on a link.

Quoted 13 lines
 drivers/staging/r8188eu/os_dep/ioctl_linux.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/r8188eu/os_dep/ioctl_linux.c +0 -1
index 56adfe4087a8..5d26dffe5c0d 100644
@@ -4250,17 +4250,12 @@ static int rtw_test(
 	DBG_88E("+%s\n", __func__);
 	len = wrqu->data.length;
 
-	pbuf = kzalloc(len, GFP_KERNEL);

drivers/staging/r8188eu/os_dep/ioctl_linux.c static int rtw_test( · line 4255

Quoted 10 lines
drivers/staging/r8188eu/os_dep/ioctl_linux.c +2 -7
-	if (!pbuf) {
-		DBG_88E("%s: no memory!\n", __func__);
-		return -ENOMEM;
-	}
-
-	if (copy_from_user(pbuf, wrqu->data.pointer, len)) {
-		kfree(pbuf);
+	pbuf = strndup_user(wrqu->data.pointer, len);
+	if (IS_ERR(pbuf)) {
 		DBG_88E("%s: copy from user fail!\n", __func__);

This printk is out of date now that we're not using the copy_from_user() function. It doesn't add useful information. Just delete it.

regards,
dan carpenter