Skip to contentSkip to conversation

drm/imagination: Fix leaks in HWRT data set handling

PATCH New Suyuan Wang 1 participant 3 messages Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/imagination: Fix leaks in HWRT data set handling

Patch 1 is v1 with the Fixes tag corrected: the error path that skips hwrt_fini_common_fw_structure() came with f1f55ed3ffe4, not with the commit that added the ioctl (thanks to the Sashiko review).

Patch 2 fixes the other leak that review pointed out: the 32-bit layer count is truncated into the 16-bit max_rts, so the SRTC and RAA objects allocated for layers > 1 can be skipped on destroy.

Found with a static analysis tool and checked by hand. Compile tested on arm64 (W=1, all files that include pvr_hwrt.h); I don't have the hardware.

Changes in v2:
- 1/2: Fixes tag now points to f1f55ed3ffe4. No code change.
- 2/2: new.

v1: https://lore.kernel.org/all/[email protected]/
---
Suyuan Wang (2):
drm/imagination: Destroy the common FW object when HWRT data setup fails drm/imagination: Don't truncate the layer count in max_rts

 drivers/gpu/drm/imagination/pvr_hwrt.c | 2 ++
 drivers/gpu/drm/imagination/pvr_hwrt.h | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
#1 lore raw

PATCH v2 1/2 drm/imagination: Destroy the common FW object when HWRT data setup fails

From: Suyuan Wang <[email protected]>

If hwrt_data_init_fw_structure() fails in pvr_hwrt_dataset_create(), the error path frees the per-data FW structures and then jumps straight to err_fini_kernel_structure, skipping hwrt_fini_common_fw_structure(). The common FW object created just before is never destroyed, and its init callback data is the hwrt that is then freed:

	err = pvr_fw_object_create(pvr_dev, ...,
				   hwrtdata_common_init, hwrt,
				   &hwrt->common_fw_obj);

Besides leaking the object, this leaves it on the device's FW object list, so a later pvr_fw_hard_reset() calls hwrtdata_common_init() with the freed hwrt, and pvr_fw_fini() warns that the list is not empty.

Destroy the common FW object on that path, in the same order as pvr_hwrt_dataset_release().

Found with a static analysis tool and checked by hand.

Fixes f1f55ed3ffe4 ("drm/imagination: Fixed oops when misusing ioctl CREATE_HWRT_DATASET")
---
 drivers/gpu/drm/imagination/pvr_hwrt.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/imagination/pvr_hwrt.c +2 -0
index 3f7c4346c52c..aa25f6adaa3b 100644
@@ -483,6 +483,8 @@ pvr_hwrt_dataset_create(struct pvr_file *pvr_file,
 	while (--i >= 0)
 		hwrt_data_fini_fw_structure(hwrt, i);
 
+	hwrt_fini_common_fw_structure(hwrt);
+
 err_fini_kernel_structure:
 	hwrt_fini_kernel_structure(hwrt);
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 drm/imagination: Don't truncate the layer count in max_rts

In reply to Suyuan Wang

From: Suyuan Wang <[email protected]>

hwrt_init_kernel_structure() stores the 32-bit args->layers from userspace in the 16-bit hwrt->max_rts:

	hwrt->max_rts = args->layers;

hwrt_data_init_fw_structure() then allocates the SRTC and RAA objects when args->layers > 1, but hwrt_data_fini_fw_structure() only frees them when hwrt->max_rts > 1. With layers = 0x10000 or 0x10001, max_rts ends up as 0 or 1, so both firmware objects are leaked when the data set is destroyed or its creation fails. Any user who can open the render node can do this with DRM_IOCTL_PVR_CREATE_HWRT_DATASET, and the objects grow with the layer count.

Make max_rts a u32, like args->layers and the max_rts field of the firmware's struct rogue_fwif_rta_ctl, so that both checks see the same value.

Found with a static analysis tool and checked by hand.

Fixes 6eedddab733b ("drm/imagination: Implement free list and HWRT create and destroy ioctls")
---
 drivers/gpu/drm/imagination/pvr_hwrt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/imagination/pvr_hwrt.h +1 -1
index 676070b20c3b..7c215e2bd35a 100644
@@ -74,7 +74,7 @@ struct pvr_hwrt_dataset {
 	struct pvr_free_list *free_lists[ROGUE_FWIF_NUM_RTDATA_FREELISTS];
 
 	/** @max_rts: Maximum render targets for this HWRT data set. */
-	u16 max_rts;
+	u32 max_rts;
 };
 
 struct pvr_hwrt_dataset *
Signature · 1 line
2.34.1