[PATCH] staging: fbtft: prefer scnprintf over sprintf in fbtft-core.c
Tomasz Unger <[email protected]>
| Newsgroups | org.kernel.vger.linux-fbdev,dev.linux.lists.linux-staging,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
Using sprintf has potential for buffer overflows if the formatted string exceeds the destination buffer size. Replace it with scnprintf, passing sizeof() of the fixed-size stack buffers (text1[50] and text2[50]) so the write is always bounded. In practice an overflow is very unlikely here: text1 only needs room for a size_t value from an SPI TX buffer length, and text2 formats three small integers (bus number, chip select, and frequency in MHz) that always come from real hardware ranges far below the theoretical worst case for their types. This is therefore a defense-in-depth hardening rather than a fix for an observed or easily triggered issue. Signed-off-by: Tomasz Unger <[email protected]> --- Verified with checkpatch.pl - no errors or warnings. Compiled the fbtft module successfully with CONFIG_FB_TFT=m (also required enabling CONFIG_FB=m, CONFIG_SPI=y and CONFIG_GPIOLIB=y, all previously disabled). fb.ko, syscopyarea.ko, sysimgblt.ko, sysfillrect.ko, fb_sys_fops.ko and fbtft.ko all load without errors in a QEMU environment (verified via insmod and lsmod). This confirms the modules load cleanly but does not exercise the changed code path, which would require an actual SPI-connected TFT display. --- drivers/staging/fbtft/fbtft-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/staging/fbtft/fbtft-core.c b/drivers/staging/fbtft/fbtft-core.c index ca0c38221c16..95c92ad55e98 100644 --- a/drivers/staging/fbtft/fbtft-core.c +++ b/drivers/staging/fbtft/fbtft-core.c @@ -784,10 +784,10 @@ int fbtft_register_framebuffer(struct fb_info *fb_info) fbtft_sysfs_init(par); if (par->txbuf.buf && par->txbuf.len >= 1024) - sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10); + scnprintf(text1, sizeof(text1), ", %zu KiB buffer memory", par->txbuf.len >> 10); if (spi) - sprintf(text2, ", spi%d.%d at %d MHz", spi->controller->bus_num, - spi_get_chipselect(spi, 0), spi->max_speed_hz / 1000000); + scnprintf(text2, sizeof(text2), ", spi%d.%d at %d MHz", spi->controller->bus_num, + spi_get_chipselect(spi, 0), spi->max_speed_hz / 1000000); fb_dbg(fb_info, "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n", fb_info->fix.id, fb_info->var.xres, fb_info->var.yres, --- base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f change-id: 20260819-fbtft-core-scnprintf-1fcf0426c834 Best regards, -- Tomasz Unger <[email protected]>