From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: "Reinette Chatre" <reinette.chatre@intel.com>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Sasha Levin" <sashal@kernel.org>,
fenghua.yu@intel.com, shuah@kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH AUTOSEL 6.6 14/16] selftests/resctrl: Protect against array overflow when reading strings
Date: Sun, 24 Nov 2024 07:52:32 -0500 [thread overview]
Message-ID: <20241124125311.3340223-14-sashal@kernel.org> (raw)
In-Reply-To: <20241124125311.3340223-1-sashal@kernel.org>
From: Reinette Chatre <reinette.chatre@intel.com>
[ Upstream commit 46058430fc5d39c114f7e1b9c6ff14c9f41bd531 ]
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>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
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 45439e726e79c..438dd86f2704c 100644
--- a/tools/testing/selftests/resctrl/resctrl_val.c
+++ b/tools/testing/selftests/resctrl/resctrl_val.c
@@ -179,7 +179,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);
@@ -197,7 +197,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 71ad2b335b83f..fe3241799841b 100644
--- a/tools/testing/selftests/resctrl/resctrlfs.c
+++ b/tools/testing/selftests/resctrl/resctrlfs.c
@@ -160,7 +160,7 @@ int get_cache_size(int cpu_no, 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);
--
2.43.0
next prev parent reply other threads:[~2024-11-24 12:54 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-24 12:52 [PATCH AUTOSEL 6.6 01/16] spi: spi-fsl-lpspi: Adjust type of scldiv Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 02/16] HID: add per device quirk to force bind to hid-generic Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 03/16] media: v4l: Add luma 16-bit interlaced pixel format Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 04/16] media: uvcvideo: " Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 05/16] media: uvcvideo: RealSense D421 Depth module metadata Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 06/16] media: uvcvideo: Add a quirk for the Kaiweets KTI-W02 infrared camera Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 07/16] media: vb2: use lock if wait_prepare/finish are NULL Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 08/16] media: cx231xx: Add support for Dexatek USB Video Grabber 1d19:6108 Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 09/16] mmc: core: Add SD card quirk for broken poweroff notification Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 10/16] mmc: sdhci-esdhc-imx: enable quirks SDHCI_QUIRK_NO_LED Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 11/16] soc: imx8m: Probe the SoC driver as platform driver Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 12/16] HID: bpf: Fix NKRO on Mistel MD770 Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 13/16] regmap: maple: Provide lockdep (sub)class for maple tree's internal lock Sasha Levin
2024-11-24 12:52 ` Sasha Levin [this message]
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 15/16] USB: gadget: pxa27x_udc: Avoid using GPIOF_ACTIVE_LOW Sasha Levin
2024-11-24 12:52 ` [PATCH AUTOSEL 6.6 16/16] HID: magicmouse: Apple Magic Trackpad 2 USB-C driver support Sasha Levin
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=20241124125311.3340223-14-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=fenghua.yu@intel.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=reinette.chatre@intel.com \
--cc=shuah@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=stable@vger.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox