Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiangshan Yi" <yijiangshan@kylinos.cn>
Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org
Subject: Re: [PATCH v2 3/3] HID: mcp2221: validate report size in mcp2221_raw_event()
Date: Tue, 28 Jul 2026 13:28:45 +0000	[thread overview]
Message-ID: <20260728132845.BEFF61F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260728131442.834604-4-yijiangshan@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Unprotected Time-Of-Check to Time-Of-Use (TOCTOU) race condition in `mcp->rxbuf` access leads to a Use-After-Free and kernel stack corruption.
- [High] The patch incompletely fixes out-of-bounds reads by only validating the payload length for `MCP2221_I2C_GET_DATA`, leaving other command branches vulnerable.
- [High] Data race on the shared `mcp->txbuf` buffer and `mcp->gp_idx` during GPIO operations corrupts concurrent USB command payloads.
- [High] Lack of request/response sequence tracking leads to completion crosstalk and state machine corruption.
--

--- Patch [3]: [PATCH v2 3/3] HID: mcp2221: validate report size in mcp2221_raw_event() ---
commit 771b21acaed8d565e6718447cf1dd1d9d3045324
Author: Jiangshan Yi <yijiangshan@kylinos.cn>

HID: mcp2221: validate report size in mcp2221_raw_event()

This patch adds a minimum size check to incoming HID reports in
mcp2221_raw_event() and validates that the source range fits within the
received report before performing a memcpy in the MCP2221_I2C_GET_DATA path.
This prevents a malicious USB device from causing an out-of-bounds read
that could leak uninitialized kernel memory.

> diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c
> index 9e03d1f733ff8..d52ce3531ab70 100644
> --- a/drivers/hid/hid-mcp2221.c
> +++ b/drivers/hid/hid-mcp2221.c
> @@ -865,6 +865,9 @@ static int mcp2221_raw_event(struct hid_device *hdev,
>  	u8 *buf;
>  	struct mcp2221 *mcp = hid_get_drvdata(hdev);
>  
> +	if (size < 4)
> +		return 0;
> +

[Severity: High]
This is a pre-existing issue, but while this patch adds a size check for
the MCP2221_I2C_GET_DATA command, does it leave other commands vulnerable?

For instance, the MCP2221_I2C_PARAM_OR_STATUS branch copies from data[50]:

drivers/hid/hid-mcp2221.c:mcp2221_raw_event() {
    ...
    case MCP2221_I2C_PARAM_OR_STATUS:
        ...
        memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
    ...
}

And MCP2221_GET_SRAM_SETTINGS accesses data[22]:

drivers/hid/hid-mcp2221.c:mcp2221_raw_event() {
    ...
    case MCP2221_GET_SRAM_SETTINGS:
        ...
        memcpy(&mcp->mode, &data[22], 4);
    ...
}

Could a short USB report of 4 bytes still leak uninitialized memory through
these paths via the IIO subsystem?

>  	switch (data[0]) {
>  
>  	case MCP2221_I2C_WR_DATA:
[ ... ]
> @@ -930,6 +933,10 @@ static int mcp2221_raw_event(struct hid_device *hdev,
>  					mcp->status = -EINVAL;
>  					break;
>  				}
> +				if (4 + data[3] > size) {
> +					mcp->status = -EINVAL;
> +					break;
> +				}
>  				buf = mcp->rxbuf;
>  				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);

[Severity: Critical]
This isn't a bug introduced by this patch, but is there an unprotected
race condition between the interrupt handler and process context here?

If an I2C read times out, mcp_i2c_smbus_read() clears mcp->rxbuf in
process context:

drivers/hid/hid-mcp2221.c:mcp_i2c_smbus_read() {
    ...
out:
    mcp->rxbuf = NULL;
    mcp->rxbuf_size = 0;
    ...
}

If a delayed USB response arrives concurrently, could mcp2221_raw_event()
check mcp->rxbuf successfully earlier in the function, but before this
memcpy runs, the process context clears it and destroys the stack frame,
causing the memcpy to corrupt kernel stack memory?

>  				mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];


[Severity: High]
This isn't a bug introduced by this patch, but does the unconditional
completion of the wait queue lead to state machine corruption?

drivers/hid/hid-mcp2221.c:mcp2221_raw_event() {
    ...
    complete(&mcp->wait_in_report);
    ...
}

If a command times out and mcp_cancel_last_cmd() resets the wait state,
could a delayed hardware response for the original command trigger this
complete() call, falsely waking up the cancellation command or subsequent
transactions early?


[Severity: High]
This is a pre-existing issue, but does mutating shared state before
acquiring the lock in the GPIO handlers cause a data race?

In mcp_gpio_set(), mcp->txbuf and mcp->gp_idx are modified before taking
mcp->lock:

drivers/hid/hid-mcp2221.c:mcp_gpio_set() {
    ...
    memset(mcp->txbuf, 0, 18);
    mcp->txbuf[0] = MCP2221_GPIO_SET;
    mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
    mcp->txbuf[mcp->gp_idx - 1] = 1;
    mcp->txbuf[mcp->gp_idx] = !!value;

    mutex_lock(&mcp->lock);
    ...
}

If another thread is concurrently executing mcp_i2c_xfer() and assembling its
I2C payload in mcp->txbuf under the lock, could this zero out and corrupt
the I2C payload while it is being actively sent? Similar unprotected
modifications also appear to occur in mcp_gpio_get().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260728131442.834604-1-yijiangshan@kylinos.cn?part=3

      reply	other threads:[~2026-07-28 13:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 13:14 [PATCH v2 0/3] DO-NOT-MERGE: HID: mcp2221: stop device IO before hid_hw_stop and fix UAF/OOB-read Jiangshan Yi
2026-07-28 13:14 ` [PATCH v2 1/3] HID: mcp2221: stop device IO before hid_hw_stop Jiangshan Yi
2026-07-28 13:14 ` [PATCH v2 2/3] HID: mcp2221: clear rxbuf after I2C/SMBus transfer completes Jiangshan Yi
2026-07-28 13:30   ` sashiko-bot
2026-07-28 13:14 ` [PATCH v2 3/3] HID: mcp2221: validate report size in mcp2221_raw_event() Jiangshan Yi
2026-07-28 13:28   ` sashiko-bot [this message]

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=20260728132845.BEFF61F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yijiangshan@kylinos.cn \
    /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