Message ID | 20231011074330.14487-1-tony@atomide.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp366313vqb; Wed, 11 Oct 2023 00:44:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8o6OZGSKXWXm6eyH2RWhrY4iJSu+75Z08PvyxjFxjx/fKrJ0nlgvs39b1kDtY6zzl5uDq X-Received: by 2002:a05:6359:b9a:b0:14d:2dd5:a777 with SMTP id gf26-20020a0563590b9a00b0014d2dd5a777mr16179443rwb.2.1697010259094; Wed, 11 Oct 2023 00:44:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697010259; cv=none; d=google.com; s=arc-20160816; b=YJugNmAc5m52UknxkjrOjPH6zUgWLQp3ExnybBqPPGkGY6aqXhcKJgXpq/0bdOP+3w PexY1yOYfp6SDn0M0+PRsXTV7kB+EZQGBYZBJ7vmx04JGMYQfIZ6mzEOk1mneK8s2rJX cQ1JdKUYHS2bbixDzsAJ2H1p13OXjK28RFEo8M1CVnJtdVOrCHTJQA2LoJumTliSeDxC Nq7/UcTpFIwgOKO3JvD3QK9yBqwRMJKO5X5Ni5Kwzi9rezDuk137dCykLAD+HUtanuoM DMlZ1h1qlHDURRhpYh5laNq/tVMap0gf+BYiQkly7f18o6M4X8Bz/RPRYVo/7nHNESJ3 X3Qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=UQLV8TOnzEF2Z0aGJU5Yry1qThjgo+pPAYxK/N1/06E=; fh=N7rIU1tcfxW5z42IdEE0deGbIEdKlSzUcn5hBMl/kIg=; b=ATFq5bQkpZC0ohnoaq3Jf7abDGt/IeNB0U/xLoCa+EZhgyz8fjdGrOpyn7MQmI0zZs B4VLrZic0a9ryqUCImTrvRaWovA0KExAChm63WGoSJoMIpdWNuj8ZICcHu+zT9XUpf12 QQw/s/biO5nNeqKSZtpHf8Jp9ly4BeC+wHKeDqvNLs2dEYi3Nudjt6CD51jLTdGf56MX F38dvInODm9OHh7kTgE10y4lBgOSOQJ3rnrU13MT1F5mfeolBMEhi0Jq6+EyJ2IMS1Rg O9JCm12XdmcEoUtkMwZp+Tt8X+YB7bIOwEKIuCvdaMWwGSn1b+o0w5gz+QDkjqHrI25K d4Kw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id l17-20020a635b51000000b005789c7e9bf2si13986524pgm.627.2023.10.11.00.44.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 00:44:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 071A58131C1D; Wed, 11 Oct 2023 00:44:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345040AbjJKHnw (ORCPT <rfc822;rua109.linux@gmail.com> + 19 others); Wed, 11 Oct 2023 03:43:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229948AbjJKHnr (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 11 Oct 2023 03:43:47 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7B48DE9 for <linux-kernel@vger.kernel.org>; Wed, 11 Oct 2023 00:43:43 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 6203480E1; Wed, 11 Oct 2023 07:43:42 +0000 (UTC) From: Tony Lindgren <tony@atomide.com> To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Petr Mladek <pmladek@suse.com>, Steven Rostedt <rostedt@goodmis.org>, John Ogness <john.ogness@linutronix.de>, Sergey Senozhatsky <senozhatsky@chromium.org> Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] printk: Check valid console index for preferred console Date: Wed, 11 Oct 2023 10:43:25 +0300 Message-ID: <20231011074330.14487-1-tony@atomide.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 11 Oct 2023 00:44:13 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778814544878849423 X-GMAIL-MSGID: 1779444229639979264 |
Series |
[v2,1/2] printk: Check valid console index for preferred console
|
|
Commit Message
Tony Lindgren
Oct. 11, 2023, 7:43 a.m. UTC
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.
Let's change the idx to short to match struct console, and return an error
on negative values. And let's also constify idx while at it.
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
Changes since v1:
- Use const short idx and return an error on negative values
---
include/linux/console.h | 2 +-
kernel/printk/printk.c | 11 +++++++++--
2 files changed, 10 insertions(+), 3 deletions(-)
Comments
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? > Let's change the idx to short to match struct console, and return an error > on negative values. And let's also constify idx while at it. > > Signed-off-by: Tony Lindgren <tony@atomide.com> > --- > > Changes since v1: > > - Use const short idx and return an error on negative values > > --- > include/linux/console.h | 2 +- > kernel/printk/printk.c | 11 +++++++++-- > 2 files changed, 10 insertions(+), 3 deletions(-) > > diff --git a/include/linux/console.h b/include/linux/console.h > --- a/include/linux/console.h > +++ b/include/linux/console.h > @@ -340,7 +340,7 @@ enum con_flush_mode { > CONSOLE_REPLAY_ALL, > }; > > -extern int add_preferred_console(char *name, int idx, char *options); > +extern int add_preferred_console(char *name, const short idx, char *options); > extern void console_force_preferred_locked(struct console *con); > extern void register_console(struct console *); > extern int unregister_console(struct console *); > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -2404,12 +2404,19 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) > console_set_on_cmdline = 1; > } > > -static int __add_preferred_console(char *name, int idx, char *options, > +static int __add_preferred_console(const char *name, const short idx, char *options, > char *brl_options, bool user_specified) > { > struct console_cmdline *c; > int i; > > + /* > + * Negative struct console index may be valid for drivers in some cases, > + * but negative index is not valid for a preferred console. > + */ > + if (idx < 0) > + return -EINVAL; Looks good to me, I'll take this through my tty tree if no one objects. thanks, greg k-h
* 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. Regards, Tony
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
* Petr Mladek <pmladek@suse.com> [231011 15:26]: > 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; > [...] OK > Resume: > > 1. We must either keep signed short in struct console or > use another check for non-yet assigned index. OK thanks for clarifying the usage for it. > 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. I'll send v3 patches tomorrow with updated commit message and comments. Thanks, Tony
On 11. 10. 23, 9:43, 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. > > Let's change the idx to short to match struct console, and return an error > on negative values. And let's also constify idx while at it. > > Signed-off-by: Tony Lindgren <tony@atomide.com> ... > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -2404,12 +2404,19 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) > console_set_on_cmdline = 1; > } > > -static int __add_preferred_console(char *name, int idx, char *options, > +static int __add_preferred_console(const char *name, const short idx, char *options, This "const char *name" change is somehow not related to 1/2, but to 2/2, I suppose. thanks,
* Jiri Slaby <jirislaby@kernel.org> [231012 05:54]: > On 11. 10. 23, 9:43, 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. > > > > Let's change the idx to short to match struct console, and return an error > > on negative values. And let's also constify idx while at it. > > > > Signed-off-by: Tony Lindgren <tony@atomide.com> > ... > > --- a/kernel/printk/printk.c > > +++ b/kernel/printk/printk.c > > @@ -2404,12 +2404,19 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) > > console_set_on_cmdline = 1; > > } > > -static int __add_preferred_console(char *name, int idx, char *options, > > +static int __add_preferred_console(const char *name, const short idx, char *options, > > This "const char *name" change is somehow not related to 1/2, but to 2/2, I > suppose. Thanks I just noticed that too, that sneaked in while I was resolving a merge conflict while changing the order of the two patches. Regards, Tony
diff --git a/include/linux/console.h b/include/linux/console.h --- a/include/linux/console.h +++ b/include/linux/console.h @@ -340,7 +340,7 @@ enum con_flush_mode { CONSOLE_REPLAY_ALL, }; -extern int add_preferred_console(char *name, int idx, char *options); +extern int add_preferred_console(char *name, const short idx, char *options); extern void console_force_preferred_locked(struct console *con); extern void register_console(struct console *); extern int unregister_console(struct console *); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2404,12 +2404,19 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) console_set_on_cmdline = 1; } -static int __add_preferred_console(char *name, int idx, char *options, +static int __add_preferred_console(const char *name, const short idx, char *options, char *brl_options, bool user_specified) { struct console_cmdline *c; int i; + /* + * Negative struct console index may be valid for drivers in some cases, + * but negative index is not valid for a preferred console. + */ + if (idx < 0) + return -EINVAL; + /* * See if this tty is not yet registered, and * if we have a slot free. @@ -2513,7 +2520,7 @@ __setup("console=", console_setup); * commonly to provide a default console (ie from PROM variables) when * the user has not supplied one. */ -int add_preferred_console(char *name, int idx, char *options) +int add_preferred_console(char *name, const short idx, char *options) { return __add_preferred_console(name, idx, options, NULL, false); }