All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sarthak Sharma <sarthak.sharma@arm.com>
To: Anshuman <anshumantewari123@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] selftests/mm: check stat() return value in khugepaged get_finfo()
Date: Thu, 20 Aug 2026 09:44:49 +0530	[thread overview]
Message-ID: <8fdf8108-9ee2-4b30-9036-ad6066f3db5f@arm.com> (raw)
In-Reply-To: <20260819121426.49500-1-anshumantewari123@gmail.com>



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,



  parent reply	other threads:[~2026-08-20  4:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-08-20 17:31 ` 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=8fdf8108-9ee2-4b30-9036-ad6066f3db5f@arm.com \
    --to=sarthak.sharma@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=anshumantewari123@gmail.com \
    --cc=david@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=shuah@kernel.org \
    /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.