Skip to contentSkip to conversation

soc: imx: imx8m-blk-ctrl: off by one in imx8m_blk_ctrl_xlate()

PATCH Applied Dan Carpenter 3 participants 4 messages Oct 11, 2021 – Oct 15, 2021

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

PATCH 1/1 soc: imx: imx8m-blk-ctrl: off by one in imx8m_blk_ctrl_xlate()

The > comparison should be >= to prevent reading one element beyond the end of the array. The onecell_data->domains[] array is allocated in imx8m_blk_ctrl_probe() and it has "onecell_data->num_domains" elements.

Fixes 5b340e7813d4 ("soc: imx: add i.MX8M blk-ctrl driver")
---
 drivers/soc/imx/imx8m-blk-ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/imx/imx8m-blk-ctrl.c +1 -1
index e172d295c441..519b3651d1d9 100644
@@ -139,7 +139,7 @@ imx8m_blk_ctrl_xlate(struct of_phandle_args *args, void *data)
 	unsigned int index = args->args[0];
 
 	if (args->args_count != 1 ||
-	    index > onecell_data->num_domains)
+	    index >= onecell_data->num_domains)
 		return ERR_PTR(-EINVAL);
 
 	return onecell_data->domains[index];
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Dan,

Am Montag, dem 11.10.2021 um 15:36 +0300 schrieb Dan Carpenter:

Quoted 3 lines

The > comparison should be >= to prevent reading one element beyond the end of the array. The onecell_data->domains[] array is allocated in imx8m_blk_ctrl_probe() and it has "onecell_data->num_domains" elements.

Thanks for the patch! I guess this was found via smatch? I should really make it a habit to use smatch on my submissions...

Quoted 2 lines
Fixes 5b340e7813d4 ("soc: imx: add i.MX8M blk-ctrl driver")
Quoted 17 lines
---
 drivers/soc/imx/imx8m-blk-ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/imx/imx8m-blk-ctrl.c +1 -1
index e172d295c441..519b3651d1d9 100644
@@ -139,7 +139,7 @@ imx8m_blk_ctrl_xlate(struct of_phandle_args *args, void *data)
 	unsigned int index = args->args[0];
 
 	if (args->args_count != 1 ||
-	    index > onecell_data->num_domains)
+	    index >= onecell_data->num_domains)
 		return ERR_PTR(-EINVAL);
 
 	return onecell_data->domains[index];
#2 lore raw
Quoted 9 lines · Lucas Stach

Hi Dan,

Am Montag, dem 11.10.2021 um 15:36 +0300 schrieb Dan Carpenter:

Quoted 3 lines

The > comparison should be >= to prevent reading one element beyond the end of the array. The onecell_data->domains[] array is allocated in imx8m_blk_ctrl_probe() and it has "onecell_data->num_domains" elements.

Thanks for the patch! I guess this was found via smatch? I should really make it a habit to use smatch on my submissions...

Yeah, but not a from a published check. I have a private check for off by one errors that warns about any > vs >= comparisons that cannot be proved as correct.

regards,
dan carpenter

Applied
#3 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

The > comparison should be >= to prevent reading one element beyond the end of the array. The onecell_data->domains[] array is allocated in imx8m_blk_ctrl_probe() and it has "onecell_data->num_domains" elements.

Fixes 5b340e7813d4 ("soc: imx: add i.MX8M blk-ctrl driver")

Applied, thanks!