Skip to contentSkip to conversation

fastrpc: Use memdup_user instead of kmalloc/copy_from_user

PATCH In review Yang Li 2 participants 2 messages May 15, 2023

Subsystems (3)
lore
Share & tools
lore

Yang Li

author
#0 lore raw

PATCH 1/1 fastrpc: Use memdup_user instead of kmalloc/copy_from_user

Use memdup_user rather than duplicating its implementation, which makes code simple and easy to understand, and silence the following warning:

./drivers/misc/fastrpc.c:1259:8-15: WARNING opportunity for memdup_user

---
 drivers/misc/fastrpc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/misc/fastrpc.c +3 -10
index f60bbf99485c..a897dab13c61 100644
@@ -1256,16 +1256,9 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;
-		goto err;
-	}
-
-	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
-		err = -EFAULT;
-		goto err_name;
-	}
+	name = memdup_user((void __user *)(uintptr_t)init.name, init.namelen);
+	if (IS_ERR(name))
+		return PTR_ERR(name);
 
 	if (!fl->cctx->remote_heap) {
 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
Signature · 1 line
2.20.1.7.g153144c
#1 lore raw

Hi Yang Li,

Quoted 24 lines · Yang Li

Use memdup_user rather than duplicating its implementation, which makes code simple and easy to understand, and silence the following warning:

./drivers/misc/fastrpc.c:1259:8-15: WARNING opportunity for memdup_user

---
 drivers/misc/fastrpc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/misc/fastrpc.c +0 -3
index f60bbf99485c..a897dab13c61 100644
@@ -1256,16 +1256,9 @@ static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
 		goto err;
 	}
 
-	name = kzalloc(init.namelen, GFP_KERNEL);
-	if (!name) {
-		err = -ENOMEM;

drivers/misc/fastrpc.c static int fastrpc_init_create_static_process(struct fastrpc_user *fl, · line 1261

Quoted 10 lines
drivers/misc/fastrpc.c +3 -7
-		goto err;
-	}
-
-	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
-		err = -EFAULT;
-		goto err_name;
-	}
+	name = memdup_user((void __user *)(uintptr_t)init.name, init.namelen);
+	if (IS_ERR(name))
+		return PTR_ERR(name);

Don't we still need a "goto err;" here to free args?

Something like this:

  if (IS_ERR(name)) {
      err = PTR_ERR(name);
      goto err;
  }

Regards
Luca

Quoted 3 lines
drivers/misc/fastrpc.c +0 -0
 	if (!fl->cctx->remote_heap) {
 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,