Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Reinette Chatre <reinette.chatre@intel.com>
Cc: fenghua.yu@intel.com, shuah@kernel.org, tony.luck@intel.com,
	peternewman@google.com, babu.moger@amd.com,
	"Maciej Wieczór-Retman" <maciej.wieczor-retman@intel.com>,
	linux-kselftest@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH V3 05/15] selftests/resctrl: Protect against array overflow when reading strings
Date: Fri, 18 Oct 2024 11:53:21 +0300 (EEST)	[thread overview]
Message-ID: <70d4206c-3a5f-e699-0608-f70751f124eb@linux.intel.com> (raw)
In-Reply-To: <4adf01b3ee7019163ea4fc00b5d03d514d41b4b7.1729218182.git.reinette.chatre@intel.com>

[-- Attachment #1: Type: text/plain, Size: 2986 bytes --]

On Thu, 17 Oct 2024, Reinette Chatre wrote:

> resctrl selftests discover system properties via a variety of sysfs files.
> The MBM and MBA tests need to discover the event and umask with which to
> configure the performance event used to measure read memory bandwidth.
> This is done by parsing the contents of
> /sys/bus/event_source/devices/uncore_imc_<imc instance>/events/cas_count_read
> Similarly, the resctrl selftests discover the cache size via
> /sys/bus/cpu/devices/cpu<id>/cache/index<index>/size.
> 
> Take care to do bounds checking when using fscanf() to read the
> contents of files into a string buffer because by default fscanf() assumes
> arbitrarily long strings. If the file contains more bytes than the array
> can accommodate then an overflow will occur.
> 
> Provide a maximum field width to the conversion specifier to protect
> against array overflow. The maximum is one less than the array size because
> string input stores a terminating null byte that is not covered by the
> maximum field width.
> 
> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
> ---
> This makes the code robust against any changes in information read
> from sysfs. The existing sysfs content fit well into the arrays, thus
> this is not considered a bugfix.
> 
> Changes since V2:
> - New patch
> ---
>  tools/testing/selftests/resctrl/resctrl_val.c | 4 ++--
>  tools/testing/selftests/resctrl/resctrlfs.c   | 2 +-
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/resctrl/resctrl_val.c b/tools/testing/selftests/resctrl/resctrl_val.c
> index e88d5ca30517..c9dd70ce3ea8 100644
> --- a/tools/testing/selftests/resctrl/resctrl_val.c
> +++ b/tools/testing/selftests/resctrl/resctrl_val.c
> @@ -159,7 +159,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
>  
>  		return -1;
>  	}
> -	if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
> +	if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
>  		ksft_perror("Could not get iMC cas count read");
>  		fclose(fp);
>  
> @@ -177,7 +177,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
>  
>  		return -1;
>  	}
> -	if  (fscanf(fp, "%s", cas_count_cfg) <= 0) {
> +	if  (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
>  		ksft_perror("Could not get iMC cas count write");
>  		fclose(fp);
>  
> diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c
> index 250c320349a7..a53cd1cb6e0c 100644
> --- a/tools/testing/selftests/resctrl/resctrlfs.c
> +++ b/tools/testing/selftests/resctrl/resctrlfs.c
> @@ -182,7 +182,7 @@ int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size
>  
>  		return -1;
>  	}
> -	if (fscanf(fp, "%s", cache_str) <= 0) {
> +	if (fscanf(fp, "%63s", cache_str) <= 0) {
>  		ksft_perror("Could not get cache_size");
>  		fclose(fp);
>  
> 

Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

-- 
 i.

  reply	other threads:[~2024-10-18  8:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-18  2:33 [PATCH V3 00/15] selftests/resctrl: Support diverse platforms with MBM and MBA tests Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 01/15] selftests/resctrl: Make functions only used in same file static Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 02/15] selftests/resctrl: Print accurate buffer size as part of MBM results Reinette Chatre
2024-10-18  8:46   ` Ilpo Järvinen
2024-10-18 17:34     ` Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 03/15] selftests/resctrl: Fix memory overflow due to unhandled wraparound Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 04/15] selftests/resctrl: Protect against array overrun during iMC config parsing Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 05/15] selftests/resctrl: Protect against array overflow when reading strings Reinette Chatre
2024-10-18  8:53   ` Ilpo Järvinen [this message]
2024-10-18  2:33 ` [PATCH V3 06/15] selftests/resctrl: Make wraparound handling obvious Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 07/15] selftests/resctrl: Remove "once" parameter required to be false Reinette Chatre
2024-10-18  8:54   ` Ilpo Järvinen
2024-10-18  2:33 ` [PATCH V3 08/15] selftests/resctrl: Only support measured read operation Reinette Chatre
2024-10-18  8:55   ` Ilpo Järvinen
2024-10-18  2:33 ` [PATCH V3 09/15] selftests/resctrl: Remove unused measurement code Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 10/15] selftests/resctrl: Make benchmark parameter passing robust Reinette Chatre
2024-10-18  9:03   ` Ilpo Järvinen
2024-10-18 17:34     ` Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 11/15] selftests/resctrl: Ensure measurements skip initialization of default benchmark Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 12/15] selftests/resctrl: Use cache size to determine "fill_buf" buffer size Reinette Chatre
2024-10-18  9:06   ` Ilpo Järvinen
2024-10-18  2:33 ` [PATCH V3 13/15] selftests/resctrl: Do not compare performance counters and resctrl at low bandwidth Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 14/15] selftests/resctrl: Keep results from first test run Reinette Chatre
2024-10-18  2:33 ` [PATCH V3 15/15] selftests/resctrl: Replace magic constants used as array size Reinette Chatre
2024-10-18  9:08   ` Ilpo Järvinen

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=70d4206c-3a5f-e699-0608-f70751f124eb@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=babu.moger@amd.com \
    --cc=fenghua.yu@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=peternewman@google.com \
    --cc=reinette.chatre@intel.com \
    --cc=shuah@kernel.org \
    --cc=tony.luck@intel.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