Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yingjie Wang" <1075151112@qq.com>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count
Date: Sun, 06 Dec 2026 08:11:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-252259@kernel.org> (raw)
In-Reply-To: <tencent_E59F5D41E556C99EC4443B971E2734FFCA08@qq.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] serial: 8250: missing Fixes trailer for nr_uarts sysfs bug fix
- [Low] serial: 8250: unsynchronized nr_uarts_param access in
  serial8250_init()
--

commit c2dc0eff28b8be5787216dd942c3e3a4a656669f
Author: Yingjie Wang <1075151112@qq.com>

> serial: 8250: Keep the nr_uarts parameter separate from the port count
>
> nr_uarts is the nr_uarts module parameter, and since commit 9d86719f8769
> ("serial: 8250: Allow using ports higher than SERIAL_8250_RUNTIME_UARTS")
> it is also the number of serial8250_ports[] entries that have been set
> up: registering a port when none of them is free sets up the next entry
> and increments nr_uarts.  The parameter is writable through sysfs, so a
> write changes that count behind the driver's back.
>
> A value above UART_NR makes the loops over the ports index past the end
> of serial8250_ports[].  With KASAN, writing 33 to
> /sys/module/8250/parameters/nr_uarts and unbinding the serial8250
> platform device reports a global-out-of-bounds read in
> serial8250_remove().  A smaller value makes serial8250_remove() skip
> ports that are still registered, and the next registration that finds
> no free entry sets up an entry that is in use again: after writing 1,
> rebinding a PCI serial port in a QEMU guest made it take over ttyS1
> from 8250_pnp, with an "ida_free called for id=1 which is not
> allocated" warning.  A larger value lets a registration use an entry
> that was never set up.
>
> Keep the parameter in a variable of its own and only use it when the
> initial ports are set up, where it is still clamped to UART_NR.
> nr_uarts then only counts the entries that have been set up.
> The parameter can still be written, but that no longer affects the
> ports, and reading it returns the configured value instead of the
> current number of entries, as it did before the commit above.
>
> Signed-off-by: Yingjie Wang <1075151112@qq.com>

[Severity: Low]
This fixes a KASAN global-out-of-bounds read and port-tracking corruption
when /sys/module/8250/parameters/nr_uarts is written.  Should it include:

  Fixes: a61c2d78ce61 ("[SERIAL] Make the number of UARTs registered configurable.")
  Fixes: 9d86719f8769 ("serial: 8250: Allow using ports higher than SERIAL_8250_RUNTIME_UARTS")

[ ... ]

> diff --git a/drivers/tty/serial/8250/8250_platform.c b/drivers/tty/serial/8250/8250_platform.c
> index 1a41a4ac25f88..1457652816761 100644
> --- a/drivers/tty/serial/8250/8250_platform.c
> +++ b/drivers/tty/serial/8250/8250_platform.c

[ ... ]

> @@ -62,8 +68,10 @@ static void __init __serial8250_isa_init_ports(void)
>  {
>  	int i;
>  
> +	nr_uarts = nr_uarts_param;
>  	if (nr_uarts > UART_NR)
>  		nr_uarts = UART_NR;
> +	nr_uarts_param = nr_uarts;

[Severity: Low]
Can this access to nr_uarts_param race with sysfs reads or writes when
the driver is built as a module?

load_module() exposes /sys/module/8250/parameters/nr_uarts in
mod_sysfs_setup() before do_init_module() runs serial8250_init().
Sysfs accesses serialize on kernel_param_lock(), while serial8250_init()
and __serial8250_isa_init_ports() touch nr_uarts_param without it:

CPU0 (modprobe)                       CPU1 (sysfs)
load_module()
  mod_sysfs_setup()
  do_init_module()
    serial8250_init()
      if (nr_uarts_param == 0)
      serial8250_isa_init_ports()
        DO_ONCE(...)
          nr_uarts = nr_uarts_param;  param_attr_store()
                                        kernel_param_lock()
                                        param_set_uint()  <-- data race
                                        kernel_param_unlock()
          nr_uarts_param = nr_uarts;  <-- clobbers write

The new unconditional store to nr_uarts_param introduces a KCSAN data
race against concurrent sysfs reads or writes and can clobber a sysfs
write even when the initial value did not exceed UART_NR.

A concurrent write of 0 between the check in serial8250_init() and
__serial8250_isa_init_ports() also bypasses the -ENODEV return and loads
the driver with nr_uarts set to 0.

Since DO_ONCE() holds once_lock with interrupts disabled,
kernel_param_lock(THIS_MODULE) cannot be taken in
__serial8250_isa_init_ports().  Snapshotting and clamping nr_uarts_param
under kernel_param_lock(THIS_MODULE) in serial8250_init() avoids the race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_E59F5D41E556C99EC4443B971E2734FFCA08@qq.com?part=1

      reply	other threads:[~2026-12-06  8:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  7:33 [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count Yingjie Wang
2026-12-06  8:11 ` 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=sashiko-outbox-252259@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=1075151112@qq.com \
    --cc=linux-serial@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