Skip to contentSkip to conversation

HID: core: Sanitize event code and type when mapping input

PATCH Superseded Marc Zyngier 1 participant 2 messages Aug 27, 2020 – Sep 1, 2020

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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
index b8eabf206e74..88e19996427e 100644
@@ -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
index 3f94b4954225..e3152155c4b8 100644
@@ -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
index 7a660411c562..75859e492984 100644
@@ -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
#1 lore raw
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 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(-)

[...]

Quoted 21 lines
drivers/mfd/syscon.c +1 -0
index 7a660411c562..75859e492984 100644
@@ -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 -1
 		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);
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...