Skip to contentSkip to conversation

misc: fastrpc: Use memdup_user()

PATCH Superseded Thorsten Blum 3 participants 6 messages May 7, 2024 – May 28, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: Use memdup_user()

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

WARNING opportunity for memdup_user

---
 drivers/misc/fastrpc.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/misc/fastrpc.c +3 -8
index 4c67e2c5a82e..2857cddaf812 100644
@@ -1259,17 +1259,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;
+	name = memdup_user((void __user *)(uintptr_t)init.name, init.namelen);
+	if (IS_ERR(name)) {
+		err = PTR_ERR(name);
 		goto err;
 	}
 
-	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
-		err = -EFAULT;
-		goto err_name;
-	}
-
 	if (!fl->cctx->remote_heap) {
 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
 						&fl->cctx->remote_heap);
Signature · 1 line
2.45.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Thorsten Blum

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

WARNING opportunity for memdup_user

The patch looks correct to me.

Quoted 11 lines
---
 drivers/misc/fastrpc.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/misc/fastrpc.c +0 -0
index 4c67e2c5a82e..2857cddaf812 100644
@@ -1259,17 +1259,12 @@ static int 
 fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;

drivers/misc/fastrpc.c static int · line 1267

Quoted 10 lines
drivers/misc/fastrpc.c +3 -3
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;
+	name = memdup_user((void __user *)(uintptr_t)init.name, init.namelen);
+	if (IS_ERR(name)) {
+		err = PTR_ERR(name);
 		goto err;
 	}

There is also a chance to simplify this further using u64_to_user_ptr() instead of the double cast if you want.

Arnd

#2 lore raw

PATCH v2 1/1 misc: fastrpc: Use memdup_user()

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

WARNING opportunity for memdup_user

---
Changes in v2:
- Use u64_to_user_ptr() as suggested by Arnd Bergmann (thanks!)
- Preserve Acked-by: tag

---
 drivers/misc/fastrpc.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/misc/fastrpc.c +3 -8
index 4c67e2c5a82e..694fc083b1bd 100644
@@ -1259,17 +1259,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;
+	name = memdup_user(u64_to_user_ptr(init.name), init.namelen);
+	if (IS_ERR(name)) {
+		err = PTR_ERR(name);
 		goto err;
 	}
 
-	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
-		err = -EFAULT;
-		goto err_name;
-	}
-
 	if (!fl->cctx->remote_heap) {
 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
 						&fl->cctx->remote_heap);
Signature · 1 line
2.45.0
#3 lore raw

PATCH RESEND v2 1/1 misc: fastrpc: Use memdup_user()

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

WARNING opportunity for memdup_user

---
Changes in v2:
- Use u64_to_user_ptr() as suggested by Arnd Bergmann (thanks!)
- Preserve Acked-by: tag

---
 drivers/misc/fastrpc.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/misc/fastrpc.c +3 -8
index 4c67e2c5a82e..694fc083b1bd 100644
@@ -1259,17 +1259,12 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;
+	name = memdup_user(u64_to_user_ptr(init.name), init.namelen);
+	if (IS_ERR(name)) {
+		err = PTR_ERR(name);
 		goto err;
 	}
 
-	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
-		err = -EFAULT;
-		goto err_name;
-	}
-
 	if (!fl->cctx->remote_heap) {
 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
 						&fl->cctx->remote_heap);
Signature · 1 line
2.45.1
#4 lore raw
Quoted 8 lines · Thorsten Blum

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

[...]

Applied, thanks!

[1/1] misc: fastrpc: Use memdup_user()

      commit: a16833330e2fa60912af6abebde711bf2c672cf9

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>
#5 lore raw
In reply to Thorsten Blum
Quoted 8 lines · Thorsten Blum

Switching to memdup_user() overwrites the allocated memory only once, whereas kzalloc() followed by copy_from_user() initializes the allocated memory to zero and then immediately overwrites it.

Fixes the following Coccinelle/coccicheck warning reported by memdup_user.cocci:

[...]

Applied, thanks!

[1/1] misc: fastrpc: Use memdup_user()

      commit: a16833330e2fa60912af6abebde711bf2c672cf9

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>