Skip to contentSkip to conversation

gpio: virtuser: avoid non-constant format string

PATCH Superseded Arnd Bergmann 3 participants 4 messages Jul 19, 2024 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 this instance to use a "%s" format instead to print the string argument.

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..c55b72e426c7 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);
+	sprintf(data->consumer, "%s", id);
 	atomic_set(&data->irq, 0);
 	atomic_set(&data->irq_count, 0);
 
Signature · 1 line
2.39.2
#1 lore raw
Quoted 34 lines · Arnd Bergmann

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 this instance to use a "%s" format instead to print the string argument.

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 -2
index 0e0d55da4f01..c55b72e426c7 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);
+       sprintf(data->consumer, "%s", id);
        atomic_set(&data->irq, 0);
        atomic_set(&data->irq_count, 0);
 
--
Quoted 2 lines

2.39.2

I know this should not happen as the string is checked for length when it is set over configfs but while we're at it: maybe make it 100% correct by using snprintf(data->consumer, sizeof(data->consumer), ... ?

Bart

#2 lore raw
Quoted 13 lines · Bartosz Golaszewski
Quoted 13 lines
drivers/gpio/gpio-virtuser.c +1 -1
index 0e0d55da4f01..c55b72e426c7 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);
+       sprintf(data->consumer, "%s", id);
        atomic_set(&data->irq, 0);
        atomic_set(&data->irq_count, 0);
 
Quoted 7 lines
Quoted 3 lines

--
2.39.2

I know this should not happen as the string is checked for length when it is set over configfs but while we're at it: maybe make it 100% correct by using snprintf(data->consumer, sizeof(data->consumer), ...?

Actually I now think this should just be

     strscpy(data->consumer, id);

There was never a reason to use sprintf() here at all. strscpy() does both the correct size check and avoids treating it as a format string.

Arnd

#3 lore raw
Quoted 25 lines · Arnd Bergmann
Quoted 13 lines · Bartosz Golaszewski
Quoted 13 lines
drivers/gpio/gpio-virtuser.c +1 -1
index 0e0d55da4f01..c55b72e426c7 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);
+       sprintf(data->consumer, "%s", id);
        atomic_set(&data->irq, 0);
        atomic_set(&data->irq_count, 0);
 
Quoted 7 lines
Quoted 3 lines

--
2.39.2

I know this should not happen as the string is checked for length when it is set over configfs but while we're at it: maybe make it 100% correct by using snprintf(data->consumer, sizeof(data->consumer), ...?

Actually I now think this should just be

Quoted 7 lines
     strscpy(data->consumer, id);

There was never a reason to use sprintf() here at all. strscpy() does both the correct size check and avoids treating it as a format string.

Arnd

Even better!

Bart