public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] accel/qaic: Fix dereferencing freed memory
@ 2023-06-10  2:12 Sukrut Bellary
  2023-06-12 11:22 ` Christian König
  2023-06-12 12:44 ` Pranjal Ramajor Asha Kanojiya
  0 siblings, 2 replies; 7+ messages in thread
From: Sukrut Bellary @ 2023-06-10  2:12 UTC (permalink / raw)
  To: Jeffrey Hugo, Oded Gabbay, Sumit Semwal, Christian König
  Cc: Sukrut Bellary, linux-arm-msm, dri-devel, linaro-mm-sig,
	linux-media, linux-kernel, kernel-janitors

smatch warning:
	drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
		dereferencing freed memory 'obj->import_attach'

obj->import_attach is detached and freed using dma_buf_detach().
But used after free to decrease the dmabuf ref count using
dma_buf_put().

Fixes: ff13be830333 ("accel/qaic: Add datapath")
Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
---
 drivers/accel/qaic/qaic_data.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
index e42c1f9ffff8..7cba4d680ea8 100644
--- a/drivers/accel/qaic/qaic_data.c
+++ b/drivers/accel/qaic/qaic_data.c
@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc
 static void qaic_free_object(struct drm_gem_object *obj)
 {
 	struct qaic_bo *bo = to_qaic_bo(obj);
+	struct dma_buf *dmabuf;
 
 	if (obj->import_attach) {
 		/* DMABUF/PRIME Path */
+		dmabuf = obj->import_attach->dmabuf;
 		dma_buf_detach(obj->import_attach->dmabuf, obj->import_attach);
-		dma_buf_put(obj->import_attach->dmabuf);
+		dma_buf_put(dmabuf);
 	} else {
 		/* Private buffer allocation path */
 		qaic_free_sgt(bo->sgt);
-- 
2.34.1


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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-10  2:12 [PATCH] accel/qaic: Fix dereferencing freed memory Sukrut Bellary
@ 2023-06-12 11:22 ` Christian König
  2023-06-12 13:03   ` Pranjal Ramajor Asha Kanojiya
  2023-06-12 12:44 ` Pranjal Ramajor Asha Kanojiya
  1 sibling, 1 reply; 7+ messages in thread
From: Christian König @ 2023-06-12 11:22 UTC (permalink / raw)
  To: Sukrut Bellary, Jeffrey Hugo, Oded Gabbay, Sumit Semwal
  Cc: linux-arm-msm, dri-devel, linaro-mm-sig, linux-media,
	linux-kernel, kernel-janitors



Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
> smatch warning:
> 	drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
> 		dereferencing freed memory 'obj->import_attach'
>
> obj->import_attach is detached and freed using dma_buf_detach().
> But used after free to decrease the dmabuf ref count using
> dma_buf_put().
>
> Fixes: ff13be830333 ("accel/qaic: Add datapath")
> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
> ---
>   drivers/accel/qaic/qaic_data.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
> index e42c1f9ffff8..7cba4d680ea8 100644
> --- a/drivers/accel/qaic/qaic_data.c
> +++ b/drivers/accel/qaic/qaic_data.c
> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc
>   static void qaic_free_object(struct drm_gem_object *obj)
>   {
>   	struct qaic_bo *bo = to_qaic_bo(obj);
> +	struct dma_buf *dmabuf;

Maybe move that variable into the if.

>   
>   	if (obj->import_attach) {
>   		/* DMABUF/PRIME Path */
> +		dmabuf = obj->import_attach->dmabuf;
>   		dma_buf_detach(obj->import_attach->dmabuf, obj->import_attach);
> -		dma_buf_put(obj->import_attach->dmabuf);
> +		dma_buf_put(dmabuf);

I strongly assume you are not using the GEM prime helpers for this?

Christian.

>   	} else {
>   		/* Private buffer allocation path */
>   		qaic_free_sgt(bo->sgt);


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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-10  2:12 [PATCH] accel/qaic: Fix dereferencing freed memory Sukrut Bellary
  2023-06-12 11:22 ` Christian König
@ 2023-06-12 12:44 ` Pranjal Ramajor Asha Kanojiya
  1 sibling, 0 replies; 7+ messages in thread
From: Pranjal Ramajor Asha Kanojiya @ 2023-06-12 12:44 UTC (permalink / raw)
  To: Sukrut Bellary, Jeffrey Hugo, Oded Gabbay, Sumit Semwal,
	Christian König
  Cc: linux-arm-msm, dri-devel, linaro-mm-sig, linux-media,
	linux-kernel, kernel-janitors



On 6/10/2023 7:42 AM, Sukrut Bellary wrote:
> smatch warning:
> 	drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
> 		dereferencing freed memory 'obj->import_attach'
> 
> obj->import_attach is detached and freed using dma_buf_detach().
> But used after free to decrease the dmabuf ref count using
> dma_buf_put().
> 
> Fixes: ff13be830333 ("accel/qaic: Add datapath")
> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
> ---
>   drivers/accel/qaic/qaic_data.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
> index e42c1f9ffff8..7cba4d680ea8 100644
> --- a/drivers/accel/qaic/qaic_data.c
> +++ b/drivers/accel/qaic/qaic_data.c
> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc
>   static void qaic_free_object(struct drm_gem_object *obj)
>   {
>   	struct qaic_bo *bo = to_qaic_bo(obj);
> +	struct dma_buf *dmabuf;
>   
>   	if (obj->import_attach) {
>   		/* DMABUF/PRIME Path */
> +		dmabuf = obj->import_attach->dmabuf;
>   		dma_buf_detach(obj->import_attach->dmabuf, obj->import_attach);
> -		dma_buf_put(obj->import_attach->dmabuf);
> +		dma_buf_put(dmabuf);
>   	} else {
>   		/* Private buffer allocation path */
>   		qaic_free_sgt(bo->sgt);

Reviewed-by: Pranjal Ramajor Asha Kanojiya <quic_pkanojiy@quicinc.com>

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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-12 11:22 ` Christian König
@ 2023-06-12 13:03   ` Pranjal Ramajor Asha Kanojiya
  2023-06-12 13:21     ` Christian König
  0 siblings, 1 reply; 7+ messages in thread
From: Pranjal Ramajor Asha Kanojiya @ 2023-06-12 13:03 UTC (permalink / raw)
  To: Christian König, Sukrut Bellary, Jeffrey Hugo, Oded Gabbay,
	Sumit Semwal
  Cc: linux-arm-msm, kernel-janitors, linux-kernel, dri-devel,
	linaro-mm-sig, linux-media



On 6/12/2023 4:52 PM, Christian König wrote:
> 
> 
> Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
>> smatch warning:
>>     drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
>>         dereferencing freed memory 'obj->import_attach'
>>
>> obj->import_attach is detached and freed using dma_buf_detach().
>> But used after free to decrease the dmabuf ref count using
>> dma_buf_put().
>>
>> Fixes: ff13be830333 ("accel/qaic: Add datapath")
>> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
>> ---
>>   drivers/accel/qaic/qaic_data.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/accel/qaic/qaic_data.c 
>> b/drivers/accel/qaic/qaic_data.c
>> index e42c1f9ffff8..7cba4d680ea8 100644
>> --- a/drivers/accel/qaic/qaic_data.c
>> +++ b/drivers/accel/qaic/qaic_data.c
>> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct 
>> drm_gem_object *obj, struct vm_area_struc
>>   static void qaic_free_object(struct drm_gem_object *obj)
>>   {
>>       struct qaic_bo *bo = to_qaic_bo(obj);
>> +    struct dma_buf *dmabuf;
> 
> Maybe move that variable into the if.
> 
>>       if (obj->import_attach) {
>>           /* DMABUF/PRIME Path */
>> +        dmabuf = obj->import_attach->dmabuf;
>>           dma_buf_detach(obj->import_attach->dmabuf, obj->import_attach);
>> -        dma_buf_put(obj->import_attach->dmabuf);
>> +        dma_buf_put(dmabuf);
> 
> I strongly assume you are not using the GEM prime helpers for this?
> 
> Christian.

Driver uses drm_gem_prime_fd_to_handle() helper function but it also 
registers for ->gem_prime_import() which is internally called by 
drm_gem_prime_fd_to_handle(). All the operations done in 
gem_prime_import() are undone here.

> 
>>       } else {
>>           /* Private buffer allocation path */
>>           qaic_free_sgt(bo->sgt);
> 

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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-12 13:03   ` Pranjal Ramajor Asha Kanojiya
@ 2023-06-12 13:21     ` Christian König
  2023-06-12 15:09       ` Jeffrey Hugo
  0 siblings, 1 reply; 7+ messages in thread
From: Christian König @ 2023-06-12 13:21 UTC (permalink / raw)
  To: Pranjal Ramajor Asha Kanojiya, Sukrut Bellary, Jeffrey Hugo,
	Oded Gabbay, Sumit Semwal
  Cc: linux-arm-msm, kernel-janitors, linux-kernel, dri-devel,
	linaro-mm-sig, linux-media

Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
>
>
> On 6/12/2023 4:52 PM, Christian König wrote:
>>
>>
>> Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
>>> smatch warning:
>>>     drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
>>>         dereferencing freed memory 'obj->import_attach'
>>>
>>> obj->import_attach is detached and freed using dma_buf_detach().
>>> But used after free to decrease the dmabuf ref count using
>>> dma_buf_put().
>>>
>>> Fixes: ff13be830333 ("accel/qaic: Add datapath")
>>> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
>>> ---
>>>   drivers/accel/qaic/qaic_data.c | 4 +++-
>>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/accel/qaic/qaic_data.c 
>>> b/drivers/accel/qaic/qaic_data.c
>>> index e42c1f9ffff8..7cba4d680ea8 100644
>>> --- a/drivers/accel/qaic/qaic_data.c
>>> +++ b/drivers/accel/qaic/qaic_data.c
>>> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct 
>>> drm_gem_object *obj, struct vm_area_struc
>>>   static void qaic_free_object(struct drm_gem_object *obj)
>>>   {
>>>       struct qaic_bo *bo = to_qaic_bo(obj);
>>> +    struct dma_buf *dmabuf;
>>
>> Maybe move that variable into the if.
>>
>>>       if (obj->import_attach) {
>>>           /* DMABUF/PRIME Path */
>>> +        dmabuf = obj->import_attach->dmabuf;
>>>           dma_buf_detach(obj->import_attach->dmabuf, 
>>> obj->import_attach);
>>> -        dma_buf_put(obj->import_attach->dmabuf);
>>> +        dma_buf_put(dmabuf);
>>
>> I strongly assume you are not using the GEM prime helpers for this?
>>
>> Christian.
>
> Driver uses drm_gem_prime_fd_to_handle() helper function but it also 
> registers for ->gem_prime_import() which is internally called by 
> drm_gem_prime_fd_to_handle(). All the operations done in 
> gem_prime_import() are undone here.

Then why don't you use drm_prime_gem_destroy() which is the cleanup 
helper for imports created by ->gem_prime_import() ?

That looks pretty much identical to what you do here manually.

Regards,
Christian.

>
>>
>>>       } else {
>>>           /* Private buffer allocation path */
>>>           qaic_free_sgt(bo->sgt);
>>


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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-12 13:21     ` Christian König
@ 2023-06-12 15:09       ` Jeffrey Hugo
  2023-06-12 15:47         ` Pranjal Ramajor Asha Kanojiya
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Hugo @ 2023-06-12 15:09 UTC (permalink / raw)
  To: Christian König, Pranjal Ramajor Asha Kanojiya,
	Sukrut Bellary, Oded Gabbay, Sumit Semwal
  Cc: linux-arm-msm, kernel-janitors, linux-kernel, dri-devel,
	linaro-mm-sig, linux-media

On 6/12/2023 7:21 AM, Christian König wrote:
> Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
>>
>>
>> On 6/12/2023 4:52 PM, Christian König wrote:
>>>
>>>
>>> Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
>>>> smatch warning:
>>>>     drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
>>>>         dereferencing freed memory 'obj->import_attach'
>>>>
>>>> obj->import_attach is detached and freed using dma_buf_detach().
>>>> But used after free to decrease the dmabuf ref count using
>>>> dma_buf_put().
>>>>
>>>> Fixes: ff13be830333 ("accel/qaic: Add datapath")
>>>> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
>>>> ---
>>>>   drivers/accel/qaic/qaic_data.c | 4 +++-
>>>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/accel/qaic/qaic_data.c 
>>>> b/drivers/accel/qaic/qaic_data.c
>>>> index e42c1f9ffff8..7cba4d680ea8 100644
>>>> --- a/drivers/accel/qaic/qaic_data.c
>>>> +++ b/drivers/accel/qaic/qaic_data.c
>>>> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct 
>>>> drm_gem_object *obj, struct vm_area_struc
>>>>   static void qaic_free_object(struct drm_gem_object *obj)
>>>>   {
>>>>       struct qaic_bo *bo = to_qaic_bo(obj);
>>>> +    struct dma_buf *dmabuf;
>>>
>>> Maybe move that variable into the if.
>>>
>>>>       if (obj->import_attach) {
>>>>           /* DMABUF/PRIME Path */
>>>> +        dmabuf = obj->import_attach->dmabuf;
>>>>           dma_buf_detach(obj->import_attach->dmabuf, 
>>>> obj->import_attach);
>>>> -        dma_buf_put(obj->import_attach->dmabuf);
>>>> +        dma_buf_put(dmabuf);
>>>
>>> I strongly assume you are not using the GEM prime helpers for this?
>>>
>>> Christian.
>>
>> Driver uses drm_gem_prime_fd_to_handle() helper function but it also 
>> registers for ->gem_prime_import() which is internally called by 
>> drm_gem_prime_fd_to_handle(). All the operations done in 
>> gem_prime_import() are undone here.
> 
> Then why don't you use drm_prime_gem_destroy() which is the cleanup 
> helper for imports created by ->gem_prime_import() ?
> 
> That looks pretty much identical to what you do here manually.

I think destroy() wasn't used because we are new to DRM and sometimes 
confused by the multitude of options.  I appreciate the suggestion and 
will follow up to see if destroy() will work here, or identify what 
would prevent the use of it.

-Jeff

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

* Re: [PATCH] accel/qaic: Fix dereferencing freed memory
  2023-06-12 15:09       ` Jeffrey Hugo
@ 2023-06-12 15:47         ` Pranjal Ramajor Asha Kanojiya
  0 siblings, 0 replies; 7+ messages in thread
From: Pranjal Ramajor Asha Kanojiya @ 2023-06-12 15:47 UTC (permalink / raw)
  To: Jeffrey Hugo, Christian König, Sukrut Bellary, Oded Gabbay,
	Sumit Semwal
  Cc: linux-arm-msm, kernel-janitors, linux-kernel, dri-devel,
	linaro-mm-sig, linux-media



On 6/12/2023 8:39 PM, Jeffrey Hugo wrote:
> On 6/12/2023 7:21 AM, Christian König wrote:
>> Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
>>>
>>>
>>> On 6/12/2023 4:52 PM, Christian König wrote:
>>>>
>>>>
>>>> Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
>>>>> smatch warning:
>>>>>     drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error:
>>>>>         dereferencing freed memory 'obj->import_attach'
>>>>>
>>>>> obj->import_attach is detached and freed using dma_buf_detach().
>>>>> But used after free to decrease the dmabuf ref count using
>>>>> dma_buf_put().
>>>>>
>>>>> Fixes: ff13be830333 ("accel/qaic: Add datapath")
>>>>> Signed-off-by: Sukrut Bellary <sukrut.bellary@linux.com>
>>>>> ---
>>>>>   drivers/accel/qaic/qaic_data.c | 4 +++-
>>>>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>>>>
>>>>> diff --git a/drivers/accel/qaic/qaic_data.c 
>>>>> b/drivers/accel/qaic/qaic_data.c
>>>>> index e42c1f9ffff8..7cba4d680ea8 100644
>>>>> --- a/drivers/accel/qaic/qaic_data.c
>>>>> +++ b/drivers/accel/qaic/qaic_data.c
>>>>> @@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct 
>>>>> drm_gem_object *obj, struct vm_area_struc
>>>>>   static void qaic_free_object(struct drm_gem_object *obj)
>>>>>   {
>>>>>       struct qaic_bo *bo = to_qaic_bo(obj);
>>>>> +    struct dma_buf *dmabuf;
>>>>
>>>> Maybe move that variable into the if.
>>>>
>>>>>       if (obj->import_attach) {
>>>>>           /* DMABUF/PRIME Path */
>>>>> +        dmabuf = obj->import_attach->dmabuf;
>>>>>           dma_buf_detach(obj->import_attach->dmabuf, 
>>>>> obj->import_attach);
>>>>> -        dma_buf_put(obj->import_attach->dmabuf);
>>>>> +        dma_buf_put(dmabuf);
>>>>
>>>> I strongly assume you are not using the GEM prime helpers for this?
>>>>
>>>> Christian.
>>>
>>> Driver uses drm_gem_prime_fd_to_handle() helper function but it also 
>>> registers for ->gem_prime_import() which is internally called by 
>>> drm_gem_prime_fd_to_handle(). All the operations done in 
>>> gem_prime_import() are undone here.
>>
>> Then why don't you use drm_prime_gem_destroy() which is the cleanup 
>> helper for imports created by ->gem_prime_import() ?
>>
>> That looks pretty much identical to what you do here manually.
> 
> I think destroy() wasn't used because we are new to DRM and sometimes 
> confused by the multitude of options.  I appreciate the suggestion and 
> will follow up to see if destroy() will work here, or identify what 
> would prevent the use of it.
> 
> -Jeff

Thank you Christian for your suggestion. I agree with you driver can use 
  drm_prime_gem_destroy() here.

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

end of thread, other threads:[~2023-06-12 15:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-10  2:12 [PATCH] accel/qaic: Fix dereferencing freed memory Sukrut Bellary
2023-06-12 11:22 ` Christian König
2023-06-12 13:03   ` Pranjal Ramajor Asha Kanojiya
2023-06-12 13:21     ` Christian König
2023-06-12 15:09       ` Jeffrey Hugo
2023-06-12 15:47         ` Pranjal Ramajor Asha Kanojiya
2023-06-12 12:44 ` Pranjal Ramajor Asha Kanojiya

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