platform/x86: hp-wmi: Fix WMI error handling
cover platform/x86: hp-wmi: Fix WMI error handling
Fix several places where hp-wmi either trusts an undersized firmware response or mistakes a positive HP firmware error for success.
The affected paths handle the common WMI response header, tablet-mode switch updates, the OMEN key event, Victus GPU thermal modes and Victus fan-speed control. Each patch is independent and can be applied on its own.
Emre Cecanpunar (5):
platform/x86: hp-wmi: validate WMI response header size platform/x86: hp-wmi: handle firmware errors in tablet mode query platform/x86: hp-wmi: handle errors in the OMEN key event platform/x86: hp-wmi: normalize GPU thermal mode errors platform/x86: hp-wmi: report fan speed command failures
drivers/platform/x86/hp/hp-wmi.c | 64 +++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 21 deletions(-)
base-commit: 2b3a5dabe89e330413af403246b648c1890f368f
PATCH 1/5 platform/x86: hp-wmi: validate WMI response header size
hp_wmi_perform_query() reads struct bios_return without checking that the firmware response buffer is large enough to contain it. A truncated response can therefore cause an out-of-bounds read.
Reject responses shorter than the mandatory header before accessing its fields or calculating the payload offset.
--- drivers/platform/x86/hp/hp-wmi.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +5 -0
@@ -670,6 +670,11 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command, ret = -EINVAL ; goto out_free; } +if (obj->buffer.length <sizeof (*bios_return)) { +pr_warn ("query 0x%x returned a short buffer\n" , query); + ret = -EINVAL ; + goto out_free; + } bios_return = (struct bios_return *)obj->buffer.pointer; ret = bios_return->return_code;
Ilpo Järvinen
maintainerRe: [PATCH 1/5] platform/x86: hp-wmi: validate WMI response header size
Quoted 18 lines · Emre Cecanpunar
hp_wmi_perform_query()reads structbios_returnwithout checking that the firmware response buffer is large enough to contain it. A truncated response can therefore cause an out-of-bounds read.Reject responses shorter than the mandatory header before accessing its fields or calculating the payload offset.
Fixes 62ec30d45ecb ("misc: add HP WMI laptop extras driver")Signed-off-by Emre Cecanpunar <[email protected]>--- drivers/platform/x86/hp/hp-wmi.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c static int hp_wmi_perform_query(int query, enum hp_wmi_command command, · line 677
Quoted 8 lines
drivers/platform/x86/hp/hp-wmi.c +5
-0@@ -670,6 +670,11 @@ static int hp_wmi_perform_query(int query, enum hp_wmi_command command, ret = -EINVAL ; goto out_free; } +if (obj->buffer.length <sizeof (*bios_return)) { +pr_warn ("query 0x%x returned a short buffer\n" , query); + ret = -EINVAL ; + goto out_free; + }
Hi,
This driver is using a deprecated WMI API. The new API might be able to do size checks for you so the correct course of action seems to modernize this driver instead.
IIRC, I have recently mentioned this same thing to somebody else so somebody might already be cooking a modernization patch.
Signature · 1 line
i.
PATCH 2/5 platform/x86: hp-wmi: handle firmware errors in tablet mode query
In reply to Emre Cecanpunarhp_wmi_get_tablet_mode() accepts any nonnegative return value from hp_wmi_perform_query() as success. HP firmware errors are positive, so a rejected query makes the zeroed response look like laptop mode.
Treat every nonzero result as an error and preserve negative transport errors. The event and resume paths pass the result directly to the input subsystem, where a negative errno would look like an asserted switch, so skip the switch update when the query fails.
--- drivers/platform/x86/hp/hp-wmi.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +17 -9
@@ -798,8 +798,8 @@ static int hp_wmi_get_tablet_mode(void) ret =hp_wmi_perform_query (HPWMI_SYSTEM_DEVICE_MODE ,HPWMI_READ , system_device_mode,zero_if_sup (system_device_mode),sizeof (system_device_mode)); -if (ret <0 ) -return ret; +if (ret) +return ret <0 ? ret : -EINVAL ;return system_device_mode[0 ] ==DEVICE_MODE_TABLET ; }
@@ -1198,7 +1198,7 @@ static void hp_wmi_notify(union acpi_object *obj, void *context) {u32 event_id, event_data;u32 *location; -int key_code; +int key_code, state;if (!obj)return ;
@@ -1228,9 +1228,12 @@ static void hp_wmi_notify(union acpi_object *obj, void *context)if (test_bit (SW_DOCK , hp_wmi_input_dev->swbit))input_report_switch (hp_wmi_input_dev,SW_DOCK ,hp_wmi_get_dock_state ()); -if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) -input_report_switch (hp_wmi_input_dev,SW_TABLET_MODE , -hp_wmi_get_tablet_mode ()); +if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) { + state =hp_wmi_get_tablet_mode (); +if (state >=0 ) +input_report_switch (hp_wmi_input_dev, +SW_TABLET_MODE , state); + }input_sync (hp_wmi_input_dev);break ;case HPWMI_PARK_HDD :
@@ -2431,6 +2434,8 @@ static void __exit hp_wmi_bios_remove(struct platform_device *device)static int hp_wmi_resume_handler (struct device *device) { +int state; +/* * Hardware state may have changed while suspended, so trigger * input events for the current state. As this is a switch,
@@ -2441,9 +2446,12 @@ static int hp_wmi_resume_handler(struct device *device)if (test_bit (SW_DOCK , hp_wmi_input_dev->swbit))input_report_switch (hp_wmi_input_dev,SW_DOCK ,hp_wmi_get_dock_state ()); -if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) -input_report_switch (hp_wmi_input_dev,SW_TABLET_MODE , -hp_wmi_get_tablet_mode ()); +if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) { + state =hp_wmi_get_tablet_mode (); +if (state >=0 ) +input_report_switch (hp_wmi_input_dev, +SW_TABLET_MODE , state); + }input_sync (hp_wmi_input_dev); }
Ilpo Järvinen
maintainerRe: [PATCH 2/5] platform/x86: hp-wmi: handle firmware errors in tablet mode query
Quoted 3 lines · Emre Cecanpunar
hp_wmi_get_tablet_mode()accepts any nonnegative return value fromhp_wmi_perform_query()as success. HP firmware errors are positive, so a rejected query makes the zeroed response look like laptop mode.
Does this problem actually happen with some hw? Please add the info.
Quoted 16 lines
Treat every nonzero result as an error and preserve negative transport errors. The event and resume paths pass the result directly to the input subsystem, where a negative errno would look like an asserted switch, so skip the switch update when the query fails.
Fixes 520ee4ea1cc6 ("platform/x86: hp-wmi: Fix SW_TABLET_MODE detection method")Signed-off-by Emre Cecanpunar <[email protected]>--- drivers/platform/x86/hp/hp-wmi.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c static int hp_wmi_get_tablet_mode(void) · line 802
Quoted 7 lines
drivers/platform/x86/hp/hp-wmi.c +2
-2@@ -798,8 +798,8 @@ static int hp_wmi_get_tablet_mode(void) ret =hp_wmi_perform_query (HPWMI_SYSTEM_DEVICE_MODE ,HPWMI_READ , system_device_mode,zero_if_sup (system_device_mode),sizeof (system_device_mode)); -if (ret <0 ) -return ret; +if (ret) +return ret <0 ? ret : -EINVAL ;
Please use 2 separate ifs for clarity.
Quoted 13 lines
drivers/platform/x86/hp/hp-wmi.c +1
-1return system_device_mode[0 ] ==DEVICE_MODE_TABLET ; }@@ -1198,7 +1198,7 @@ static void hp_wmi_notify(union acpi_object *obj, void *context) {u32 event_id, event_data;u32 *location; -int key_code; +int key_code, state;if (!obj)return ;@@ -1228,9 +1228,12 @@ static void hp_wmi_notify(union acpi_object *obj, void *context)if (test_bit (SW_DOCK , hp_wmi_input_dev->swbit))
drivers/platform/x86/hp/hp-wmi.c static void hp_wmi_notify(union acpi_object *obj, void *context) · line 1235
Quoted 10 lines
drivers/platform/x86/hp/hp-wmi.c +5
-3input_report_switch (hp_wmi_input_dev,SW_DOCK ,hp_wmi_get_dock_state ()); -if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) -input_report_switch (hp_wmi_input_dev,SW_TABLET_MODE , -hp_wmi_get_tablet_mode ()); +if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) { + state =hp_wmi_get_tablet_mode (); +if (state >=0 ) +input_report_switch (hp_wmi_input_dev, +SW_TABLET_MODE , state);
Braces are needed for multiline constructs...
...BUT, it looks there's some copy pasting going on so moving these into a helper both this and hp_wmi_resume_handler() could use would seem useful (in a preparatory patch). It would help with the line length so this may no longer require multiple lines with less indent.
Quoted 30 lines
drivers/platform/x86/hp/hp-wmi.c +9
-3+ }input_sync (hp_wmi_input_dev);break ;case HPWMI_PARK_HDD :@@ -2431,6 +2434,8 @@ static void __exit hp_wmi_bios_remove(struct platform_device *device)static int hp_wmi_resume_handler (struct device *device) { +int state; +/* * Hardware state may have changed while suspended, so trigger * input events for the current state. As this is a switch, @@ -2441,9 +2446,12 @@ static int hp_wmi_resume_handler(struct device *device)if (test_bit (SW_DOCK , hp_wmi_input_dev->swbit))input_report_switch (hp_wmi_input_dev,SW_DOCK ,hp_wmi_get_dock_state ()); -if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) -input_report_switch (hp_wmi_input_dev,SW_TABLET_MODE , -hp_wmi_get_tablet_mode ()); +if (test_bit (SW_TABLET_MODE , hp_wmi_input_dev->swbit)) { + state =hp_wmi_get_tablet_mode (); +if (state >=0 ) +input_report_switch (hp_wmi_input_dev, +SW_TABLET_MODE , state); + }input_sync (hp_wmi_input_dev); }
Signature · 1 line
i.
PATCH 3/5 platform/x86: hp-wmi: handle errors in the OMEN key event
In reply to Emre Cecanpunarhp_wmi_read_int() can return a negative error code, but the HPWMI_OMEN_KEY handler passes it directly to sparse_keymap_report_event(). The value is then converted to an unsigned key code, causing a bogus lookup and a KEY_UNKNOWN input event.
Stop processing the event when reading the key code fails, mirroring the existing check in the HPWMI_BEZEL_BUTTON handler.
--- drivers/platform/x86/hp/hp-wmi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +6 -2
@@ -1253,10 +1253,14 @@ static void hp_wmi_notify(union acpi_object *obj, void *context)platform_profile_cycle ();break ;case HPWMI_OMEN_KEY : -if (event_data)/* Only should be true for HP Omen */ +if (event_data) { +/* Only should be true for HP Omen */ key_code = event_data; -else + }else { key_code =hp_wmi_read_int (HPWMI_HOTKEY_QUERY ); +if (key_code <0 ) +break ; + }if (!sparse_keymap_report_event (hp_wmi_input_dev, key_code,1 , true))
Ilpo Järvinen
maintainerRe: [PATCH 3/5] platform/x86: hp-wmi: handle errors in the OMEN key event
Quoted 19 lines · Emre Cecanpunar
hp_wmi_read_int()can return a negative error code, but theHPWMI_OMEN_KEYhandler passes it directly tosparse_keymap_report_event(). The value is then converted to an unsigned key code, causing a bogus lookup and aKEY_UNKNOWNinput event.Stop processing the event when reading the key code fails, mirroring the existing check in the
HPWMI_BEZEL_BUTTONhandler.Fixes f4a31a428d0d ("platform/x86: hp-wmi: Add HP Envy special key support")Signed-off-by Emre Cecanpunar <[email protected]>--- drivers/platform/x86/hp/hp-wmi.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c static void hp_wmi_notify(union acpi_object *obj, void *context) · line 1257
Quoted 6 lines
drivers/platform/x86/hp/hp-wmi.c +2
-1@@ -1253,10 +1253,14 @@ static void hp_wmi_notify(union acpi_object *obj, void *context)platform_profile_cycle ();break ;case HPWMI_OMEN_KEY : -if (event_data)/* Only should be true for HP Omen */ +if (event_data) { +/* Only should be true for HP Omen */
Moving the comment here causes it no longer make any sense because "true" related to the if check.
Quoted 11 lines
drivers/platform/x86/hp/hp-wmi.c +4
-1key_code = event_data; - else + }else { key_code =hp_wmi_read_int (HPWMI_HOTKEY_QUERY ); +if (key_code <0 ) +break ; + }if (!sparse_keymap_report_event (hp_wmi_input_dev, key_code,1 , true))
Signature · 1 line
i.
PATCH 4/5 platform/x86: hp-wmi: normalize GPU thermal mode errors
In reply to Emre CecanpunarThe GPU thermal mode helpers return positive firmware error codes, but their callers only treat negative values as failures. If the mode query is rejected, its output parameters remain uninitialized and are then used to select a profile or sent back to firmware as the GPU slowdown temperature. A rejected mode update is likewise reported as successful.
Zero the query buffer and convert positive firmware status codes to -EINVAL in both helpers so callers never consume missing output or ignore a failed update.
--- drivers/platform/x86/hp/hp-wmi.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/platform/x86/hp/hp-wmi.c +12 -9
@@ -1885,20 +1885,21 @@ static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable, u8 *dstate, u8 *gpu_slowdown_temp) { -struct victus_gpu_power_modes gpu_power_modes; +struct victus_gpu_power_modes gpu_power_modes = {};int ret; ret =hp_wmi_perform_query (HPWMI_GET_GPU_THERMAL_MODES_QUERY ,HPWMI_GM , &gpu_power_modes,sizeof (gpu_power_modes),sizeof (gpu_power_modes)); -if (ret ==0 ) { - *ctgp_enable = gpu_power_modes.ctgp_enable ? true : false; - *ppab_enable = gpu_power_modes.ppab_enable ? true : false; - *dstate = gpu_power_modes.dstate; - *gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp; - } +if (ret) +return ret <0 ? ret : -EINVAL ; -return ret; + *ctgp_enable = gpu_power_modes.ctgp_enable ? true : false; + *ppab_enable = gpu_power_modes.ppab_enable ? true : false; + *dstate = gpu_power_modes.dstate; + *gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp; + +return 0 ; } static int victus_s_gpu_thermal_profile_set(bool ctgp_enable,
@@ -1929,8 +1930,10 @@ static int victus_s_gpu_thermal_profile_set(bool ctgp_enable, ret =hp_wmi_perform_query (HPWMI_SET_GPU_THERMAL_MODES_QUERY ,HPWMI_GM , &gpu_power_modes,sizeof (gpu_power_modes),0 ); +if (ret) +return ret <0 ? ret : -EINVAL ; -return ret; +return 0 ; }/* Note: HP_POWER_LIMIT_DEFAULT can be used to restore default PL1 and PL2 */
Ilpo Järvinen
maintainerRe: [PATCH 4/5] platform/x86: hp-wmi: normalize GPU thermal mode errors
Quoted 5 lines · Emre Cecanpunar
The GPU thermal mode helpers return positive firmware error codes, but their callers only treat negative values as failures. If the mode query is rejected, its output parameters remain uninitialized and are then used to select a profile or sent back to firmware as the GPU slowdown temperature. A rejected mode update is likewise reported as successful.
Does this happen with some HW? Please add the info.
Quoted 15 lines
Zero the query buffer and convert positive firmware status codes to
-EINVALin both helpers so callers never consume missing output or ignore a failed update.Fixes 6e4ab59b8391 ("platform/x86: hp-wmi: Add fan and thermal profile support for Victus 16-s1000")Signed-off-by Emre Cecanpunar <[email protected]>--- drivers/platform/x86/hp/hp-wmi.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable, · line 1888
Quoted 5 lines
drivers/platform/x86/hp/hp-wmi.c +1
-1@@ -1885,20 +1885,21 @@ static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable, u8 *dstate, u8 *gpu_slowdown_temp) { -struct victus_gpu_power_modes gpu_power_modes; +struct victus_gpu_power_modes gpu_power_modes = {};
Why is this necessary if you return error after the if change?
drivers/platform/x86/hp/hp-wmi.c static int victus_s_gpu_thermal_profile_get(bool *ctgp_enable, · line 1895
Quoted 13 lines
drivers/platform/x86/hp/hp-wmi.c +2
-6int ret; ret =hp_wmi_perform_query (HPWMI_GET_GPU_THERMAL_MODES_QUERY ,HPWMI_GM , &gpu_power_modes,sizeof (gpu_power_modes),sizeof (gpu_power_modes)); -if (ret ==0 ) { - *ctgp_enable = gpu_power_modes.ctgp_enable ? true : false; - *ppab_enable = gpu_power_modes.ppab_enable ? true : false; - *dstate = gpu_power_modes.dstate; - *gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp; - } +if (ret) +return ret <0 ? ret : -EINVAL ;
Use two separate ifs instead.
Quoted 11 lines
drivers/platform/x86/hp/hp-wmi.c +6
-1-return ret; + *ctgp_enable = gpu_power_modes.ctgp_enable ? true : false; + *ppab_enable = gpu_power_modes.ppab_enable ? true : false; + *dstate = gpu_power_modes.dstate; + *gpu_slowdown_temp = gpu_power_modes.gpu_slowdown_temp; + +return 0 ; } static int victus_s_gpu_thermal_profile_set(bool ctgp_enable,
drivers/platform/x86/hp/hp-wmi.c static int victus_s_gpu_thermal_profile_set(bool ctgp_enable, · line 1936
Quoted 8 lines
drivers/platform/x86/hp/hp-wmi.c +3
-1@@ -1929,8 +1930,10 @@ static int victus_s_gpu_thermal_profile_set(bool ctgp_enable, ret =hp_wmi_perform_query (HPWMI_SET_GPU_THERMAL_MODES_QUERY ,HPWMI_GM , &gpu_power_modes,sizeof (gpu_power_modes),0 ); +if (ret) +return ret <0 ? ret : -EINVAL ; -return ret; +return 0 ;
Now this could be:
if (ret > 0) return -EINVAL; return ret;
Signature · 1 line
i.
PATCH 5/5 platform/x86: hp-wmi: report fan speed command failures
In reply to Emre Cecanpunarhp_wmi_fan_speed_set() passes the raw WMI result to hp_wmi_apply_fan_settings(), which only checks for negative errors. A positive HP firmware error consequently reaches the success path and schedules keep-alive work for a fan speed that was not applied.
Convert positive firmware errors to -EINVAL before returning from the fan speed helper, matching the other fan control commands.
--- drivers/platform/x86/hp/hp-wmi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/hp/hp-wmi.c +3 -1
@@ -907,8 +907,10 @@ static int hp_wmi_fan_speed_set(struct hp_wmi_hwmon_priv *priv)return ret; ret =hp_wmi_perform_query (HPWMI_VICTUS_S_FAN_SPEED_SET_QUERY ,HPWMI_GM , &fan_speed,sizeof (fan_speed),0 ); +if (ret) +return ret <0 ? ret : -EINVAL ; -return ret; +return 0 ; }static int hp_wmi_fan_speed_reset (struct hp_wmi_hwmon_priv *priv)
Ilpo Järvinen
maintainerRe: [PATCH 5/5] platform/x86: hp-wmi: report fan speed command failures
Quoted 4 lines · Emre Cecanpunar
hp_wmi_fan_speed_set()passes the raw WMI result tohp_wmi_apply_fan_settings(), which only checks for negative errors. A positive HP firmware error consequently reaches the success path and schedules keep-alive work for a fan speed that was not applied.
Missing info on which hw this problem occurs.
Quoted 14 lines
Convert positive firmware errors to
-EINVALbefore returning from the fan speed helper, matching the other fan control commands.Fixes c203c59fb5de ("platform/x86: hp-wmi: implement fan keep-alive")Signed-off-by Emre Cecanpunar <[email protected]>--- drivers/platform/x86/hp/hp-wmi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/platform/x86/hp/hp-wmi.c +0
-0
drivers/platform/x86/hp/hp-wmi.c static int hp_wmi_fan_speed_set(struct hp_wmi_hwmon_priv *priv) · line 913
Quoted 8 lines
drivers/platform/x86/hp/hp-wmi.c +3
-1@@ -907,8 +907,10 @@ static int hp_wmi_fan_speed_set(struct hp_wmi_hwmon_priv *priv)return ret; ret =hp_wmi_perform_query (HPWMI_VICTUS_S_FAN_SPEED_SET_QUERY ,HPWMI_GM , &fan_speed,sizeof (fan_speed),0 ); +if (ret) +return ret <0 ? ret : -EINVAL ; -return ret; +return 0 ;
You know the drill by now.
Signature · 1 line
i.
Hi,
Thanks for the review.
These issues were found through code inspection, and I do not currently have hardware reports reproducing the firmware error paths.
I will drop this series for now and wait for the hp-wmi migration to the newer WMI API, since that work may address some of these issues more appropriately.
Regards,
Emre
Hi,
Thanks for the review.
These issues were found through code inspection, and I do not currently have hardware reports reproducing the firmware error paths.
I will drop this series for now and wait for the hp-wmi migration to the newer WMI API, since that work may address some of these issues more appropriately.
Regards,
Emre