From: Benjamin Tissoires <bentiss@kernel.org>
To: Jiri Kosina <jikos@kernel.org>,
Peter Hutterer <peter.hutterer@who-t.net>,
Vicki Pfau <vi@endrift.com>, Shuah Khan <shuah@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH HID v3 3/9] HID: core: remove one more kmemdup on .probe()
Date: Tue, 01 Oct 2024 16:30:07 +0200 [thread overview]
Message-ID: <20241001-hid-bpf-hid-generic-v3-3-2ef1019468df@kernel.org> (raw)
In-Reply-To: <20241001-hid-bpf-hid-generic-v3-0-2ef1019468df@kernel.org>
That last kmemdup while opening the report descriptor was required to
have a common kfree() on it.
Move that kmemdup in the only special case it's required (if there is a
.report_fixup()), and add a more elaborated check before freeing
hdev->rdesc, to avoid a double free.
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
---
no changes in v3
new in v2
---
drivers/hid/hid-core.c | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 6053e7cdc0c1..b0a22e173502 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -685,7 +685,14 @@ static void hid_close_report(struct hid_device *device)
INIT_LIST_HEAD(&report_enum->report_list);
}
- kfree(device->rdesc);
+ /*
+ * If the HID driver had a rdesc_fixup() callback, dev->rdesc
+ * will be allocated by hid-core and needs to be freed.
+ * Otherwise, it is either equal to dev_rdesc or bpf_rdesc, in
+ * which cases it'll be freed later on device removal or destroy.
+ */
+ if (device->rdesc != device->dev_rdesc && device->rdesc != device->bpf_rdesc)
+ kfree(device->rdesc);
device->rdesc = NULL;
device->rsize = 0;
@@ -1214,7 +1221,6 @@ int hid_open_report(struct hid_device *device)
struct hid_item item;
unsigned int size;
const __u8 *start;
- __u8 *buf = NULL;
const __u8 *end;
const __u8 *next;
int ret;
@@ -1241,17 +1247,23 @@ int hid_open_report(struct hid_device *device)
* on a copy of our report descriptor so it can
* change it.
*/
- buf = kmemdup(start, size, GFP_KERNEL);
+ __u8 *buf = kmemdup(start, size, GFP_KERNEL);
+
if (buf == NULL)
return -ENOMEM;
start = device->driver->report_fixup(device, buf, &size);
- }
- start = kmemdup(start, size, GFP_KERNEL);
- kfree(buf);
- if (start == NULL)
- return -ENOMEM;
+ /*
+ * The second kmemdup is required in case report_fixup() returns
+ * a static read-only memory, but we have no idea if that memory
+ * needs to be cleaned up or not at the end.
+ */
+ start = kmemdup(start, size, GFP_KERNEL);
+ kfree(buf);
+ if (start == NULL)
+ return -ENOMEM;
+ }
device->rdesc = start;
device->rsize = size;
--
2.46.0
next prev parent reply other threads:[~2024-10-01 14:30 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-01 14:30 [PATCH HID v3 0/9] HID: bpf: add a new hook to control hid-generic Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 1/9] HID: bpf: move HID-BPF report descriptor fixup earlier Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 2/9] HID: core: save one kmemdup during .probe() Benjamin Tissoires
2024-10-01 14:30 ` Benjamin Tissoires [this message]
2024-10-01 14:30 ` [PATCH HID v3 4/9] HID: bpf: allow write access to quirks field in struct hid_device Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 5/9] selftests/hid: add dependency on hid_common.h Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 6/9] selftests/hid: cleanup C tests by adding a common struct uhid_device Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 7/9] selftests/hid: allow to parametrize bus/vid/pid/rdesc on the test device Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 8/9] HID: add per device quirk to force bind to hid-generic Benjamin Tissoires
2024-10-01 14:30 ` [PATCH HID v3 9/9] selftests/hid: add test for assigning a given device " Benjamin Tissoires
2024-10-02 22:43 ` [PATCH HID v3 0/9] HID: bpf: add a new hook to control hid-generic Shuah Khan
2024-10-04 14:12 ` Benjamin Tissoires
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241001-hid-bpf-hid-generic-v3-3-2ef1019468df@kernel.org \
--to=bentiss@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=peter.hutterer@who-t.net \
--cc=shuah@kernel.org \
--cc=vi@endrift.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).