From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388477; b=GoHHePcYQIeYY8fPtLhg5WeD4rfWvb55UC1xtHxvIq7LeNzxNHLSzVn2+fKTNl+7Gxou JoPonjOVUw6tERQTp4efax9seGSGMnMS5yf6E7Mzt/EOHdnnA9//IXCBpF86ow8+OZqVg Ln7FxwR4CW/ysp7XCz2K6SLL++jDF1Wm60jJfolgXmHHLsHZSW108NNOYu/XAcXaeoLl9 4ZyXyI+gs5y4BZaPvMkmi6ecbp8CL6bnfstdMDsDcXhsKceUdW2j1a/+yqbAi8MvYRDBM 8oXR0751l5pSuVj+7UddWBuY0uDNxaCqLeIWtI/L3w3GhSybC0k72wOlVRJvfV2HUBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388477; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=RR23RgX7wv6OqbU4RdZCKxQBiv3vM7XYuxpvZbe4lt8=; b=lAegdbY3YNdwiGvRUAKMvl5x7AkVjmHrvzlHk0B1sEXzrWYI576vV0WRwGE2h3saUlsG lj7HXfFK/jtD5lNxlq5Ze1dJsKl4H7bxYmqHsq/goicvLCyX5aXQgENXpVFjJ0BKCE1jq /6DyrNZgmF2eBRI234yMAjLc3hAqeWtej86a0buTMY5txHCkyBO38bZA7gYOvWI8roIgZ lZMJ3EZLHClFuQ3/7A7ZHC8MKNKtZ0AioIh88ck3hLgEoYq+CW9DFCH5vn0hfzIyO0vLk oFwp5nLKwlg+nHrQ2/VGv1+feZe5bjAkjFUpJtHjK57Uvk4mQLrjF6jiwKGSMeL7Bow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="aFmp/FVY"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B062606A322; Fri, 4 Dec 2026 12:47:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=aFmp/FVY Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECD351F000CE; Fri, 4 Dec 2026 12:47:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388477; bh=RR23RgX7wv6OqbU4RdZCKxQBiv3vM7XYuxpvZbe4lt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=aFmp/FVYX6qGKgvEYZ07IYL8WTg5EG6IaUcE7yyZwlqgl18dQcIcdRD+MItKdUEPL 7EnSrtnZjve8o5IbsFpVQv26pd9emcWmL5hbAfFkwu0PVMES0KL4VyHhYoeiUmsGbu WfUsp126e1y0l9d//y74Cd8o6iLXFSbikcVursV4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, stable , Jiri Slaby , Nicolas Pitre , Kees Cook , Joshua Rogers , Chris Mason , Helge Deller , Thomas Zimmermann , Mingyu Wang <25181214217@stu.xidian.edu.cn>, Randy Dunlap , Ronan Pigott , Liav Mordouch Subject: [PATCH 6.1 0579/1059] vt: serialise the producers of the flip buffer Date: Fri, 4 Dec 2026 19:01:50 +0900 Message-ID: <20261204100659.158980055@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chris Mason commit 5d1d814408c1c8e797031eac8ddd40de1a9e8e93 upstream. Typing on a virtual console while the console replies to an escape sequence can make the kernel write past the end of a buffer. A user who can write to the foreground console can cause this on purpose if they can also inject key events, through /dev/uinput or by writing to the /dev/input/event* node of a keyboard. Input for a tty goes through the flip buffer of its port. A driver inserts the data with tty_insert_flip_*() and then calls tty_flip_buffer_push() to pass it on to the line discipline. The insert functions take no lock: they check that the current struct tty_buffer has room, copy the data to offset ->used, and then advance ->used. So a flip buffer supports only one producer at a time, and a driver that has several must serialise them. Four functions feed the flip buffer of a virtual console: - put_queue(), puts_queue() and fn_send_intr() insert typed characters and breaks for the keyboard handler. They run under kbd_event_lock. - respond_string() inserts the reply when a program writes an escape sequence that requests one, such as a cursor position report. It runs under the console lock. The keyboard handler gets its events from the input layer and feeds the foreground console. Key events that a program writes to /dev/uinput or to an event node reach it in the same way as typed keys. The keyboard handler and respond_string() share no lock, so they can interleave: keyboard handler respond_string() ================ ================ room check passes room check passes memcpy() to offset used used += len memcpy() to offset used used += len /* used > size of the buffer */ The room check subtracts ->used from the size, and both are unsigned. Once ->used exceeds the size, the result wraps, so the check always passes, and each later insert lands further past the buffer. Reproduced as root, with keys from a uinput keyboard and cursor position reports on the same console: KFENCE reported an out-of-bounds write 1,024 bytes past the buffer in under 10 s. Fix this by taking port->lock, with interrupts disabled, around the insert and the push in these four functions. kbd_keycode() already takes port->lock under kbd_event_lock, so this lock ordering is not new. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable Cc: Jiri Slaby Cc: Nicolas Pitre Cc: Kees Cook Cc: Joshua Rogers Cc: Chris Mason Cc: Helge Deller Cc: Thomas Zimmermann Cc: Mingyu Wang <25181214217@stu.xidian.edu.cn> Cc: Randy Dunlap Cc: Ronan Pigott Cc: Liav Mordouch Signed-off-by: Chris Mason Signed-off-by: Greg Kroah-Hartman Link: https://patch.msgid.link/20261027-tty-llm-v1-1-99b0202ae546@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman --- drivers/tty/vt/keyboard.c | 6 ++++++ drivers/tty/vt/vt.c | 2 ++ 2 files changed, 8 insertions(+) --- a/drivers/tty/vt/keyboard.c +++ b/drivers/tty/vt/keyboard.c @@ -321,15 +321,20 @@ int kbd_rate(struct kbd_repeat *rpt) /* * Helper Functions. + * + * A flip buffer allows one producer at a time, and respond_string() in vt.c + * feeds the same one under the console lock. So take the lock of the port. */ static void put_queue(struct vc_data *vc, int ch) { + guard(spinlock_irqsave)(&vc->port.lock); tty_insert_flip_char(&vc->port, ch, 0); tty_flip_buffer_push(&vc->port); } static void puts_queue(struct vc_data *vc, const char *cp) { + guard(spinlock_irqsave)(&vc->port.lock); tty_insert_flip_string(&vc->port, cp, strlen(cp)); tty_flip_buffer_push(&vc->port); } @@ -590,6 +595,7 @@ static void fn_inc_console(struct vc_dat static void fn_send_intr(struct vc_data *vc) { + guard(spinlock_irqsave)(&vc->port.lock); tty_insert_flip_char(&vc->port, 0, TTY_BREAK); tty_flip_buffer_push(&vc->port); } --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -1834,6 +1834,8 @@ static void csi_m(struct vc_data *vc) static void respond_string(const char *p, size_t len, struct tty_port *port) { + /* The keyboard handler feeds the same flip buffer. */ + guard(spinlock_irqsave)(&port->lock); tty_insert_flip_string(port, p, len); tty_flip_buffer_push(port); }