Skip to contentSkip to conversation

media: mediatek: vcodec: Consider vdecsys presence in reg range check

PATCH Superseded Nícolas F. R. A. Prado 3 participants 5 messages Jul 25, 2023 – Aug 16, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: mediatek: vcodec: Consider vdecsys presence in reg range check

Commit fe8a33978383 ("media: mediatek: vcodec: Read HW active status from syscon") allowed the driver to read the VDEC_SYS io space from a syscon instead of from the reg property when reg-names are supplied. However as part of that change, a smatch warning was introduced:

drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c:142 mtk_vcodec_get_reg_bases() error: buffer overflow 'mtk_dec_reg_names' 11 <= 11

With a correct Devicetree, that is, one that follows the dt-binding, it wouldn't be possible to trigger such a buffer overflow. Even so, update the range validation of the reg property, so that the smatch warning is fixed and if an incorrect Devicetree is ever supplied the code errors out instead of causing memory corruption.

Fixes fe8a33978383 ("media: mediatek: vcodec: Read HW active status from syscon")

---

 drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c +2 -1
index 742b6903d030..cd62b3f68072 100644
@@ -124,7 +124,8 @@ static int mtk_vcodec_get_reg_bases(struct mtk_vcodec_dev *dev)
 	/* Sizeof(u32) * 4 bytes for each register base. */
 	reg_num = of_property_count_elems_of_size(pdev->dev.of_node, "reg",
 						  sizeof(u32) * 4);
