* [PATCH] HID: hid-ntrig: Fix potential memory leak in ntrig_report_version()
@ 2025-09-17 4:50 Masami Ichikawa
2025-09-17 7:46 ` Markus Elfring
0 siblings, 1 reply; 3+ messages in thread
From: Masami Ichikawa @ 2025-09-17 4:50 UTC (permalink / raw)
To: jikos, bentiss; +Cc: minbell.kim, linux-input, linux-kernel, Masami Ichikawa
The kmalloc() was called before checking hid_is_usb(hdev). If hid_is_usb()
returned false, the function would return early and leak the allocated
memory.
It is safe to move the kmalloc() call after the hid_is_usb() check to avoid
unnecessary allocation and potential memory leak.
Fixes: 185c926283da ("HID: hid-ntrig: fix unable to handle page fault in ntrig_report_version()")
Signed-off-by: Masami Ichikawa <masami256@gmail.com>
---
drivers/hid/hid-ntrig.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/hid-ntrig.c b/drivers/hid/hid-ntrig.c
index 0f76e241e0af..52e8e7fe9681 100644
--- a/drivers/hid/hid-ntrig.c
+++ b/drivers/hid/hid-ntrig.c
@@ -142,11 +142,12 @@ static void ntrig_report_version(struct hid_device *hdev)
int ret;
char buf[20];
struct usb_device *usb_dev = hid_to_usb_dev(hdev);
- unsigned char *data = kmalloc(8, GFP_KERNEL);
+ unsigned char *data = NULL;
if (!hid_is_usb(hdev))
return;
+ data = kmalloc(8, GFP_KERNEL);
if (!data)
goto err_free;
--
2.51.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] HID: hid-ntrig: Fix potential memory leak in ntrig_report_version()
2025-09-17 4:50 [PATCH] HID: hid-ntrig: Fix potential memory leak in ntrig_report_version() Masami Ichikawa
@ 2025-09-17 7:46 ` Markus Elfring
2025-09-18 1:46 ` Masami Ichikawa
0 siblings, 1 reply; 3+ messages in thread
From: Markus Elfring @ 2025-09-17 7:46 UTC (permalink / raw)
To: Masami Ichikawa, linux-input
Cc: LKML, Benjamin Tissoires, Jiri Kosina, Minjong Kim
…
> It is safe to move the kmalloc() call after the hid_is_usb() check to avoid
> unnecessary allocation and potential memory leak.
* See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc6#n94
* How do you think about to increase the application of scope-based resource management?
https://elixir.bootlin.com/linux/v6.17-rc6/source/include/linux/slab.h#L476
* Can a summary phrase like “Prevent memory leak in ntrig_report_version()”
be nicer?
Regards,
Markus
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] HID: hid-ntrig: Fix potential memory leak in ntrig_report_version()
2025-09-17 7:46 ` Markus Elfring
@ 2025-09-18 1:46 ` Masami Ichikawa
0 siblings, 0 replies; 3+ messages in thread
From: Masami Ichikawa @ 2025-09-18 1:46 UTC (permalink / raw)
To: Markus Elfring
Cc: linux-input, LKML, Benjamin Tissoires, Jiri Kosina, Minjong Kim
Thank you for the review.
On Wed, Sep 17, 2025 at 4:46 PM Markus Elfring <Markus.Elfring@web.de> wrote:
>
> …
> > It is safe to move the kmalloc() call after the hid_is_usb() check to avoid
> > unnecessary allocation and potential memory leak.
>
> * See also:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc6#n94
>
> * How do you think about to increase the application of scope-based resource management?
> https://elixir.bootlin.com/linux/v6.17-rc6/source/include/linux/slab.h#L476
>
I think using the DEFINE_FREE macro simplifies cleanup, especially
when an error occurs.
> * Can a summary phrase like “Prevent memory leak in ntrig_report_version()”
> be nicer?
>
I see. I will rewrite commit log.
>
> Regards,
> Markus
Regards,
--
Masami Ichikawa
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-09-18 1:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-17 4:50 [PATCH] HID: hid-ntrig: Fix potential memory leak in ntrig_report_version() Masami Ichikawa
2025-09-17 7:46 ` Markus Elfring
2025-09-18 1:46 ` Masami Ichikawa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).