From a196ccc3b434baa8e758f633e7badc1a19ef05cc Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:46 +0000 Subject: [PATCH 1/2] HID: spi-hid-apple: bound reassembled reports to the report buffer spihid_assemble_message() joins multi-packet input reports in rep->buf, a SPIHID_MAX_INPUT_REPORT_SIZE (2 KiB) devm_kmalloc() buffer. The first packet's header sets the total size, and the only check was that offset, length and remain fit in a u16. A first packet announcing a report of up to 64 KiB was accepted, and each later packet was copied to rep->buf + offset, so the packets after the first 2 KiB wrote past the buffer, and the finished report was then read back at its full length. Drop a report whose announced size does not fit the buffer, and any packet that would write past it, with a rate-limited warning. Also drop the partial report when a later packet disagrees about the total size, so its bytes are not joined to a different report. Verified with a host harness built from the real old and new function text under ASan and UBSan. The old code hits a heap-buffer-overflow (a 246-byte write just past the 2 KiB buffer) on a 20,000-byte report. The new code delivers 247, 500, 1000, 2047 and 2048-byte reports unchanged, drops 2049 and 20,000-byte reports, a stray packet at offset 60,000 and a first packet with remain 65,289, drops a partial report whose total changes, and still delivers a good report afterwards. The kernel object was not built here, and no keyboard or trackpad hardware was used. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/hid/spi-hid/spi-hid-apple-core.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/drivers/hid/spi-hid/spi-hid-apple-core.c b/drivers/hid/spi-hid/spi-hid-apple-core.c index d2b62f57cd8c0..0ae1eb38aa999 100644 --- a/drivers/hid/spi-hid/spi-hid-apple-core.c +++ b/drivers/hid/spi-hid/spi-hid-apple-core.c @@ -929,6 +929,24 @@ static void spihid_assemble_message(struct spihid_apple *spihid, rep->length = 0; } + /* + * The packet header sets the report size, up to 64 KiB, but rep->buf + * holds SPIHID_MAX_INPUT_REPORT_SIZE bytes. Drop a report that does + * not fit, and any packet that would write past the buffer, so a bad + * header cannot overrun the heap. A later packet must also end where + * the first one said the report ends; that is checked below. + */ + if ((offset == 0 && length + remain > SPIHID_MAX_INPUT_REPORT_SIZE) || + offset + length > SPIHID_MAX_INPUT_REPORT_SIZE) { + dev_warn_ratelimited(dev, "oversized report off:%zu len:%zu remain:%zu\n", + offset, length, remain); + rep->device = 0; + rep->flags = 0; + rep->offset = 0; + rep->length = 0; + return; + } + if (offset == 0) { if (rep->offset != 0) { dev_warn(dev, "incomplete report off:%u len:%u", @@ -943,6 +961,11 @@ static void spihid_assemble_message(struct spihid_apple *spihid, if (offset + length + remain != rep->length) { dev_warn(dev, "incomplete report off:%u len:%u", rep->offset, rep->length); + /* Do not join this packet's report to the earlier one. */ + rep->device = 0; + rep->flags = 0; + rep->offset = 0; + rep->length = 0; return; } memcpy(rep->buf + offset, pkt->data, length); From 52d79011553668e52d0db2c50d11d5999078cfd5 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:46 +0000 Subject: [PATCH 2/2] HID: spi-hid-apple: refuse a report descriptor for an interface without a buffer Only the keyboard, trackpad and aux interfaces get a descriptor buffer at probe; the management interface's hid_desc stays NULL. A HID report descriptor message addressed to interface 0 is still accepted by spihid_process_iface_hid_report_desc(), which copies it through that NULL pointer. Refuse a descriptor for an interface that has no buffer. Probe only asks for descriptors of interfaces 1 and up, so a working device is not affected. Checked by reading the probe and message paths. Not built or run on hardware. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/hid/spi-hid/spi-hid-apple-core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/hid/spi-hid/spi-hid-apple-core.c b/drivers/hid/spi-hid/spi-hid-apple-core.c index 0ae1eb38aa999..49d51519b9b5b 100644 --- a/drivers/hid/spi-hid/spi-hid-apple-core.c +++ b/drivers/hid/spi-hid/spi-hid-apple-core.c @@ -776,7 +776,12 @@ static bool spihid_process_iface_hid_report_desc(struct spihid_apple *spihid, { struct spihid_interface *iface = spihid_get_iface(spihid, num); - if (!iface) + /* + * Only the HID interfaces have a descriptor buffer; probe asks for + * theirs alone. An unasked descriptor for the management interface + * would otherwise be copied through its NULL hid_desc. + */ + if (!iface || !iface->hid_desc) return false; if (iface->hid_desc_len == 0) {