public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions
@ 2025-11-11 15:28 Lucas De Marchi
  0 siblings, 0 replies; 4+ messages in thread
From: Lucas De Marchi @ 2025-11-11 15:28 UTC (permalink / raw)
  To: intel-xe, linux-mtd, linux-kernel
  Cc: Lucas De Marchi, Alexander Usyskin, Jani Partanen

The regions array is counted by nregions, but it's set only after
accessing it:

	[] UBSAN: array-index-out-of-bounds in drivers/mtd/devices/mtd_intel_dg.c:750:15
	[] index 0 is out of range for type '<unknown> [*]'

Fix it by also fixing an undesired behavior: the loop silently ignores
ENOMEM and continues setting the other entries.

Cc: Alexander Usyskin <alexander.usyskin@intel.com>
Reported-by: Jani Partanen <jiipee@sotapeli.fi>
Closes: https://lore.kernel.org/all/caca6c67-4f1d-49f1-948f-e63b6b937b29@sotapeli.fi
Fixes: ceb5ab3cb646 ("mtd: add driver for intel graphics non-volatile memory device")
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
---
 drivers/mtd/devices/mtd_intel_dg.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/devices/mtd_intel_dg.c b/drivers/mtd/devices/mtd_intel_dg.c
index b438ee5aacc34..114e69135b8d9 100644
--- a/drivers/mtd/devices/mtd_intel_dg.c
+++ b/drivers/mtd/devices/mtd_intel_dg.c
@@ -738,6 +738,7 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
 
 	kref_init(&nvm->refcnt);
 	mutex_init(&nvm->lock);
+	nvm->nregions = nregions;
 
 	for (n = 0, i = 0; i < INTEL_DG_NVM_REGIONS; i++) {
 		if (!invm->regions[i].name)
@@ -745,13 +746,15 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
 
 		char *name = kasprintf(GFP_KERNEL, "%s.%s",
 				       dev_name(&aux_dev->dev), invm->regions[i].name);
-		if (!name)
-			continue;
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+
 		nvm->regions[n].name = name;
 		nvm->regions[n].id = i;
 		n++;
 	}
