Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
@ 2026-08-19 12:14 Anshuman
  2026-08-20  0:13 ` SJ Park
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Anshuman @ 2026-08-19 12:14 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes
  Cc: Shuah Khan, linux-mm, linux-kselftest, linux-kernel, Anshuman

get_finfo() calls stat() to get metadata about the target directory,
but never checks the return value. On failure, stat() returns -1 and
leaves path_stat unmodified, so path_stat.st_mode may contain
uninitialized stack data.

The code then checks S_ISDIR(path_stat.st_mode) against this
potentially garbage value. This can produce a misleading "Not a
directory" error when the real problem is a nonexistent or
inaccessible path, or, in the worst case, the check could pass by
chance on garbage data and let the function continue using an
invalid path_stat for the rest of its logic.

Check the return value and fail with a clear error message if
stat() fails, matching the error-handling style already used for
statfs() and read_file() later in the same function.

Signed-off-by: Anshuman <anshumantewari123@gmail.com>
---
 tools/testing/selftests/mm/khugepaged.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
index 10e8dedcb..2240a9b4f 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -121,7 +121,8 @@ static void get_finfo(const char *dir)
 	char *str, *end;
 
 	finfo.dir = dir;
-	stat(finfo.dir, &path_stat);
+	if (stat(finfo.dir, &path_stat))
+		ksft_exit_fail_perror("stat()");
 	if (!S_ISDIR(path_stat.st_mode))
 		ksft_exit_fail_msg("%s: Not a directory (%s)\n", __func__, finfo.dir);
 	if (snprintf(finfo.path, sizeof(finfo.path), "%s/" TEST_FILE,
-- 
2.55.0



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

* Re: [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
  2026-08-19 12:14 [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo() Anshuman
@ 2026-08-20  0:13 ` SJ Park
  2026-08-20 14:32   ` Anshuman Tewari
  2026-08-20  4:14 ` Sarthak Sharma
  2026-08-20 17:31 ` David Hildenbrand (Arm)
  2 siblings, 1 reply; 5+ messages in thread
From: SJ Park @ 2026-08-20  0:13 UTC (permalink / raw)
  To: Anshuman
  Cc: SJ Park, Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Shuah Khan, linux-mm, linux-kselftest, linux-kernel, Zi Yan,
	Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Barry Song, Lance Yang, Usama Arif, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko

'get_maintainer.pl --nogit --nogit-fallback' suggests adding below recipients.
I added them.

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

On Wed, 19 Aug 2026 17:44:26 +0530 Anshuman <anshumantewari123@gmail.com> wrote:

> get_finfo() calls stat() to get metadata about the target directory,
> but never checks the return value. On failure, stat() returns -1 and
> leaves path_stat unmodified, so path_stat.st_mode may contain
> uninitialized stack data.
> 
> The code then checks S_ISDIR(path_stat.st_mode) against this
> potentially garbage value. This can produce a misleading "Not a
> directory" error when the real problem is a nonexistent or
> inaccessible path, or, in the worst case, the check could pass by
> chance on garbage data and let the function continue using an
> invalid path_stat for the rest of its logic.
> 
> Check the return value and fail with a clear error message if
> stat() fails, matching the error-handling style already used for
> statfs() and read_file() later in the same function.

Makes sense to me.

> 
> Signed-off-by: Anshuman <anshumantewari123@gmail.com>

Reviewed-by: SJ Park <sj@kernel.org>


Thanks,
SJ

[...]


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

* Re: [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
  2026-08-19 12:14 [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo() Anshuman
  2026-08-20  0:13 ` SJ Park
@ 2026-08-20  4:14 ` Sarthak Sharma
  2026-08-20 17:31 ` David Hildenbrand (Arm)
  2 siblings, 0 replies; 5+ messages in thread
From: Sarthak Sharma @ 2026-08-20  4:14 UTC (permalink / raw)
  To: Anshuman, Andrew Morton, David Hildenbrand, Lorenzo Stoakes
  Cc: Shuah Khan, linux-mm, linux-kselftest, linux-kernel



On 8/19/26 5:44 PM, Anshuman wrote:
> get_finfo() calls stat() to get metadata about the target directory,
> but never checks the return value. On failure, stat() returns -1 and
> leaves path_stat unmodified, so path_stat.st_mode may contain
> uninitialized stack data.
> 
> The code then checks S_ISDIR(path_stat.st_mode) against this
> potentially garbage value. This can produce a misleading "Not a
> directory" error when the real problem is a nonexistent or
> inaccessible path, or, in the worst case, the check could pass by
> chance on garbage data and let the function continue using an
> invalid path_stat for the rest of its logic.
> 
> Check the return value and fail with a clear error message if
> stat() fails, matching the error-handling style already used for
> statfs() and read_file() later in the same function.
> 
> Signed-off-by: Anshuman <anshumantewari123@gmail.com>

LGTM, so:

Reviewed-by: Sarthak Sharma <sarthak.sharma@arm.com>

