Skip to contentSkip to conversation

hwmon: Add "label" attribute

bug report New Dan Carpenter 3 participants 6 messages Jan 27, 2022 – Jan 31, 2022 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

Hello Paul Cercueil,

This is a semi-automatic email about new static checker warnings.

The patch 073c3ea6c530: "hwmon: Add "label" attribute" from Jan 10, 2022, leads to the following Smatch complaint:

drivers/hwmon/hwmon.c:825 __hwmon_device_register()
warn: variable dereferenced before check 'dev' (see line 810)

drivers/hwmon/hwmon.c
809

   810		if (device_property_present(dev, "label")) {
                                            ^^^

The patch adds a new unchecked dereference

811 err = device_property_read_string(dev, "label", &label);
812 if (err < 0)
813 goto free_hwmon;
814
815 hwdev->label = kstrdup(label, GFP_KERNEL);
816 if (hwdev->label == NULL) {
817 err = -ENOMEM;
818 goto free_hwmon;
819 }
820 }
821
822 hwdev->name = name;
823 hdev->class = &hwmon_class;
824 hdev->parent = dev;

   825		hdev->of_node = dev ? dev->of_node : NULL;
                                ^^^

Existing code checked for NULL

   826		hwdev->chip = chip;
   827		dev_set_drvdata(hdev, drvdata);

regards,
dan carpenter

#1 lore raw

Hi,

Quoted 7 lines · Dan Carpenter

Hello Paul Cercueil,

This is a semi-automatic email about new static checker warnings.

The patch 073c3ea6c530: "hwmon: Add "label" attribute" from Jan 10, 2022, leads to the following Smatch complaint:

Quoted 8 lines

drivers/hwmon/hwmon.c:825 __hwmon_device_register()
warn: variable dereferenced before check 'dev' (see line 810)

drivers/hwmon/hwmon.c
809

   810		if (device_property_present(dev, "label")) {
                                            ^^^

The patch adds a new unchecked dereference

I will send a patch to address that.

I'm surprised that this function can be called with dev == NULL in the first place, though.

Cheers,
-Paul

Quoted 23 lines
   811			err = device_property_read_string(dev, "label", &label);
   812			if (err < 0)
   813				goto free_hwmon;
   814
   815			hwdev->label = kstrdup(label, GFP_KERNEL);
   816			if (hwdev->label == NULL) {
   817				err = -ENOMEM;
   818				goto free_hwmon;
   819			}
   820		}
   821
   822		hwdev->name = name;
   823		hdev->class = &hwmon_class;
   824		hdev->parent = dev;
   825		hdev->of_node = dev ? dev->of_node : NULL;
                                ^^^

Existing code checked for NULL

   826		hwdev->chip = chip;
   827		dev_set_drvdata(hdev, drvdata);

regards,
dan carpenter

maintainer
#2 lore raw
Quoted 19 lines · Paul Cercueil

Hi,

Quoted 7 lines · Dan Carpenter

Hello Paul Cercueil,

This is a semi-automatic email about new static checker warnings.

The patch 073c3ea6c530: "hwmon: Add "label" attribute" from Jan 10, 2022, leads to the following Smatch complaint:

Quoted 8 lines

    drivers/hwmon/hwmon.c:825 __hwmon_device_register()
    warn: variable dereferenced before check 'dev' (see line 810)

drivers/hwmon/hwmon.c
   809
   810        if (device_property_present(dev, "label")) { ^^^ The patch adds a new unchecked dereference

Quoted 4 lines

I will send a patch to address that.

I'm surprised that this function can be called with dev == NULL in the first place, though.

Originally it was needed for the thermal subsystem, which did not provide a parent device. By the time that was reworked, it was (mis-)used by the Loongson-3 hwmon driver (which was never reviewed by a hwmon maintainer and does pretty much everything wrong).

Guenter

#3 lore raw

Hi Guenter,

Quoted 22 lines · Guenter Roeck
Quoted 21 lines · Paul Cercueil

Hi,

Quoted 7 lines · Dan Carpenter

Hello Paul Cercueil,

This is a semi-automatic email about new static checker warnings.

The patch 073c3ea6c530: "hwmon: Add "label" attribute" from Jan 10, 2022, leads to the following Smatch complaint:

Quoted 8 lines

drivers/hwmon/hwmon.c:825 __hwmon_device_register()
warn: variable dereferenced before check 'dev' (see line 810)

drivers/hwmon/hwmon.c
809

   810        if (device_property_present(dev, "label")) {
                                            ^^^

The patch adds a new unchecked dereference

I will send a patch to address that.

Quoted 7 lines
Quoted 2 lines

I'm surprised that this function can be called with dev == NULL in the first place, though.

Originally it was needed for the thermal subsystem, which did not provide a parent device. By the time that was reworked, it was (mis-)used by the Loongson-3 hwmon driver (which was never reviewed by a hwmon maintainer and does pretty much everything wrong).

Where is that Loongson-3 hwmon driver? I can't find it anywhere.

Maybe we can change that now?

-Paul

maintainer
#4 lore raw
Quoted 28 lines · Paul Cercueil

Hi Guenter,

Quoted 25 lines · Guenter Roeck
Quoted 23 lines · Paul Cercueil

Hi,

Quoted 7 lines · Dan Carpenter

Hello Paul Cercueil,

This is a semi-automatic email about new static checker warnings.

The patch 073c3ea6c530: "hwmon: Add "label" attribute" from Jan 10, 2022, leads to the following Smatch complaint:

Quoted 8 lines

    drivers/hwmon/hwmon.c:825 __hwmon_device_register()
    warn: variable dereferenced before check 'dev' (see line 810)

drivers/hwmon/hwmon.c
   809
   810        if (device_property_present(dev, "label")) { ^^^ The patch adds a new unchecked dereference

I will send a patch to address that.

I'm surprised that this function can be called with dev == NULL in the first place, though.

Quoted 7 lines
Quoted 4 lines

Originally it was needed for the thermal subsystem, which did not provide a parent device. By the time that was reworked, it was (mis-)used by the Loongson-3 hwmon driver (which was never reviewed by a hwmon maintainer and does pretty much everything wrong).

Where is that Loongson-3 hwmon driver? I can't find it anywhere.

drivers/platform/mips/cpu_hwmon.c

Quoted 2 lines

Maybe we can change that now?

It should be a platform driver, it should only instantiate on hardware supporting it, it should leave the name attribute alone, it should not generate its sysfs attributes but use hwmon_channel_info / hwmon_chip_info / hwmon_ops, and it should use the is_visible callback in struct hwmon_ops lm90_ops to determine if attributes are visible. This is just the problems I noticed after a few minutes of looking into the code; there may be more. This would be a lot of work, with no means to test the result.

It might make more sense to add a warning to the hwmon core if dev is NULL. We should also have a warning in hwmon_device_register_with_info() if the struct hwmon_chip_info pointer is NULL (the API should really not be used in that case), but that would require changing the thermal code to use with_groups(). Even that would be less than perfect since it still lets people abuse the with_groups API (calling hwmon_device_register_with_groups with NULL groups pointer does not really make sense either). Given the nature of the thermal code, I don't know if it would even be possible to fix that.

Guenter

#5 lore raw

PATCH 1/1 hwmon: Fix possible NULL pointer

In reply to Dan Carpenter

The recent addition of the label attribute added some code that read the "label" device property, without checking first that "dev" was non-NULL.

Fix this issue by first checking that "dev" is non-NULL.

Fixes ccd98cba6a18 ("hwmon: Add "label" attribute")
---
 drivers/hwmon/hwmon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/hwmon.c +1 -1
index e36ea82da147..5915fedee69b 100644
@@ -807,7 +807,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 		hdev->groups = groups;
 	}
 
-	if (device_property_present(dev, "label")) {
+	if (dev && device_property_present(dev, "label")) {
 		err = device_property_read_string(dev, "label", &label);
 		if (err < 0)
 			goto free_hwmon;
Signature · 1 line
2.34.1