Skip to contentSkip to conversation

drm/panthor: snapshot firmware interface counts before loops

PATCH In review Osama Abdelkader 5 participants 9 messages Jul 20 – Aug 3

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

PATCH 1/1 drm/panthor: snapshot firmware interface counts before loops

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds, but the memory is shared with the MCU firmware and can be changed after validation.

Read each count once with READ_ONCE() and use the validated snapshot as the loop bound. This keeps the loop bounds consistent with the validation and prevents the compiler from reloading a firmware-controlled count during iteration.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
---
 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +8 -6
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Osama Abdelkader

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds, but the memory is shared with the MCU firmware and can be changed after validation.

Again, you're making statements that need to be proven here. How is MCU firmware going to change the content of the memory shared with the driver?

Quoted 5 lines

Read each count once with READ_ONCE() and use the validated snapshot as the loop bound. This keeps the loop bounds consistent with the validation and prevents the compiler from reloading a firmware-controlled count during iteration.

As an optimisation I'm fine with it, so:

Best regards,
Liviu

Quoted 72 lines
Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
---
 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +8 -6
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
Signature · 1 line
2.43.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#2 lore raw
Quoted 9 lines · Liviu Dudau
Quoted 4 lines · Osama Abdelkader

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds, but the memory is shared with the MCU firmware and can be changed after validation.

Again, you're making statements that need to be proven here. How is MCU firmware going to change the content of the memory shared with the driver?

Thanks for the review. yes, this was flagged by Sashiko while reviewing the previous patch, but I agree the commit message shouldn't present it as a proven firmware mutation bug.

The useful part here is just making the validated count the same value used as the loop bound. since It's an optimisation, should I send v2 without Fixes and Cc stable tags?

Quoted 6 lines
Quoted 5 lines

Read each count once with READ_ONCE() and use the validated snapshot as the loop bound. This keeps the loop bounds consistent with the validation and prevents the compiler from reloading a firmware-controlled count during iteration.

Quoted 7 lines

As an optimisation I'm fine with it, so:

Best regards,
Liviu

Best regards,
Osama

Quoted 82 lines
Quoted 72 lines
Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
---
 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +8 -6
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
Signature · 1 line
2.43.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer Reviewed-by
#3 lore raw
Quoted 11 lines · Osama Abdelkader
Quoted 9 lines · Liviu Dudau
Quoted 4 lines · Osama Abdelkader

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds, but the memory is shared with the MCU firmware and can be changed after validation.

Again, you're making statements that need to be proven here. How is MCU firmware going to change the content of the memory shared with the driver?

Quoted 7 lines

Thanks for the review. yes, this was flagged by Sashiko while reviewing the previous patch, but I agree the commit message shouldn't present it as a proven firmware mutation bug.

The useful part here is just making the validated count the same value used as the loop bound. since It's an optimisation, should I send v2 without Fixes and Cc stable tags?

I think the issue here is that Sashiko is thinking the firmware isn't trusted. And there's some truth to that - we do perform validation of the firmware binary and sanity check some of the values it writes. But this is more as a robustness check rather than a real trust boundary. The firmware changing values under the kernel's feet is more than just a robustness bug, it's only really likely to happen with an actually malicious firmware.

But the actual change is reasonable, as Liviu says it could even be a (very minor) optimisation. So if you could send a v2 with a reworded commit message then feel free to add:

Thanks,
Steve

Quoted 98 lines
Quoted 6 lines
Quoted 5 lines

Read each count once with READ_ONCE() and use the validated snapshot as the loop bound. This keeps the loop bounds consistent with the validation and prevents the compiler from reloading a firmware-controlled count during iteration.

Quoted 7 lines

As an optimisation I'm fine with it, so:

Best regards,
Liviu

Best regards,
Osama

Quoted 82 lines
Quoted 72 lines
Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
---
 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +8 -6
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
Signature · 1 line
2.43.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#4 lore raw

PATCH v2 1/1 drm/panthor: use local variables for firmware interface counts

panthor_fw_init_ifaces() validates the firmware interface group count before iterating over the CSG interfaces. panthor_init_csg_iface() does the same for the per-group stream count before iterating over the CS interfaces.

Store those validated counts in local variables and use the locals as the loop bounds. This avoids reading the same control interface fields twice and makes it explicit that the loops use the values that were just validated.

---
v2:
- Reword as an optimization/cleanup instead of a firmware trust-boundary fix.
- Drop the Fixes and stable tags.

 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +8 -6
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH v2] drm/panthor: use local variables for firmware interface counts

Quoted 33 lines · On Mon, 3 Aug 2026 14:48:06 +0200 Osama Abdelkader…

panthor_fw_init_ifaces() validates the firmware interface group count before iterating over the CSG interfaces. panthor_init_csg_iface() does the same for the per-group stream count before iterating over the CS interfaces.

Store those validated counts in local variables and use the locals as the loop bounds. This avoids reading the same control interface fields twice and makes it explicit that the loops use the values that were just validated.

---
v2:
- Reword as an optimization/cleanup instead of a firmware trust-boundary fix.
- Drop the Fixes and stable tags.

 drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -0
index e2fcbd639c3c..6e6da98d795e 100644
@@ -959,6 +959,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||

drivers/gpu/drm/panthor/panthor_fw.c static int panthor_init_csg_iface(struct panthor_device *ptdev, · line 976

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
@@ -972,8 +973,8 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	stream_num = READ_ONCE(csg_iface->control->stream_num);

We need a comment to explain the READ_ONCE(), otherwise new readers (and given my inability to remember things, I consider myself a new reader after 2 weeks :-)) will keep wondering why we're forcing the compiler to read the memory only once. IIUC, that's here to protect against self-modifying control sections, so maybe say that. Or if we consider that the FW is trusted/sure, drop the READ_ONCE()...

Quoted 42 lines
drivers/gpu/drm/panthor/panthor_fw.c +6 -4
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -990,7 +991,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1015,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1034,17 +1036,17 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
 			return ret;
 	}
 
 	drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",
#6 lore raw

PATCH v3 1/1 drm/panthor: use local variables for firmware interface counts

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds.

Read each count once with READ_ONCE() and store it in a local variable. This avoids reading the same control section field twice and makes it explicit that the loop uses the same value that passed validation to protect against self-modifying control sections.

---
v3:
- Add comments explaining the READ_ONCE() usage.

 drivers/gpu/drm/panthor/panthor_fw.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +18 -6
index 8411c468bdac..b2ccb81f280c 100644
@@ -954,6 +954,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -967,8 +968,13 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	/*
+	 * To protect against self-modifying control sections
+	 * take a single snapshot from the control section so validation and
+	 * iteration use the same value.
+	 */
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -986,7 +992,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1010,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1035,13 +1042,18 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	/*
+	 * To protect against self-modifying control sections
+	 * take a single snapshot from the control section so validation and
+	 * iteration use the same value.
+	 */
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v3] drm/panthor: use local variables for firmware interface counts

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch prevents TOCTOU vulnerabilities during firmware interface initialization by using READ_ONCE() for group_num and stream_num, but fails to apply this protection to panthor_sched_init(), which re-reads these exact same fields directly from the volatile firmware-shared memory. This can lead to an initialization mismatch and a subsequent NULL pointer dereference.
- [High] Userspace queue creation validates group_args->queues.count against the hardcoded MAX_CS_PER_CSG, but fails to validate it against the firmware-reported and initialized cs_slot_count, allowing a NULL pointer dereference.
- [Medium] The patch adds READ_ONCE() for group_num and stream_num but leaves group_stride and stream_stride unprotected, allowing potential out-of-bounds memory accesses if the compiler rematerializes the reads.
--

--- Patch [1]: [PATCH v3] drm/panthor: use local variables for firmware interface counts --- commit 49abfdca3f9a1c66d7525434ea16162686b42354
Author: Osama Abdelkader <[email protected]>

drm/panthor: use local variables for firmware interface counts

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds.

drivers/gpu/drm/panthor/panthor_fw.c static int panthor_init_csg_iface(struct panthor_device *ptdev, · line 957

Quoted 8 lines
drivers/gpu/drm/panthor/panthor_fw.c +0 -0
index 10b7e12c1f1ed..fbc4d10aed1fb 100644
@@ -955,6 +955,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);

[Severity: Medium]
This isn't a bug introduced by this patch, but since we are preventing TOCTOU vulnerabilities with READ_ONCE(), should group_stride (and stream_stride in panthor_init_cs_iface) also be protected?

If the compiler rematerializes the reads for the strides after the bounds checks, could a malicious firmware modify the stride concurrently and cause out-of-bounds pointer arithmetic when setting up the interface pointers?

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_fw.c +1 -0
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||

[ ... ]

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_fw.c +0 -2
@@ -1036,13 +1043,18 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {

drivers/gpu/drm/panthor/panthor_fw.c static int panthor_fw_init_ifaces(struct panthor_device *ptdev) · line 1055

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +7 -0
+	/*
+	 * To protect against self-modifying control sections
+	 * take a single snapshot from the control section so validation and
+	 * iteration use the same value.
+	 */
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}

