[PATCH v2] crypto: caam: Add check for kcalloc() in test_len()

Guangshuo Li posted 1 patch 1 week, 2 days ago
There is a newer version of this series
drivers/crypto/caam/caamrng.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] crypto: caam: Add check for kcalloc() in test_len()
Posted by Guangshuo Li 1 week, 2 days ago
As kcalloc() may fail, check its return value to avoid a NULL pointer
dereference when passing the buffer to rng->read().

Fixes: 2be0d806e25e ("crypto: caam - add a test for the RNG")
Cc: stable@vger.kernel.org
---
changelog:
v2:
- Return -ENOMEM directly on allocation failure.

Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/crypto/caam/caamrng.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c
index b3d14a7f4dd1..357860ee532c 100644
--- a/drivers/crypto/caam/caamrng.c
+++ b/drivers/crypto/caam/caamrng.c
@@ -182,6 +182,9 @@ static inline void test_len(struct hwrng *rng, size_t len, bool wait)
 
 	buf = kcalloc(CAAM_RNG_MAX_FIFO_STORE_SIZE, sizeof(u8), GFP_KERNEL);
 
+	if (!buf) {
+		return -ENOMEM;
+	}
 	while (len > 0) {
 		read_len = rng->read(rng, buf, len, wait);
 
-- 
2.43.0
Re: [PATCH v2] crypto: caam: Add check for kcalloc() in test_len()
Posted by kernel test robot 1 week, 1 day ago
Hi Guangshuo,

kernel test robot noticed the following build errors:

[auto build test ERROR on herbert-cryptodev-2.6/master]
[also build test ERROR on herbert-crypto-2.6/master linus/master v6.17-rc7 next-20250922]
[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/Guangshuo-Li/crypto-caam-Add-check-for-kcalloc-in-test_len/20250922-235723
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
patch link:    https://lore.kernel.org/r/20250922155322.1825714-1-lgs201920130244%40gmail.com
patch subject: [PATCH v2] crypto: caam: Add check for kcalloc() in test_len()
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20250923/202509231807.ZFBBKMM4-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250923/202509231807.ZFBBKMM4-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/202509231807.ZFBBKMM4-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/crypto/caam/caamrng.c: In function 'test_len':
>> drivers/crypto/caam/caamrng.c:186:24: error: 'return' with a value, in function returning void [-Wreturn-mismatch]
     186 |                 return -ENOMEM;
         |                        ^
   drivers/crypto/caam/caamrng.c:176:20: note: declared here
     176 | static inline void test_len(struct hwrng *rng, size_t len, bool wait)
         |                    ^~~~~~~~


vim +/return +186 drivers/crypto/caam/caamrng.c

   174	
   175	#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
   176	static inline void test_len(struct hwrng *rng, size_t len, bool wait)
   177	{
   178		u8 *buf;
   179		int read_len;
   180		struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
   181		struct device *dev = ctx->ctrldev;
   182	
   183		buf = kcalloc(CAAM_RNG_MAX_FIFO_STORE_SIZE, sizeof(u8), GFP_KERNEL);
   184	
   185		if (!buf) {
 > 186			return -ENOMEM;
   187		}
   188		while (len > 0) {
   189			read_len = rng->read(rng, buf, len, wait);
   190	
   191			if (read_len < 0 || (read_len == 0 && wait)) {
   192				dev_err(dev, "RNG Read FAILED received %d bytes\n",
   193					read_len);
   194				kfree(buf);
   195				return;
   196			}
   197	
   198			print_hex_dump_debug("random bytes@: ",
   199				DUMP_PREFIX_ADDRESS, 16, 4,
   200				buf, read_len, 1);
   201	
   202			len = len - read_len;
   203		}
   204	
   205		kfree(buf);
   206	}
   207	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2] crypto: caam: Add check for kcalloc() in test_len()
Posted by kernel test robot 1 week, 1 day ago
Hi Guangshuo,

kernel test robot noticed the following build warnings:

[auto build test WARNING on herbert-cryptodev-2.6/master]
[also build test WARNING on herbert-crypto-2.6/master linus/master v6.17-rc7 next-20250922]
[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/Guangshuo-Li/crypto-caam-Add-check-for-kcalloc-in-test_len/20250922-235723
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
patch link:    https://lore.kernel.org/r/20250922155322.1825714-1-lgs201920130244%40gmail.com
patch subject: [PATCH v2] crypto: caam: Add check for kcalloc() in test_len()
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20250923/202509231744.SGr3Dh19-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project cafc064fc7a96b3979a023ddae1da2b499d6c954)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250923/202509231744.SGr3Dh19-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/202509231744.SGr3Dh19-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from <built-in>:3:
   In file included from include/linux/compiler_types.h:171:
   include/linux/compiler-clang.h:28:9: warning: '__SANITIZE_ADDRESS__' macro redefined [-Wmacro-redefined]
      28 | #define __SANITIZE_ADDRESS__
         |         ^
   <built-in>:371:9: note: previous definition is here
     371 | #define __SANITIZE_ADDRESS__ 1
         |         ^
>> drivers/crypto/caam/caamrng.c:186:3: warning: void function 'test_len' should not return a value [-Wreturn-mismatch]
     186 |                 return -ENOMEM;
         |                 ^      ~~~~~~~
   2 warnings generated.


vim +/test_len +186 drivers/crypto/caam/caamrng.c

   174	
   175	#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
   176	static inline void test_len(struct hwrng *rng, size_t len, bool wait)
   177	{
   178		u8 *buf;
   179		int read_len;
   180		struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
   181		struct device *dev = ctx->ctrldev;
   182	
   183		buf = kcalloc(CAAM_RNG_MAX_FIFO_STORE_SIZE, sizeof(u8), GFP_KERNEL);
   184	
   185		if (!buf) {
 > 186			return -ENOMEM;
   187		}
   188		while (len > 0) {
   189			read_len = rng->read(rng, buf, len, wait);
   190	
   191			if (read_len < 0 || (read_len == 0 && wait)) {
   192				dev_err(dev, "RNG Read FAILED received %d bytes\n",
   193					read_len);
   194				kfree(buf);
   195				return;
   196			}
   197	
   198			print_hex_dump_debug("random bytes@: ",
   199				DUMP_PREFIX_ADDRESS, 16, 4,
   200				buf, read_len, 1);
   201	
   202			len = len - read_len;
   203		}
   204	
   205		kfree(buf);
   206	}
   207	

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