linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/i915: zero fill vma name buffer
@ 2021-09-15 19:23 Tim Gardner
  2021-09-16  8:23 ` [Intel-gfx] " Tvrtko Ursulin
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Gardner @ 2021-09-15 19:23 UTC (permalink / raw)
  To: linux-kernel
  Cc: tim.gardner, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
	David Airlie, Daniel Vetter, intel-gfx, dri-devel

In capture_vma() Coverity complains of a possible buffer overrun. Even
though this is a static function where all call sites can be checked,
limiting the copy length could save some future grief.

CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
   by copying name without checking the length.
5. parameter_as_source: Note: This defect has an elevated risk because the
   source argument is a parameter of the current function.
1326        strcpy(c->name, name);

Fix any possible overflows by using strncpy(). Zero fill the name buffer to
guarantee ASCII string NULL termination.

Cc: Jani Nikula <jani.nikula@linux.intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: David Airlie <airlied@linux.ie>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: intel-gfx@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 drivers/gpu/drm/i915/i915_gpu_error.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
index 9cf6ac575de1..154df174e2d7 100644
--- a/drivers/gpu/drm/i915/i915_gpu_error.c
+++ b/drivers/gpu/drm/i915/i915_gpu_error.c
@@ -1297,10 +1297,11 @@ static bool record_context(struct i915_gem_context_coredump *e,
 	return simulated;
 }
 
+#define VMA_NAME_LEN 16
 struct intel_engine_capture_vma {
 	struct intel_engine_capture_vma *next;
 	struct i915_vma *vma;
-	char name[16];
+	char name[VMA_NAME_LEN];
 };
 
 static struct intel_engine_capture_vma *
@@ -1314,7 +1315,7 @@ capture_vma(struct intel_engine_capture_vma *next,
 	if (!vma)
 		return next;
 
-	c = kmalloc(sizeof(*c), gfp);
+	c = kzalloc(sizeof(*c), gfp);
 	if (!c)
 		return next;
 
@@ -1323,7 +1324,7 @@ capture_vma(struct intel_engine_capture_vma *next,
 		return next;
 	}
 
-	strcpy(c->name, name);
+	strncpy(c->name, name, VMA_NAME_LEN-1);
 	c->vma = vma; /* reference held while active */
 
 	c->next = next;
-- 
2.33.0


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

* Re: [Intel-gfx] [PATCH] drm/i915: zero fill vma name buffer
  2021-09-15 19:23 [PATCH] drm/i915: zero fill vma name buffer Tim Gardner
@ 2021-09-16  8:23 ` Tvrtko Ursulin
  2021-09-16 10:43   ` Jani Nikula
  0 siblings, 1 reply; 6+ messages in thread
From: Tvrtko Ursulin @ 2021-09-16  8:23 UTC (permalink / raw)
  To: Tim Gardner, linux-kernel
  Cc: Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, David Airlie,
	Daniel Vetter, intel-gfx, dri-devel


On 15/09/2021 20:23, Tim Gardner wrote:
> In capture_vma() Coverity complains of a possible buffer overrun. Even
> though this is a static function where all call sites can be checked,
> limiting the copy length could save some future grief.
> 
> CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
> 4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
>     by copying name without checking the length.
> 5. parameter_as_source: Note: This defect has an elevated risk because the
>     source argument is a parameter of the current function.
> 1326        strcpy(c->name, name);
> 
> Fix any possible overflows by using strncpy(). Zero fill the name buffer to
> guarantee ASCII string NULL termination.
> 
> Cc: Jani Nikula <jani.nikula@linux.intel.com>
> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
> Cc: David Airlie <airlied@linux.ie>
> Cc: Daniel Vetter <daniel@ffwll.ch>
> Cc: intel-gfx@lists.freedesktop.org
> Cc: dri-devel@lists.freedesktop.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
> ---
>   drivers/gpu/drm/i915/i915_gpu_error.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
> index 9cf6ac575de1..154df174e2d7 100644
> --- a/drivers/gpu/drm/i915/i915_gpu_error.c
> +++ b/drivers/gpu/drm/i915/i915_gpu_error.c
> @@ -1297,10 +1297,11 @@ static bool record_context(struct i915_gem_context_coredump *e,
>   	return simulated;
>   }
>   
> +#define VMA_NAME_LEN 16
>   struct intel_engine_capture_vma {
>   	struct intel_engine_capture_vma *next;
>   	struct i915_vma *vma;
> -	char name[16];
> +	char name[VMA_NAME_LEN];
>   };
>   
>   static struct intel_engine_capture_vma *
> @@ -1314,7 +1315,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>   	if (!vma)
>   		return next;
>   
> -	c = kmalloc(sizeof(*c), gfp);
> +	c = kzalloc(sizeof(*c), gfp);
>   	if (!c)
>   		return next;
>   
> @@ -1323,7 +1324,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>   		return next;
>   	}
>   
> -	strcpy(c->name, name);
> +	strncpy(c->name, name, VMA_NAME_LEN-1);

GCC is supposed to catch any problems here as you say in the commit message.

But to fix I suggest a single line change to strlcpy(c->name, name, 
sizeof(c->name)) which always null terminates as bonus.

Probably same in i915_vma_coredump_create() which with strncpy would 
have a theoretical chance of attempting to copy over a 
non-null-terminated string.

Regards,

Tvrtko

>   	c->vma = vma; /* reference held while active */
>   
>   	c->next = next;
> 

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

* Re: [Intel-gfx] [PATCH] drm/i915: zero fill vma name buffer
  2021-09-16  8:23 ` [Intel-gfx] " Tvrtko Ursulin
@ 2021-09-16 10:43   ` Jani Nikula
  2021-09-16 11:37     ` Tim Gardner
  0 siblings, 1 reply; 6+ messages in thread
From: Jani Nikula @ 2021-09-16 10:43 UTC (permalink / raw)
  To: Tvrtko Ursulin, Tim Gardner, linux-kernel
  Cc: Joonas Lahtinen, Rodrigo Vivi, David Airlie, Daniel Vetter,
	intel-gfx, dri-devel

On Thu, 16 Sep 2021, Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com> wrote:
> On 15/09/2021 20:23, Tim Gardner wrote:
>> In capture_vma() Coverity complains of a possible buffer overrun. Even
>> though this is a static function where all call sites can be checked,
>> limiting the copy length could save some future grief.
>> 
>> CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
>> 4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
>>     by copying name without checking the length.
>> 5. parameter_as_source: Note: This defect has an elevated risk because the
>>     source argument is a parameter of the current function.
>> 1326        strcpy(c->name, name);
>> 
>> Fix any possible overflows by using strncpy(). Zero fill the name buffer to
>> guarantee ASCII string NULL termination.
>> 
>> Cc: Jani Nikula <jani.nikula@linux.intel.com>
>> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
>> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
>> Cc: David Airlie <airlied@linux.ie>
>> Cc: Daniel Vetter <daniel@ffwll.ch>
>> Cc: intel-gfx@lists.freedesktop.org
>> Cc: dri-devel@lists.freedesktop.org
>> Cc: linux-kernel@vger.kernel.org
>> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
>> ---
>>   drivers/gpu/drm/i915/i915_gpu_error.c | 7 ++++---
>>   1 file changed, 4 insertions(+), 3 deletions(-)
>> 
>> diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
>> index 9cf6ac575de1..154df174e2d7 100644
>> --- a/drivers/gpu/drm/i915/i915_gpu_error.c
>> +++ b/drivers/gpu/drm/i915/i915_gpu_error.c
>> @@ -1297,10 +1297,11 @@ static bool record_context(struct i915_gem_context_coredump *e,
>>   	return simulated;
>>   }
>>   
>> +#define VMA_NAME_LEN 16
>>   struct intel_engine_capture_vma {
>>   	struct intel_engine_capture_vma *next;
>>   	struct i915_vma *vma;
>> -	char name[16];
>> +	char name[VMA_NAME_LEN];
>>   };
>>   
>>   static struct intel_engine_capture_vma *
>> @@ -1314,7 +1315,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>>   	if (!vma)
>>   		return next;
>>   
>> -	c = kmalloc(sizeof(*c), gfp);
>> +	c = kzalloc(sizeof(*c), gfp);
>>   	if (!c)
>>   		return next;
>>   
>> @@ -1323,7 +1324,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>>   		return next;
>>   	}
>>   
>> -	strcpy(c->name, name);
>> +	strncpy(c->name, name, VMA_NAME_LEN-1);
>
> GCC is supposed to catch any problems here as you say in the commit message.
>
> But to fix I suggest a single line change to strlcpy(c->name, name, 
> sizeof(c->name)) which always null terminates as bonus.

strscpy() is preferred over both strncpy() and strlcpy(). :)

BR,
Jani.

>
> Probably same in i915_vma_coredump_create() which with strncpy would 
> have a theoretical chance of attempting to copy over a 
> non-null-terminated string.
>
> Regards,
>
> Tvrtko
>
>>   	c->vma = vma; /* reference held while active */
>>   
>>   	c->next = next;
>> 

-- 
Jani Nikula, Intel Open Source Graphics Center

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

* Re: [Intel-gfx] [PATCH] drm/i915: zero fill vma name buffer
  2021-09-16 10:43   ` Jani Nikula
@ 2021-09-16 11:37     ` Tim Gardner
  2021-09-16 12:26       ` [PATCH v2] drm/i915: use strscpy() to avoid buffer overrun Tim Gardner
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Gardner @ 2021-09-16 11:37 UTC (permalink / raw)
  To: Jani Nikula, Tvrtko Ursulin, linux-kernel
  Cc: Joonas Lahtinen, Rodrigo Vivi, David Airlie, Daniel Vetter,
	intel-gfx, dri-devel



On 9/16/21 4:43 AM, Jani Nikula wrote:
> On Thu, 16 Sep 2021, Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com> wrote:
>> On 15/09/2021 20:23, Tim Gardner wrote:
>>> In capture_vma() Coverity complains of a possible buffer overrun. Even
>>> though this is a static function where all call sites can be checked,
>>> limiting the copy length could save some future grief.
>>>
>>> CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
>>> 4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
>>>      by copying name without checking the length.
>>> 5. parameter_as_source: Note: This defect has an elevated risk because the
>>>      source argument is a parameter of the current function.
>>> 1326        strcpy(c->name, name);
>>>
>>> Fix any possible overflows by using strncpy(). Zero fill the name buffer to
>>> guarantee ASCII string NULL termination.
>>>
>>> Cc: Jani Nikula <jani.nikula@linux.intel.com>
>>> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
>>> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
>>> Cc: David Airlie <airlied@linux.ie>
>>> Cc: Daniel Vetter <daniel@ffwll.ch>
>>> Cc: intel-gfx@lists.freedesktop.org
>>> Cc: dri-devel@lists.freedesktop.org
>>> Cc: linux-kernel@vger.kernel.org
>>> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
>>> ---
>>>    drivers/gpu/drm/i915/i915_gpu_error.c | 7 ++++---
>>>    1 file changed, 4 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
>>> index 9cf6ac575de1..154df174e2d7 100644
>>> --- a/drivers/gpu/drm/i915/i915_gpu_error.c
>>> +++ b/drivers/gpu/drm/i915/i915_gpu_error.c
>>> @@ -1297,10 +1297,11 @@ static bool record_context(struct i915_gem_context_coredump *e,
>>>    	return simulated;
>>>    }
>>>    
>>> +#define VMA_NAME_LEN 16
>>>    struct intel_engine_capture_vma {
>>>    	struct intel_engine_capture_vma *next;
>>>    	struct i915_vma *vma;
>>> -	char name[16];
>>> +	char name[VMA_NAME_LEN];
>>>    };
>>>    
>>>    static struct intel_engine_capture_vma *
>>> @@ -1314,7 +1315,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>>>    	if (!vma)
>>>    		return next;
>>>    
>>> -	c = kmalloc(sizeof(*c), gfp);
>>> +	c = kzalloc(sizeof(*c), gfp);
>>>    	if (!c)
>>>    		return next;
>>>    
>>> @@ -1323,7 +1324,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>>>    		return next;
>>>    	}
>>>    
>>> -	strcpy(c->name, name);
>>> +	strncpy(c->name, name, VMA_NAME_LEN-1);
>>
>> GCC is supposed to catch any problems here as you say in the commit message.
>>
>> But to fix I suggest a single line change to strlcpy(c->name, name,
>> sizeof(c->name)) which always null terminates as bonus.
> 
> strscpy() is preferred over both strncpy() and strlcpy(). :)
> 
> BR,
> Jani.
> 

Good call. v2 on the way.

rtg

>>
>> Probably same in i915_vma_coredump_create() which with strncpy would
>> have a theoretical chance of attempting to copy over a
>> non-null-terminated string.
>>
>> Regards,
>>
>> Tvrtko
>>
>>>    	c->vma = vma; /* reference held while active */
>>>    
>>>    	c->next = next;
>>>
> 

-- 
-----------
Tim Gardner
Canonical, Inc

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

* [PATCH v2] drm/i915: use strscpy() to avoid buffer overrun
  2021-09-16 11:37     ` Tim Gardner
@ 2021-09-16 12:26       ` Tim Gardner
  2021-09-17  7:37         ` [Intel-gfx] " Tvrtko Ursulin
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Gardner @ 2021-09-16 12:26 UTC (permalink / raw)
  To: intel-gfx
  Cc: tim.gardner, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
	David Airlie, Daniel Vetter, dri-devel, linux-kernel

In capture_vma() Coverity complains of a possible buffer overrun. Even
though this is a static function where all call sites can be checked,
limiting the copy length could save some future grief.

CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
   by copying name without checking the length.
5. parameter_as_source: Note: This defect has an elevated risk because the
   source argument is a parameter of the current function.
1326        strcpy(c->name, name);

Fix any possible overflows by using strscpy() which guarantees NULL termination.

Also correct 2 other strcpy() call sites with the same potential for Coverity
warnings or overruns.

v2 - Change $SUBJECT from "drm/i915: zero fill vma name buffer"
     Use strscpy() instead of strncpy(). Its a much simpler change.

Cc: Jani Nikula <jani.nikula@linux.intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: David Airlie <airlied@linux.ie>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: intel-gfx@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 drivers/gpu/drm/i915/i915_gpu_error.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
index 9cf6ac575de1..7f246f51959d 100644
--- a/drivers/gpu/drm/i915/i915_gpu_error.c
+++ b/drivers/gpu/drm/i915/i915_gpu_error.c
@@ -1015,7 +1015,7 @@ i915_vma_coredump_create(const struct intel_gt *gt,
 		return NULL;
 	}
 
-	strcpy(dst->name, name);
+	strscpy(dst->name, name, sizeof(dst->name));
 	dst->next = NULL;
 
 	dst->gtt_offset = vma->node.start;
