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=1796433036; b=2fNR0n8ZXF7rP4m7ah79Oe8WG5tpktXt12UAl08IJw/7EufCyKs8b98+0sdQPZvS8Why 6zA7YhyZ0C4J7ch7ldI/KjaV1Gi5wRLi4CEmLxVEcuOyT3UspLzBHurR94Z6H5VmDxGRD HaUmcrvTKaZ5jeiy0ZTRlkz58d6p4FbfPgQN2Vh88/aLXMWf0A+Qu6eMU2xoIEeGD0Zqe chS/qYaxJHTx6ovjMHBNmMdhXliIzDyNtk6h0OvPHLocB7fLHhyBF29KD6Q+2p4J2Wjba VEnnw4r+hoXLXDgANhSrpJnPPXoxePH43uY3G8xDuc4YW89HimUaMLuGvapkGIe86VA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796433036; h=Received:DKIM-Signature:Received:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To:User-Agent; bh=qVQ1DBlCPGEi6MWEqU3l1yvO1/T4THhkdO+LKkPdMN4=; b=g8c/gu5ZvCZ1kWNU7na7O+didDVrjtQQ0mZ3gj+l5sAnRZcGSapeBlQMTMPjfqcr8KX+ B1NzqzpBfP7Gbm08/3JHVfFwDdsISBhiHFtr34gLN67VzjrBSW0IycoMZ+NQX6m7a8GA9 VgXyfXP/RKt15abCbrlRNlEv1+yeB1MiqpoeVUNhCAM7JqtyzZlUdrNA+uzYZ3hmD97PL drJeUwlf7/GyI2bzBvm32Lsj1+LYtC96kXRRmwHQ1PP9qGI/yd6s/nPR5ZO9Rbef6Djbg QV3ejFhyzKgOpo6rB1mlmYx1sYVtypD+skLZp/8RDvPKrdhiX62R5IcXUmyhkP1Flpg== 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=lfDMZZmX; 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 3B458606A322; Sat, 5 Dec 2026 01:10:35 +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=lfDMZZmX Received: by smtp.kernel.org (Postfix) with ESMTPSA id 761B21F0092B; Sat, 5 Dec 2026 01:10:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796433035; bh=qVQ1DBlCPGEi6MWEqU3l1yvO1/T4THhkdO+LKkPdMN4=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=lfDMZZmXTEVDODAMTZwj9OyJ/DlEazGlAKcNiNy+2h/ONiXT8zOmp4c84t2ufQ22V DOtJbe1mVtvFzoPuFkWi5rz8P056isaHIdTwkEVyYLm/6XcMR10Weyi1jleS8EJFs+ zU5PV894AdOEYporxAas/RH3/GoKRW3fAvczUy/qMsUFOnP1p9Gx4cRgGaAlO2O47G fup8LZ8slq9UVhqj/zMXki+Rs/qRwEREhADa0NiJZAG+Z5Z5SDx8i7zDqjQYTKpNyO YYMVx5mQ8JURJOkEj2aVZF2OK7rj2Ljn2P7ZXuJdxkui+9PomDVmh0SkrOcKjTmy4q xmB1i3GhfB+Pg== Received: by pali.im (Postfix) id 6CE056DF; Sat, 5 Dec 2026 02:10:32 +0100 (CET) Date: Sat, 5 Dec 2026 02:10:32 +0100 From: Pali =?utf-8?B?Um9ow6Fy?= To: Haoran Wang Cc: Mario Limonciello , Dell.Client.Kernel@dell.com, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] tools/wmi: dell-smbios-example: fix 1-byte stack buffer overflow in find_token() Message-ID: <20261205011032.eqigh3kjghmlgopf@pali> References: <179628536137.12336.17075012915006817243@gmail.com> 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 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <179628536137.12336.17075012915006817243@gmail.com> User-Agent: NeoMutt/20180716 On Thursday 03 December 2026 16:09:21 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 Looks good now, Reviewed-by: Pali Rohár > --- > 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");