All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] media: venus: Annotate flex arrays with __counted_by()
@ 2026-06-07 11:19 Mohammed EL Kadiri
  2026-06-07 11:35 ` sashiko-bot
  2026-06-08  8:01 ` Konrad Dybcio
  0 siblings, 2 replies; 3+ messages in thread
From: Mohammed EL Kadiri @ 2026-06-07 11:19 UTC (permalink / raw)
  To: quic_vgarodia, quic_dikshita, bryan.odonoghue, mchehab
  Cc: kees, gustavoars, dmitry.baryshkov, linux-media, linux-arm-msm,
	linux-hardening, linux-kernel, Mohammed EL Kadiri

Add __counted_by() annotations to flexible array members:

- hfi_capabilities::data, counted by num_capabilities
- hfi_profile_level_supported::profile_level, counted by profile_count
- hfi_resource_ocmem_requirement_info::requirements, counted by num_entries

This improves run-time bounds checking via CONFIG_UBSAN_BOUNDS and
compile-time object size resolution via __builtin_dynamic_object_size().

Assisted-by: Claude:claude-opus-4
Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Signed-off-by: Mohammed EL Kadiri <med08elkadiri@gmail.com>
---
 drivers/media/platform/qcom/venus/hfi_helper.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/qcom/venus/hfi_helper.h b/drivers/media/platform/qcom/venus/hfi_helper.h
