tools/mm/slabinfo: fix buffer overflows in fread operations
Subsystems (2)
PATCH 1/1 tools/mm/slabinfo: fix buffer overflows in fread operations
The fread() calls in read_slab_obj() and read_debug_slab_obj() can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. If fread() returns sizeof(buffer), this writes beyond the allocated buffer boundaries.
Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
--- tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/mm/slabinfo.c +2 -2
@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }
@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }
Signature · 1 line
2.34.1
Quoted 11 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Signed-off-by Kaushlendra Kumar <[email protected]>---
A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Quoted 27 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +2
-2@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }Signature · 1 line
2.34.1
Signature · 2 lines
Cheers, Harry / Hyeonggon
Kumar, Kaushlendra
Reviewed-by Harry YooQuoted 9 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Quoted 7 lines
Signed-off-by Kaushlendra Kumar <[email protected]>---
Reviewed-by Harry Yoo <[email protected]>A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Thanks for the review!
I discovered this issue while testing some local modifications to the code. For now, let's keep the current buffer size and address the overflow with this fix.
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c index renamed from tools/mm/slabinfo.c +0
-0
1433eff99feb..1a7f2874c625100644tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); } --
Quoted 1 line
2.34.1
________________________________
From: Harry Yoo <[email protected]>
Sent: Friday, August 29, 2025 5:21 PM
To: Kumar, Kaushlendra <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations
Quoted 11 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Signed-off-by Kaushlendra Kumar <[email protected]>---
A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); } --
Quoted 1 line
2.34.1
--
Cheers,
Harry / Hyeonggon
Harry Yoo
Aug. 29, 2025, 11:51 a.m. UTC | #1
Quoted 9 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Quoted 7 lines
Signed-off-by Kaushlendra Kumar <[email protected]>---
Reviewed-by Harry Yoo <[email protected]>A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Found during Code Review
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); } --
Quoted 1 line
2.34.1
Kumar, Kaushlendra
Aug. 29, 2025, 1:12 p.m. UTC | #2
Quoted 9 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Quoted 7 lines
Signed-off-by Kaushlendra Kumar <[email protected]>---
Reviewed-by Harry Yoo <[email protected]>A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Thanks for the review!
I discovered this issue while testing some local modifications to the code. For now, let's keep the current buffer size and address the overflow with this fix.
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c index renamed from tools/mm/slabinfo.c +0
-0
1433eff99feb..1a7f2874c625100644tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0] = 0; l = 0; } else { - l = fread(buffer, 1, sizeof(buffer), f); + l = fread(buffer, 1, sizeof(buffer) - 1, f); buffer[l] = 0; fclose(f); } --
Quoted 1 line
2.34.1
________________________________
From: Harry Yoo <[email protected]>
Sent: Friday, August 29, 2025 5:21 PM
To: Kumar, Kaushlendra <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations
Quoted 11 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Signed-off-by Kaushlendra Kumar <[email protected]>---
A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); } --
Quoted 1 line
2.34.1
--
Cheers,
Harry / Hyeonggon
________________________________
From: Harry Yoo <[email protected]>
Sent: Friday, August 29, 2025 5:21 PM
To: Kumar, Kaushlendra <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations
Quoted 11 lines · Kaushlendra Kumar
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Signed-off-by Kaushlendra Kumar <[email protected]>---
A side question, did you observe this while using the tool? Perhaps that means we need to make the buffer bigger.
Quoted 26 lines
tools/mm/slabinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +2
-3@@ -228,7 +228,7 @@ static unsigned long read_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); }@@ -247,7 +247,7 @@ static unsigned long read_debug_slab_obj(struct slabinfo *s, const char *name) buffer[0 ] =0 ; l =0 ; }else { - l =fread (buffer,1 ,sizeof (buffer), f); + l =fread (buffer,1 ,sizeof (buffer) -1 , f); buffer[l] =0 ;fclose (f); } --
Quoted 1 line
2.34.1
--
Cheers,
Harry / Hyeonggon
Quoted 10 lines · On Fri, 29 Aug 2025 15:29:47 +0530 Kaushlendra Kumar…
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Signed-off-by Kaushlendra Kumar <[email protected]>
Thanks,
SJ
[...]
Quoted 11 lines · SeongJae Park
Quoted 9 lines · On Fri, 29 Aug 2025 15:29:47 +0530 Kaushlendra Kumar…
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Quoted 7 lines
Quoted 1 line
Signed-off-by Kaushlendra Kumar <[email protected]>Acked-by SeongJae Park <[email protected]>Thanks,
SJ
Hi SeongJae,
Thank you for the Acked-by!
Since this patch has received your acknowledgment, are we going to merge this patch? Should I expect it to be picked up for the next kernel release, or are there any additional steps needed from my side?
I appreciate your review and guidance on the merge process.
Best regards,
Kaushlendra
Quoted 20 lines · Kaushlendra
Quoted 11 lines · SeongJae Park
Quoted 9 lines · On Fri, 29 Aug 2025 15:29:47 +0530 Kaushlendra Kumar…
The
fread()calls inread_slab_obj()andread_debug_slab_obj()can read up to sizeof(buffer) bytes, but then unconditionally write a null terminator at buffer[l]. Iffread()returns sizeof(buffer), this writes beyond the allocated buffer boundaries.Fix by limiting reads to sizeof(buffer) - 1 bytes in both functions, ensuring space is always reserved for null termination. This prevents buffer overflows while maintaining proper string handling.
Quoted 7 lines
Quoted 1 line
Signed-off-by Kaushlendra Kumar <[email protected]>Acked-by SeongJae Park <[email protected]>Thanks,
SJ
Quoted 7 lines
Hi SeongJae,
Thank you for the Acked-by!
Since this patch has received your acknowledgment, are we going to merge this patch? Should I expect it to be picked up for the next kernel release, or are there any additional steps needed from my side?
I'm not a maintainer or a reviewr of slabinfo, so my Acked-by: doesn't mean many things. I think the next steps and decisions are up to Andrew.
And I think Andrew should be busy for the preparation of the next merge window. I'd suggest pinging Andrew again, after the next MM pull requests for 6.18-rc1 are done.
Thanks,
SJ
[...]