Linux Input/HID development
 help / color / mirror / Atom feed
From: Jinmo Yang <jinmo44.yang@gmail.com>
To: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Cc: jikos@kernel.org, benjamin.tissoires@redhat.com,
	stable@vger.kernel.org, Jinmo Yang <jinmo44.yang@gmail.com>
Subject: [PATCH v2] HID: wacom: fix slab-out-of-bounds write in wacom_wac_queue_insert
Date: Fri, 29 May 2026 02:59:45 +0900	[thread overview]
Message-ID: <20260528175945.2987781-1-jinmo44.yang@gmail.com> (raw)
In-Reply-To: <20260524135203.1996265-1-jinmo44.yang@gmail.com>

wacom_wac_queue_insert() calls kfifo_skip() in a loop when the kfifo
doesn't have enough space for the incoming report. If the kfifo is
empty, kfifo_skip() reads stale data left in the kmalloc'd buffer
via __kfifo_peek_n() and interprets it as a record length, advancing
fifo->out by that garbage value. This corrupts the internal kfifo
state, causing kfifo_unused() to return a value much larger than the
actual buffer size, which bypasses __kfifo_in_r()'s guard:

  if (len + recsize > kfifo_unused(fifo))
      return 0;

kfifo_copy_in() then performs an out-of-bounds memcpy, writing up to
3842 bytes past the 256-byte buffer.

Add a !kfifo_is_empty() condition to the while loop so kfifo_skip()
is never called on an empty fifo, and check the return value of
kfifo_in() to reject reports that are too large for the fifo.

Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Fixes: 5e013ad20689 ("HID: wacom: Remove static WACOM_PKGLEN_MAX limit")
Cc: stable@vger.kernel.org
Signed-off-by: Jinmo Yang <jinmo44.yang@gmail.com>
---
Changes in v2:
- Instead of a size check at the top, add !kfifo_is_empty() to the
  while loop condition to prevent kfifo_skip() on an empty fifo
  (Suggested by Dmitry Torokhov)
- Check kfifo_in() return value to reject oversized reports instead
  of a separate guard

 drivers/hid/wacom_sys.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c
index a32320b35..489ca68f1 100644
--- a/drivers/hid/wacom_sys.c
+++ b/drivers/hid/wacom_sys.c
@@ -54,7 +54,7 @@ static void wacom_wac_queue_insert(struct hid_device *hdev,
 {
 	bool warned = false;
 
-	while (kfifo_avail(fifo) < size) {
+	while (kfifo_avail(fifo) < size && !kfifo_is_empty(fifo)) {
 		if (!warned)
 			hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
 		warned = true;
@@ -62,7 +62,9 @@ static void wacom_wac_queue_insert(struct hid_device *hdev,
 		kfifo_skip(fifo);
 	}
 
-	kfifo_in(fifo, raw_data, size);
+	if (!kfifo_in(fifo, raw_data, size))
+		hid_warn_ratelimited(hdev, "%s: report is too large (%d)\n",
+				     __func__, size);
 }
 
 static void wacom_wac_queue_flush(struct hid_device *hdev,
-- 
2.53.0


  parent reply	other threads:[~2026-05-28 17:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-24 13:52 [PATCH 0/1] HID: wacom: fix slab-out-of-bounds write in kfifo_copy_in Jinmo Yang
2026-05-24 13:52 ` [PATCH 1/1] HID: wacom: validate report size before kfifo insert Jinmo Yang
2026-05-24 14:31   ` sashiko-bot
2026-05-27 19:47   ` Dmitry Torokhov
2026-05-27 21:41     ` Dmitry Torokhov
2026-05-28 17:59 ` Jinmo Yang [this message]
2026-05-28 18:42   ` [PATCH v2] HID: wacom: fix slab-out-of-bounds write in wacom_wac_queue_insert sashiko-bot
2026-05-29 21:33     ` Dmitry Torokhov
2026-05-29 21:34   ` Dmitry Torokhov

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=20260528175945.2987781-1-jinmo44.yang@gmail.com \
    --to=jinmo44.yang@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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