> ---
>  tools/testing/selftests/mm/khugepaged.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 10e8dedcb..2240a9b4f 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -121,7 +121,8 @@ static void get_finfo(const char *dir)
>  	char *str, *end;
>  
>  	finfo.dir = dir;
> -	stat(finfo.dir, &path_stat);
> +	if (stat(finfo.dir, &path_stat))
> +		ksft_exit_fail_perror("stat()");
>  	if (!S_ISDIR(path_stat.st_mode))
>  		ksft_exit_fail_msg("%s: Not a directory (%s)\n", __func__, finfo.dir);
>  	if (snprintf(finfo.path, sizeof(finfo.path), "%s/" TEST_FILE,



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

* Re: [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
  2026-08-20  0:13 ` SJ Park
@ 2026-08-20 14:32   ` Anshuman Tewari
  0 siblings, 0 replies; 5+ messages in thread
From: Anshuman Tewari @ 2026-08-20 14:32 UTC (permalink / raw)
  To: SJ Park
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Shuah Khan,
	linux-mm, linux-kselftest, linux-kernel, Zi Yan, Baolin Wang,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko

Thanks for the review, SJ — and for the extra maintainer list from
--nogit-fallback, good to know for next time.

Anshuman


On Thu, 20 Aug 2026 at 05:44, SJ Park <sj@kernel.org> wrote:
>
> 'get_maintainer.pl --nogit --nogit-fallback' suggests adding below recipients.
> I added them.
>
> - 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>
>
> On Wed, 19 Aug 2026 17:44:26 +0530 Anshuman <anshumantewari123@gmail.com> wrote:
>
> > get_finfo() calls stat() to get metadata about the target directory,
> > but never checks the return value. On failure, stat() returns -1 and
> > leaves path_stat unmodified, so path_stat.st_mode may contain
> > uninitialized stack data.
> >
> > The code then checks S_ISDIR(path_stat.st_mode) against this
> > potentially garbage value. This can produce a misleading "Not a
> > directory" error when the real problem is a nonexistent or
> > inaccessible path, or, in the worst case, the check could pass by
> > chance on garbage data and let the function continue using an
> > invalid path_stat for the rest of its logic.
> >
> > Check the return value and fail with a clear error message if
> > stat() fails, matching the error-handling style already used for
> > statfs() and read_file() later in the same function.
>
> Makes sense to me.
>
> >
> > Signed-off-by: Anshuman <anshumantewari123@gmail.com>
>
> Reviewed-by: SJ Park <sj@kernel.org>
>
>
> Thanks,
> SJ
>
> [...]


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

* Re: [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
  2026-08-19 12:14 [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo() Anshuman
  2026-08-20  0:13 ` SJ Park
  2026-08-20  4:14 ` Sarthak Sharma
@ 2026-08-20 17:31 ` David Hildenbrand (Arm)
  2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-08-20 17:31 UTC (permalink / raw)
  To: Anshuman, Andrew Morton, Lorenzo Stoakes
  Cc: Shuah Khan, linux-mm, linux-kselftest, linux-kernel

On 8/19/26 14:14, Anshuman wrote:
> get_finfo() calls stat() to get metadata about the target directory,
> but never checks the return value. On failure, stat() returns -1 and
> leaves path_stat unmodified, so path_stat.st_mode may contain
> uninitialized stack data.
> 
> The code then checks S_ISDIR(path_stat.st_mode) against this
> potentially garbage value. This can produce a misleading "Not a
> directory" error when the real problem is a nonexistent or
> inaccessible path, or, in the worst case, the check could pass by
> chance on garbage data and let the function continue using an
> invalid path_stat for the rest of its logic.
> 
> Check the return value and fail with a clear error message if
> stat() fails, matching the error-handling style already used for
> statfs() and read_file() later in the same function.
> 
> Signed-off-by: Anshuman <anshumantewari123@gmail.com>
> ---
>  tools/testing/selftests/mm/khugepaged.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index 10e8dedcb..2240a9b4f 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -121,7 +121,8 @@ static void get_finfo(const char *dir)
>  	char *str, *end;
>  
>  	finfo.dir = dir;
> -	stat(finfo.dir, &path_stat);
> +	if (stat(finfo.dir, &path_stat))
> +		ksft_exit_fail_perror("stat()");
>  	if (!S_ISDIR(path_stat.st_mode))
>  		ksft_exit_fail_msg("%s: Not a directory (%s)\n", __func__, finfo.dir);
>  	if (snprintf(finfo.path, sizeof(finfo.path), "%s/" TEST_FILE,

From out selftests that should mostly be impossible to trigger, as
run_vmtests.sh will just create that directory.

So it's a valid improvement, but mostly irrelevant in practice

1Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David


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

end of thread, other threads:[~2026-08-20 17:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-19 12:14 [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo() Anshuman
2026-08-20  0:13 ` SJ Park
2026-08-20 14:32   ` Anshuman Tewari
2026-08-20  4:14 ` Sarthak Sharma
2026-08-20 17:31 ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox