Skip to contentSkip to conversation

eeprom: at25: Restore missing allocation

PATCH Superseded Kees Cook 2 participants 3 messages Jan 7, 2022 – Jan 12, 2022 Char and misc drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 eeprom: at25: Restore missing allocation

Building under CONFIG_FORTIFY_SOURCE=y and -Warray-bounds complained about strncpy() being used against an empty object. It turns out this was due to the at25 allocation going missing during a conflict resolution. Restore this, and while we're here take the opportunity to do another strncpy() replacement, since it's use is deprecated[1].

Seen as:

In function 'strncpy',

    inlined from 'at25_fw_to_chip.constprop' at drivers/misc/eeprom/at25.c:312:2:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
In function 'strncpy',
    inlined from 'at25_fram_to_chip' at drivers/misc/eeprom/at25.c:373:2,
    inlined from 'at25_probe' at drivers/misc/eeprom/at25.c:453:10:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

Fixes af40d16042d6 ("Merge v5.15-rc5 into char-misc-next")
Cc Greg Kroah-Hartman <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Jiri Prchal <[email protected]>
---
 drivers/misc/eeprom/at25.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/misc/eeprom/at25.c +6 -2
index c3305bdda69c..1a19fa5728c8 100644
@@ -309,7 +309,7 @@ static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
 	u32 val;
 	int err;
 
-	strncpy(chip->name, "at25", sizeof(chip->name));
+	strscpy(chip->name, "at25", sizeof(chip->name));
 
 	err = device_property_read_u32(dev, "size", &val);
 	if (err)
@@ -370,7 +370,7 @@ static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
 	u8 id[FM25_ID_LEN];
 	int i;
 
-	strncpy(chip->name, "fm25", sizeof(chip->name));
+	strscpy(chip->name, "fm25", sizeof(chip->name));
 
 	/* Get ID of chip */
 	fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
@@ -440,6 +440,10 @@ static int at25_probe(struct spi_device *spi)
 		return -ENXIO;
 	}
 
+	at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);
+	if (!at25)
+		return -ENOMEM;
+
 	mutex_init(&at25->lock);
 	at25->spi = spi;
 	spi_set_drvdata(spi, at25);
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Kees Cook

Building under CONFIG_FORTIFY_SOURCE=y and -Warray-bounds complained about strncpy() being used against an empty object. It turns out this was due to the at25 allocation going missing during a conflict resolution. Restore this, and while we're here take the opportunity to do another strncpy() replacement, since it's use is deprecated[1].

Seen as:

In function 'strncpy',

    inlined from 'at25_fw_to_chip.constprop' at drivers/misc/eeprom/at25.c:312:2:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
In function 'strncpy',
    inlined from 'at25_fram_to_chip' at drivers/misc/eeprom/at25.c:373:2,
    inlined from 'at25_probe' at drivers/misc/eeprom/at25.c:453:10:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
Quoted 1 line

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

Thanks!
With or without the below comment being addressed

Quoted 33 lines
Fixes af40d16042d6 ("Merge v5.15-rc5 into char-misc-next")
Cc Greg Kroah-Hartman <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Jiri Prchal <[email protected]>
---
 drivers/misc/eeprom/at25.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/misc/eeprom/at25.c +2 -2
index c3305bdda69c..1a19fa5728c8 100644
@@ -309,7 +309,7 @@ static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
        u32 val;
        int err;
 
-       strncpy(chip->name, "at25", sizeof(chip->name));
+       strscpy(chip->name, "at25", sizeof(chip->name));
 
        err = device_property_read_u32(dev, "size", &val);
        if (err)
@@ -370,7 +370,7 @@ static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
        u8 id[FM25_ID_LEN];
        int i;
 
-       strncpy(chip->name, "fm25", sizeof(chip->name));
+       strscpy(chip->name, "fm25", sizeof(chip->name));
 
        /* Get ID of chip */
        fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);

drivers/misc/eeprom/at25.c static int at25_probe(struct spi_device *spi) · line 443

Quoted 4 lines
drivers/misc/eeprom/at25.c +1 -0
@@ -440,6 +440,10 @@ static int at25_probe(struct spi_device *spi)
                return -ENXIO;
        }
 
+       at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);

I would use sizeof(*at25) but I think you restored the exact context.

Quoted 9 lines
drivers/misc/eeprom/at25.c +3 -1
+       if (!at25)
+               return -ENOMEM;
+
        mutex_init(&at25->lock);
        at25->spi = spi;
        spi_set_drvdata(spi, at25);
--

2.30.2

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw
Quoted 28 lines · Andy Shevchenko
Quoted 27 lines · Kees Cook

Building under CONFIG_FORTIFY_SOURCE=y and -Warray-bounds complained about strncpy() being used against an empty object. It turns out this was due to the at25 allocation going missing during a conflict resolution. Restore this, and while we're here take the opportunity to do another strncpy() replacement, since it's use is deprecated[1].

Seen as:

In function 'strncpy',

    inlined from 'at25_fw_to_chip.constprop' at drivers/misc/eeprom/at25.c:312:2:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
In function 'strncpy',
    inlined from 'at25_fram_to_chip' at drivers/misc/eeprom/at25.c:373:2,
    inlined from 'at25_probe' at drivers/misc/eeprom/at25.c:453:10:
./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
   48 | #define __underlying_strncpy    __builtin_strncpy
      |                                 ^
./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
   59 |         return __underlying_strncpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~~
Quoted 5 lines
Quoted 1 line

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings

Thanks!
With or without the below comment being addressed

Thanks!

Quoted 37 lines
Quoted 37 lines
Fixes af40d16042d6 ("Merge v5.15-rc5 into char-misc-next")
Cc Greg Kroah-Hartman <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Jiri Prchal <[email protected]>
---
 drivers/misc/eeprom/at25.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/misc/eeprom/at25.c +2 -2
index c3305bdda69c..1a19fa5728c8 100644
@@ -309,7 +309,7 @@ static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
        u32 val;
        int err;
 
-       strncpy(chip->name, "at25", sizeof(chip->name));
+       strscpy(chip->name, "at25", sizeof(chip->name));
 
        err = device_property_read_u32(dev, "size", &val);
        if (err)
@@ -370,7 +370,7 @@ static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
        u8 id[FM25_ID_LEN];
        int i;
 
-       strncpy(chip->name, "fm25", sizeof(chip->name));
+       strscpy(chip->name, "fm25", sizeof(chip->name));
 
        /* Get ID of chip */
        fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
@@ -440,6 +440,10 @@ static int at25_probe(struct spi_device *spi)
                return -ENXIO;
        }
 
Quoted 3 lines

drivers/misc/eeprom/at25.c static int at25_probe(struct spi_device *spi) · line 443

Quoted 1 line
drivers/misc/eeprom/at25.c +1 -0
+       at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);

I would use sizeof(*at25) but I think you restored the exact context.

Yeah, I just restore the chunk exactly as it was.

Greg, should I send a v2 with this adjusted?

-Kees

Quoted 15 lines
Quoted 10 lines
drivers/misc/eeprom/at25.c +3 -1
+       if (!at25)
+               return -ENOMEM;
+
        mutex_init(&at25->lock);
        at25->spi = spi;
        spi_set_drvdata(spi, at25);
--

2.30.2

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 1 line
Kees Cook