misc: fastrpc: Use memdup_user()
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
@@ -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
Quoted 10 lines · Thorsten Blum
Switching to
memdup_user()overwrites the allocated memory only once, whereaskzalloc()followed bycopy_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_userSigned-off-by Thorsten Blum <[email protected]>
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@@ -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
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
@@ -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
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
@@ -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
Srinivas Kandagatla
AppliedQuoted 8 lines · Thorsten Blum
Switching to
memdup_user()overwrites the allocated memory only once, whereaskzalloc()followed bycopy_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]>
Srinivas Kandagatla
AppliedQuoted 8 lines · Thorsten Blum
Switching to
memdup_user()overwrites the allocated memory only once, whereaskzalloc()followed bycopy_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]>