From: Harry Yoo <harry.yoo@oracle.com>
To: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org, vbabka@suse.cz,
cl@linux.com, rientjes@google.com, roman.gushchin@linux.dev
Subject: Re: [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations
Date: Fri, 29 Aug 2025 20:51:17 +0900 [thread overview]
Message-ID: <aLGUNRIKgRNkgY0v@hyeyoo> (raw)
In-Reply-To: <20250829095947.1828094-1-kaushlendra.kumar@intel.com>
On Fri, Aug 29, 2025 at 03:29:47PM +0530, Kaushlendra Kumar wrote:
> 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>
> ---
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
A side question, did you observe this while using the tool?
Perhaps that means we need to make the buffer bigger.
> 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
--
Cheers,
Harry / Hyeonggon
next prev parent reply other threads:[~2025-08-29 11:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-29 9:59 [PATCH] tools/mm/slabinfo: fix buffer overflows in fread operations Kaushlendra Kumar
2025-08-29 11:51 ` Harry Yoo [this message]
2025-08-29 13:12 ` Kumar, Kaushlendra
2025-08-29 15:30 ` Kumar, Kaushlendra
2025-08-29 18:52 ` SeongJae Park
2025-09-22 2:40 ` Kumar, Kaushlendra
2025-09-22 9:45 ` SeongJae Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aLGUNRIKgRNkgY0v@hyeyoo \
--to=harry.yoo@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=kaushlendra.kumar@intel.com \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.