Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp113252rdg; Wed, 11 Oct 2023 23:43:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+ZvrWQiSLsZXb7HsPg5VkXiRPlfs7KfbAGNFcLAG1wS0GKHv6noPsQMSvpAOREUV9wE9w X-Received: by 2002:a0d:c787:0:b0:583:b186:d817 with SMTP id j129-20020a0dc787000000b00583b186d817mr22560535ywd.27.1697092992787; Wed, 11 Oct 2023 23:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697092992; cv=none; d=google.com; s=arc-20160816; b=uji5490rutjH9YbsNeopUaOAURk6DQqy8q5bnD4GIJmwZwwi+ah3NrbLFqg1f1vNML lM43Dm+olL+kk5IUQW3HfaGJ5S2P4LJE+EDz2YY6Mp3rbtYPh3rvaHp6Hz79czp1ctVc e8j7NUKgDsSBFVtGZrV6S8yKrnyvBHnACUqUa8TjFEnOSoaIqYojL4ktnWc4HA+EXRTl /EJW0vC0yxxmeQ/bSm5haTV9Iaq17JIEB2l3yGM+BtrqSKO/xRnMoPUY7GXARABv7+93 z6WC6EbDDi4jq/L9Pedo6x7K8tpqtdKkAjZntPQunQeElJ1DsfYH6Lz5IdNVIWgpLobv nCyw== 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 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i10-20020a17090a974a00b00273fa934cc1si1575466pjw.167.2023.10.11.23.43.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 23:43:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 D27A08213F12; Wed, 11 Oct 2023 23:43:11 -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 S1377457AbjJLGnI (ORCPT + 99 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 Content-Transfer-Encoding: 8bit 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:12 -0700 (PDT) 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 --- 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); } -- 2.42.0