Skip to contentSkip to conversation

mm/damon: Adjust the size of kbuf array to avoid overflow

PATCH In review Xin Hao 2 participants 2 messages Oct 13, 2021

Subsystems (2)
lore
Share & tools
lore

Xin Hao

author
#0 lore raw

PATCH 1/1 mm/damon: Adjust the size of kbuf array to avoid overflow

In order to avoid the 'count' size space of kbuf array is used up, but a "\0" is still added.

---
 mm/damon/dbgfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/damon/dbgfs.c +1 -1
index faee070977d8..20c61eed54af 100644
@@ -247,7 +247,7 @@ static ssize_t dbgfs_kdamond_pid_read(struct file *file,
 	char *kbuf;
 	ssize_t len;
 
-	kbuf = kmalloc(count, GFP_KERNEL);
+	kbuf = kmalloc(count + 1, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
 
Signature · 1 line
2.31.0
maintainer
#1 lore raw

Hi Xin,

Quoted 2 lines · On Wed, 13 Oct 2021 19:48:54 +0800 Xin Hao <[email protected]> wrote:

In order to avoid the 'count' size space of kbuf array is used up, but a "\0" is still added.

Thank you for this patch! :)

But... I unsure how this can cause a buffer overflow, as 'kbuf' is accessed by only size-specified functions, namely 'scnprintf()' and 'simple_read_from_buffer()'.

If I'm missing something, please feel free to let me know.

Thanks,
SJ

Quoted 22 lines
---
 mm/damon/dbgfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/damon/dbgfs.c +1 -1
index faee070977d8..20c61eed54af 100644
@@ -247,7 +247,7 @@ static ssize_t dbgfs_kdamond_pid_read(struct file *file,
 	char *kbuf;
 	ssize_t len;
 
-	kbuf = kmalloc(count, GFP_KERNEL);
+	kbuf = kmalloc(count + 1, GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
 
Signature · 1 line
2.31.0