mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup()
@ 2024-06-17  9:33 Dan Carpenter
  2024-06-17 15:55 ` Danilo Krummrich
  2024-06-18  5:33 ` Markus Elfring
  0 siblings, 2 replies; 4+ messages in thread
From: Dan Carpenter @ 2024-06-17  9:33 UTC (permalink / raw)
  To: Karol Herbst
  Cc: Lyude Paul, Danilo Krummrich, David Airlie, Daniel Vetter,
	dri-devel, nouveau, linux-kernel, kernel-janitors

Use kmemdup_array() because we're allocating an array.

The main difference between kmemdup() and kmemdup_array() is that the
kmemdup_array() function has integer overflow checking built it.  The
"args->in_sync.count" variable is a u32 so integer overflows would only
be a concern on 32bit systems.  Fortunately, however, the u_memcpya()
function has integer overflow checking which means that it is not an
issue.

Still using kmemdup_array() is more appropriate and makes auditing the
code easier.

Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/gpu/drm/nouveau/nouveau_sched.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouveau/nouveau_sched.c
index 32fa2e273965..53d8b0584a56 100644
--- a/drivers/gpu/drm/nouveau/nouveau_sched.c
+++ b/drivers/gpu/drm/nouveau/nouveau_sched.c
@@ -45,10 +45,10 @@ nouveau_job_init(struct nouveau_job *job,
 		if (job->sync)
 			return -EINVAL;
 
-		job->in_sync.data = kmemdup(args->in_sync.s,
-					 sizeof(*args->in_sync.s) *
-					 args->in_sync.count,
-					 GFP_KERNEL);
+		job->in_sync.data = kmemdup_array(args->in_sync.s,
+					args->in_sync.count,
+					sizeof(*args->in_sync.s),
+					GFP_KERNEL);
 		if (!job->in_sync.data)
 			return -ENOMEM;
 	}
@@ -60,10 +60,10 @@ nouveau_job_init(struct nouveau_job *job,
 			goto err_free_in_sync;
 		}
 
