public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Colin King (gmail)" <colin.i.king@gmail.com>
To: Shuah Khan <skhan@linuxfoundation.org>,
	Fenghua Yu <fenghua.yu@intel.com>,
	Reinette Chatre <reinette.chatre@intel.com>,
	Shuah Khan <shuah@kernel.org>, Babu Moger <babu.moger@amd.com>,
	Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
	linux-kselftest@vger.kernel.org
Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] selftests/resctrl: Fix null pointer dereference on open failed
Date: Mon, 25 Apr 2022 17:06:19 +0100	[thread overview]
Message-ID: <604ea061-c566-e032-67cf-9bc622aa76ae@gmail.com> (raw)
In-Reply-To: <04a35a47-d83a-67a1-5ed4-ba314c6e1ecf@linuxfoundation.org>

On 25/04/2022 16:51, Shuah Khan wrote:
> On 4/24/22 3:15 PM, Colin Ian King wrote:
>> Currently if opening /dev/null fails to open then file pointer fp
>> is null and further access to fp via fprintf will cause a null
>> pointer dereference. Fix this by returning a negative error value
>> when a null fp is detected.
>>
> 
> How did you find this problem and how can it be reproduced? Is there
> a case where test fails to open "/dev/null"?

Found with static analysis, cppcheck. Open on /dev/null is unlikely to 
fail, but it's good to fail reliably rather than have a SIGSEGV :-)

Colin

> 
>> Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
>> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
>> ---
>>   tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/testing/selftests/resctrl/fill_buf.c 
>> b/tools/testing/selftests/resctrl/fill_buf.c
>> index 51e5cf22632f..56ccbeae0638 100644
>> --- a/tools/testing/selftests/resctrl/fill_buf.c
>> +++ b/tools/testing/selftests/resctrl/fill_buf.c
>> @@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char 
>> *start_ptr, unsigned char *end_ptr,
>>       /* Consume read result so that reading memory is not optimized 
>> out. */
>>       fp = fopen("/dev/null", "w");
>> -    if (!fp)
>> +    if (!fp) {
>>           perror("Unable to write to /dev/null");
>> +        return -1;
>> +    }
>>       fprintf(fp, "Sum: %d ", ret);
>>       fclose(fp);
>>
> 
> thanks,
> -- Shuah


  reply	other threads:[~2022-04-25 16:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-24 21:15 [PATCH] selftests/resctrl: Fix null pointer dereference on open failed Colin Ian King
2022-04-25 15:51 ` Shuah Khan
2022-04-25 16:06   ` Colin King (gmail) [this message]
2022-04-25 16:21     ` Shuah Khan

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=604ea061-c566-e032-67cf-9bc622aa76ae@gmail.com \
    --to=colin.i.king@gmail.com \
    --cc=babu.moger@amd.com \
    --cc=fenghua.yu@intel.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=reinette.chatre@intel.com \
    --cc=sai.praneeth.prakhya@intel.com \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox