All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values
@ 2026-12-03 11:00 Thorsten Blum
  2026-12-03 18:17 ` Daniel Dadap
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2026-12-03 11:00 UTC (permalink / raw)
  To: Daniel Dadap, Hans de Goede, Ilpo Järvinen,
	Thomas Weißschuh
  Cc: Thorsten Blum, platform-driver-x86

wmi_brightness_notify() reads both the maximum and current brightness as
u32 directly into the signed int fields of struct backlight_properties.
Values above INT_MAX are interpreted as negative, which could cause the
driver to register a backlight with an invalid brightness range.

Read both values into temporary u32 variables, validate the maximum and
cap the current brightness before assigning them to the signed
max_brightness and brightness fields.

Maximum brightness values above INT_MAX now cause probe to fail with
-EINVAL instead of registering a backlight with a negative maximum
brightness.

In nvidia_wmi_ec_backlight_get_brightness(), also cap the brightness
at the registered maximum, since a u32 above INT_MAX would otherwise be
returned as a negative value.

Fixes: ad62cd93198b ("platform/x86: Add driver for ACPI WMAA EC-based backlight control")
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
Found by code inspection and compile-tested only.

 drivers/platform/x86/nvidia-wmi-ec-backlight.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/platform/x86/nvidia-wmi-ec-backlight.c b/drivers/platform/x86/nvidia-wmi-ec-backlight.c
index b4eebfb96fe5..b1cc0b00fa4b 100644
--- a/drivers/platform/x86/nvidia-wmi-ec-backlight.c
+++ b/drivers/platform/x86/nvidia-wmi-ec-backlight.c
@@ -5,6 +5,8 @@
 
 #include <linux/acpi.h>
 #include <linux/backlight.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/platform_data/x86/nvidia-wmi-ec-backlight.h>
 #include <linux/types.h>
@@ -79,7 +81,7 @@ static int nvidia_wmi_ec_backlight_get_brightness(struct backlight_device *bd)
 	if (ret < 0)
 		return ret;
 
-	return level;
+	return min_t(u32, level, bd->props.max_brightness);
 }
 
 static const struct backlight_ops nvidia_wmi_ec_backlight_ops = {
@@ -91,6 +93,8 @@ static int nvidia_wmi_ec_backlight_probe(struct wmi_device *wdev, const void *ct
 {
 	struct backlight_properties props = {};
 	struct backlight_device *bdev;
+	u32 max_brightness;
+	u32 brightness;
 	int ret;
 
 	/* drivers/acpi/video_detect.c also checks that SOURCE == EC */
@@ -104,16 +108,22 @@ static int nvidia_wmi_ec_backlight_probe(struct wmi_device *wdev, const void *ct
 	props.type = BACKLIGHT_FIRMWARE;
 
 	ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL,
-	                           WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL,
-	                           &props.max_brightness);
+				    WMI_BRIGHTNESS_MODE_GET_MAX_LEVEL, &max_brightness);
 	if (ret)
 		return ret;
 
+	if (max_brightness > INT_MAX)
+		return dev_err_probe(&wdev->dev, -EINVAL, "Invalid maximum brightness: %u\n",
+				     max_brightness);
+
 	ret = wmi_brightness_notify(wdev, WMI_BRIGHTNESS_METHOD_LEVEL,
-	                           WMI_BRIGHTNESS_MODE_GET, &props.brightness);
+				    WMI_BRIGHTNESS_MODE_GET, &brightness);
 	if (ret)
 		return ret;
 
+	props.max_brightness = max_brightness;
+	props.brightness = min(brightness, max_brightness);
+
 	bdev = devm_backlight_device_register(&wdev->dev,
 	                                      "nvidia_wmi_ec_backlight",
 					      &wdev->dev, wdev,

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 11:00 [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values Thorsten Blum
2026-12-03 18:17 ` Daniel Dadap

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.