index f44059f19505..e96d458c52fa 100644
--- a/drivers/media/platform/qcom/venus/hfi_helper.h
+++ b/drivers/media/platform/qcom/venus/hfi_helper.h
@@ -616,7 +616,7 @@ struct hfi_capability {
 
 struct hfi_capabilities {
 	u32 num_capabilities;
-	struct hfi_capability data[];
+	struct hfi_capability data[] __counted_by(num_capabilities);
 };
 
 #define HFI_DEBUG_MSG_LOW	0x01
@@ -802,7 +802,7 @@ struct hfi_profile_level {
 
 struct hfi_profile_level_supported {
 	u32 profile_count;
-	struct hfi_profile_level profile_level[];
+	struct hfi_profile_level profile_level[] __counted_by(profile_count);
 };
 
 struct hfi_quality_vs_speed {
@@ -1085,7 +1085,7 @@ struct hfi_resource_ocmem_requirement {
 
 struct hfi_resource_ocmem_requirement_info {
 	u32 num_entries;
-	struct hfi_resource_ocmem_requirement requirements[];
+	struct hfi_resource_ocmem_requirement requirements[] __counted_by(num_entries);
 };
 
 struct hfi_property_sys_image_version_info_type {
-- 
2.43.0


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

* Re: [PATCH v2] media: venus: Annotate flex arrays with __counted_by()
  2026-06-07 11:19 [PATCH v2] media: venus: Annotate flex arrays with __counted_by() Mohammed EL Kadiri
@ 2026-06-07 11:35 ` sashiko-bot
  2026-06-08  8:01 ` Konrad Dybcio
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-06-07 11:35 UTC (permalink / raw)
  To: Mohammed EL Kadiri; +Cc: linux-media

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

New issues:
- [High] Applying `__counted_by` to structs that directly overlay unvalidated, untrusted firmware messages defeats compiler-based bounds checking.

Pre-existing issues:
- [High] The Venus HFI parser does not validate that the remaining buffer bytes are sufficient to hold the variable-length flexible array payloads indicated by the firmware, leading to severe out-of-bounds reads during memcpy().
- [High] Several parsing functions return an incorrect consumed payload size, causing the parser loop to desynchronize and misinterpret flexible array data as new property headers.
- [High] `parse_raw_formats()` calculates the total payload size incorrectly by assuming all elements have the same number of planes as the final element.
--

commit 475f2f7188ec43f866da689f1e919b2cf914c64a
Author: Mohammed EL Kadiri <med08elkadiri@gmail.com>

media: venus: Annotate flex arrays with __counted_by()

Add __counted_by() annotations to flexible array members in Venus HFI helper
structures. This aims to improve run-time bounds checking and compile-time
object size resolution.

> diff --git a/drivers/media/platform/qcom/venus/hfi_helper.h b/drivers/media/platform/qcom/venus/hfi_helper.h
> index f44059f19505a..e96d458c52fa2 100644
> --- a/drivers/media/platform/qcom/venus/hfi_helper.h
> +++ b/drivers/media/platform/qcom/venus/hfi_helper.h
> @@ -616,7 +616,7 @@ struct hfi_capability {
>  
>  struct hfi_capabilities {
>  	u32 num_capabilities;
> -	struct hfi_capability data[];
> +	struct hfi_capability data[] __counted_by(num_capabilities);
>  };

[Severity: High]
Does applying __counted_by() to the data array here defeat compiler-based
bounds checking?

Since num_capabilities is read directly from untrusted firmware packets without
first being validated against the actual packet allocation size (rem_bytes),
applying __counted_by() could cause the compiler to wrongly trust the
attacker-controlled count.

This isn't a bug introduced by this patch, but the Venus HFI parser does not
validate that the remaining buffer bytes are sufficient to hold the
variable-length flexible array payloads:

drivers/media/platform/qcom/venus/hfi_parser.c:hfi_parser() {
	...
		case HFI_PROPERTY_PARAM_CAPABILITY_SUPPORTED:
			if (rem_bytes <= sizeof(struct hfi_capabilities))
				return HFI_ERR_SYS_INSUFFICIENT_RESOURCES;

			ret = parse_caps(core, codecs, domain, payload);
	...
}

drivers/media/platform/qcom/venus/hfi_parser.c:parse_caps() {
	...
	struct hfi_capabilities *caps = data;
	struct hfi_capability *cap = caps->data;
	u32 num_caps = caps->num_capabilities;
	struct hfi_capability caps_arr[MAX_CAP_ENTRIES] = {};

	if (num_caps > MAX_CAP_ENTRIES)
		return -EINVAL;

	memcpy(caps_arr, cap, num_caps * sizeof(*cap));
	...
}

This is a pre-existing issue, but parse_caps() returns the static header size
instead of the dynamic array size:

drivers/media/platform/qcom/venus/hfi_parser.c:parse_caps() {
	...
	return sizeof(*caps);
}

This causes the parser loop in hfi_parser() to incorrectly advance into the
middle of the flexible array data payload, leading to parsing desynchronization:

drivers/media/platform/qcom/venus/hfi_parser.c:hfi_parser() {
	...
		words += ret / sizeof(u32);
		rem_bytes -= ret;
	}
	...
}

[ ... ]

> @@ -802,7 +802,7 @@ struct hfi_profile_level {
>  
>  struct hfi_profile_level_supported {
>  	u32 profile_count;
> -	struct hfi_profile_level profile_level[];
> +	struct hfi_profile_level profile_level[] __counted_by(profile_count);
>  };

[Severity: High]
Does applying __counted_by() to the profile_level array here also defeat
compiler bounds checking for the same reasons? profile_count is read from
untrusted firmware without validation against rem_bytes.

This is a pre-existing issue, but parse_profile_level() performs an
out-of-bounds memcpy() by blindly trusting the unvalidated profile_count
field against the unchecked remainder:

drivers/media/platform/qcom/venus/hfi_parser.c:parse_profile_level() {
	struct hfi_profile_level_supported *pl = data;
	struct hfi_profile_level *proflevel = pl->profile_level;
	struct hfi_profile_level pl_arr[HFI_MAX_PROFILE_COUNT] = {};

	if (pl->profile_count > HFI_MAX_PROFILE_COUNT)
		return -EINVAL;

	memcpy(pl_arr, proflevel, pl->profile_count * sizeof(*proflevel));
	...
}

[ ... ]

> @@ -1085,7 +1085,7 @@ struct hfi_resource_ocmem_requirement {
>  
>  struct hfi_resource_ocmem_requirement_info {
>  	u32 num_entries;
> -	struct hfi_resource_ocmem_requirement requirements[];
> +	struct hfi_resource_ocmem_requirement requirements[] __counted_by(num_entries);
>  };

[Severity: High]
Can applying __counted_by() to the requirements array here lead to similar
bounds checking bypasses if num_entries is untrusted?

This isn't a bug introduced by this patch, but similar payload size
miscalculations occur in other parsing functions, such as parse_raw_formats().
The total payload size is calculated incorrectly by assuming all elements have
the same number of planes as the final element:

drivers/media/platform/qcom/venus/hfi_parser.c:parse_raw_formats() {
	...
	while (entries) {
		num_planes = pinfo->num_planes;
		...
		pinfo = (void *)pinfo + sizeof(*constr) * num_planes +
			2 * sizeof(u32);
		entries--;
	}
	...
	size = fmt->format_entries * (sizeof(*constr) * num_planes + 2 * sizeof(u32))
		+ 2 * sizeof(u32);

	return size;
}

Can this cause hfi_parser() to either advance its pointer too far or too little
if the firmware provides entries with varying plane counts?

Also a pre-existing issue, parse_alloc_mode() returns the static header size
instead of the dynamic array size:

drivers/media/platform/qcom/venus/hfi_parser.c:parse_alloc_mode() {
	struct hfi_buffer_alloc_mode_supported *mode = data;
	...
	return sizeof(*mode);
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260607111933.6398-1-med08elkadiri@gmail.com?part=1

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

* Re: [PATCH v2] media: venus: Annotate flex arrays with __counted_by()
  2026-06-07 11:19 [PATCH v2] media: venus: Annotate flex arrays with __counted_by() Mohammed EL Kadiri
  2026-06-07 11:35 ` sashiko-bot
@ 2026-06-08  8:01 ` Konrad Dybcio
  1 sibling, 0 replies; 3+ messages in thread
From: Konrad Dybcio @ 2026-06-08  8:01 UTC (permalink / raw)
  To: Mohammed EL Kadiri, quic_vgarodia, quic_dikshita, bryan.odonoghue,
	mchehab
  Cc: kees, gustavoars, dmitry.baryshkov, linux-media, linux-arm-msm,
	linux-hardening, linux-kernel

On 6/7/26 1:19 PM, Mohammed EL Kadiri wrote:
> Add __counted_by() annotations to flexible array members:
> 
> - hfi_capabilities::data, counted by num_capabilities
> - hfi_profile_level_supported::profile_level, counted by profile_count
> - hfi_resource_ocmem_requirement_info::requirements, counted by num_entries
> 
> This improves run-time bounds checking via CONFIG_UBSAN_BOUNDS and
> compile-time object size resolution via __builtin_dynamic_object_size().
> 
> Assisted-by: Claude:claude-opus-4
> Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
> Signed-off-by: Mohammed EL Kadiri <med08elkadiri@gmail.com>
> ---

Reviewed-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>

Konrad

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

end of thread, other threads:[~2026-06-08  8:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-07 11:19 [PATCH v2] media: venus: Annotate flex arrays with __counted_by() Mohammed EL Kadiri
2026-06-07 11:35 ` sashiko-bot
2026-06-08  8:01 ` Konrad Dybcio

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.