accel/qaic: Fix dereferencing freed memory
Subsystems (5)
PATCH 1/1 accel/qaic: Fix dereferencing freed memory
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().
--- drivers/accel/qaic/qaic_data.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/accel/qaic/qaic_data.c +3 -1
@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_strucstatic 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);
Signature · 1 line
2.34.1
Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
Quoted 19 lines
smatch warning:
drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error: dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>--- drivers/accel/qaic/qaic_data.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +0
-0
drivers/accel/qaic/qaic_data.c static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc · line 616
Quoted 4 lines
drivers/accel/qaic/qaic_data.c +1
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_strucstatic 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.
drivers/accel/qaic/qaic_data.c static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc · line 622
Quoted 7 lines
drivers/accel/qaic/qaic_data.c +2
-1if (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.
Quoted 3 lines
drivers/accel/qaic/qaic_data.c +0
-0}else {/* Private buffer allocation path */ qaic_free_sgt (bo->sgt);
Quoted 37 lines · Christian König
Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
Quoted 19 lines
smatch warning:
drivers/accel/qaic/qaic_data.c:620qaic_free_object()error:
dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>---
drivers/accel/qaic/qaic_data.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +0
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(structQuoted 5 lines
drm_gem_object*obj, structvm_area_struc
static voidqaic_free_object(struct drm_gem_object *obj){
structqaic_bo*bo =to_qaic_bo(obj);
+ structdma_buf*dmabuf;Maybe move that variable into the if.
Quoted 6 lines
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);
Quoted 3 lines
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.
Quoted 5 lines
Quoted 4 lines
} else {
/* Private buffer allocation path */
qaic_free_sgt(bo->sgt);
Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
Quoted 40 lines · Christian König
Quoted 37 lines
Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
Quoted 19 lines
smatch warning:
drivers/accel/qaic/qaic_data.c:620qaic_free_object()error:
dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>---
drivers/accel/qaic/qaic_data.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +0
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(structQuoted 5 lines
drm_gem_object*obj, structvm_area_struc
static voidqaic_free_object(struct drm_gem_object *obj){
structqaic_bo*bo =to_qaic_bo(obj);
+ structdma_buf*dmabuf;Maybe move that variable into the if.
Quoted 6 lines
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);
Quoted 5 lines
Quoted 3 lines
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 bydrm_gem_prime_fd_to_handle(). All the operations done ingem_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.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
} else {
/* Private buffer allocation path */
qaic_free_sgt(bo->sgt);
Quoted 43 lines · Christian König
Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
Quoted 42 lines · Christian König
Quoted 39 lines
Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
Quoted 19 lines
smatch warning:
drivers/accel/qaic/qaic_data.c:620qaic_free_object()error:
dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>---
drivers/accel/qaic/qaic_data.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +0
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(structQuoted 5 lines
drm_gem_object*obj, structvm_area_struc
static voidqaic_free_object(struct drm_gem_object *obj){
structqaic_bo*bo =to_qaic_bo(obj);
+ structdma_buf*dmabuf;Maybe move that variable into the if.
Quoted 6 lines
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?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Christian.
Driver uses
drm_gem_prime_fd_to_handle()helper function but it also registers for ->gem_prime_import() which is internally called bydrm_gem_prime_fd_to_handle(). All the operations done ingem_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
Quoted 48 lines · Jeffrey Hugo
Quoted 47 lines · Christian König
Am 12.06.23 um 15:03 schrieb Pranjal Ramajor Asha Kanojiya:
Quoted 45 lines · Christian König
Quoted 37 lines
Am 10.06.23 um 04:12 schrieb Sukrut Bellary:
Quoted 19 lines
smatch warning:
drivers/accel/qaic/qaic_data.c:620qaic_free_object()error:
dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>---
drivers/accel/qaic/qaic_data.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +0
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(structQuoted 5 lines
drm_gem_object*obj, structvm_area_struc
static voidqaic_free_object(struct drm_gem_object *obj){
structqaic_bo*bo =to_qaic_bo(obj);
+ structdma_buf*dmabuf;Maybe move that variable into the if.
Quoted 6 lines
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);Quoted 3 lines
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 bydrm_gem_prime_fd_to_handle(). All the operations done ingem_prime_import()are undone here.
Quoted 7 lines
Quoted 3 lines
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 ifdestroy()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.
Quoted 23 lines · Sukrut Bellary
smatch warning:
drivers/accel/qaic/qaic_data.c:620 qaic_free_object() error: dereferencing freed memory 'obj->import_attach'
obj->import_attachis detached and freed usingdma_buf_detach(). But used after free to decrease the dmabuf ref count usingdma_buf_put().Fixes ff13be830333 ("accel/qaic: Add datapath")Signed-off-by Sukrut Bellary <[email protected]>--- drivers/accel/qaic/qaic_data.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/accel/qaic/qaic_data.c +1
-0@@ -613,11 +613,13 @@ static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_strucstatic void qaic_free_object (struct drm_gem_object *obj) {struct qaic_bo *bo =to_qaic_bo (obj); +struct dma_buf *dmabuf;
drivers/accel/qaic/qaic_data.c static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struc · line 625
Quoted 10 lines
drivers/accel/qaic/qaic_data.c +2
-1if (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);