-		job->out_sync.data = kmemdup(args->out_sync.s,
-					  sizeof(*args->out_sync.s) *
-					  args->out_sync.count,
-					  GFP_KERNEL);
+		job->out_sync.data = kmemdup_array(args->out_sync.s,
+					args->out_sync.count,
+					sizeof(*args->out_sync.s),
+					GFP_KERNEL);
 		if (!job->out_sync.data) {
 			ret = -ENOMEM;
 			goto err_free_in_sync;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup()
  2024-06-17  9:33 [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup() Dan Carpenter
@ 2024-06-17 15:55 ` Danilo Krummrich
  2024-06-18  7:00   ` Dan Carpenter
  2024-06-18  5:33 ` Markus Elfring
  1 sibling, 1 reply; 4+ messages in thread
From: Danilo Krummrich @ 2024-06-17 15:55 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Lyude Paul, David Airlie, Daniel Vetter, dri-devel, nouveau,
	linux-kernel, kernel-janitors, Karol Herbst

On 6/17/24 11:33, Dan Carpenter wrote:
> Use kmemdup_array() because we're allocating an array.
> 
> The main difference between kmemdup() and kmemdup_array() is that the
> kmemdup_array() function has integer overflow checking built it.  The
> "args->in_sync.count" variable is a u32 so integer overflows would only
> be a concern on 32bit systems.  Fortunately, however, the u_memcpya()
> function has integer overflow checking which means that it is not an
> issue.
> 
> Still using kmemdup_array() is more appropriate and makes auditing the
> code easier.

Indeed, we shouldn't rely on the previous check here, good catch.

> 
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
>   drivers/gpu/drm/nouveau/nouveau_sched.c | 16 ++++++++--------
>   1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouveau/nouveau_sched.c
> index 32fa2e273965..53d8b0584a56 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_sched.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_sched.c
> @@ -45,10 +45,10 @@ nouveau_job_init(struct nouveau_job *job,
>   		if (job->sync)
>   			return -EINVAL;
>   
> -		job->in_sync.data = kmemdup(args->in_sync.s,
> -					 sizeof(*args->in_sync.s) *
> -					 args->in_sync.count,
> -					 GFP_KERNEL);
> +		job->in_sync.data = kmemdup_array(args->in_sync.s,
> +					args->in_sync.count,
> +					sizeof(*args->in_sync.s),
> +					GFP_KERNEL);
>   		if (!job->in_sync.data)
>   			return -ENOMEM;

Not sure if this is what we want for kmemdup_array(). It just saturates the
size. This doesn't prevent accessing the array out of bounds later on. I mean,
it's rather unlikely to get such a huge amount of physically contiguous memory
anyways, but wouldn't it be cleaner to let kmemdup_array() return
ERR_PTR(-EOVERFLOW) on overflow, just like memdup_array_user()[1]?

AFAICS, there's just two users of kmemdup_array(), hence it should be an easy
change. :-)

[1] https://elixir.bootlin.com/linux/latest/source/include/linux/string.h#L30

>   	}
> @@ -60,10 +60,10 @@ nouveau_job_init(struct nouveau_job *job,
>   			goto err_free_in_sync;
>   		}
>   
> -		job->out_sync.data = kmemdup(args->out_sync.s,
> -					  sizeof(*args->out_sync.s) *
> -					  args->out_sync.count,
> -					  GFP_KERNEL);
> +		job->out_sync.data = kmemdup_array(args->out_sync.s,
> +					args->out_sync.count,
> +					sizeof(*args->out_sync.s),
> +					GFP_KERNEL);
>   		if (!job->out_sync.data) {
>   			ret = -ENOMEM;
>   			goto err_free_in_sync;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup()
  2024-06-17  9:33 [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup() Dan Carpenter
  2024-06-17 15:55 ` Danilo Krummrich
@ 2024-06-18  5:33 ` Markus Elfring
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2024-06-18  5:33 UTC (permalink / raw)
  To: Dan Carpenter, nouveau, dri-devel, Karol Herbst
  Cc: kernel-janitors, LKML, Daniel Vetter, Danilo Krummrich,
	David Airlie, Lyude Paul

…
> kmemdup_array() function has integer overflow checking built it. …

                                                         built-in?

Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup()
  2024-06-17 15:55 ` Danilo Krummrich
@ 2024-06-18  7:00   ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2024-06-18  7:00 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Lyude Paul, David Airlie, Daniel Vetter, dri-devel, nouveau,
	linux-kernel, kernel-janitors, Karol Herbst

On Mon, Jun 17, 2024 at 05:55:33PM +0200, Danilo Krummrich wrote:
> On 6/17/24 11:33, Dan Carpenter wrote:
> > Use kmemdup_array() because we're allocating an array.
> > 
> > The main difference between kmemdup() and kmemdup_array() is that the
> > kmemdup_array() function has integer overflow checking built it.  The
> > "args->in_sync.count" variable is a u32 so integer overflows would only
> > be a concern on 32bit systems.  Fortunately, however, the u_memcpya()
> > function has integer overflow checking which means that it is not an
> > issue.
> > 
> > Still using kmemdup_array() is more appropriate and makes auditing the
> > code easier.
> 
> Indeed, we shouldn't rely on the previous check here, good catch.
> 
> > 
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > ---
> >   drivers/gpu/drm/nouveau/nouveau_sched.c | 16 ++++++++--------
> >   1 file changed, 8 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouveau/nouveau_sched.c
> > index 32fa2e273965..53d8b0584a56 100644
> > --- a/drivers/gpu/drm/nouveau/nouveau_sched.c
> > +++ b/drivers/gpu/drm/nouveau/nouveau_sched.c
> > @@ -45,10 +45,10 @@ nouveau_job_init(struct nouveau_job *job,
> >   		if (job->sync)
> >   			return -EINVAL;
> > -		job->in_sync.data = kmemdup(args->in_sync.s,
> > -					 sizeof(*args->in_sync.s) *
> > -					 args->in_sync.count,
> > -					 GFP_KERNEL);
> > +		job->in_sync.data = kmemdup_array(args->in_sync.s,
> > +					args->in_sync.count,
> > +					sizeof(*args->in_sync.s),
> > +					GFP_KERNEL);
> >   		if (!job->in_sync.data)
> >   			return -ENOMEM;
> 
> Not sure if this is what we want for kmemdup_array(). It just saturates the
> size. This doesn't prevent accessing the array out of bounds later on. I mean,
> it's rather unlikely to get such a huge amount of physically contiguous memory
> anyways, but wouldn't it be cleaner to let kmemdup_array() return
> ERR_PTR(-EOVERFLOW) on overflow, just like memdup_array_user()[1]?
> 
> AFAICS, there's just two users of kmemdup_array(), hence it should be an easy
> change. :-)
> 
> [1] https://elixir.bootlin.com/linux/latest/source/include/linux/string.h#L30
> 

We can't change the return values.

kmemdup_array() has to match kmemdup().  <-- returns NULL
memdup_array_user() has to match memdup_user().  <-- returns error pointer

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-18  7:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-17  9:33 [PATCH] drm/nouveau: Use kmemdup_array() instead of kmemdup() Dan Carpenter
2024-06-17 15:55 ` Danilo Krummrich
2024-06-18  7:00   ` Dan Carpenter
2024-06-18  5:33 ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®