[PATCH v2 0/4] HID: Fix some memory leaks in drivers/hid

Günther Noack posted 4 patches 1 month, 1 week ago
drivers/hid/hid-apple.c      |  4 +---
drivers/hid/hid-asus.c       | 15 +++++++++++----
drivers/hid/hid-magicmouse.c |  4 +---
include/linux/hid.h          |  6 ++++++
4 files changed, 19 insertions(+), 10 deletions(-)
[PATCH v2 0/4] HID: Fix some memory leaks in drivers/hid
Posted by Günther Noack 1 month, 1 week ago
Hello!

These patches fix a few memory leaks in HID report descriptor fixups.

FWIW, a good ad-hoc way to look for usages of allocation functions in
these is:

  awk '/static.*report_fixup.*/,/^}/ { print FILENAME, $0 }' drivers/hid/hid-*.c \
    | grep -E '(malloc|kzalloc|kcalloc|kmemdup)'

The devm_* variants are safe in this context, because they tie the
allocated memory to the lifetime of the driver.

For transparency, I generated these commits with Gemini-CLI,
starting with this prompt:

    We are working in the Linux kernel. In the HID drivers in
    `drivers/hid/hid-*.c`, the `report_fixup` driver hook is a function
    that gets a byte buffer (with size) as input and that may modify that
    byte buffer, and optionally return a pointer to a new byte buffer and
    update the size.  The returned value is *not* memory-managed by the
    caller though and will not be freed subsequently.  When the
    `report_fixup` implementation allocates a new buffer and returns that,
    that will not get freed by the caller.  Validate this assessment and
    fix up all HID drivers where that mistake is made.

(and then a little bit of additional nudging for the details).

—Günther

---

Revision history:

V2:

  * Add a commit documenting the memory allocation properties of report_fixup().
  * Reword the commit message for the three memory leak fixes.

V1: Original patch set

https://lore.kernel.org/all/20260217160125.1097578-1-gnoack@google.com/


Günther Noack (4):
  HID: Document memory allocation properties of report_fixup()
  HID: apple: avoid memory leak in apple_report_fixup()
  HID: magicmouse: avoid memory leak in magicmouse_report_fixup()
  HID: asus: avoid memory leak in asus_report_fixup()

 drivers/hid/hid-apple.c      |  4 +---
 drivers/hid/hid-asus.c       | 15 +++++++++++----
 drivers/hid/hid-magicmouse.c |  4 +---
 include/linux/hid.h          |  6 ++++++
 4 files changed, 19 insertions(+), 10 deletions(-)

-- 
2.53.0.371.g1d285c8824-goog
Re: [PATCH v2 0/4] HID: Fix some memory leaks in drivers/hid
Posted by Benjamin Tissoires 1 month, 1 week ago
On Thu, 19 Feb 2026 16:43:34 +0100, Günther Noack wrote:
> These patches fix a few memory leaks in HID report descriptor fixups.
> 
> FWIW, a good ad-hoc way to look for usages of allocation functions in
> these is:
> 
>   awk '/static.*report_fixup.*/,/^}/ { print FILENAME, $0 }' drivers/hid/hid-*.c \
>     | grep -E '(malloc|kzalloc|kcalloc|kmemdup)'
> 
> [...]

Applied to hid/hid.git (for-7.0/upstream-fixes), thanks!

[1/4] HID: Document memory allocation properties of report_fixup()
      https://git.kernel.org/hid/hid/c/6b3e458806e3
[2/4] HID: apple: avoid memory leak in apple_report_fixup()
      https://git.kernel.org/hid/hid/c/239c15116d80
[3/4] HID: magicmouse: avoid memory leak in magicmouse_report_fixup()
      https://git.kernel.org/hid/hid/c/91e8c6e601bd
[4/4] HID: asus: avoid memory leak in asus_report_fixup()
      https://git.kernel.org/hid/hid/c/2bad24c17742

Cheers,
-- 
Benjamin Tissoires <bentiss@kernel.org>