Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp746464lqm; Wed, 1 May 2024 14:39:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVsvnVGO/ILCVmlDuB0ca9FHPAk7+v30k7ukqF7voK5UDYJo00fi4G75eSfU+PJY31XDvFzHGJdyVot2EegtKLmub2rk+18aaalxQsI9A== X-Google-Smtp-Source: AGHT+IFrUUk7Pu5l0wL1ALlpPA0yIkMMhHzxa9i+R0NIlijHWFG6pADKMGEUptxl7AMyw+I972/U X-Received: by 2002:a05:622a:1116:b0:43a:5f5e:c10b with SMTP id e22-20020a05622a111600b0043a5f5ec10bmr1769093qty.20.1714599572840; Wed, 01 May 2024 14:39:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714599572; cv=pass; d=google.com; s=arc-20160816; b=OoW3W7mvILVGN8CuPZ+ryYLx2e9nOyaelUj0o3yQsFh1ajhpq3/HhBw6sJJq8MaRRz zj4AyzsiBBOE2KZH/EIIdHWIknc9ZQUrO67bNuYeZr7BzHNVC6G/wskpX7bqii8knuIl tsbycUoJY1V4dqnx2sI+euuhdI6YxffilvzS/7EhwC35gD9c6zS1RrMRrNZGWYtxlQua SFc+js3SucLeZ49Ecp9ukt4kJuNwcyMWQI7zmIoukU8/o65nXn6HmIKduuVqMZuQrK0c nJkFFloR73dqwNsqi3E9AwMqlvS53XwaHubME9o7k5A96WpiYVvocGt0EngRYTI0qrr1 gGDg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=b/uendQadFoqJ6JUcsRVTxUOv2sgr+9vrXfQIk/X5eI=; fh=1qj0gU6ul+VXNsI0zeULT+R8lqe6GiH1VjWT5yeVPeg=; b=YFqzhaBbstAt6ep+lNeuL5MYvpkyy9uLU3WtiKitMcE0wm46r7Bg1hnXQla0uSz8wA 2VGfXzdJ7/qbyPVnJaYZZdmmNz88tIbADJ1n6FH2/ShMWiGSJjJmUAOGV6Nlsqil6LGr nxFy2HHdynYcx5piQdqP87zqaJlRMWftyC4g6yPTuORvIhvlC5QU4z1pbBib8SgbddHF /l2aRTlXQWNJ6qAU0idt05i8Wr7BNXSizdBO6wnhrnbHW9bHwgFK0SjbK5sebSyA5/If 55qq7ey7t+zhUPCJauqt9qEipD1jA+7vkzNZU77goN3iZzA0XTIr6OAdcnTxee+V+8nA uklw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=ByewqnLA; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-165771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165771-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id n18-20020ac85a12000000b0043adaca2718si8495693qta.163.2024.05.01.14.39.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 14:39:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=ByewqnLA; arc=pass (i=1 spf=pass spfdomain=wanadoo.fr dkim=pass dkdomain=wanadoo.fr dmarc=pass fromdomain=wanadoo.fr); spf=pass (google.com: domain of linux-kernel+bounces-165771-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165771-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7C6041C214D1 for ; Wed, 1 May 2024 21:39:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A602A165FC8; Wed, 1 May 2024 21:39:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="ByewqnLA" Received: from msa.smtpout.orange.fr (smtp-81.smtpout.orange.fr [80.12.242.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C891D1474B9; Wed, 1 May 2024 21:39:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.12.242.81 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714599565; cv=none; b=k9cU46AVX9iwV+ROodLZy4ePKlXIZ+88JlRC4dVGVYHf9WDy9Q/7fVvrZiDkJhpGt29ZmWf37LM8+1yWoTRTIrsh3UUxL/LCn5yueugCCGJO41OdNN1S33ql61slGbePbPWmLlgvWHufyi1FrNFGhXXsTlY281X+FnjE5WqELYQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714599565; c=relaxed/simple; bh=xCX7DN4ArF6BjEHpKxCUjLo/sTx+uQ/UYj/ZWCCrQdg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fz74hdWVBMWm/KuNlK12gy9cqHsSEcnt6padWPV9nl5kiKzOYFgVMmYqJIfy4fbOz/k2VQtnE455bRqHxHYuTwbVOU/yqWxcubkX9b3oESxZgSLvhJiO2YvlCUAcAACiYDQvXSsdVqPDVbFAbNPUQslmTkHBZWzJ3VkF0zhmdQI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=wanadoo.fr; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b=ByewqnLA; arc=none smtp.client-ip=80.12.242.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Received: from [192.168.1.37] ([86.243.17.157]) by smtp.orange.fr with ESMTPA id 2HfZsx4ATpDee2HfZs0ZRe; Wed, 01 May 2024 23:39:19 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1714599559; bh=b/uendQadFoqJ6JUcsRVTxUOv2sgr+9vrXfQIk/X5eI=; h=Message-ID:Date:MIME-Version:Subject:To:From; b=ByewqnLAT2Hq/nsspQoWd1u5FHIvkZeGSt1pmJ2Wx5+rjP6Fx8/kvAQCMQO72OAqj MNJaRSHWXELm6vkDaxlTbPyeqQ9EaPPxTm8IQp+IxNZVmtZkOmilhhAdOTF4+kGb3Q dyg8HzRbYOIBftXHuyiuPCG4DNLX+RTzxyLZtmtloV/fti9bBu/+7kBDexWti99G+Z i6xaugiME0GA3nvrQz33lS62KFGKyDIV4gLm5LmstkcbkIkLcV8eALveny6aUWpDRh bib6knrZ1DqxDy3YbRxqaoETm2rbM+YB/fmlbzPwO5PFe+YXZsdq4CTzZSQJyn1rvE KxE3i/ZZOxbew== X-ME-Helo: [192.168.1.37] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Wed, 01 May 2024 23:39:19 +0200 X-ME-IP: 86.243.17.157 Message-ID: <7cdc63b3-31cc-442c-8c2e-75adb2c76b52@wanadoo.fr> Date: Wed, 1 May 2024 23:39:16 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] printk: cleanup deprecated uses of strncpy/strcpy To: Justin Stitt , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20240429-strncpy-kernel-printk-printk-c-v1-1-4da7926d7b69@google.com> Content-Language: en-MW From: Christophe JAILLET In-Reply-To: <20240429-strncpy-kernel-printk-printk-c-v1-1-4da7926d7b69@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Le 30/04/2024 à 01:06, Justin Stitt a écrit : > Cleanup some deprecated uses of strncpy() and strcpy() [1]. > > There doesn't seem to be any bugs with the current code but the > readability of this code could benefit from a quick makeover while > removing some deprecated stuff as a benefit. > > The most interesting replacement made in this patch involves > concatenating "ttyS" with a digit-led user-supplied string. Instead of > doing two distinct string copies with carefully managed offsets and > lengths, let's use the more robust and self-explanatory scnprintf(). > scnprintf will 1) respect the bounds of @buf, 2) null-terminate @buf, 3) > do the concatenation. This allows us to drop the manual NUL-byte assignment. > > Also, since isdigit() is used about a dozen lines after the open-coded > version we'll replace it for uniformity's sake. > > All the strcpy() --> strscpy() replacements are trivial as the source > strings are literals and much smaller than the destination size. No > behavioral change here. > > Use the new 2-argument version of strscpy() introduced in Commit > e6584c3964f2f ("string: Allow 2-argument strscpy()"). However, to make > this work fully (since the size must be known at compile time), also > update the extern-qualified declaration to have the proper size > information. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 [2] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [3] > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt > --- > --- > include/linux/printk.h | 2 +- > kernel/printk/printk.c | 20 +++++++++----------- > 2 files changed, 10 insertions(+), 12 deletions(-) > > diff --git a/include/linux/printk.h b/include/linux/printk.h > index 955e31860095..b3a29c27abe9 100644 > --- a/include/linux/printk.h > +++ b/include/linux/printk.h > @@ -71,7 +71,7 @@ extern void console_verbose(void); > > /* strlen("ratelimit") + 1 */ > #define DEVKMSG_STR_MAX_SIZE 10 > -extern char devkmsg_log_str[]; > +extern char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE]; > struct ctl_table; > > extern int suppress_printk; > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c > index adf99c05adca..64617bcda070 100644 > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -178,9 +178,9 @@ static int __init control_devkmsg(char *str) > * Set sysctl string accordingly: > */ > if (devkmsg_log == DEVKMSG_LOG_MASK_ON) > - strcpy(devkmsg_log_str, "on"); > + strscpy(devkmsg_log_str, "on"); > else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) > - strcpy(devkmsg_log_str, "off"); > + strscpy(devkmsg_log_str, "off"); > /* else "ratelimit" which is set by default. */ > > /* > @@ -209,7 +209,7 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, > return -EINVAL; > > old = devkmsg_log; > - strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE); > + strscpy(old_str, devkmsg_log_str); > } > > err = proc_dostring(table, write, buffer, lenp, ppos); > @@ -227,7 +227,7 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, > > /* ... and restore old setting. */ > devkmsg_log = old; > - strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE); > + strscpy(devkmsg_log_str, old_str); > > return -EINVAL; > } > @@ -2506,21 +2506,19 @@ static int __init console_setup(char *str) > /* > * Decode str into name, index, options. > */ > - if (str[0] >= '0' && str[0] <= '9') { > - strcpy(buf, "ttyS"); > - strncpy(buf + 4, str, sizeof(buf) - 5); > + if (isdigit(str[0])) { > + scnprintf(buf, sizeof(buf), "ttyS%s", str); > } else { > - strncpy(buf, str, sizeof(buf) - 1); > + strscpy(buf, str); > } Hi, Nit: The { } around each branch can now also be removed. CJ > - buf[sizeof(buf) - 1] = 0; > options = strchr(str, ','); > if (options) > *(options++) = 0; > #ifdef __sparc__ > if (!strcmp(str, "ttya")) > - strcpy(buf, "ttyS0"); > + strscpy(buf, "ttyS0"); > if (!strcmp(str, "ttyb")) > - strcpy(buf, "ttyS1"); > + strscpy(buf, "ttyS1"); > #endif > for (s = buf; *s; s++) > if (isdigit(*s) || *s == ',') > > --- > base-commit: 9e4bc4bcae012c98964c3c2010debfbd9e5b229f > change-id: 20240429-strncpy-kernel-printk-printk-c-6a72fe6d0715 > > Best regards, > -- > Justin Stitt > > >