@@ -1279,7 +1279,7 @@ static bool record_context(struct i915_gem_context_coredump *e,
 	rcu_read_lock();
 	task = pid_task(ctx->pid, PIDTYPE_PID);
 	if (task) {
-		strcpy(e->comm, task->comm);
+		strscpy(e->comm, task->comm, sizeof(e->comm));
 		e->pid = task->pid;
 	}
 	rcu_read_unlock();
@@ -1323,7 +1323,7 @@ capture_vma(struct intel_engine_capture_vma *next,
 		return next;
 	}
 
-	strcpy(c->name, name);
+	strscpy(c->name, name, sizeof(c->name));
 	c->vma = vma; /* reference held while active */
 
 	c->next = next;
-- 
2.33.0


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

* Re: [Intel-gfx] [PATCH v2] drm/i915: use strscpy() to avoid buffer overrun
  2021-09-16 12:26       ` [PATCH v2] drm/i915: use strscpy() to avoid buffer overrun Tim Gardner
@ 2021-09-17  7:37         ` Tvrtko Ursulin
  0 siblings, 0 replies; 6+ messages in thread
From: Tvrtko Ursulin @ 2021-09-17  7:37 UTC (permalink / raw)
  To: Tim Gardner, intel-gfx
  Cc: Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, David Airlie,
	Daniel Vetter, dri-devel, linux-kernel


On 16/09/2021 13:26, Tim Gardner wrote:
> In capture_vma() Coverity complains of a possible buffer overrun. Even
> though this is a static function where all call sites can be checked,
> limiting the copy length could save some future grief.
> 
> CID 93300 (#1 of 1): Copy into fixed size buffer (STRING_OVERFLOW)
> 4. fixed_size_dest: You might overrun the 16-character fixed-size string c->name
>     by copying name without checking the length.
> 5. parameter_as_source: Note: This defect has an elevated risk because the
>     source argument is a parameter of the current function.
> 1326        strcpy(c->name, name);
> 
> Fix any possible overflows by using strscpy() which guarantees NULL termination.
> 
> Also correct 2 other strcpy() call sites with the same potential for Coverity
> warnings or overruns.
> 
> v2 - Change $SUBJECT from "drm/i915: zero fill vma name buffer"
>       Use strscpy() instead of strncpy(). Its a much simpler change.
> 
> Cc: Jani Nikula <jani.nikula@linux.intel.com>
> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
> Cc: David Airlie <airlied@linux.ie>
> Cc: Daniel Vetter <daniel@ffwll.ch>
> Cc: intel-gfx@lists.freedesktop.org
> Cc: dri-devel@lists.freedesktop.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
> ---
>   drivers/gpu/drm/i915/i915_gpu_error.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c
> index 9cf6ac575de1..7f246f51959d 100644
> --- a/drivers/gpu/drm/i915/i915_gpu_error.c
> +++ b/drivers/gpu/drm/i915/i915_gpu_error.c
> @@ -1015,7 +1015,7 @@ i915_vma_coredump_create(const struct intel_gt *gt,
>   		return NULL;
>   	}
>   
> -	strcpy(dst->name, name);
> +	strscpy(dst->name, name, sizeof(dst->name));
>   	dst->next = NULL;
>   
>   	dst->gtt_offset = vma->node.start;
> @@ -1279,7 +1279,7 @@ static bool record_context(struct i915_gem_context_coredump *e,
>   	rcu_read_lock();
>   	task = pid_task(ctx->pid, PIDTYPE_PID);
>   	if (task) {
> -		strcpy(e->comm, task->comm);
> +		strscpy(e->comm, task->comm, sizeof(e->comm));
>   		e->pid = task->pid;
>   	}
>   	rcu_read_unlock();
> @@ -1323,7 +1323,7 @@ capture_vma(struct intel_engine_capture_vma *next,
>   		return next;
>   	}
>   
> -	strcpy(c->name, name);
> +	strscpy(c->name, name, sizeof(c->name));
>   	c->vma = vma; /* reference held while active */
>   
>   	c->next = next;
> 

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Regards,

Tvrtko

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

end of thread, other threads:[~2021-09-17  7:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-15 19:23 [PATCH] drm/i915: zero fill vma name buffer Tim Gardner
2021-09-16  8:23 ` [Intel-gfx] " Tvrtko Ursulin
2021-09-16 10:43   ` Jani Nikula
2021-09-16 11:37     ` Tim Gardner
2021-09-16 12:26       ` [PATCH v2] drm/i915: use strscpy() to avoid buffer overrun Tim Gardner
2021-09-17  7:37         ` [Intel-gfx] " Tvrtko Ursulin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).