Skip to contentSkip to conversation

ext4: use kcalloc instead of open coded arithmetic

PATCH New [email protected] 1 participant 1 message Dec 15, 2023 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: use kcalloc instead of open coded arithmetic

From: Yang Guang <[email protected]>

Dynamic size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.

---
 fs/ext4/hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/hash.c +1 -1
index deabe29da7fb..7a9afac1597c 100644
@@ -302,7 +302,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 
 	if (len && IS_CASEFOLDED(dir) &&
 	   (!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir))) {
-		buff = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
+		buff = kcalloc(PATH_MAX, sizeof(char), GFP_KERNEL);
 		if (!buff)
 			return -ENOMEM;
 
Signature · 1 line
2.25.1