Skip to contentSkip to conversation

media: iris: add missing packet size checks

PATCH New Hungyu Lin 1 participant 4 messages Nov 26

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

v2 cover media: iris: add missing packet size checks

venus got two fixes in May 2025 for packets that the firmware sends through the shared queues:

49befc830daa ("media: venus: Add a check for packet size after reading from shared memory") 06d6770ff0d8 ("media: venus: Fix OOB read due to missing payload bound check")

The iris gen1 code has the same logic and has neither of them.

Patch 1 rejects a packet size that is smaller than the size field itself. Patch 2 makes sure that the size field of a packet did not change while the packet was copied out of the shared queue. Patch 3 uses that size to bound the walk over the properties of a sequence change event.

Changes in v2:
- Add patch 1. A packet size of 1 to 3 was accepted, so the check in patch 2 could compare bytes that were not copied (Konrad)
- Link to v1: https://lore.kernel.org/r/[email protected]

Hungyu Lin (3):
media: iris: reject packets that are smaller than the size field
media: iris: check the packet size after reading from shared memory
media: iris: fix out-of-bounds read in gen1 sequence change event

 .../qcom/iris/iris_hfi_gen1_response.c        | 73 ++++++++++++++-----
 .../media/platform/qcom/iris/iris_hfi_queue.c |  4 +-
 2 files changed, 57 insertions(+), 20 deletions(-)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 media: iris: reject packets that are smaller than the size field

iris_hfi_queue_read() only rejects a packet size of zero. For a size of 1 to 3 it copies less than the size field, returns success and leaves the read index where it was, because the index counts 32-bit words. The gen1 response handler drops such a packet as too small and reads the queue again, so it gets the same entry again and again and the interrupt thread does not return.

venus_read_queue() converts the size to words before it checks it for zero. Reject a size that is smaller than one word here as well.

Fixes fb583a214337 ("media: iris: introduce host firmware interface with necessary hooks")
---
 drivers/media/platform/qcom/iris/iris_hfi_queue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/iris/iris_hfi_queue.c +1 -1
index bf6db23b53e2..12a7fd0982c5 100644
@@ -81,7 +81,7 @@ static int iris_hfi_queue_read(struct iris_iface_q_info *qinfo, void *packet)
 		return -ENODATA;
 
 	packet_size = *read_ptr;
-	if (!packet_size)
+	if (packet_size < sizeof(u32))
 		return -EINVAL;
 
 	new_read_idx = read_idx + packet_size;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/3 media: iris: check the packet size after reading from shared memory

In reply to Hungyu Lin

iris_hfi_queue_read() takes the size of a packet from the first word of the packet in the shared queue and then copies that many bytes, including that word, out of the queue. The firmware can change the word in between, so the size field of the copy can be larger than what was copied. The gen1 response handlers use that field as the length of the packet, for example to walk the buffer requirements in iris_hfi_gen1_session_property_info().

venus got the same check in commit 49befc830daa ("media: venus: Add a check for packet size after reading from shared memory"). Compare the size field of the copy with the size that was used for the copy and fail the read if they differ.

Fixes fb583a214337 ("media: iris: introduce host firmware interface with necessary hooks")
---
 drivers/media/platform/qcom/iris/iris_hfi_queue.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/platform/qcom/iris/iris_hfi_queue.c +2 -0
index 12a7fd0982c5..55711f332a50 100644
@@ -95,6 +95,8 @@ static int iris_hfi_queue_read(struct iris_iface_q_info *qinfo, void *packet)
 			       qinfo->kernel_vaddr, residue);
 			new_read_idx = residue;
 		}
+		if (*(u32 *)packet != packet_size)
+			return -EINVAL;
 	} else {
 		new_read_idx = write_idx;
 		ret = -EBADMSG;
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/3 media: iris: fix out-of-bounds read in gen1 sequence change event

In reply to Hungyu Lin

iris_hfi_gen1_read_changed_params() walks the properties of a HFI_EVENT_SESSION_SEQUENCE_CHANGED event. The number of properties comes from the firmware in event_data2 and the payload is never checked against the size of the packet. A count that is larger than what the packet holds makes the loop read past the end of the packet and, if it is large enough, past the end of the response buffer. The values that are read end up in the formats and in the crop rectangle that user space can query.

The same code in venus was fixed by commit 06d6770ff0d8 ("media: venus:
Fix OOB read due to missing payload bound check"). Do the same here: keep track of the bytes that are left in the packet and put the instance into the error state when a property does not fit.

Fixes 84e17adae3e3 ("media: iris: add support for dynamic resolution change")
---
 .../qcom/iris/iris_hfi_gen1_response.c        | 73 ++++++++++++++-----
 1 file changed, 54 insertions(+), 19 deletions(-)
drivers/media/platform/qcom/iris/iris_hfi_gen1_response.c +54 -19
index ffa98139f0eb..a5b43a408e55 100644
@@ -34,72 +34,102 @@ static void iris_hfi_gen1_read_changed_params(struct iris_inst *inst,
 	struct hfi_framesize *frame_sz;
 	struct vb2_queue *dst_q;
 	struct v4l2_ctrl *ctrl;
-	u32 full_range, ptype;
+	u32 full_range, ptype, rem_bytes;
+	u32 size_read = 0;
+
+	rem_bytes = pkt->shdr.hdr.size - sizeof(*pkt);
 
 	do {
+		if (rem_bytes < sizeof(u32))
+			goto error;
 		ptype = *((u32 *)data_ptr);
+
+		data_ptr += sizeof(u32);
+		rem_bytes -= sizeof(u32);
+
 		switch (ptype) {
 		case HFI_PROPERTY_PARAM_FRAME_SIZE:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_framesize))
+				goto error;
+
 			frame_sz = (struct hfi_framesize *)data_ptr;
 			event.width = frame_sz->width;
 			event.height = frame_sz->height;
-			data_ptr += sizeof(*frame_sz);
+			size_read = sizeof(struct hfi_framesize);
 			break;
 		case HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_profile_level))
+				goto error;
+
 			profile_level = (struct hfi_profile_level *)data_ptr;
 			event.profile = profile_level->profile;
 			event.level = profile_level->level;
-			data_ptr += sizeof(*profile_level);
+			size_read = sizeof(struct hfi_profile_level);
 			break;
 		case HFI_PROPERTY_PARAM_VDEC_PIXEL_BITDEPTH:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_bit_depth))
+				goto error;
+
 			pixel_depth = (struct hfi_bit_depth *)data_ptr;
 			event.bit_depth = pixel_depth->bit_depth;
-			data_ptr += sizeof(*pixel_depth);
+			size_read = sizeof(struct hfi_bit_depth);
 			break;
 		case HFI_PROPERTY_PARAM_VDEC_PIC_STRUCT:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_pic_struct))
+				goto error;
+
 			pic_struct = (struct hfi_pic_struct *)data_ptr;
 			event.pic_struct = pic_struct->progressive_only;
-			data_ptr += sizeof(*pic_struct);
+			size_read = sizeof(struct hfi_pic_struct);
 			break;
 		case HFI_PROPERTY_PARAM_VDEC_COLOUR_SPACE:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_colour_space))
+				goto error;
+
 			colour_info = (struct hfi_colour_space *)data_ptr;
 			event.colour_space = colour_info->colour_space;
-			data_ptr += sizeof(*colour_info);
+			size_read = sizeof(struct hfi_colour_space);
 			break;
 		case HFI_PROPERTY_CONFIG_VDEC_ENTROPY:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(u32))
+				goto error;
+
 			event.entropy_mode = *(u32 *)data_ptr;
-			data_ptr += sizeof(u32);
+			size_read = sizeof(u32);
 			break;
 		case HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_buffer_requirements))
+				goto error;
+
 			bufreq = (struct hfi_buffer_requirements *)data_ptr;
 			event.buf_count = bufreq->count_min;
-			data_ptr += sizeof(*bufreq);
+			size_read = sizeof(struct hfi_buffer_requirements);
 			break;
 		case HFI_INDEX_EXTRADATA_INPUT_CROP:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_extradata_input_crop))
+				goto error;
+
 			crop = (struct hfi_extradata_input_crop *)data_ptr;
 			event.input_crop.left = crop->left;
 			event.input_crop.top = crop->top;
 			event.input_crop.width = crop->width;
 			event.input_crop.height = crop->height;
-			data_ptr += sizeof(*crop);
+			size_read = sizeof(struct hfi_extradata_input_crop);
 			break;
 		case HFI_PROPERTY_PARAM_VDEC_DPB_COUNTS:
-			data_ptr += sizeof(u32);
+			if (rem_bytes < sizeof(struct hfi_dpb_counts))
+				goto error;
+
 			iris_vpu_dpb_count = (struct hfi_dpb_counts *)data_ptr;
 			event.buf_count = iris_vpu_dpb_count->fw_min_count;
-			data_ptr += sizeof(*iris_vpu_dpb_count);
+			size_read = sizeof(struct hfi_dpb_counts);
 			break;
 		default:
+			size_read = 0;
 			break;
 		}
+		data_ptr += size_read;
+		rem_bytes -= size_read;
 		num_properties_changed--;
 	} while (num_properties_changed > 0);
 
@@ -170,6 +200,11 @@ static void iris_hfi_gen1_read_changed_params(struct iris_inst *inst,
 			event.bit_depth, event.pic_struct);
 		iris_inst_change_state(inst, IRIS_INST_ERROR);
 	}
+
+	return;
+
+error:
+	iris_inst_change_state(inst, IRIS_INST_ERROR);
 }
 
 static void iris_hfi_gen1_event_seq_changed(struct iris_inst *inst,
Signature · 1 line
2.43.0