Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Linmao Li" <lilinmao@kylinos.cn>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH] Input: matrix-keymap - reject keycodes above KEY_MAX
Date: Wed, 09 Sep 2026 10:19:34 +0000	[thread overview]
Message-ID: <20260909101935.578E31F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260909100739.3107556-1-lilinmao@kylinos.cn>

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

Pre-existing issues:
- [High] Missing bounds checks on matrix dimensions causes an integer overflow when calculating max_keys, leading to an undersized heap allocation and a subsequent out-of-bounds write.
--

commit 821b9503dcfef31a275bc1fec44950b4eda55e94
Author: Linmao Li <lilinmao@kylinos.cn>

Input: matrix-keymap - reject keycodes above KEY_MAX

This commit adds bounds checking for keycodes in matrix_keypad_map_key() to
ensure they do not exceed KEY_MAX. This prevents a malformed firmware or
platform keymap from writing past the bounds of the capability bitmap.

> diff --git a/drivers/input/matrix-keymap.c b/drivers/input/matrix-keymap.c
> index 3bea3575a0a94..e4227e96aa4e7 100644
> --- a/drivers/input/matrix-keymap.c
> +++ b/drivers/input/matrix-keymap.c
> @@ -35,6 +35,13 @@ static bool matrix_keypad_map_key(struct input_dev *input_dev,
>  		return false;
>  	}
>  
> +	if (code > KEY_MAX) {
> +		dev_err(input_dev->dev.parent,
> +			"%s: invalid keycode %u in keymap entry 0x%x\n",
> +			__func__, code, key);
> +		return false;
> +	}
> +
>  	keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;

[Severity: High]
This is a pre-existing issue, but since this patch aims to mitigate malformed
firmware and keymaps causing out-of-bounds writes, does the bounds check
earlier in this function fully protect against integer overflow?

If a maliciously crafted Device Tree provides a large value for
keypad,num-rows (e.g., 0x80000001), it is read without bounds checking. When
matrix_keypad_build_keymap() computes max_keys:

drivers/input/matrix-keymap.c:matrix_keypad_build_keymap() {
    ...
    size_t max_keys = rows << row_shift;
    ...
    keymap = devm_kcalloc(&input_dev->dev, max_keys, sizeof(*keymap),
                          GFP_KERNEL);
}

The 32-bit shift overflows, resulting in a tiny allocation for keymap.

Then in matrix_keypad_map_key(), the earlier bounds check:

    if (row >= rows || col >= cols) {
        return false;
    }

compares the extracted 8-bit row against the large unshifted rows value
(0x80000001), completely bypassing the safety check.

This allows MATRIX_SCAN_CODE to calculate an index far outside the tiny
buffer, causing an out-of-bounds heap write when assigning the keycode here.
Should rows and cols be validated against a maximum sane value before the
shift to prevent this overflow?

>  	__set_bit(code, input_dev->keybit);
>

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

      reply	other threads:[~2026-09-09 10:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09 10:07 [PATCH] Input: matrix-keymap - reject keycodes above KEY_MAX Linmao Li
2026-09-09 10:19 ` 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=20260909101935.578E31F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=lilinmao@kylinos.cn \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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