[PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()

Abdun Nihaal posted 1 patch 6 days, 6 hours ago
There is a newer version of this series
drivers/mfd/mc13xxx-core.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()
Posted by Abdun Nihaal 6 days, 6 hours ago
The memory allocated for cell.name using kmemdup() is not freed when
mfd_add_devices() fails. Fix that by checking return code and freeing.

Fixes: 8e00593557c3 ("mfd: Add mc13892 support to mc13xxx")
Signed-off-by: Abdun Nihaal <nihaal@cse.iitm.ac.in>
---
Compile tested only. Issue found using static analysis.

 drivers/mfd/mc13xxx-core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/mc13xxx-core.c b/drivers/mfd/mc13xxx-core.c
index 920797b806ce..1ebf849d80fe 100644
--- a/drivers/mfd/mc13xxx-core.c
+++ b/drivers/mfd/mc13xxx-core.c
@@ -365,6 +365,7 @@ EXPORT_SYMBOL_GPL(mc13xxx_adc_do_conversion);
 static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
 		const char *format, void *pdata, size_t pdata_size)
 {
+	int ret;
 	char buf[30];
 	const char *name = mc13xxx_get_chipname(mc13xxx);
 
@@ -381,8 +382,11 @@ static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
 	if (!cell.name)
 		return -ENOMEM;
 
-	return mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
+	ret = mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
 			       regmap_irq_get_domain(mc13xxx->irq_data));
+	if (ret)
+		kfree(cell.name);
+	return ret;
 }
 
 static int mc13xxx_add_subdevice(struct mc13xxx *mc13xxx, const char *format)
-- 
2.43.0
Re: [PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()
Posted by kernel test robot 5 days, 5 hours ago
Hi Abdun,

kernel test robot noticed the following build errors:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on lee-mfd/for-mfd-fixes lee-leds/for-leds-next linus/master v6.18-rc7 next-20251126]
[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/Abdun-Nihaal/mfd-mc13xxx-core-Fix-memory-leak-in-mc13xxx_add_subdevice_pdata/20251126-012428
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
patch link:    https://lore.kernel.org/r/20251125171936.98101-1-nihaal%40cse.iitm.ac.in
patch subject: [PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()
config: x86_64-buildonly-randconfig-003-20251126 (https://download.01.org/0day-ci/archive/20251127/202511270127.8j2J6hwF-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251127/202511270127.8j2J6hwF-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511270127.8j2J6hwF-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/mfd/mc13xxx-core.c:388:3: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     388 |                 kfree(cell.name);
         |                 ^
   1 error generated.


vim +/kfree +388 drivers/mfd/mc13xxx-core.c

   364	
   365	static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
   366			const char *format, void *pdata, size_t pdata_size)
   367	{
   368		int ret;
   369		char buf[30];
   370		const char *name = mc13xxx_get_chipname(mc13xxx);
   371	
   372		struct mfd_cell cell = {
   373			.platform_data = pdata,
   374			.pdata_size = pdata_size,
   375		};
   376	
   377		/* there is no asnprintf in the kernel :-( */
   378		if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
   379			return -E2BIG;
   380	
   381		cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
   382		if (!cell.name)
   383			return -ENOMEM;
   384	
   385		ret = mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
   386				       regmap_irq_get_domain(mc13xxx->irq_data));
   387		if (ret)
 > 388			kfree(cell.name);
   389		return ret;
   390	}
   391	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()
Posted by kernel test robot 5 days, 8 hours ago
Hi Abdun,

kernel test robot noticed the following build errors:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on lee-mfd/for-mfd-fixes lee-leds/for-leds-next linus/master v6.18-rc7 next-20251126]
[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/Abdun-Nihaal/mfd-mc13xxx-core-Fix-memory-leak-in-mc13xxx_add_subdevice_pdata/20251126-012428
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
patch link:    https://lore.kernel.org/r/20251125171936.98101-1-nihaal%40cse.iitm.ac.in
patch subject: [PATCH] mfd: mc13xxx-core: Fix memory leak in mc13xxx_add_subdevice_pdata()
config: i386-randconfig-141-20251126 (https://download.01.org/0day-ci/archive/20251126/202511262250.jglT8Ap3-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251126/202511262250.jglT8Ap3-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511262250.jglT8Ap3-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/mfd/mc13xxx-core.c: In function 'mc13xxx_add_subdevice_pdata':
>> drivers/mfd/mc13xxx-core.c:388:17: error: implicit declaration of function 'kfree' [-Wimplicit-function-declaration]
     388 |                 kfree(cell.name);
         |                 ^~~~~


vim +/kfree +388 drivers/mfd/mc13xxx-core.c

   364	
   365	static int mc13xxx_add_subdevice_pdata(struct mc13xxx *mc13xxx,
   366			const char *format, void *pdata, size_t pdata_size)
   367	{
   368		int ret;
   369		char buf[30];
   370		const char *name = mc13xxx_get_chipname(mc13xxx);
   371	
   372		struct mfd_cell cell = {
   373			.platform_data = pdata,
   374			.pdata_size = pdata_size,
   375		};
   376	
   377		/* there is no asnprintf in the kernel :-( */
   378		if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
   379			return -E2BIG;
   380	
   381		cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
   382		if (!cell.name)
   383			return -ENOMEM;
   384	
   385		ret = mfd_add_devices(mc13xxx->dev, -1, &cell, 1, NULL, 0,
   386				       regmap_irq_get_domain(mc13xxx->irq_data));
   387		if (ret)
 > 388			kfree(cell.name);
   389		return ret;
   390	}
   391	

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