-	nvm->nregions = n; /* in case where kasprintf fail */
 
 	nvm->base = devm_ioremap_resource(device, &invm->bar);
 	if (IS_ERR(nvm->base)) {




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

* [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions
@ 2026-01-15  5:22 Alexander Usyskin
  2026-01-15  5:58 ` Raag Jadav
  2026-01-19 10:49 ` Miquel Raynal
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander Usyskin @ 2026-01-15  5:22 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd,
	linux-kernel
  Cc: Reuven Abliyev, Alexander Usyskin, Gustavo A . R . Silva,
	Raag Jadav, Jani Partanen, Lucas De Marchi

The regions array is counted by nregions, but it's set only after
accessing it:

[] UBSAN: array-index-out-of-bounds in drivers/mtd/devices/mtd_intel_dg.c:750:15
[] index 0 is out of range for type '<unknown> [*]'

Fix it by also fixing an undesired behavior: the loop silently ignores
ENOMEM and continues setting the other entries.

CC: Gustavo A. R. Silva <gustavoars@kernel.org>
CC: Raag Jadav <raag.jadav@intel.com>
Reported-by: Jani Partanen <jiipee@sotapeli.fi>
Closes: https://lore.kernel.org/all/caca6c67-4f1d-49f1-948f-e63b6b937b29@sotapeli.fi
Fixes: ceb5ab3cb646 ("mtd: add driver for intel graphics non-volatile memory device")
Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>
---
 drivers/mtd/devices/mtd_intel_dg.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/devices/mtd_intel_dg.c b/drivers/mtd/devices/mtd_intel_dg.c
index 2bab30dcd35f..7f751c48a76d 100644
--- a/drivers/mtd/devices/mtd_intel_dg.c
+++ b/drivers/mtd/devices/mtd_intel_dg.c
@@ -770,6 +770,7 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
 
 	kref_init(&nvm->refcnt);
 	mutex_init(&nvm->lock);
+	nvm->nregions = nregions;
 
 	for (n = 0, i = 0; i < INTEL_DG_NVM_REGIONS; i++) {
 		if (!invm->regions[i].name)
@@ -777,13 +778,15 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
 
 		char *name = kasprintf(GFP_KERNEL, "%s.%s",
 				       dev_name(&aux_dev->dev), invm->regions[i].name);
-		if (!name)
-			continue;
+		if (!name) {
+			ret = -ENOMEM;
+			goto err;
+		}
+
 		nvm->regions[n].name = name;
 		nvm->regions[n].id = i;
 		n++;
 	}
-	nvm->nregions = n; /* in case where kasprintf fail */
 
 	ret = devm_pm_runtime_enable(device);
 	if (ret < 0) {
-- 
2.43.0


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

* Re: [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions
  2026-01-15  5:22 [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions Alexander Usyskin
@ 2026-01-15  5:58 ` Raag Jadav
  2026-01-19 10:49 ` Miquel Raynal
  1 sibling, 0 replies; 4+ messages in thread
From: Raag Jadav @ 2026-01-15  5:58 UTC (permalink / raw)
  To: Alexander Usyskin
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd,
	linux-kernel, Reuven Abliyev, Gustavo A . R . Silva,
	Jani Partanen, Lucas De Marchi

On Thu, Jan 15, 2026 at 07:22:37AM +0200, Alexander Usyskin wrote:
> The regions array is counted by nregions, but it's set only after
> accessing it:
> 
> [] UBSAN: array-index-out-of-bounds in drivers/mtd/devices/mtd_intel_dg.c:750:15
> [] index 0 is out of range for type '<unknown> [*]'
> 
> Fix it by also fixing an undesired behavior: the loop silently ignores
> ENOMEM and continues setting the other entries.
> 
> CC: Gustavo A. R. Silva <gustavoars@kernel.org>
> CC: Raag Jadav <raag.jadav@intel.com>
> Reported-by: Jani Partanen <jiipee@sotapeli.fi>
> Closes: https://lore.kernel.org/all/caca6c67-4f1d-49f1-948f-e63b6b937b29@sotapeli.fi
> Fixes: ceb5ab3cb646 ("mtd: add driver for intel graphics non-volatile memory device")
> Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
> Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>

Reviewed-by: Raag Jadav <raag.jadav@intel.com>

> ---
>  drivers/mtd/devices/mtd_intel_dg.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mtd/devices/mtd_intel_dg.c b/drivers/mtd/devices/mtd_intel_dg.c
> index 2bab30dcd35f..7f751c48a76d 100644
> --- a/drivers/mtd/devices/mtd_intel_dg.c
> +++ b/drivers/mtd/devices/mtd_intel_dg.c
> @@ -770,6 +770,7 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
>  
>  	kref_init(&nvm->refcnt);
>  	mutex_init(&nvm->lock);
> +	nvm->nregions = nregions;
>  
>  	for (n = 0, i = 0; i < INTEL_DG_NVM_REGIONS; i++) {
>  		if (!invm->regions[i].name)
> @@ -777,13 +778,15 @@ static int intel_dg_mtd_probe(struct auxiliary_device *aux_dev,
>  
>  		char *name = kasprintf(GFP_KERNEL, "%s.%s",
>  				       dev_name(&aux_dev->dev), invm->regions[i].name);
> -		if (!name)
> -			continue;
> +		if (!name) {
> +			ret = -ENOMEM;
> +			goto err;
> +		}
> +
>  		nvm->regions[n].name = name;
>  		nvm->regions[n].id = i;
>  		n++;
>  	}
> -	nvm->nregions = n; /* in case where kasprintf fail */
>  
>  	ret = devm_pm_runtime_enable(device);
>  	if (ret < 0) {
> -- 
> 2.43.0
> 

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

* Re: [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions
  2026-01-15  5:22 [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions Alexander Usyskin
  2026-01-15  5:58 ` Raag Jadav
@ 2026-01-19 10:49 ` Miquel Raynal
  1 sibling, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-01-19 10:49 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel,
	Alexander Usyskin
  Cc: Reuven Abliyev, Gustavo A . R . Silva, Raag Jadav, Jani Partanen,
	Lucas De Marchi

On Thu, 15 Jan 2026 07:22:37 +0200, Alexander Usyskin wrote:
> The regions array is counted by nregions, but it's set only after
> accessing it:
> 
> [] UBSAN: array-index-out-of-bounds in drivers/mtd/devices/mtd_intel_dg.c:750:15
> [] index 0 is out of range for type '<unknown> [*]'
> 
> Fix it by also fixing an undesired behavior: the loop silently ignores
> ENOMEM and continues setting the other entries.
> 
> [...]

Applied to mtd/next, thanks!

[1/1] mtd: intel-dg: Fix accessing regions before setting nregions
      commit: 779c59274d03cc5c07237a2c845dfb71cff77705

Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl


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

end of thread, other threads:[~2026-01-19 10:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-15  5:22 [PATCH] mtd: intel-dg: Fix accessing regions before setting nregions Alexander Usyskin
2026-01-15  5:58 ` Raag Jadav
2026-01-19 10:49 ` Miquel Raynal
  -- strict thread matches above, loose matches on Subject: below --
2025-11-11 15:28 Lucas De Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox