Re: [PATCH v2 1/2] printk: Check valid console index for preferred console

Petr Mladek
2023-10-11T15:26:15Z

On Wed 2023-10-11 12:18:03, Tony Lindgren wrote:
> * Greg Kroah-Hartman <gregkh@linuxfoundation.org> [231011 07:53]:
> > On Wed, Oct 11, 2023 at 10:43:25AM +0300, Tony Lindgren wrote:
> > > Let's check for valid console index values to avoid bogus console index
> > > numbers from kernel command line. While struct console uses short for
> > > index, and negative index values are used by some device drivers, we do
> > > not want to allow negative values for preferred console.
> >
> > What drivers use a negative index for the console?
>
> This is based on grepping with $ git grep "co->index =" drivers/tty/
>
> Not sure what all might be stopping making struct console index unsigned.

The value -1 is used for initializing struct console, see:

$> git grep -A10 "struct console.*=" | \
grep -e "struct console" -e index | \
grep -B1 index
[...]
drivers/tty/serial/8250/8250_core.c:static struct console univ8250_console = {
drivers/tty/serial/8250/8250_core.c- .index = -1,
[...]
drivers/tty/serial/imx.c:static struct console imx_uart_console = {
drivers/tty/serial/imx.c- .index = -1,
drivers/tty/serial/ip22zilog.c:static struct console ip22zilog_console = {
drivers/tty/serial/ip22zilog.c- .index = -1,
drivers/tty/serial/kgdb_nmi.c:static struct console kgdb_nmi_console = {
drivers/tty/serial/kgdb_nmi.c- .index = -1,
drivers/tty/serial/lantiq.c:static struct console lqasc_console = {
drivers/tty/serial/lantiq.c- .index = -1,
drivers/tty/serial/liteuart.c:static struct console liteuart_console = {
drivers/tty/serial/liteuart.c- .index = -1,
drivers/tty/serial/lpc32xx_hs.c:static struct console lpc32xx_hsuart_console = {
drivers/tty/serial/lpc32xx_hs.c- .index = -1,
drivers/tty/serial/ma35d1_serial.c:static struct console ma35d1serial_console = {
drivers/tty/serial/ma35d1_serial.c- .index = -1,
drivers/tty/serial/mcf.c:static struct console mcf_console = {
drivers/tty/serial/mcf.c- .index = -1,
[...]

It means that the index still has be get assigned. For example, it is
used here:

static int try_enable_preferred_console(struct console *newcon,
bool user_specified)
{
[...]
if (newcon->index < 0)
newcon->index = c->index;
[...]

Resume:

1. We must either keep signed short in struct console or
use another check for non-yet assigned index.

2. We should fix the commit message and the comment. We should
explain that negative value is used in struct console
to distinguish a non-yet-registered/assigned index/port.

Best Regards,
Petr