2021-10-18 08:06:48

by 王擎

[permalink] [raw]
Subject: [PATCH] firmware: tegra: switch over to memdup_user()

This patch fixes the following Coccinelle warning:

drivers/firmware/tegra/bpmp-debugfs.c:379: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation
This is a little bit restricted to reduce false positives

Signed-off-by: Qing Wang <[email protected]>
---
drivers/firmware/tegra/bpmp-debugfs.c | 13 +++----------
1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/drivers/firmware/tegra/bpmp-debugfs.c b/drivers/firmware/tegra/bpmp-debugfs.c
index 6d66fe0..0435709
--- a/drivers/firmware/tegra/bpmp-debugfs.c
+++ b/drivers/firmware/tegra/bpmp-debugfs.c
@@ -376,18 +376,11 @@ static ssize_t bpmp_debug_store(struct file *file, const char __user *buf,
if (!filename)
return -ENOENT;

- databuf = kmalloc(count, GFP_KERNEL);
- if (!databuf)
- return -ENOMEM;
-
- if (copy_from_user(databuf, buf, count)) {
- err = -EFAULT;
- goto free_ret;
- }
+ databuf = memdup_user(buf, count);
+ if (IS_ERR(databuf))
+ return ERR_PTR(PTR_ERR(databuf));

err = mrq_debug_write(bpmp, filename, databuf, count);
-
-free_ret:
kfree(databuf);

return err ?: count;
--
2.7.4


2021-10-21 01:18:15

by Michał Mirosław

[permalink] [raw]
Subject: Re: [PATCH] firmware: tegra: switch over to memdup_user()

On Mon, Oct 18, 2021 at 12:59:40AM -0700, Qing Wang wrote:
[...]
> --- a/drivers/firmware/tegra/bpmp-debugfs.c
> +++ b/drivers/firmware/tegra/bpmp-debugfs.c
> @@ -376,18 +376,11 @@ static ssize_t bpmp_debug_store(struct file *file, const char __user *buf,
> if (!filename)
> return -ENOENT;
>
> - databuf = kmalloc(count, GFP_KERNEL);
> - if (!databuf)
> - return -ENOMEM;
> -
> - if (copy_from_user(databuf, buf, count)) {
> - err = -EFAULT;
> - goto free_ret;
> - }
> + databuf = memdup_user(buf, count);
> + if (IS_ERR(databuf))
> + return ERR_PTR(PTR_ERR(databuf));

ERR_PTR() is too much here.

Best Regards
Micha??Miros?aw