Re: [PATCH v2] tools/wmi: dell-smbios-example: fix 1-byte stack buffer overflow in find_token()

From: Pali Rohár

Date: Fri Dec 04 2026 - 20:10:40 EST


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 <haoranwangsec@xxxxxxxxx>

Looks good now,

Reviewed-by: Pali Rohár <pali@xxxxxxxxxx>

> ---
> 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");