public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Shuah Khan <skhan@linuxfoundation.org>
To: Ma Ke <make24@iscas.ac.cn>,
	shuah@kernel.org, usama.anjum@collabora.com,
	amer.shanawany@gmail.com, swarupkotikalapudi@gmail.com,
	kees@kernel.org, luto@kernel.org, akpm@linux-foundation.org
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH] selftests/capabilities: Fix possible file leak in copy_fromat_to
Date: Wed, 26 Jun 2024 13:37:06 -0600	[thread overview]
Message-ID: <67828a44-9730-45fd-bc4e-73075cd3dc4e@linuxfoundation.org> (raw)
In-Reply-To: <524e2d98-f75f-4004-bd3b-39b64864bc3c@linuxfoundation.org>

On 6/26/24 13:33, Shuah Khan wrote:
> On 6/26/24 01:20, 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.
>>
>> Fixes: 32ae976ed3b5 ("selftests/capabilities: Add tests for capability evolution")
>> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
>> ---
>> Found this error through static analysis.
> 
> Please add this line to change adding details about the tool you used
> to find this problem.
> 
>> ---
>>   tools/testing/selftests/capabilities/test_execve.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/tools/testing/selftests/capabilities/test_execve.c b/tools/testing/selftests/capabilities/test_execve.c
>> index 47bad7ddc5bc..de187eff177d 100644
>> --- a/tools/testing/selftests/capabilities/test_execve.c
>> +++ b/tools/testing/selftests/capabilities/test_execve.c
>> @@ -149,6 +149,10 @@ static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
>>           ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));
>>       int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
>> +    if (to == -1) {
> 
> Changing this check to < instead of checking for just -1 can catch
> other error returns.

While you are fining this, can you fix this as well:

  int from = openat(fromfd, fromname, O_RDONLY);
         if (from == -1)
                 ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));

Same change to check for <

thanks,
-- Shuah




      reply	other threads:[~2024-06-26 19:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-26  7:20 [PATCH] selftests/capabilities: Fix possible file leak in copy_fromat_to Ma Ke
2024-06-26 13:16 ` Markus Elfring
2024-06-26 19:33 ` Shuah Khan
2024-06-26 19:37   ` 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=67828a44-9730-45fd-bc4e-73075cd3dc4e@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