All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/damon/sysfs-schemes: using kmalloc_array() and size_add()
@ 2025-04-21  6:24 Su Hui
  2025-04-21 17:07 ` SeongJae Park
  2025-04-22 10:38 ` Dan Carpenter
  0 siblings, 2 replies; 8+ messages in thread
From: Su Hui @ 2025-04-21  6:24 UTC (permalink / raw)
  To: sj, akpm
  Cc: Su Hui, damon, linux-mm, linux-kernel, kernel-janitors,
	linux-hardening

It's safer to using kmalloc_array() and size_add() because it can
prevent possible overflow problem.

Signed-off-by: Su Hui <suhui@nfschina.com>
---
 mm/damon/sysfs-schemes.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
index 23b562df0839..79220aba436f 100644
--- a/mm/damon/sysfs-schemes.c
+++ b/mm/damon/sysfs-schemes.c
@@ -465,7 +465,8 @@ static ssize_t memcg_path_store(struct kobject *kobj,
 {
 	struct damon_sysfs_scheme_filter *filter = container_of(kobj,
 			struct damon_sysfs_scheme_filter, kobj);
-	char *path = kmalloc(sizeof(*path) * (count + 1), GFP_KERNEL);
+	char *path = kmalloc_array(size_add(count, 1), sizeof(*path),
+				   GFP_KERNEL);
 
 	if (!path)
 		return -ENOMEM;
@@ -2035,7 +2036,7 @@ static int damon_sysfs_memcg_path_to_id(char *memcg_path, unsigned short *id)
 	if (!memcg_path)
 		return -EINVAL;
 
-	path = kmalloc(sizeof(*path) * PATH_MAX, GFP_KERNEL);
+	path = kmalloc_array(PATH_MAX, sizeof(*path), GFP_KERNEL);
 	if (!path)
 		return -ENOMEM;
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-04-23  5:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-21  6:24 [PATCH] mm/damon/sysfs-schemes: using kmalloc_array() and size_add() Su Hui
2025-04-21 17:07 ` SeongJae Park
2025-04-22 10:38 ` Dan Carpenter
2025-04-22 10:44   ` Dan Carpenter
2025-04-22 18:23     ` SeongJae Park
2025-04-22 18:50       ` Christophe JAILLET
2025-04-23  2:04         ` Su Hui
2025-04-23  5:38           ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.