The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [BUG] tty: sysrq: duplicate input handler registration on v7.4-rc5
@ 2026-12-02  8:18 Rahul Priolkar
  2026-12-02  9:00 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Rahul Priolkar @ 2026-12-02  8:18 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-serial, linux-kernel, Dmitry Torokhov, linux-input

Hello,

On Linux v7.4-rc5 (fe6ff8d5d675), we observed duplicate entries for
the SysRq input handler on a defconfig-based x86_64 guest. In each
trial, we updated kernel.sysrq and checked /proc/bus/input/handlers
for exactly one SysRq entry. With concurrent updates, the handler
appeared 60 times in one listing after 98 trials. Updating kernel.sysrq
requires root in the initial user namespace.

Our reading of drivers/tty/sysrq.c is that sysrq_toggle_support() can
register the static handler twice because the enable-state check and
registration are not serialized. Does that match your understanding of
the failure?

A debug build with DEBUG_LIST and BUG_ON_DATA_CORRUPTION stopped in
the first run. The relevant frames were:

  kernel BUG at lib/list_debug.c:37!
  RIP: 0010:__list_add_valid_or_report
   input_register_handler
   sysrq_register_handler
   sysrq_sysctl_handler

On the unmodified kernel, we ran the updates one at a time as a
control. All 3,000 trials showed exactly one handler entry. We then
tested an rc5 build that included a mutex around sysrq_toggle_support()
along with our other candidate fixes. Across 6,000 concurrent-update
trials, we saw no duplicate entries or kernel warnings. I can share
the candidate patch if useful.

Thanks,
Rahul Priolkar

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] tty: sysrq: duplicate input handler registration on v7.4-rc5
  2026-12-02  8:18 [BUG] tty: sysrq: duplicate input handler registration on v7.4-rc5 Rahul Priolkar
@ 2026-12-02  9:00 ` Greg Kroah-Hartman
  2026-12-02  9:23   ` [PATCH] tty: sysrq: serialize sysrq_toggle_support() Rahul Priolkar
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-02  9:00 UTC (permalink / raw)
  To: Rahul Priolkar
  Cc: Jiri Slaby, linux-serial, linux-kernel, Dmitry Torokhov,
	linux-input

On Wed, Dec 02, 2026 at 12:18:32AM -0800, Rahul Priolkar wrote:
> Hello,
> 
> On Linux v7.4-rc5 (fe6ff8d5d675), we observed duplicate entries for
> the SysRq input handler on a defconfig-based x86_64 guest. In each
> trial, we updated kernel.sysrq and checked /proc/bus/input/handlers
> for exactly one SysRq entry. With concurrent updates, the handler
> appeared 60 times in one listing after 98 trials. Updating kernel.sysrq
> requires root in the initial user namespace.
> 
> Our reading of drivers/tty/sysrq.c is that sysrq_toggle_support() can
> register the static handler twice because the enable-state check and
> registration are not serialized. Does that match your understanding of
> the failure?
> 
> A debug build with DEBUG_LIST and BUG_ON_DATA_CORRUPTION stopped in
> the first run. The relevant frames were:
> 
>   kernel BUG at lib/list_debug.c:37!
>   RIP: 0010:__list_add_valid_or_report
>    input_register_handler
>    sysrq_register_handler
>    sysrq_sysctl_handler
> 
> On the unmodified kernel, we ran the updates one at a time as a
> control. All 3,000 trials showed exactly one handler entry. We then
> tested an rc5 build that included a mutex around sysrq_toggle_support()
> along with our other candidate fixes. Across 6,000 concurrent-update
> trials, we saw no duplicate entries or kernel warnings. I can share
> the candidate patch if useful.

Always send a patch.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] tty: sysrq: serialize sysrq_toggle_support()
  2026-12-02  9:00 ` Greg Kroah-Hartman
@ 2026-12-02  9:23   ` Rahul Priolkar
  2026-12-02  9:47     ` Greg Kroah-Hartman
  2026-12-02  9:57     ` Greg Kroah-Hartman
  0 siblings, 2 replies; 5+ messages in thread
From: Rahul Priolkar @ 2026-12-02  9:23 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: linux-serial, linux-kernel, Dmitry Torokhov, linux-input

Hi,

Thanks for the response. Here's the patch.

-- >8 --

Concurrent enable-mask updates in sysrq_toggle_support() can both
register the static SysRq input handler, corrupting input_handler_list.

Serialize the mask update and the corresponding handler registration or
unregistration with a mutex. Both callers run in process context.

