* Re: [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values
2026-12-03 11:00 [PATCH] platform/x86: nvidia-wmi-ec-backlight: Bound brightness values Thorsten Blum
@ 2026-12-03 18:17 ` Daniel Dadap
0 siblings, 0 replies; 2+ messages in thread
From: Daniel Dadap @ 2026-12-03 18:17 UTC (permalink / raw)
To: Thorsten Blum, Hans de Goede, Ilpo Järvinen,
Thomas Weißschuh
Cc: platform-driver-x86@vger.kernel.org
Thanks, Thorsten. Since this was found by inspection, I am taking this as
defensive validation against bogus values coming from the firmware, rather
than addressing a concrete problem with a system actually supplying such
values. That makes sense to me.
On Thu, Dec 03, 2026 at 12:00:52PM +0100, Thorsten Blum wrote:
> 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);
Rather than checking after calling wmi_brightness_notify, since these values
are ultimately intended to feed the backlight API, which uses < 0 values for
error reporting, maybe we could centralize the INT_MAX validation inside of
wmi_brightness_notify(), and return -ERANGE if we get unrepresentable values
above INT_MAX from the underlying WMI/ACPI call. This would slightly change
the behavior from the currently proposed patch in the case of a > INT_MAX
value for current brightness, rejecting it as unrepresentable as opposed to
clamping it to max_brightness as if it were a representable but out-of-range
value above the device's reported maximum. Since any such value is probably
bogus in the first place, either behavior sounds reasonable.
This would also reject > INT_MAX values in the WMI_BRIGHTNESS_METHOD_SOURCE
case, where they're also bogus. We don't actually ever call _METHOD_SOURCE
in this driver, since Hans outsourced the _SOURCE_EC check to video.ko a
while back. nvidia_wmi_ec_supported() in video_detect.c explicitly checks
for the specific _SOURCE_EC value, so unexpected values already fail this
check. We could share the WMI helper between the two drivers and add strict
validation of source values we get from the firmware, but the payoff seems
minimal, so I don't think it makes sense to expand the scope of this patch
to cover that.
It seems like the whitespace changed here: I think the error was on my part
with the original code, looks like I had been following the convention of
"hard tab for logical indentation; spaces for visual alignment", since I
didn't realize that the kernel's convention is actually "hard tab for both
indentation AND alignment, spaces to finish aligning after the nearest tab
stop". I can put a separate cleanup change together to fix the rest of the
whitespace in this file (or you can, if you like); sorry about that.
> 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);
> +
Rather than separately clamping here and in the _get_brightness() callback,
it might make sense to extract a helper from _get_brightness() that takes a
struct wmi_device and max_brightness limit, and clamps the value we get back
from wmi_brightness_notify() against the caller-supplied maximum, maybe with
a dev_warn_once() as a diagnostic to inform users that the value got clamped.
Then _get_brightness() passes the struct wmi_device from the private data in
the struct backlight_device, along with bd->props.max_brightness, and for the
pre-registration use case in _probe(), pass wdev and props.max_brightness
directly, since those haven't been bound to the struct backlight_device yet.
> bdev = devm_backlight_device_register(&wdev->dev,
> "nvidia_wmi_ec_backlight",
> &wdev->dev, wdev,
^ permalink raw reply [flat|nested] 2+ messages in thread