diff mbox series

[1/2] drm: Use size_t type for len variable in drm_copy_field()

Message ID 20220701120755.2135100-2-javierm@redhat.com (mailing list archive)
State Superseded, archived
Headers show
Series drm: A couple of fixes for drm_copy_field() helper function | expand

Commit Message

Javier Martinez Canillas July 1, 2022, 12:07 p.m. UTC
The strlen() function returns a size_t which is an unsigned int on 32-bit
arches and an unsigned long on 64-bit arches. But in the drm_copy_field()
function, the strlen() return value is assigned to an 'int len' variable.

Later, the len variable is passed as copy_from_user() third argument that
is an unsigned long parameter as well.

In theory, this can lead to an integer overflow via type conversion. Since
the assignment happens to a signed int lvalue instead of a size_t lvalue.

In practice though, that's unlikely since the values copied are set by DRM
drivers and not controlled by userspace. But using a size_t for len is the
correct thing to do anyways.

Reported-by: Peter Robinson <pbrobinson@gmail.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

 drivers/gpu/drm/drm_ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Thomas Zimmermann July 4, 2022, 12:27 p.m. UTC | #1
Am 01.07.22 um 14:07 schrieb Javier Martinez Canillas:
> The strlen() function returns a size_t which is an unsigned int on 32-bit
> arches and an unsigned long on 64-bit arches. But in the drm_copy_field()
> function, the strlen() return value is assigned to an 'int len' variable.
> 
> Later, the len variable is passed as copy_from_user() third argument that
> is an unsigned long parameter as well.
> 
> In theory, this can lead to an integer overflow via type conversion. Since
> the assignment happens to a signed int lvalue instead of a size_t lvalue.
> 
> In practice though, that's unlikely since the values copied are set by DRM
> drivers and not controlled by userspace. But using a size_t for len is the
> correct thing to do anyways.
> 
> Reported-by: Peter Robinson <pbrobinson@gmail.com>
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>

Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>

> ---
> 
>   drivers/gpu/drm/drm_ioctl.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
> index 8faad23dc1d8..e1b9a03e619c 100644
> --- a/drivers/gpu/drm/drm_ioctl.c
> +++ b/drivers/gpu/drm/drm_ioctl.c
> @@ -472,7 +472,7 @@ EXPORT_SYMBOL(drm_invalid_op);
>    */
>   static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
>   {
> -	int len;
> +	size_t len;
>   
>   	/* don't overflow userbuf */
>   	len = strlen(value);
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
index 8faad23dc1d8..e1b9a03e619c 100644
--- a/drivers/gpu/drm/drm_ioctl.c
+++ b/drivers/gpu/drm/drm_ioctl.c
@@ -472,7 +472,7 @@  EXPORT_SYMBOL(drm_invalid_op);
  */
 static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
 {
-	int len;
+	size_t len;
 
 	/* don't overflow userbuf */
 	len = strlen(value);