linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations
@ 2025-08-29  9:59 Kaushlendra Kumar
  2025-08-29 11:51 ` Harry Yoo
  2025-08-29 18:52 ` SeongJae Park
  0 siblings, 2 replies; 5+ messages in thread
From: Kaushlendra Kumar @ 2025-08-29  9:59 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, Kaushlendra Kumar

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.

Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
 tools/mm/slabinfo.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/mm/slabinfo.c b/tools/mm/slabinfo.c
index 1433eff99feb..1a7f2874c625 100644
--- a/tools/mm/slabinfo.c
+++ b/tools/mm/slabinfo.c
@@ -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);
 	}
-- 
2.34.1



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

end of thread, other threads:[~2025-08-29 18:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-29  9:59 [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations Kaushlendra Kumar
2025-08-29 11:51 ` Harry Yoo
2025-08-29 13:12   ` Kumar, Kaushlendra
2025-08-29 15:30   ` Kumar, Kaushlendra
2025-08-29 18:52 ` SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).