From: Anshuman <anshumantewari123@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>, Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R . Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org,
Anshuman <anshumantewari123@gmail.com>
Subject: [PATCH] selftests/mm: check strdup() and fix buf leak in parse_test_type()
Date: Fri, 21 Aug 2026 17:14:16 +0530 [thread overview]
Message-ID: <20260821114416.12255-1-anshumantewari123@gmail.com> (raw)
The return value of strdup() is never checked before being passed to
strsep() and strcmp(). If strdup() fails and returns NULL, strsep()
returns NULL as well, and the subsequent strcmp(NULL, "all") is
undefined behavior, likely causing a crash.
Additionally, buf is never freed. strsep() advances the buf pointer
past the first token, so by the time buf would normally be freed,
the original pointer returned by strdup() has already been
overwritten and is no longer available.
Check strdup()'s return value and fail cleanly on allocation failure.
Keep a separate pointer to the original allocation so it can be
freed once buf is done being used, after all parsing has completed
successfully.
Signed-off-by: Anshuman <anshumantewari123@gmail.com>
---
tools/testing/selftests/mm/khugepaged.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 10e8dedcb..a84fb87bd 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1110,6 +1110,7 @@ static void parse_test_type(int argc, char **argv)
{
int opt;
char *buf;
+ char *c;
const char *token;
while ((opt = getopt(argc, argv, "s:h")) != -1) {
@@ -1135,7 +1136,10 @@ static void parse_test_type(int argc, char **argv)
}
buf = strdup(argv[0]);
- token = strsep(&buf, ":");
+ if (!buf)
+ ksft_exit_fail_msg("Insufficient memory\n");
+ c = buf;
+ token = strsep(&c, ":");
if (!strcmp(token, "all")) {
khugepaged_context = &__khugepaged_context;
@@ -1148,26 +1152,27 @@ static void parse_test_type(int argc, char **argv)
usage();
}
- if (!buf)
+ if (!c)
usage();
- if (!strcmp(buf, "all")) {
+ if (!strcmp(c, "all")) {
read_only_file_ops = &__read_only_file_ops;
read_write_file_read_ops = &__read_write_file_read_ops;
read_write_file_write_ops = &__read_write_file_write_ops;
anon_ops = &__anon_ops;
shmem_ops = &__shmem_ops;
- } else if (!strcmp(buf, "anon")) {
+ } else if (!strcmp(c, "anon")) {
anon_ops = &__anon_ops;
- } else if (!strcmp(buf, "file")) {
+ } else if (!strcmp(c, "file")) {
read_only_file_ops = &__read_only_file_ops;
read_write_file_read_ops = &__read_write_file_read_ops;
read_write_file_write_ops = &__read_write_file_write_ops;
- } else if (!strcmp(buf, "shmem")) {
+ } else if (!strcmp(c, "shmem")) {
shmem_ops = &__shmem_ops;
} else {
usage();
}
+ free(buf);
if (!read_only_file_ops && !read_write_file_read_ops &&
!read_write_file_write_ops)
--
2.55.0
next reply other threads:[~2026-08-21 11:45 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-21 11:44 Anshuman [this message]
2026-08-21 14:18 ` [PATCH] selftests/mm: check strdup() and fix buf leak in parse_test_type() David Hildenbrand (Arm)
2026-08-21 16:09 ` Anshuman Tewari
2026-08-21 16:18 ` David Hildenbrand (Arm)
2026-08-21 20:14 ` Anshuman Tewari
2026-08-25 11:03 ` David Hildenbrand (Arm)
2026-08-25 18:20 ` Anshuman Tewari
2026-08-25 18:32 ` David Hildenbrand (Arm)
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=20260821114416.12255-1-anshumantewari123@gmail.com \
--to=anshumantewari123@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nico.pache@linux.dev \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox