HID: uclogic: Correct devm device reference for hidinput input_dev name
PATCH 1/2 HID: uclogic: Correct devm device reference for hidinput input_dev name
From: Rahul Rameshbabu <[email protected]>
[ Upstream commit dd613a4e45f8d35f49a63a2064e5308fa5619e29 ]
Reference the HID device rather than the input device for the devm allocation of the input_dev name. Referencing the input_dev would lead to a use-after-free when the input_dev was unregistered and subsequently fires a uevent that depends on the name. At the point of firing the uevent, the name would be freed by devres management.
Use devm_kasprintf to simplify the logic for allocating memory and formatting the input_dev name string.
--- drivers/hid/hid-uclogic-core.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/hid/hid-uclogic-core.c +3 -10
@@ -104,10 +104,8 @@ static int uclogic_input_configured(struct hid_device *hdev, {struct uclogic_drvdata *drvdata =hid_get_drvdata (hdev);struct uclogic_params *params = &drvdata->params; -char *name;const char *suffix =NULL ;struct hid_field *field; -size_t len;/* no report associated (HID_QUIRK_MULTI_INPUT not set) */ if (!hi->report)
@@ -145,14 +143,9 @@ static int uclogic_input_configured(struct hid_device *hdev,break ; } -if (suffix) { - len =strlen (hdev->name) +2 +strlen (suffix); - name =devm_kzalloc (&hi->input->dev, len,GFP_KERNEL ); -if (name) { -snprintf (name, len,"%s %s" , hdev->name, suffix); - hi->input->name = name; - } - } +if (suffix) + hi->input->name =devm_kasprintf (&hdev->dev,GFP_KERNEL , +"%s %s" , hdev->name, suffix);return 0 ; }
Signature · 1 line
2.43.0
PATCH 2/2 HID: uclogic: Add NULL check in uclogic_input_configured()
From: Henry Martin <[email protected]>
[ Upstream commit bd07f751208ba190f9b0db5e5b7f35d5bb4a8a1e ]
devm_kasprintf() returns NULL when memory allocation fails. Currently, uclogic_input_configured() does not check for this case, which results in a NULL pointer dereference.
Add NULL check after devm_kasprintf() to prevent this issue.
--- drivers/hid/hid-uclogic-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hid/hid-uclogic-core.c +4 -1
@@ -143,9 +143,12 @@ static int uclogic_input_configured(struct hid_device *hdev,break ; } -if (suffix) +if (suffix) { hi->input->name =devm_kasprintf (&hdev->dev,GFP_KERNEL ,"%s %s" , hdev->name, suffix); +if (!hi->input->name) +return -ENOMEM ; + }return 0 ; }
Signature · 1 line
2.43.0