Skip to contentSkip to conversation

gpio: virtuser: avoid non-constant format string

PATCH Applied Arnd Bergmann 2 participants 2 messages Jul 19, 2024 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpio: virtuser: avoid non-constant format string

From: Arnd Bergmann <[email protected]>

Using a string variable as an sprintf format is potentially dangerous, and gcc can warn about this:

drivers/gpio/gpio-virtuser.c: In function 'gpio_virtuser_dbgfs_init_line_attrs':
drivers/gpio/gpio-virtuser.c:808:9: error: format not a string literal and no format arguments [-Werror=format-security]

  808 |         sprintf(data->consumer, id);
      |         ^~~~~~~

Change it to a simpler strscpy() instead to just copy it and check the destination buffer size.

Fixes 91581c4b3f29 ("gpio: virtuser: new virtual testing driver for the GPIO API")
---
 drivers/gpio/gpio-virtuser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-virtuser.c +1 -1
index 0e0d55da4f01..ccc47ea0b3e1 100644
@@ -805,7 +805,7 @@ static int gpio_virtuser_dbgfs_init_line_attrs(struct device *dev,
 		return -ENOMEM;
 
 	data->ad.desc = desc;
-	sprintf(data->consumer, id);
+	strscpy(data->consumer, id);
 	atomic_set(&data->irq, 0);
 	atomic_set(&data->irq_count, 0);
 
Signature · 1 line
2.39.2
#1 lore raw

From: Bartosz Golaszewski <[email protected]>

Quoted 9 lines · Arnd Bergmann

Using a string variable as an sprintf format is potentially dangerous, and gcc can warn about this:

drivers/gpio/gpio-virtuser.c: In function 'gpio_virtuser_dbgfs_init_line_attrs':
drivers/gpio/gpio-virtuser.c:808:9: error: format not a string literal and no format arguments [-Werror=format-security]

  808 |         sprintf(data->consumer, id);
      |         ^~~~~~~

[...]

Applied, thanks!

[1/1] gpio: virtuser: avoid non-constant format string

      commit: 3ae08e47742eeebf2190900d31ddac53fdd13a5b

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>