HID: core: Sanitize event code and type when mapping input
PATCH v3 1/1 HID: core: Sanitize event code and type when mapping input
When calling into hid_map_usage(), the passed event code is blindly stored as is, even if it doesn't fit in the associated bitmap.
This event code can come from a variety of sources, including devices masquerading as input devices, only a bit more "programmable".
Instead of taking the event code at face value, check that it actually fits the corresponding bitmap, and if it doesn't:
- spit out a warning so that we know which device is acting up
- NULLify the bitmap pointer so that we catch unexpected uses
Code paths that can make use of untrusted inputs can now check that the mapping was indeed correct and bail out if not.
---
* From v2:
- Don't prematurely narrow the event code so that hid_map_usage() catches illegal values beyond the 16bit limit.
* From v1:
- Dropped the input.c changes, and turned hid_map_usage() into the validation primitive.
- Handle mapping failures in hidinput_configure_usage() and mt_touch_input_mapping() (on top of hid_map_usage_clear() which was already handled)
drivers/hid/hid-input.c | 4 ++++ drivers/hid/hid-multitouch.c | 2 ++ drivers/mfd/syscon.c | 2 +- include/linux/hid.h | 42 +++++++++++++++++++++++++----------- 4 files changed, 36 insertions(+), 14 deletions(-)
drivers/hid/hid-input.c +4 -0
@@ -1132,6 +1132,10 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel } mapped: +/* Mapping failed, bail out */ +if (!bit) +return ; +if (device->driver->input_mapped && device->driver->input_mapped (device, hidinput, field, usage, &bit, &max) <0 ) {
drivers/hid/hid-multitouch.c +2 -0
@@ -856,6 +856,8 @@ static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi, code =BTN_0 + ((usage->hid -1 ) &HID_USAGE );hid_map_usage (hi, usage, bit, max,EV_KEY , code); +if (!*bit) +return -1 ;input_set_capability (hi->input,EV_KEY , code);return 1 ;
drivers/mfd/syscon.c +1 -1
@@ -108,6 +108,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk) syscon_config.max_register =resource_size (&res) - reg_io_width; regmap =regmap_init_mmio (NULL , base, &syscon_config); +kfree (syscon_config.name);if (IS_ERR (regmap)) {pr_err ("regmap init failed\n" ); ret =PTR_ERR (regmap);
@@ -144,7 +145,6 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)regmap_exit (regmap); err_regmap:iounmap (base); -kfree (syscon_config.name); err_map:kfree (syscon);return ERR_PTR (ret);
include/linux/hid.h +29 -13
70 lines not on this page · raw message
Signature · 1 line
2.27.0
Quoted 34 lines · Marc Zyngier
When calling into
hid_map_usage(), the passed event code is blindly stored as is, even if it doesn't fit in the associated bitmap.This event code can come from a variety of sources, including devices masquerading as input devices, only a bit more "programmable".
Instead of taking the event code at face value, check that it actually fits the corresponding bitmap, and if it doesn't:
- spit out a warning so that we know which device is acting up
- NULLify the bitmap pointer so that we catch unexpected usesCode paths that can make use of untrusted inputs can now check that the mapping was indeed correct and bail out if not.
Signed-off-by Marc Zyngier <[email protected]>---
* From v2:
- Don't prematurely narrow the event code so thathid_map_usage()catches illegal values beyond the 16bit limit.* From v1:
- Dropped theinput.cchanges, and turnedhid_map_usage()into the validation primitive.
- Handle mapping failures inhidinput_configure_usage()andmt_touch_input_mapping()(on top ofhid_map_usage_clear()which was already handled)drivers/hid/hid-input.c | 4 ++++ drivers/hid/hid-multitouch.c | 2 ++ drivers/mfd/syscon.c | 2 +- include/linux/hid.h | 42 +++++++++++++++++++++++++----------- 4 files changed, 36 insertions(+), 14 deletions(-)
[...]
Quoted 21 lines
drivers/mfd/syscon.c +1
-0@@ -108,6 +108,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk) syscon_config.max_register = resource_size(&res) - reg_io_width; regmap =regmap_init_mmio (NULL , base, &syscon_config); +kfree (syscon_config.name);if (IS_ERR (regmap)) {pr_err ("regmap init failed\n" );drivers/mfd/syscon.c +0
-1ret =PTR_ERR (regmap);@@ -144,7 +145,6 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)regmap_exit (regmap ); err_regmap:iounmap (base); -kfree (syscon_config.name); err_map:kfree (syscon);
Quoted 1 line
return
ERR_PTR(ret);
This hunk is totally unrelated, and is from another fix I was working on at the same time... Sorry for the nois, I'll post v4 (hopefully final) now.
M.
Signature · 1 line
Jazz is not dead. It just smells funny...