From: Haoran Wang <haoranwangsec@gmail.com>
To: Pali Rohár <pali@kernel.org>
Cc: Mario Limonciello <mario.limonciello@amd.com>,
Dell.Client.Kernel@dell.com, platform-driver-x86@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] tools/wmi: dell-smbios-example: fix 1-byte stack buffer overflow in find_token()
Date: Thu, 03 Dec 2026 16:09:21 +0800 [thread overview]
Message-ID: <179628536137.12336.17075012915006817243@gmail.com> (raw)
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 <haoranwangsec@gmail.com>
---
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");
reply other threads:[~2026-12-03 8:09 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=179628536137.12336.17075012915006817243@gmail.com \
--to=haoranwangsec@gmail.com \
--cc=Dell.Client.Kernel@dell.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=pali@kernel.org \
--cc=platform-driver-x86@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
all inboxes | Powered by JetHome®