From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796424309; b=5J5APnWWtm3PvWau8lWYjZ9pC6viwRG8twJYQjQfjfbxN53X1zw0yySF6iWEixgzV2ws Og89khbddoP6Jl5U2lbpgvZxorveCpGfSf1kNDs1RO1GEBq8xIGbUOsfxzyskAfoIqvbx uzJcypGDr4es065nFQlobznuP0CMW/HkhlxQAAu6VVjmNZfUsEkKT31GIIOQ9kPssJg80 6wvUJXHvXKtmEzRGO9MZ/29aOCGtSEOlSXTz3UCiM41EWyM7Cfd6R+hLw+4h8GRMWTP4N bErq+RYZYygg//KDp//XeWVQ64fKDfBYo1KEFYGMOTF2znerbRssUmtUkaPdPau/aKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796424309; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:Content-Language:To:Cc:References:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=rBsw9oUlwM7WHJC4XDcq9ODRiZ80UMCjqqH8LY6k/BM=; b=DvCfnvInaLq1aZjAt7kkBDYldryrsz22evZTH5K5rPHR8YNbpQNyu+M7pwGvRqnS394z uej5cpVtrwuvuMAAg4bS2FU/Uhr8mzoQH0G1fie7t514cPnYRJWG7xkb965JsmL2/J/HS U8YShFdEGNIXqOsIuOSP9S5h2hCvkG2PHyUpC7JUsooY08oVCq7jHxeiDwWekhJh9hgis pbhRo45iRL3YXl/i3r9TNTPLcHWDYYR8TvMwyjIE8XKpiPMr5VuanTYy04AMaBn0A4tFw DZkbp1dgS+53Vc4cMA0EApRKKfPfhWUOQ6eU9JN6TL2BXesa7BkkIEjDzWq5HdYTwWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="TIQ/RPUX"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 795CF606A322; Fri, 4 Dec 2026 22:45:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TIQ/RPUX Received: by smtp.kernel.org (Postfix) with ESMTPSA id B26B91F00936; Fri, 4 Dec 2026 22:45:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796424309; bh=rBsw9oUlwM7WHJC4XDcq9ODRiZ80UMCjqqH8LY6k/BM=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=TIQ/RPUXQSF4OPBalOXutTRzsW12A0HlRDykoHaqEpD2D6gOFazhmixyww+CUR4o1 wR6Q2sRyx8jdlN0l6841tTZZRaK63SPsOk72rx/zWSNLWEtMphrW3kk10TGjUy5esK myzAmaNHvV6EA+m975gz2+e3hXr0DAWds5NSOd1FxCCm0kWtfh0Sdh34dne5JF+fFI JiujzOY/f0p9nwxhdXN4ffUBHCwkdD0mZ7IIBuAS3tQPfoeHLbuKxcE5E2G+1k5Emy H+/gJFwRt4/1iysGi41g4Gz0cJ2e1wXp3OGAqwZigFgeOeTUX8OE9xThFsRvypCVHX ImUjgoumm/aEQ== Message-ID: Date: Fri, 4 Dec 2026 16:45:07 -0600 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] tools/wmi: dell-smbios-example: fix 1-byte stack buffer overflow in find_token() Content-Language: en-US To: Haoran Wang , =?UTF-8?Q?Pali_Roh=C3=A1r?= Cc: Dell.Client.Kernel@dell.com, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org References: <179628536137.12336.17075012915006817243@gmail.com> From: Mario Limonciello In-Reply-To: <179628536137.12336.17075012915006817243@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/3/26 02:09, Haoran Wang wrote: > find_token() builds sysfs paths for Dell SMBIOS token files with sprintf() > into stack buffers sized to the formatted string length but without the > NUL terminator, so sprintf() writes one byte past each buffer. The paths > are built from the compile-time-bounded token_sysfs literal (46 chars) > and a __u16 token formatted via %04x (always four hex digits), so the > formatted strings are 57 and 60 chars plus the NUL, a 1-byte stack OOB > on every invocation, regardless of whether the sysfs file exists. > > Size the buffers from the inputs rather than by magic numbers, using > sizeof(token_sysfs) + sizeof("/XXXX_value") and > sizeof(token_sysfs) + sizeof("/XXXX_location"), so the length is provably > derived from the data. Replace sprintf() with snprintf() bounded by the > destination buffer size and check for truncation. > > Fixes: 9d64fc08f6fe ("tools/wmi: add a sample for dell smbios communication over WMI") > Signed-off-by: Haoran Wang Reviewed-by: Mario Limonciello (AMD) > --- > Changes in v2: > - As suggested by Pali Rohár, derive the buffer sizes from the inputs > with sizeof() instead of hard-coded magic numbers, and use snprintf() > with a truncation check instead of sprintf(). > > tools/wmi/dell-smbios-example.c | 20 +++++++++++--------- > 1 file changed, 11 insertions(+), 9 deletions(-) > > diff --git a/tools/wmi/dell-smbios-example.c b/tools/wmi/dell-smbios-example.c > index 1f3e7ab..ff2d400 100644 > --- a/tools/wmi/dell-smbios-example.c > +++ b/tools/wmi/dell-smbios-example.c > @@ -26,7 +26,7 @@ > * application they're hardcoded > */ > static const char *ioctl_devfs = "/dev/wmi/dell-smbios"; > -static const char *token_sysfs = > +static const char token_sysfs[] = > "/sys/bus/platform/devices/dell-smbios.0/tokens"; > > static void show_buffer(struct dell_wmi_smbios_buffer *buffer) > @@ -52,15 +52,16 @@ static int run_wmi_smbios_cmd(struct dell_wmi_smbios_buffer *buffer) > > static int find_token(__u16 token, __u16 *location, __u16 *value) > { > - char location_sysfs[60]; > - char value_sysfs[57]; > + char location_sysfs[sizeof(token_sysfs) + sizeof("/XXXX_location")]; > + char value_sysfs[sizeof(token_sysfs) + sizeof("/XXXX_value")]; > char buf[4096]; > FILE *f; > int ret; > > - ret = sprintf(value_sysfs, "%s/%04x_value", token_sysfs, token); > - if (ret < 0) { > - printf("sprintf value failed\n"); > + ret = snprintf(value_sysfs, sizeof(value_sysfs), > + "%s/%04x_value", token_sysfs, token); > + if (ret < 0 || ret >= sizeof(value_sysfs)) { > + printf("snprintf value failed\n"); > return 2; > } > f = fopen(value_sysfs, "rb"); > @@ -72,9 +73,10 @@ static int find_token(__u16 token, __u16 *location, __u16 *value) > fclose(f); > *value = (__u16) strtol(buf, NULL, 16); > > - ret = sprintf(location_sysfs, "%s/%04x_location", token_sysfs, token); > - if (ret < 0) { > - printf("sprintf location failed\n"); > + ret = snprintf(location_sysfs, sizeof(location_sysfs), > + "%s/%04x_location", token_sysfs, token); > + if (ret < 0 || ret >= sizeof(location_sysfs)) { > + printf("snprintf location failed\n"); > return 1; > } > f = fopen(location_sysfs, "rb");