Fixes: 97f5f0cd8cd0 ("Input: implement SysRq as a separate input handler")
Cc: stable@vger.kernel.org
Signed-off-by: Rahul Priolkar <rahul.priolkar@asu.edu>
---
 drivers/tty/sysrq.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index f4571d05b..882d7ae43 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -1100,11 +1100,17 @@ static inline void sysrq_unregister_handler(void)
 
 #endif /* CONFIG_INPUT */
 
+/* Serializes enable-mask updates with the handler (un)registration they imply. */
+static DEFINE_MUTEX(sysrq_toggle_mutex);
+
 int sysrq_toggle_support(int enable_mask)
 {
-	bool was_enabled = sysrq_on();
+	bool was_enabled;
 
-	sysrq_enabled = enable_mask;
+	guard(mutex)(&sysrq_toggle_mutex);
+
+	was_enabled = sysrq_on();
+	WRITE_ONCE(sysrq_enabled, enable_mask);
 
 	if (was_enabled != sysrq_on()) {
 		if (sysrq_on())
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: sysrq: serialize sysrq_toggle_support()
  2026-12-02  9:23   ` [PATCH] tty: sysrq: serialize sysrq_toggle_support() Rahul Priolkar
@ 2026-12-02  9:47     ` Greg Kroah-Hartman
  2026-12-02  9:57     ` Greg Kroah-Hartman
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-02  9:47 UTC (permalink / raw)
  To: Rahul Priolkar
  Cc: Jiri Slaby, linux-serial, linux-kernel, Dmitry Torokhov,
	linux-input

On Wed, Dec 02, 2026 at 01:23:06AM -0800, Rahul Priolkar wrote:
> Hi,
> 
> Thanks for the response. Here's the patch.
> 
> -- >8 --
> 
> Concurrent enable-mask updates in sysrq_toggle_support() can both
> register the static SysRq input handler, corrupting input_handler_list.
> 
> Serialize the mask update and the corresponding handler registration or
> unregistration with a mutex. Both callers run in process context.
> 
> Fixes: 97f5f0cd8cd0 ("Input: implement SysRq as a separate input handler")
> Cc: stable@vger.kernel.org
> Signed-off-by: Rahul Priolkar <rahul.priolkar@asu.edu>
> ---
>  drivers/tty/sysrq.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
> index f4571d05b..882d7ae43 100644
> --- a/drivers/tty/sysrq.c
> +++ b/drivers/tty/sysrq.c
> @@ -1100,11 +1100,17 @@ static inline void sysrq_unregister_handler(void)
>  
>  #endif /* CONFIG_INPUT */
>  
> +/* Serializes enable-mask updates with the handler (un)registration they imply. */
> +static DEFINE_MUTEX(sysrq_toggle_mutex);
> +
>  int sysrq_toggle_support(int enable_mask)
>  {
> -	bool was_enabled = sysrq_on();
> +	bool was_enabled;
>  
> -	sysrq_enabled = enable_mask;
> +	guard(mutex)(&sysrq_toggle_mutex);
> +
> +	was_enabled = sysrq_on();
> +	WRITE_ONCE(sysrq_enabled, enable_mask);

Why WRITE_ONCE if you have a mutex?

And you have a mutex that only works for writing?

what exactly is the "bug" here you are trying to fix?

confused,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: sysrq: serialize sysrq_toggle_support()
  2026-12-02  9:23   ` [PATCH] tty: sysrq: serialize sysrq_toggle_support() Rahul Priolkar
  2026-12-02  9:47     ` Greg Kroah-Hartman
@ 2026-12-02  9:57     ` Greg Kroah-Hartman
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-02  9:57 UTC (permalink / raw)
  To: Rahul Priolkar
  Cc: Jiri Slaby, linux-serial, linux-kernel, Dmitry Torokhov,
	linux-input

On Wed, Dec 02, 2026 at 01:23:06AM -0800, Rahul Priolkar wrote:
> Hi,
> 
> Thanks for the response. Here's the patch.
> 
> -- >8 --
> 
> Concurrent enable-mask updates in sysrq_toggle_support() can both
> register the static SysRq input handler, corrupting input_handler_list.

Wait, this is a root-only thing, right?  So just "don't do that"?  :)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02  9:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  8:18 [BUG] tty: sysrq: duplicate input handler registration on v7.4-rc5 Rahul Priolkar
2026-12-02  9:00 ` Greg Kroah-Hartman
2026-12-02  9:23   ` [PATCH] tty: sysrq: serialize sysrq_toggle_support() Rahul Priolkar
2026-12-02  9:47     ` Greg Kroah-Hartman
2026-12-02  9:57     ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox