From: Shuah Khan <skhan@linuxfoundation.org>
To: Ma Ke <make24@iscas.ac.cn>,
shuah@kernel.org, usama.anjum@collabora.com,
swarupkotikalapudi@gmail.com, amer.shanawany@gmail.com,
kees@kernel.org, akpm@linux-foundation.org, luto@kernel.org
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH v2] selftests/capabilities: Fix possible file leak in copy_fromat_to
Date: Thu, 27 Jun 2024 11:29:52 -0600 [thread overview]
Message-ID: <8b040e83-5eda-4b59-96a9-d76cb4f80d70@linuxfoundation.org> (raw)
In-Reply-To: <20240627015732.2974078-1-make24@iscas.ac.cn>
On 6/26/24 19:57, Ma Ke wrote:
> The open() function returns -1 on error. openat() and open() initialize
> 'from' and 'to', and only 'from' validated with 'if' statement. If the
> initialization of variable 'to' fails, we should better check the value
> of 'to' and close 'from' to avoid possible file leak. Improve the checking
> of 'from' additionally.
>
> Fixes: 32ae976ed3b5 ("selftests/capabilities: Add tests for capability evolution")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v2:
> - modified the patch according to suggestions;
> - found by customized static analysis tool.
Care to give more details on this customized static analysis
tool?
> ---
> tools/testing/selftests/capabilities/test_execve.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/capabilities/test_execve.c b/tools/testing/selftests/capabilities/test_execve.c
> index 47bad7ddc5bc..6406ab6aa1f5 100644
> --- a/tools/testing/selftests/capabilities/test_execve.c
> +++ b/tools/testing/selftests/capabilities/test_execve.c
> @@ -145,10 +145,14 @@ static void chdir_to_tmpfs(void)
> static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
> {
> int from = openat(fromfd, fromname, O_RDONLY);
> - if (from == -1)
> + if (from < 0)
> ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));
>
> int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
> + if (to < 0) {
> + close(from);
> + ksft_exit_fail_msg("open copy destination - %s\n", strerror(errno));
> + }
>
> while (true) {
> char buf[4096];
thanks,
-- Shuah
prev parent reply other threads:[~2024-06-27 17:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-27 1:57 Ma Ke
2024-06-27 6:50 ` Markus Elfring
2024-06-27 17:29 ` Shuah Khan [this message]
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=8b040e83-5eda-4b59-96a9-d76cb4f80d70@linuxfoundation.org \
--to=skhan@linuxfoundation.org \
--cc=akpm@linux-foundation.org \
--cc=amer.shanawany@gmail.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=luto@kernel.org \
--cc=make24@iscas.ac.cn \
--cc=shuah@kernel.org \
--cc=swarupkotikalapudi@gmail.com \
--cc=usama.anjum@collabora.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
all inboxes | Powered by JetHome®