]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
HID: hid-ntrig: Prevent memory leak in ntrig_report_version()
authorMasami Ichikawa <masami256@gmail.com>
Sun, 21 Sep 2025 05:31:02 +0000 (14:31 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 6 Dec 2025 21:12:35 +0000 (06:12 +0900)
[ Upstream commit 53f731f5bba0cf03b751ccceb98b82fadc9ccd1e ]

Use a scope-based cleanup helper for the buffer allocated with kmalloc()
in ntrig_report_version() to simplify the cleanup logic and prevent
memory leaks (specifically the !hid_is_usb()-case one).

[jkosina@suse.com: elaborate on the actual existing leak]
Fixes: 185c926283da ("HID: hid-ntrig: fix unable to handle page fault in ntrig_report_version()")
Signed-off-by: Masami Ichikawa <masami256@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/hid/hid-ntrig.c

index a1128c5315fffacb0bdf2a9e771f48acc6a0616a..3c41f6841f7753cd065d9851b95d3e3d1d1aa310 100644 (file)
@@ -142,13 +142,13 @@ 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 __free(kfree) = kmalloc(8, GFP_KERNEL);
 
        if (!hid_is_usb(hdev))
                return;
 
        if (!data)
-               goto err_free;
+               return;
 
        ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
                              USB_REQ_CLEAR_FEATURE,
@@ -163,9 +163,6 @@ static void ntrig_report_version(struct hid_device *hdev)
                hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n",
                         buf, data[2], data[3], data[4], data[5]);
        }
-
-err_free:
-       kfree(data);
 }
 
 static ssize_t show_phys_width(struct device *dev,