Skip to contentSkip to conversation

hwmon: (lm90) Fix error return value from detect function

PATCH In review Guenter Roeck 3 participants 5 messages Aug 8, 2022 – Aug 10, 2022

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (lm90) Fix error return value from detect function

lm90_detect_nuvoton() is supposed to return NULL if it can not detect a chip, or a pointer to the chip name if it does. Under some circumstances it returns an error pointer instead. Some versions of gcc interpret an ERR_PTR as region of size 0 and generate an error message.

In function ‘__fortify_strlen’,
inlined from ‘strlcpy’ at ./include/linux/fortify-string.h:159:10, inlined from ‘lm90_detect’ at drivers/hwmon/lm90.c:2550:2:
./include/linux/fortify-string.h:50:33: error:
‘__builtin_strlen’ reading 1 or more bytes from a region of size 0
50 | #define __underlying_strlen __builtin_strlen
| ^
./include/linux/fortify-string.h:141:24: note:

      in expansion of macro ‘__underlying_strlen’
    141 |                 return __underlying_strlen(p);
        |                        ^~~~~~~~~~~~~~~~~~~

Returning NULL instead of ERR_PTR() fixes the problem.

Fixes c7cebce984a2 ("hwmon: (lm90) Rework detect function")
Cc Linus Torvalds <[email protected]>
Cc Kees Cook <[email protected]>

---
It is interesting that some versions of gcc interpret an ERR_PTR this way. It did find a real bug, though the error message is quite confusing. Would it be possible to enhance the fortify functions to detect a constant ERR_PTR at compile time ? I think that might be quite useful.

 drivers/hwmon/lm90.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/lm90.c +1 -1
index 03d07da8c2dc..221de01a327a 100644
@@ -2321,7 +2321,7 @@ static const char *lm90_detect_nuvoton(struct i2c_client *client, int chip_id,
 	const char *name = NULL;
 
 	if (config2 < 0)
-		return ERR_PTR(-ENODEV);
+		return NULL;
 
 	if (address == 0x4c && !(config1 & 0x2a) && !(config2 & 0xf8)) {
 		if (chip_id == 0x01 && convrate <= 0x09) {
Signature · 1 line
2.36.2
Tested-by on v1 1/1
#1 lore raw
Quoted 17 lines · Guenter Roeck

lm90_detect_nuvoton() is supposed to return NULL if it can not detect a chip, or a pointer to the chip name if it does. Under some circumstances it returns an error pointer instead. Some versions of gcc interpret an ERR_PTR as region of size 0 and generate an error message.

In function ‘__fortify_strlen’,
inlined from ‘strlcpy’ at ./include/linux/fortify-string.h:159:10, inlined from ‘lm90_detect’ at drivers/hwmon/lm90.c:2550:2:
./include/linux/fortify-string.h:50:33: error:
‘__builtin_strlen’ reading 1 or more bytes from a region of size 0
50 | #define __underlying_strlen __builtin_strlen
| ^
./include/linux/fortify-string.h:141:24: note:

      in expansion of macro ‘__underlying_strlen’
    141 |                 return __underlying_strlen(p);
        |                        ^~~~~~~~~~~~~~~~~~~
Quoted 7 lines

Returning NULL instead of ERR_PTR() fixes the problem.

Fixes c7cebce984a2 ("hwmon: (lm90) Rework detect function")
Cc Linus Torvalds <[email protected]>
Cc Kees Cook <[email protected]>

Thanks,

Ingo

#2 lore raw
In reply to Guenter Roeck
Quoted 24 lines · Guenter Roeck

lm90_detect_nuvoton() is supposed to return NULL if it can not detect a chip, or a pointer to the chip name if it does. Under some circumstances it returns an error pointer instead. Some versions of gcc interpret an ERR_PTR as region of size 0 and generate an error message.

In function ‘__fortify_strlen’,
inlined from ‘strlcpy’ at ./include/linux/fortify-string.h:159:10, inlined from ‘lm90_detect’ at drivers/hwmon/lm90.c:2550:2:
./include/linux/fortify-string.h:50:33: error:
‘__builtin_strlen’ reading 1 or more bytes from a region of size 0
50 | #define __underlying_strlen __builtin_strlen
| ^
./include/linux/fortify-string.h:141:24: note:

      in expansion of macro ‘__underlying_strlen’
    141 |                 return __underlying_strlen(p);
        |                        ^~~~~~~~~~~~~~~~~~~

Returning NULL instead of ERR_PTR() fixes the problem.

Fixes c7cebce984a2 ("hwmon: (lm90) Rework detect function")
Cc Linus Torvalds <[email protected]>
Cc Kees Cook <[email protected]>
Quoted 5 lines

---
It is interesting that some versions of gcc interpret an ERR_PTR this way. It did find a real bug, though the error message is quite confusing. Would it be possible to enhance the fortify functions to detect a constant ERR_PTR at compile time ? I think that might be quite useful.

Yeah, that should be possible. I suspect something like this might work:

	BUILD_BUG_ON(__builtin_constant_p(src) && IS_ERR_VALUE(src));
	BUILD_BUG_ON(__builtin_constant_p(dst) && IS_ERR_VALUE(dst));

Though I'm not sure how it'd play with GCC value range checker.

Signature · 1 line
Kees Cook
Reviewed-by
#3 lore raw
Quoted 18 lines · Kees Cook
Quoted 17 lines · Guenter Roeck

lm90_detect_nuvoton() is supposed to return NULL if it can not detect a chip, or a pointer to the chip name if it does. Under some circumstances it returns an error pointer instead. Some versions of gcc interpret an ERR_PTR as region of size 0 and generate an error message.

In function ‘__fortify_strlen’,
inlined from ‘strlcpy’ at ./include/linux/fortify-string.h:159:10, inlined from ‘lm90_detect’ at drivers/hwmon/lm90.c:2550:2:
./include/linux/fortify-string.h:50:33: error:
‘__builtin_strlen’ reading 1 or more bytes from a region of size 0
50 | #define __underlying_strlen __builtin_strlen
| ^
./include/linux/fortify-string.h:141:24: note:

      in expansion of macro ‘__underlying_strlen’
    141 |                 return __underlying_strlen(p);
        |                        ^~~~~~~~~~~~~~~~~~~
Quoted 6 lines
Quoted 6 lines

Returning NULL instead of ERR_PTR() fixes the problem.

Fixes c7cebce984a2 ("hwmon: (lm90) Rework detect function")
Cc Linus Torvalds <[email protected]>
Cc Kees Cook <[email protected]>
Quoted 7 lines
Quoted 6 lines

---
It is interesting that some versions of gcc interpret an ERR_PTR this way. It did find a real bug, though the error message is quite confusing. Would it be possible to enhance the fortify functions to detect a constant ERR_PTR at compile time ? I think that might be quite useful.

Quoted 6 lines

Yeah, that should be possible. I suspect something like this might work:

	BUILD_BUG_ON(__builtin_constant_p(src) && IS_ERR_VALUE(src));
	BUILD_BUG_ON(__builtin_constant_p(dst) && IS_ERR_VALUE(dst));

Though I'm not sure how it'd play with GCC value range checker.

Yeah, looks like this doesn't work. These are all only able to check for a single value. The GCC diagnostics depend on its internal value range checking. This tripped because of the (sometimes buggy) "void * cast of a literal value is always a NULL pointer dereference, so its size must always be zero" which we've had to repeatedly work around. In this case, it was a real error, though. :P

I'm hoping we can teach future GCC "treat literal casts in range $foo-$bar to be NULL derefs", and we can hand it the ERR_PTR range.

-Kees

Signature · 1 line
Kees Cook
author maintainer
#4 lore raw
Quoted 40 lines · Kees Cook
Quoted 18 lines · Kees Cook
Quoted 17 lines · Guenter Roeck

lm90_detect_nuvoton() is supposed to return NULL if it can not detect a chip, or a pointer to the chip name if it does. Under some circumstances it returns an error pointer instead. Some versions of gcc interpret an ERR_PTR as region of size 0 and generate an error message.

In function ‘__fortify_strlen’,
inlined from ‘strlcpy’ at ./include/linux/fortify-string.h:159:10, inlined from ‘lm90_detect’ at drivers/hwmon/lm90.c:2550:2:
./include/linux/fortify-string.h:50:33: error:
‘__builtin_strlen’ reading 1 or more bytes from a region of size 0
50 | #define __underlying_strlen __builtin_strlen
| ^
./include/linux/fortify-string.h:141:24: note:

      in expansion of macro ‘__underlying_strlen’
    141 |                 return __underlying_strlen(p);
        |                        ^~~~~~~~~~~~~~~~~~~
Quoted 6 lines
Quoted 6 lines

Returning NULL instead of ERR_PTR() fixes the problem.

Fixes c7cebce984a2 ("hwmon: (lm90) Rework detect function")
Cc Linus Torvalds <[email protected]>
Cc Kees Cook <[email protected]>
Quoted 12 lines
Quoted 6 lines

---
It is interesting that some versions of gcc interpret an ERR_PTR this way. It did find a real bug, though the error message is quite confusing. Would it be possible to enhance the fortify functions to detect a constant ERR_PTR at compile time ? I think that might be quite useful.

Yeah, that should be possible. I suspect something like this might work:

	BUILD_BUG_ON(__builtin_constant_p(src) && IS_ERR_VALUE(src));
	BUILD_BUG_ON(__builtin_constant_p(dst) && IS_ERR_VALUE(dst));
Quoted 9 lines
Quoted 1 line

Though I'm not sure how it'd play with GCC value range checker.

Yeah, looks like this doesn't work. These are all only able to check for a single value. The GCC diagnostics depend on its internal value range checking. This tripped because of the (sometimes buggy) "void * cast of a literal value is always a NULL pointer dereference, so its size must always be zero" which we've had to repeatedly work around. In this case, it was a real error, though. :P

Guess it would have been too easy. I wonder if it might be able to come up with a coccinelle script to find such issues. Anyway, thanks for trying and for the review.

Guenter