Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Krzysztof Karas" <krzysztof.karas@intel.com>,
	intel-gfx@lists.freedesktop.org
Subject: Re: [PATCH 2/2] drm/i915/query: Use __counted_by() to annotate flexible arrays
Date: Thu, 19 Nov 2026 14:42:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-223310@kernel.org> (raw)
In-Reply-To: <20261119143510.2971237-3-krzysztof.karas@intel.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/i915/query: out-of-bounds regions[] access in
  query_memregion_info()
--

commit b2640bf13b3424df44fa1f7fb0465d67b8308127
Author: Krzysztof Karas <krzysztof.karas@intel.com>

drm/i915/query: Use __counted_by() to annotate flexible arrays

This commit annotates flexible array members in struct
drm_i915_query_engine_info and struct drm_i915_query_memory_regions with
__counted_by(), and allocates temporary kernel buffers in
query_engine_info() and query_memregion_info() before copying the results
to user space.

> diff --git a/drivers/gpu/drm/i915/i915_query.c b/drivers/gpu/drm/i915/i915_query.c
> index 11157fb14db31..46fee1591e489 100644
> --- a/drivers/gpu/drm/i915/i915_query.c
> +++ b/drivers/gpu/drm/i915/i915_query.c

[ ... ]

> @@ -466,70 +467,74 @@ static int query_memregion_info(struct drm_i915_private *i915,
>  {
>  	struct drm_i915_query_memory_regions __user *query_ptr =
>  		u64_to_user_ptr(query_item->data_ptr);
> -	struct drm_i915_memory_region_info __user *info_ptr =
> -		&query_ptr->regions[0];
> -	struct drm_i915_memory_region_info info = { };
> -	struct drm_i915_query_memory_regions query;
> +	struct drm_i915_query_memory_regions *query;
> +	struct drm_i915_query_memory_regions hdr;
>  	struct intel_memory_region *mr;
> +	unsigned int num_regions = 0;
> +	int ret, id, i, n = 0;
>  	u32 total_length;
> -	int ret, id, i;
>  
>  	if (query_item->flags != 0)
>  		return -EINVAL;
>  
> -	total_length = sizeof(query);
>  	for_each_memory_region(mr, i915, id) {
>  		if (mr->private)
>  			continue;
>  
> -		total_length += sizeof(info);
> +		num_regions++;
>  	}
> +	total_length = struct_size(query, regions, num_regions);
>  
> -	ret = copy_query_item(&query, sizeof(query), total_length, query_item);
> +	ret = copy_query_item(&hdr, sizeof(hdr), total_length, query_item);
>  	if (ret != 0)
>  		return ret;
>  
> -	if (query.num_regions)
> +	if (hdr.num_regions)
>  		return -EINVAL;
>  
> -	for (i = 0; i < ARRAY_SIZE(query.rsvd); i++) {
> -		if (query.rsvd[i])
> +	for (i = 0; i < ARRAY_SIZE(hdr.rsvd); i++) {
> +		if (hdr.rsvd[i])
>  			return -EINVAL;
>  	}
>  
> +	query = kzalloc_flex(*query, regions, num_regions);
> +	if (!query)
> +		return -ENOMEM;
> +
> +	query->num_regions = num_regions;
> +
>  	for_each_memory_region(mr, i915, id) {
> +		struct drm_i915_memory_region_info *info = &query->regions[n++];
> +
>  		if (mr->private)
>  			continue;

[Severity: High]
Can indexing query->regions[n++] before checking mr->private access past
the end of query->regions[]?

num_regions only counts non-private regions, so when the second loop hits
a private stolen memory region (set up with mem->private = true in
i915_gem_stolen_smem_setup() or i915_gem_stolen_lmem_setup()), n is
incremented on the skipped region and &query->regions[n++] indexes
query->regions[num_regions], triggering a CONFIG_UBSAN_BOUNDS splat (or
writing past the end of the kzalloc_flex() buffer if a non-private region
follows a private one).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119143510.2971237-1-krzysztof.karas@intel.com?part=2

  reply	other threads:[~2026-11-19 14:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:35 [PATCH 0/2] drm/i915: Annotate flexible array members with __counted_by() attribute Krzysztof Karas
2026-11-19 14:35 ` [PATCH 1/2] drm/i915: Annotate flexible array members that require minimal changes Krzysztof Karas
2026-11-19 14:44   ` Ville Syrjälä
2026-11-19 14:35 ` [PATCH 2/2] drm/i915/query: Use __counted_by() to annotate flexible arrays Krzysztof Karas
2026-11-19 14:42   ` sashiko-bot [this message]
2026-11-19 16:50 ` ✓ i915.CI.BAT: success for drm/i915: Annotate flexible array members with __counted_by() attribute Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-223310@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=krzysztof.karas@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox