Skip to contentSkip to conversation

platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe()

PATCH Superseded Thorsten Blum 2 participants 3 messages Nov 20 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe()

gmux_read32() returns a u32, but the maximum brightness is stored in the signed ->max_brightness field. Large values can thus become negative and bypass the GMUX_MAX_BRIGHTNESS check.

Cast props.max_brightness to u32 for the check so that these values also trigger the warning and are capped at GMUX_MAX_BRIGHTNESS.

Fixes 917ee75a5916 ("platform/x86: Add driver for Apple gmux device")

---
Found by code inspection; not reproduced on hardware.

 drivers/platform/x86/apple-gmux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/apple-gmux.c +1 -1
index 9c728ac4e045..4d3a6d2393f7 100644
@@ -881,7 +881,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 		 * brightness at this value, but print a warning if the hardware
 		 * reports something higher so that it can be fixed.
 		 */
-		if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
+		if (WARN_ON((u32)props.max_brightness > GMUX_MAX_BRIGHTNESS))
 			props.max_brightness = GMUX_MAX_BRIGHTNESS;
 
 		bdev = backlight_device_register("gmux_backlight", &pnp->dev,
maintainer
#1 lore raw
Quoted 20 lines · Thorsten Blum

gmux_read32() returns a u32, but the maximum brightness is stored in the signed ->max_brightness field. Large values can thus become negative and bypass the GMUX_MAX_BRIGHTNESS check.

Cast props.max_brightness to u32 for the check so that these values also trigger the warning and are capped at GMUX_MAX_BRIGHTNESS.

Fixes 917ee75a5916 ("platform/x86: Add driver for Apple gmux device")

---
Found by code inspection; not reproduced on hardware.

 drivers/platform/x86/apple-gmux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/apple-gmux.c +0 -0
index 9c728ac4e045..4d3a6d2393f7 100644

drivers/platform/x86/apple-gmux.c static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) · line 884

Quoted 5 lines
drivers/platform/x86/apple-gmux.c +1 -1
@@ -881,7 +881,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 		 * brightness at this value, but print a warning if the hardware
 		 * reports something higher so that it can be fixed.
 		 */
-		if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
+		if (WARN_ON((u32)props.max_brightness > GMUX_MAX_BRIGHTNESS))

???

This looks very wrong way to fix the problem (using int as proxy to carry it until it's bounds checked).

And why is it using WARN_ON()? Is it an indication of programming error that gmux_read32() returns a too large value??

Signature · 1 line
 i.
#2 lore raw
Quoted 30 lines · Ilpo Järvinen
Quoted 20 lines · Thorsten Blum

gmux_read32() returns a u32, but the maximum brightness is stored in the signed ->max_brightness field. Large values can thus become negative and bypass the GMUX_MAX_BRIGHTNESS check.

Cast props.max_brightness to u32 for the check so that these values also trigger the warning and are capped at GMUX_MAX_BRIGHTNESS.

Fixes 917ee75a5916 ("platform/x86: Add driver for Apple gmux device")

---
Found by code inspection; not reproduced on hardware.

 drivers/platform/x86/apple-gmux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/apple-gmux.c +0 -0
index 9c728ac4e045..4d3a6d2393f7 100644

drivers/platform/x86/apple-gmux.c static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) · line 884

Quoted 5 lines
drivers/platform/x86/apple-gmux.c +1 -1
@@ -881,7 +881,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 		 * brightness at this value, but print a warning if the hardware
 		 * reports something higher so that it can be fixed.
 		 */
-		if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
+		if (WARN_ON((u32)props.max_brightness > GMUX_MAX_BRIGHTNESS))

???

Quoted 2 lines

This looks very wrong way to fix the problem (using int as proxy to carry it until it's bounds checked).

I considered a local u32 variable before assigning it to props.max_brightness, but then chose the smaller change.

Quoted 2 lines

And why is it using WARN_ON()? Is it an indication of programming error that gmux_read32() returns a too large value??

It's a hardware value, so dev_warn() would be better.

I'll send a v2.

Thanks,
Thorsten