From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8B661F9A88; Sun, 24 Nov 2024 12:54:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732452859; cv=none; b=P42BQirjJhn+Iqfw0GJMrNbyw7z/ohmf46oykSsqg9v9npD6YkNr/5LpRC2QbhIhRRNYcHf84ERmG6+wtvLKDI31dDrQL1KugB16hO9AOqL7TaT9/NZRySSfaKiOduvnGkX/MuRnUyFnW8ZVviLr3yt127vLnmEJ4g0GwmBf+uQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732452859; c=relaxed/simple; bh=CqsyVe2JLibohiygczxoBNTt0Sl7Xzr6LMc0NBlGukU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TPNXC3Op2jhPDodUxSu27wQzKvvraxYhqyp5JUsEeWw4zA9Addj8QwUhsnrKHOChYZMaUEG5uFCyYq/BZ4vNlgWVd/koJ32wpuvmpW3xZoH+xj2rrNFxlXtjk7ckUpBY6CcJY9oqe8O9vYoS6mVCr8y0X0bU1z9+/tntPDLMQPU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pzyVNeyM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pzyVNeyM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C16BC4CED1; Sun, 24 Nov 2024 12:54:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732452859; bh=CqsyVe2JLibohiygczxoBNTt0Sl7Xzr6LMc0NBlGukU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pzyVNeyMDSpMByBvmH5JoZjsHvFTmRSuY7fN6gJSzx6Jq7s+HGGVtKh+XyYBV4LXd 6hYQkx+9yh+PQt/O8W2CwLYU2510cogLcXwF35y3DyUrYGzy4mwRela1U2JNfrmLt3 3l80cUnoTC/qJXEnxNf7gEHGOWWWyY43EEZWo9GqN2OFufmw7uMBtte+njc8yUHnPc CvZj8+mgmR7vUCOHJRm+XO6gVcTmluuhT6x7A/zBuD5pMQlRAoF2Cnk2yDRQapiA/I GASEgpqnozqE3/+g4N1oFVgVyOewOAzTAON+NUrLSjuKFjSJJkMDDYS8nLd6dzRxnB t/KixsDHC/nrQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Reinette Chatre , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Shuah Khan , Sasha Levin , 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 Message-ID: <20241124125311.3340223-14-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241124125311.3340223-1-sashal@kernel.org> References: <20241124125311.3340223-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.63 Content-Transfer-Encoding: 8bit From: Reinette Chatre [ 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_/events/cas_count_read Similarly, the resctrl selftests discover the cache size via /sys/bus/cpu/devices/cpu/cache/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 Reviewed-by: Ilpo Järvinen Signed-off-by: Shuah Khan Signed-off-by: Sasha Levin --- 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