All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thorsten Blum <blum@kernel.org>
To: "Daniel Dadap" <ddadap@nvidia.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Thomas Weißschuh" <linux@weissschuh.net>
Cc: Thorsten Blum <blum@kernel.org>, platform-driver-x86@vger.kernel.org
Subject: [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values
Date: Thu,  3 Dec 2026 12:00:52 +0100	[thread overview]
Message-ID: <20261203110051.2078319-2-blum@kernel.org> (raw)

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,

             reply	other threads:[~2026-12-03 11:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:00 Thorsten Blum [this message]
2026-12-03 18:17 ` [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values Daniel Dadap

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203110051.2078319-2-blum@kernel.org \
    --to=blum@kernel.org \
    --cc=ddadap@nvidia.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux@weissschuh.net \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.