From patchwork Mon Apr 29 23:06:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13647853 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 784C12E405 for ; Mon, 29 Apr 2024 23:06:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714432017; cv=none; b=cJiqVZ6vd65pZw/Bc43mydEAcH0QuLafyghj9S5jW79LGOoOoX30Xama3a2rF+6T3bNgUppI9cWNrSh/VIIfrcropyAo8VKIqk79+KJSng2qef8dBRc6Hisj5gWv6usiZMgZmkAEd3eF0m/cNZG+okSM5Dyrbw8qv1WkRVsydSA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714432017; c=relaxed/simple; bh=T5msb/FwiOM2B8FoBye3KYi+F/yGogHxh2GWH/afDsU=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=qPvmJiv0G00+N20ezoigyMWk0R26+JkC2vNt1YC7wWsZ6Zog1zFHzO82xD2Y+01eWn9J/i1oedZRqoaMyZlzwhQn4yNkNbfxxl40S+odDT5qk1IwUA9k1C8I93ZqI2T5yGDJXYx5PCDAFXpxf2v7rcab4lRsIid2M8Yg6ca2gJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mH6FW0XD; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mH6FW0XD" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-de54be7066bso9133011276.0 for ; Mon, 29 Apr 2024 16:06:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714432014; x=1715036814; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=LXyAxLtGa1196wXalz5OFqmoW+jEqlgIdoAjVVHAISs=; b=mH6FW0XDHWrH8rvdfSWcP29vIPZPluqm/G0zE999cWr25zeMpNTdURnTPeHDtghf6r 6I/Cu3FQ2nma4A7IB1HcLDSFx+hnQDqWZREb2cRLiaf/t7uGra/UQldQ+xMC6+sosFxx 3imhigqQGmSRZGof5B1fpbaOiOxyqW+xMd7iHU8WeTqvUf5LJCjpE7X5IS/p7Fvz/9O6 zJXnrwMdejs8J8e3Pu5GTbymfVqypvv8nBfAKgJ0p28XpV6WpFKLy1wGCbDViQd/DOww L/ADrvnOp/EmZiTVqmGA2IQYok3EEVQDPc7cNQJ5F3e71oKX0ykpaGNpWmJith1kUkvz Mjcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714432014; x=1715036814; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=LXyAxLtGa1196wXalz5OFqmoW+jEqlgIdoAjVVHAISs=; b=kYlGYEXQfCZS9iOpscHzVOd+Vfr/X7vMwFDHEQb4sWZig7Edh1/slT3HB2UQ7M68sZ eaXY1TlPasA5vATJqQNPpvcJiqgn8hJFNH44l2RvFxy4vYtw4Q0QOAHOtFOznLbHWaPA A5oiVgNbTKfch9ENti6VD3MLq/7ekqz3RTs3A4UQ9WIuju6s6pRqCqVNB3iKoifQfK66 49SWBx+07bl24UzfVvIBAxvL7gdbgxJqe+wL5oOkFv+NzU6fNI+FqtWVFojOLPQVsrQV PpuYK71wwIAC59sOLHoqmY7wLKJbDLjHPJF7zwhjwaqBahZimwVTOIwU3PTI/tW4Q/sR PlUQ== X-Forwarded-Encrypted: i=1; AJvYcCUI6fgyncbxRwXah/j1TnfRnQ4aeqzsWycyZhxy5M9wWpI+J5lhHnoSqawbtEHBmR4G08LbkTkCDBt/2mQfR6+qwPUH7dHdIC9dEkhG4RPI X-Gm-Message-State: AOJu0YxkxWgd6iU9SxAEosVHQAoY1Vd+hnUuOknWD/OtyoHysssST+UP RP+Dt2G0lh7fvBu57ZUnUKof0OFQeTGK/1GAGxpT5l4Ymv6z8TMf7C5IXOeOm3rdp/WHcd5wEiK TS2RixdXA13fkhTvDuJH4Zw== X-Google-Smtp-Source: AGHT+IGeIbhUCHMAFmraPY9YQhVZzsy985ANsGkhLyO1PUv0ne2OlVhD5fpVPeG5st0Esedb7gmgGjG+DqpsiLavSg== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:2b03:b0:de4:845b:8f08 with SMTP id fi3-20020a0569022b0300b00de4845b8f08mr3758442ybb.2.1714432014609; Mon, 29 Apr 2024 16:06:54 -0700 (PDT) Date: Mon, 29 Apr 2024 23:06:54 +0000 Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAA0oMGYC/zWN2wpAQBBAf0XzbGptLvEr8qA1mGhssxLJv9uUp 9N5OeeGQMoUoEluUDo48CZRsjQBN/cyEfIQHayxucltjWFXcf7ChVRoRa8s+/LDYdlXdqRyMFV WQIx4pZHPb9B2z/MCzwNM8HAAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1714432013; l=4237; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=T5msb/FwiOM2B8FoBye3KYi+F/yGogHxh2GWH/afDsU=; b=Sl5cyYRJ2u3ZR6g8GOSU49y8O3CeWfvN4QwouXDwsRoiAcxCVm+Ietg6oUzBENx/qK1zjQykn 07KFwNfS42mCzYeg0uLLrwnJ+OPMzXluQE2Nle26OKU9EGHO710tWOg X-Mailer: b4 0.12.3 Message-ID: <20240429-strncpy-kernel-printk-printk-c-v1-1-4da7926d7b69@google.com> Subject: [PATCH] printk: cleanup deprecated uses of strncpy/strcpy From: Justin Stitt To: Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt 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 Reviewed-by: Petr Mladek --- --- include/linux/printk.h | 2 +- kernel/printk/printk.c | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) --- base-commit: 9e4bc4bcae012c98964c3c2010debfbd9e5b229f change-id: 20240429-strncpy-kernel-printk-printk-c-6a72fe6d0715 Best regards, -- Justin Stitt 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); } - 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 == ',')