From patchwork Thu Oct 12 06:42:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 151782 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp1027241vqb; Wed, 11 Oct 2023 23:43:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqCTMNNVfqGvLZe882Yozg1BziPBi+td75O96b9VTY6DJ7vhvYImXfPLllTRXeSQLuB0pa X-Received: by 2002:a05:6a00:3916:b0:68e:2fd4:288a with SMTP id fh22-20020a056a00391600b0068e2fd4288amr26210819pfb.3.1697092994310; Wed, 11 Oct 2023 23:43:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697092994; cv=none; d=google.com; s=arc-20160816; b=MlQyyrvTrrSL5fWI9vj9KgNDd2Om65qxhv6upUTO59xRg5rF0aJ0LKOJ92HF2NYUs1 OR9/Tc8bXT75aPuZapja672hbEnzotV8gJTj7qxK3M2WiXXQG/YE48PqbK0w5WPoPZJM lfjyydXXaacM4oZ7zAi7moMZEBba207J1XRvkaC8yirH/Zq6FwLG6gCmbS807ymkJSFz t458M/RTNKfRHR720zMNc2QVDZ0mFBdzLmpFz6oDnCiB4tfdcilxDvF1S8P1zjrNF8SU FrvK2g3WWblksdr+vPFpeUi8TSksTFTqtLBvIOC19j5M9VJ9tHHlV8/Oph0J0Q/DQapp 1gzQ== 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=drPfUsCKbe6hAgrbaie1cpXtpWCZB3OmIUxuFTq635g=; fh=o8Wp60O6t9aGNhRKrDSNhfZE8Iko7nroMui1s7mpLf8=; b=ny4X2gDTAqlKcSfpXV4r+daaOwZlN05aI1+LP2ysq8U85/mfWY58iF/K4iy/L248/B 8m+aIDoGXWwjM3VysIyX/PUT00prczNRWnm9XposCeIUp/RMJj463MKcG9NuRwXRSf2r 6i4Is1zQiun/V3SZdykt2wKczoam4/bU21kUlzCwDPL1hnpwVYAMVs0yZstVxCHr9IzG dBWV03y3QCf66NYaRhI01HPXJLszLsbxZXw1DtJtYPv2st60r62XmTPOqhpVTw6ZWScI 4in0UtzmZYk/bcuJoA1YmKJm7s3Lb6urKBs64wkDxhHFd8RkLG2FT/aUsT9spXj/UHPt d03g== 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:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id f18-20020a056a001ad200b0068fdb060072si7587731pfv.341.2023.10.11.23.43.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 23:43:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 9D5938213F18; Wed, 11 Oct 2023 23:43:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377465AbjJLGnI (ORCPT + 19 others); Thu, 12 Oct 2023 02:43:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343563AbjJLGnH (ORCPT ); Thu, 12 Oct 2023 02:43:07 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F1A6ECC for ; Wed, 11 Oct 2023 23:43:05 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 7663F80A3; Thu, 12 Oct 2023 06:43:04 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: Jiri Slaby , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] printk: Check valid console index for preferred console Date: Thu, 12 Oct 2023 09:42:56 +0300 Message-ID: <20231012064300.50221-1-tony@atomide.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 11 Oct 2023 23:43:13 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778814544878849423 X-GMAIL-MSGID: 1779530983686090577 Let's check for valid console index values for preferred console to avoid bogus console index numbers from kernel command line. Let's also return an error for negative index numbers for the preferred console. Unlike for device drivers, a negative index is not valid for the preferred console. Let's also constify idx while at it. Signed-off-by: Tony Lindgren Reviewed-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- Changes since v2: - Fix a mismerge for const char *name while changing the patch order as noted by Jiri - Clarify patch description and code comments for struct console negative index usage as noted by Petr Changes since v1: - Use const short idx and return an error on negative values --- include/linux/console.h | 2 +- kernel/printk/printk.c | 12 ++++++++++-- 2 files changed, 11 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,20 @@ 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(char *name, const short idx, char *options, char *brl_options, bool user_specified) { struct console_cmdline *c; int i; + /* + * We use a signed short index for struct console for device drivers to + * indicate a not yet assigned index or port. However, a negative index + * value is not valid for preferred console. + */ + if (idx < 0) + return -EINVAL; + /* * See if this tty is not yet registered, and * if we have a slot free. @@ -2513,7 +2521,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); }