-	if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE) {
+	if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE ||
+	    (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
 		dev_err(&pdev->dev, "Invalid register property size: %d\n", reg_num);
 		return -EINVAL;
 	}
Signature · 1 line
2.41.0
maintainer Reviewed-by on v1 1/1
#1 lore raw

Il 25/07/23 22:40, Nícolas F. R. A. Prado ha scritto:

Quoted 28 lines

Commit fe8a33978383 ("media: mediatek: vcodec: Read HW active status from syscon") allowed the driver to read the VDEC_SYS io space from a syscon instead of from the reg property when reg-names are supplied. However as part of that change, a smatch warning was introduced:

drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c:142 mtk_vcodec_get_reg_bases() error: buffer overflow 'mtk_dec_reg_names' 11 <= 11

With a correct Devicetree, that is, one that follows the dt-binding, it wouldn't be possible to trigger such a buffer overflow. Even so, update the range validation of the reg property, so that the smatch warning is fixed and if an incorrect Devicetree is ever supplied the code errors out instead of causing memory corruption.

Fixes fe8a33978383 ("media: mediatek: vcodec: Read HW active status from syscon")

---

  drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c +0 -0
index 742b6903d030..cd62b3f68072 100644

drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c static int mtk_vcodec_get_reg_bases(struct mtk_vcodec_dev *dev) · line 127

Quoted 5 lines
drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c +1 -1
@@ -124,7 +124,8 @@ static int mtk_vcodec_get_reg_bases(struct mtk_vcodec_dev *dev)
  	/* Sizeof(u32) * 4 bytes for each register base. */
  	reg_num = of_property_count_elems_of_size(pdev->dev.of_node, "reg",
  						  sizeof(u32) * 4);
-	if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE) {
+	if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE ||

You could also simplify this like

int num_max_vdec_regs;

....

num_max_vdec_regs = no_vdecsys_reg ?

		    ARRAY_SIZE(mtk_dec_reg_names) : NUM_MAX_VDEC_REG_BASE;

if (reg_num <= 0 || reg_num > num_max_vdec_regs) ....

I'd go for the proposed solution, as it looks better in my eyes, but it's ultimately your choice and probably just a personal preference.

That said, if you want to keep this commit as it is, you still get my

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c +1 -0
+	    (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
  		dev_err(&pdev->dev, "Invalid register property size: %d\n", reg_num);
  		return -EINVAL;
  	}
#2 lore raw
In reply to Nícolas F. R. A. Prado
kernel test robot noticed the following build errors:

Hi Nícolas,

kernel test robot noticed the following build errors:

[auto build test ERROR on media-tree/master]
[also build test ERROR on linus/master v6.5-rc3 next-20230726] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/N-colas-F-R-A-Prado/media-mediatek-vcodec-Consider-vdecsys-presence-in-reg-range-check/20230726-044320
base:   git://linuxtv.org/media_tree.git master
patch link:    https://lore.kernel.org/r/20230725204043.569799-1-nfraprado%40collabora.com
patch subject: [PATCH] media: mediatek: vcodec: Consider vdecsys presence in reg range check
config: arm-randconfig-r046-20230726 (https://download.01.org/0day-ci/archive/20230726/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230726/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c: In function 'mtk_vcodec_get_reg_bases':

>> drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c:90:15: error: 'has_vdecsys_reg' undeclared (first use in this function)
      90 |             (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
         |               ^~~~~~~~~~~~~~~

drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c:90:15: note: each undeclared identifier is reported only once for each function it appears in

vim +/has_vdecsys_reg +90 drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c

    80	
    81	static int mtk_vcodec_get_reg_bases(struct mtk_vcodec_dev *dev)
    82	{
    83		struct platform_device *pdev = dev->plat_dev;
    84		int reg_num, i;
    85	
    86		/* Sizeof(u32) * 4 bytes for each register base. */
    87		reg_num = of_property_count_elems_of_size(pdev->dev.of_node, "reg",
    88							  sizeof(u32) * 4);
    89		if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE ||
  > 90		    (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
    91			dev_err(&pdev->dev, "Invalid register property size: %d\n", reg_num);
    92			return -EINVAL;
    93		}
    94	
    95		for (i = 0; i < reg_num; i++) {
    96			dev->reg_base[i] = devm_platform_ioremap_resource(pdev, i);
    97			if (IS_ERR(dev->reg_base[i]))
    98				return PTR_ERR(dev->reg_base[i]);
    99	

100 mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
101 }
102
103 return 0;
104 }
105

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
Quoted 9 lines · kernel test robot

Hi Nícolas,

kernel test robot noticed the following build errors:

[auto build test ERROR on media-tree/master]
[also build test ERROR on linus/master v6.5-rc3 next-20230726] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

This patch should be applied to Hans' tree
(https://git.linuxtv.org/hverkuil/media_tree.git/log/?h=for-v6.6i), since the commit I'm fixing has only made its way there yet.

As a suggestion for improvement, consider checking whether the commit(s) listed in the Fixes: tag(s) of the patches exist in the tree you're applying them to.

Anyway, I've sent a v2 now that has enough context that shouldn't even apply to the main media tree.

Thanks,
Nícolas

#4 lore raw
In reply to Nícolas F. R. A. Prado
kernel test robot noticed the following build errors:

Hi Nícolas,

kernel test robot noticed the following build errors:

[auto build test ERROR on v6.5-rc6]
[also build test ERROR on linus/master]
[cannot apply to media-tree/master next-20230816]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/N-colas-F-R-A-Prado/media-mediatek-vcodec-Consider-vdecsys-presence-in-reg-range-check/20230816-145454
base:   v6.5-rc6
patch link:    https://lore.kernel.org/r/20230725204043.569799-1-nfraprado%40collabora.com
patch subject: [PATCH] media: mediatek: vcodec: Consider vdecsys presence in reg range check
config: arm64-randconfig-r014-20230816 (https://download.01.org/0day-ci/archive/20230816/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce: (https://download.01.org/0day-ci/archive/20230816/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c:90:8: error: use of undeclared identifier 'has_vdecsys_reg'
      90 |             (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
         |               ^

1 error generated.

vim +/has_vdecsys_reg +90 drivers/media/platform/mediatek/vcodec/mtk_vcodec_dec_drv.c

    80	
    81	static int mtk_vcodec_get_reg_bases(struct mtk_vcodec_dev *dev)
    82	{
    83		struct platform_device *pdev = dev->plat_dev;
    84		int reg_num, i;
    85	
    86		/* Sizeof(u32) * 4 bytes for each register base. */
    87		reg_num = of_property_count_elems_of_size(pdev->dev.of_node, "reg",
    88							  sizeof(u32) * 4);
    89		if (reg_num <= 0 || reg_num > NUM_MAX_VDEC_REG_BASE ||
  > 90		    (!has_vdecsys_reg && reg_num > NUM_MAX_VDEC_REG_BASE - 1)) {
    91			dev_err(&pdev->dev, "Invalid register property size: %d\n", reg_num);
    92			return -EINVAL;
    93		}
    94	
    95		for (i = 0; i < reg_num; i++) {
    96			dev->reg_base[i] = devm_platform_ioremap_resource(pdev, i);
    97			if (IS_ERR(dev->reg_base[i]))
    98				return PTR_ERR(dev->reg_base[i]);
    99	

100 mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
101 }
102
103 return 0;
104 }
105

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki