diff --git a/drivers/hid/spi-hid/spi-hid-apple-core.c b/drivers/hid/spi-hid/spi-hid-apple-core.c index d2b62f57cd8c0..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) { @@ -929,6 +934,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 +966,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);