Skip to contentSkip to conversation

tools/mm/slabinfo: fix buffer overflows in fread operations

PATCH In review Kaushlendra Kumar 3 participants 7 messages Aug 29, 2025 – Sep 22, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · 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.

---

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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
author Reviewed-by Harry Yoo
#2 lore raw
Quoted 9 lines · 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.

Quoted 7 lines

---

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..1a7f2874c625 100644

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

________________________________
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 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.

---

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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

author Reviewed-by Harry Yoo Reviewed-by Harry Yoo
#3 lore raw
In reply to Harry Yoo

Harry Yoo
Aug. 29, 2025, 11:51 a.m. UTC | #1

Quoted 9 lines · 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.

Quoted 7 lines

---

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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 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.

Quoted 7 lines

---

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..1a7f2874c625 100644

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

________________________________
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 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.

---

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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 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.

---

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
index 1433eff99feb..1a7f2874c625 100644
@@ -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

Acked-by on v1 1/1
#4 lore raw
In reply to Kaushlendra Kumar
Quoted 10 lines · On Fri, 29 Aug 2025 15:29:47 +0530 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.

Thanks,
SJ

[...]

#5 lore raw
Quoted 11 lines · SeongJae Park
Quoted 9 lines · On Fri, 29 Aug 2025 15:29:47 +0530 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.

Quoted 7 lines
Quoted 1 line

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

#6 lore raw
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 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.

Quoted 7 lines
Quoted 1 line

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

[...]