From patchwork Thu Aug 10 18:39:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13349819 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CACFBC001DE for ; Thu, 10 Aug 2023 18:39:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=XKcDiMsx0Qb/M8vm8dooQ79fjFevMVyBJDS083ls8RM=; b=fev njTnOQ8HucGnEl8CzxWPRcfroQotP7yZCXrR3w4qkJseMGJyEXEQHSnYG1F/rIjlRQ2tfj3MUINor fKeoyv1CjEo1RglEN/hOnRBTgCSdDBpH/hzBGkpitBzPErHAjPTyC3LnoYH1E9EthwnHAV5mLydOm QolYSP/55VJJgKaQtGGE8woC7Bnu4aJJzpPKrzJKWTmZAAONfCIWBUGL4zic8Iq3Da3B34kHeKbFx 2F04F5zAcp7cXCYuGUgH3IdqA1aIZ4SElABlV5RZ30BQUBYEAVnRZY+38hLk9s36FbU5CkBoxWX1j lHEj2Lo47NplRvW1jG6yRy143X8xCzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qUAZ2-008RlI-1W; Thu, 10 Aug 2023 18:39:16 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qUAZ0-008RkM-15 for linux-arm-kernel@lists.infradead.org; Thu, 10 Aug 2023 18:39:15 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-586a8ee15c6so16503517b3.0 for ; Thu, 10 Aug 2023 11:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691692752; x=1692297552; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=9s/VShouY5UecShKdRRU+6iUoUZRUswiXCe+DYQQHMc=; b=iu3wIj0hrSChbkmbXE8YRK7qQMDFnxyfxL9xH4h5buWYXje/v9DExUqA4AGyVrqEQK GEcz0VT6GDwvVqm/rLr5zRKeOiIzE5XzDl6byKRnCcrRAV26nllvHU/Y/B2zRvUHyQjX pOywS2CTntHwjYSgM/5a1a1My+TepYis1+/gqp0nTAkppUPD8J6lMC7/qnVsG6/LfVwB dvKS+/w2Y+2gvSNed2wlajBMBC3y3XLUj2NUgCtaxNMsx8LxNCuxu164jCdL+WFJ5rNj 5lSQ0eaIduXwSUxkONLhL9zf4mIuEO79oC6g3RyVhRsLeSklSR2VjyH5etCqZnzrHjN4 Usuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691692752; x=1692297552; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9s/VShouY5UecShKdRRU+6iUoUZRUswiXCe+DYQQHMc=; b=kJclilFgmqRnnhICletQ4VpA2Wo/RSS/cPQ/6Almw7bD8jbvoX0UKgSZQq7z14N/NO mOVphVkd5ateMrlXEkdVaFJa3EOGA26C1XkT3uYcnLDQ99Vz1e0GsvgHt9b/tuQp52mC 1Okxk84nOawY1xsGMNnTZjq3QgY/kP3s1Kel8OoFLcn3yp7LGxmBXYD5iItn6vzgqMwt G57mA7ilQLjZpj8BXX3zKc9tkrs257esr+jJufMRhqz+7AqIm/+YjZ7K1b2/RzAnzzoq U27HOlzzXFWgtQxlkDx3dKo2ZC84LPvgxy4K5eFyHBcwCMpyGw6sKbF4yFb1oC5zC2VT /OiA== X-Gm-Message-State: AOJu0Yxgt6omGXnE2dhZyjqjCd3gPsfXL8bzm5Qpra167sFND0MXmDfh yL1ZoAmpgjzSGviQYWbnHlQhk6OS5+2Df16TlQ== X-Google-Smtp-Source: AGHT+IFd/BetrjKTvc+2UKvsj3Xl2aqeBqnWIgCBibfJD2tswKguWZfZhsx1vj78xyqo4dnFURE7yVJC6tcvaqhFKw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a81:b609:0:b0:583:5039:d4a0 with SMTP id u9-20020a81b609000000b005835039d4a0mr55197ywh.0.1691692752000; Thu, 10 Aug 2023 11:39:12 -0700 (PDT) Date: Thu, 10 Aug 2023 18:39:03 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMYu1WQC/x2MywqAIBAAfyX2nOAjwvqV6FC65R4yWSOK6N+TL gNzmHkgIxNm6KsHGE/KtMciqq7AhSmuKMgXBy21kVZJkQ+OLt1iYhcKtrYRajHeaDv7brZQwsS 40PVPh/F9P858Y0xkAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1691692751; l=2010; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=aXS5AtkQbMqazJ7/YYIzdIPN5iWfiNdlNiBoDrKML1c=; b=4OnNBtGbw+0TcmcgHLZW3RtqHKsRbDxE7jZ+jEJGUYUpPJ/mIRl49NEwoSjr4tkFGdavlUEKT 2ZUXdFZnghwCk7IMHYL2xzzS1o6VeUhjChRT4JvTADIQtj9TAeke/OP X-Mailer: b4 0.12.3 Message-ID: <20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com> Subject: [PATCH] arm64/sysreg: refactor deprecated strncpy From: Justin Stitt To: Catalin Marinas , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Kees Cook , linux-hardening@vger.kernel.org, Justin Stitt X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230810_113914_375047_6C6A824A X-CRM114-Status: GOOD ( 13.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org `strncpy` is deprecated for use on NUL-terminated destination strings [1]. Which seems to be the case here due to the forceful setting of `buf`'s tail to 0. A suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on its destination buffer argument which is _not_ the case for `strncpy`! In this case, there is some behavior being used in conjunction with `strncpy` that `strscpy` already implements. This means we can drop some of the extra stuff like `... -1` and `buf[len] = 0` This should have no functional change and yet uses a more robust and less ambiguous interface whilst reducing code complexity. Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- For reference, see a part of `strscpy`'s implementation here: | /* Hit buffer length without finding a NUL; force NUL-termination. */ | if (res) | dest[res-1] = '\0'; Note: compile tested --- arch/arm64/kernel/idreg-override.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) --- base-commit: 52a93d39b17dc7eb98b6aa3edb93943248e03b2f change-id: 20230810-strncpy-arch-arm64-1f3d328bd9b8 Best regards, -- Justin Stitt diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c index 2fe2491b692c..482dc5c71e90 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) if (!len) return; - len = min(len, ARRAY_SIZE(buf) - 1); - strncpy(buf, cmdline, len); - buf[len] = 0; + len = min(len, ARRAY_SIZE(buf)); + strscpy(buf, cmdline, len); if (strcmp(buf, "--") == 0) return;