HID: input: allocate input_dev name/phys/uniq using hid device devres
PATCH 1/1 HID: input: allocate input_dev name/phys/uniq using hid device devres
input_dev->name, phys, and uniq are directly assigned from hid_device.
It causes a slab-use-after-free bug if the input device outlives the parent HID device during teardown. Fix this by using devm_kstrdup() tied to &hid->dev for name, phys, and uniq strings. The string lifetime is tied to the parent HID device, preventing a potential slab-use-after-free bug. Also add OOM error handling for these allocations.
--- drivers/hid/hid-input.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/hid/hid-input.c +16 -3
@@ -2125,10 +2125,21 @@ static struct hid_input *hidinput_allocate(struct hid_device *hid, input_dev->close = hidinput_close; input_dev->setkeycode = hidinput_setkeycode; input_dev->getkeycode = hidinput_getkeycode; + +const char *name = hidinput->name ? hidinput->name : hid->name; + + input_dev->name =devm_kstrdup (&hid->dev, name,GFP_KERNEL ); +if (!input_dev->name) + goto fail; + + input_dev->phys =devm_kstrdup (&hid->dev, hid->phys,GFP_KERNEL ); +if (!input_dev->phys) + goto fail; + + input_dev->uniq =devm_kstrdup (&hid->dev, hid->uniq,GFP_KERNEL ); +if (!input_dev->uniq) + goto fail; - input_dev->name = hidinput->name ? hidinput->name : hid->name; - input_dev->phys = hid->phys; - input_dev->uniq = hid->uniq; input_dev->id.bustype = hid->bus; input_dev->id.vendor = hid->vendor; input_dev->id.product = hid->product;
@@ -2144,6 +2155,8 @@ static struct hid_input *hidinput_allocate(struct hid_device *hid,return hidinput; fail: +if (hidinput) +kfree (hidinput->name);kfree (hidinput);input_free_device (input_dev);hid_err (hid,"Out of memory during hid input probe\n" );
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Use-After-Free of input_dev string fields (name, phys, uniq) on device unbind. - [Low] Memory leak…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Use-After-Free of input_dev string fields (name, phys, uniq) on device unbind.
- [Low] Memory leak of devm_kstrdup strings in the error path.
--
--- Patch [1]: [PATCH] HID: input: allocate input_dev name/phys/uniq using hid device devres --- commit ae1934cecc0a7213604df29b6d6486b4090d9b54
Author: Hyeonsu Choi <[email protected]>
HID: input: allocate input_dev name/phys/uniq using hid device devres
This commit modifies hidinput_allocate() to dynamically allocate the input_dev name, phys, and uniq strings using devm_kstrdup() tied to the parent HID device. It also introduces an error handling path to clean up if the allocations fail.
Quoted 4 lines
drivers/hid/hid-input.c +0
-0@@ -2126,9 +2126,20 @@ static struct hid_input *hidinput_allocate(struct hid_device *hid,
[ ... ]
drivers/hid/hid-input.c static struct hid_input *hidinput_allocate(struct hid_device *hid, · line 2135
Quoted 10 lines
drivers/hid/hid-input.c +7
-3- input_dev->name = hidinput->name ? hidinput->name : hid->name; - input_dev->phys = hid->phys; - input_dev->uniq = hid->uniq; +const char *name = hidinput->name ? hidinput->name : hid->name; + + input_dev->name =devm_kstrdup (&hid->dev, name,GFP_KERNEL ); +if (!input_dev->name) + goto fail; + + input_dev->phys =devm_kstrdup (&hid->dev, hid->phys,GFP_KERNEL );
[Severity: High]
If a userspace process holds an open file descriptor for the evdev node (/dev/input/eventX), the input_dev will outlive the parent hid->dev. When the HID device is unbound, devres will free these strings.
Can this cause a use-after-free when userspace later queries the name, phys, or uniq strings via ioctl (e.g., EVIOCGNAME)?
Quoted 7 lines
drivers/hid/hid-input.c +7
-0+if (!input_dev->phys) + goto fail; + + input_dev->uniq =devm_kstrdup (&hid->dev, hid->uniq,GFP_KERNEL ); +if (!input_dev->uniq) + goto fail; +
[ ... ]
drivers/hid/hid-input.c static struct hid_input *hidinput_allocate(struct hid_device *hid, · line 2162
Quoted 9 lines
drivers/hid/hid-input.c +2
-0@@ -2144,6 +2155,8 @@ static struct hid_input *hidinput_allocate(struct hid_device *hid,return hidinput; fail: +if (hidinput) +kfree (hidinput->name);kfree (hidinput);input_free_device (input_dev);hid_err (hid,"Out of memory during hid input probe\n" );
[Severity: Low]
If devm_kstrdup() fails for phys or uniq, the code jumps here to the error path. The earlier successful devm_kstrdup() allocations remain tied to &hid->dev.
If the caller continues the device probe successfully after this failure, will these strings be leaked for the lifetime of the HID device?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · sashiko-bot
[Severity: High]
If a userspace process holds an open file descriptor for the evdev node (/dev/input/eventX), theinput_devwill outlive the parenthid->dev. When the HID device is unbound, devres will free these strings.Can this cause a use-after-free when userspace later queries the name, phys, or uniq strings via ioctl (e.g., EVIOCGNAME)?
Thanks for the review.
Using &hid->dev here is intentional.
Using the input device for devm-managed input_dev strings is problematic; see CVE-2023-53454, where the allocation was moved to the HID device because the input device unregister path may still use the name for uevents.
Quoted 7 lines
[Severity: Low]
Ifdevm_kstrdup()fails for phys or uniq, the code jumps here to the error path. The earlier successfuldevm_kstrdup()allocations remain tied to&hid->dev.If the caller continues the device probe successfully after this failure, will these strings be leaked for the lifetime of the HID device?
The name/phys/uniq copies are attached to &hid->dev, so the clean-up is handled by HID device devres.
However, if it is better to explicitly unwind, I can add that in v2.
Thanks,
Hyeonsu
Adding maintainers and mailing list to CC. Sorry for the omission.
Quoted 32 lines · On Tue, Jul 14, 2026 at 10:33 PM chlgustn3171 <[email protected]> wrote:
Quoted 7 lines · sashiko-bot
[Severity: High]
If a userspace process holds an open file descriptor for the evdev node (/dev/input/eventX), theinput_devwill outlive the parenthid->dev. When the HID device is unbound, devres will free these strings.Can this cause a use-after-free when userspace later queries the name, phys, or uniq strings via ioctl (e.g., EVIOCGNAME)?
Thanks for the review.
Using
&hid->devhere is intentional.
Using the input device for devm-managedinput_devstrings is problematic; see CVE-2023-53454, where the allocation was moved to the HID device because the input device unregister path may still use the name for uevents.Quoted 7 lines
[Severity: Low]
Ifdevm_kstrdup()fails for phys or uniq, the code jumps here to the error path. The earlier successfuldevm_kstrdup()allocations remain tied to&hid->dev.If the caller continues the device probe successfully after this failure, will these strings be leaked for the lifetime of the HID device?
The name/phys/uniq copies are attached to
&hid->dev, so the clean-up is handled by HID device devres.
However, if it is better to explicitly unwind, I can add that in v2.Thanks,
Hyeonsu
Hi,
Quoted 20 lines · On Tue, Jul 14, 2026 at 10:39:09PM +0900, chlgustn3171 wrote:
Adding maintainers and mailing list to CC. Sorry for the omission.
Quoted 17 lines · On Tue, Jul 14, 2026 at 10:33 PM chlgustn3171 <[email protected]> wrote:
Quoted 7 lines · sashiko-bot
[Severity: High]
If a userspace process holds an open file descriptor for the evdev node (/dev/input/eventX), theinput_devwill outlive the parenthid->dev. When the HID device is unbound, devres will free these strings.Can this cause a use-after-free when userspace later queries the name, phys, or uniq strings via ioctl (e.g., EVIOCGNAME)?
Thanks for the review.
Using
&hid->devhere is intentional.
Using the input device for devm-managedinput_devstrings is problematic; see CVE-2023-53454, where the allocation was moved to the HID device because the input device unregister path may still use the name for uevents.
Thank you for the patch. However, I do not believe it fixes anything, as moving to devres will not extend (and may even shorten) life time of name, phys and uniq. Normally they are character array members of HID device, and HID device continues existing past drivers disconnecting from it. OTOH devres-controlled resources will get freed when driver unbinds from a device.
Additionally, HID devices are parents of input devices, and should stick around because input devices hold references to their parents until device_del() is called in input_unregister_device(). However before that it will disconnect this input device from all the handlers, including evdev, evdev will mark the device as !exist and evdev ioctl will not attempt to process ioctls for such device.
I however am not sure why we see this syzkaller report. Maybe there is memory corruption somewhere or maybe syzkaller itself gets confused when tracing numerous allocations.
Thanks.
Signature · 1 line
Dmitry
Hi Dmitry,
Thanks for the detailed explanation on devres lifecycle and device refcounting.
I agree the patch is unnecessary, and that the syzbot trace is likely a false positive or external memory corruption. I will drop this patch.
Thanks again for the review.
Regards, Hyeonsu
Quoted 47 lines · Dmitry Torokhov
Hi,
Quoted 20 lines · On Tue, Jul 14, 2026 at 10:39:09PM +0900, chlgustn3171 wrote:
Adding maintainers and mailing list to CC. Sorry for the omission.
Quoted 17 lines · On Tue, Jul 14, 2026 at 10:33 PM chlgustn3171 <[email protected]> wrote:
Quoted 7 lines · sashiko-bot
[Severity: High]
If a userspace process holds an open file descriptor for the evdev node (/dev/input/eventX), theinput_devwill outlive the parenthid->dev. When the HID device is unbound, devres will free these strings.Can this cause a use-after-free when userspace later queries the name, phys, or uniq strings via ioctl (e.g., EVIOCGNAME)?
Thanks for the review.
Using
&hid->devhere is intentional.
Using the input device for devm-managedinput_devstrings is problematic; see CVE-2023-53454, where the allocation was moved to the HID device because the input device unregister path may still use the name for uevents.Thank you for the patch. However, I do not believe it fixes anything, as moving to devres will not extend (and may even shorten) life time of name, phys and uniq. Normally they are character array members of HID device, and HID device continues existing past drivers disconnecting from it. OTOH devres-controlled resources will get freed when driver unbinds from a device.
Additionally, HID devices are parents of input devices, and should stick around because input devices hold references to their parents until
device_del()is called ininput_unregister_device(). However before that it will disconnect this input device from all the handlers, including evdev, evdev will mark the device as !exist and evdev ioctl will not attempt to process ioctls for such device.I however am not sure why we see this syzkaller report. Maybe there is memory corruption somewhere or maybe syzkaller itself gets confused when tracing numerous allocations.
Thanks.
--
Dmitry