[Severity: High]
This is a pre-existing issue, but does the scheduler also need to use these protected values?

In drivers/gpu/drm/panthor/panthor_sched.c:panthor_sched_init(), the code re-reads these fields directly from the volatile firmware memory:

	sched->csg_slot_count = num_groups;
	sched->cs_slot_count = csg_iface->control->stream_num;

If the firmware increases stream_num after the bounds checks here in panthor_fw_init_ifaces(), could the scheduler configure cs_slot_count to a larger value than the initialized structures in ptdev->fw->iface?

Later, when iterating up to these counts in tick_ctx_apply() or csg_slot_prog_locked(), could the driver retrieve a zero-initialized interface structure and dereference NULL pointers?

[Severity: High]
This is also a pre-existing issue, but is the userspace queue count properly bounded by the initialized firmware slot count?

In drivers/gpu/drm/panthor/panthor_drv.c:panthor_ioctl_group_create(), the queue count is validated against the hardcoded maximum:

	if (!args->queues.count || args->queues.count > MAX_CS_PER_CSG)
		return -EINVAL;

However, if the firmware reports fewer slots than MAX_CS_PER_CSG, panthor_fw_init_ifaces() will only initialize that smaller number of stream interfaces.

If unprivileged userspace requests more queues than the firmware reported (but less than MAX_CS_PER_CSG), could cs_slot_prog_locked() fetch an uninitialized stream interface and crash when dereferencing cs_iface->input->ringbuf_base?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v3 1/1
#8 lore raw

Re: [PATCH v3] drm/panthor: use local variables for firmware interface counts

In reply to Osama Abdelkader
Quoted 11 lines · Osama Abdelkader

The firmware exposes the global group count and per-group stream count in the shared control interface. These values are validated before being used as loop bounds.

Read each count once with READ_ONCE() and store it in a local variable. This avoids reading the same control section field twice and makes it explicit that the loop uses the same value that passed validation to protect against self-modifying control sections.

Best regards,
Liviu

Quoted 77 lines

---
v3:
- Add comments explaining the READ_ONCE() usage.

 drivers/gpu/drm/panthor/panthor_fw.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +18 -6
index 8411c468bdac..b2ccb81f280c 100644
@@ -954,6 +954,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
 	u64 iface_offset = CSF_GROUP_CONTROL_OFFSET +
 			   ((u64)csg_idx * glb_iface->control->group_stride);
+	u32 stream_num;
 	unsigned int i;
 
 	if (iface_offset > shared_section_sz ||
@@ -967,8 +968,13 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 	csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va,
 						 sizeof(*csg_iface->output));
 
-	if (csg_iface->control->stream_num < MIN_CS_PER_CSG ||
-	    csg_iface->control->stream_num > MAX_CS_PER_CSG)
+	/*
+	 * To protect against self-modifying control sections
+	 * take a single snapshot from the control section so validation and
+	 * iteration use the same value.
+	 */
+	stream_num = READ_ONCE(csg_iface->control->stream_num);
+	if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG)
 		return -EINVAL;
 
 	if (!csg_iface->input || !csg_iface->output) {
@@ -986,7 +992,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,
 		}
 	}
 
-	for (i = 0; i < csg_iface->control->stream_num; i++) {
+	for (i = 0; i < stream_num; i++) {
 		int ret = panthor_init_cs_iface(ptdev, csg_idx, i);
 
 		if (ret)
@@ -1010,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 {
 	struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global;
 	u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem);
+	u32 group_num;
 	unsigned int i;
 
 	if (!ptdev->fw->shared_section->mem->kmap)
@@ -1035,13 +1042,18 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev)
 		return -EINVAL;
 	}
 
-	if (glb_iface->control->group_num > MAX_CSGS ||
-	    glb_iface->control->group_num < MIN_CSGS) {
+	/*
+	 * To protect against self-modifying control sections
+	 * take a single snapshot from the control section so validation and
+	 * iteration use the same value.
+	 */
+	group_num = READ_ONCE(glb_iface->control->group_num);
+	if (group_num > MAX_CSGS || group_num < MIN_CSGS) {
 		drm_err(&ptdev->base, "Invalid number of control groups");
 		return -EINVAL;
 	}
 
-	for (i = 0; i < glb_iface->control->group_num; i++) {
+	for (i = 0; i < group_num; i++) {
 		int ret = panthor_init_csg_iface(ptdev, i);
 
 		if (ret)
Signature · 1 line
2.43.0
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