From patchwork Fri Aug 11 16:33:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13351154 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 D23FDEB64DD for ; Fri, 11 Aug 2023 16:34:30 +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=ox0rtXJqTeQWA70W61yVHdP4clq2iDq/Sl39f2JS0vM=; b=mVR 8xTUNof2fLDd/QDKkvelH4RkX3ek1L5uJXT6tmZrINKH2XY1bdVd9pPnosvosNkP4lS4cXr8bro3w COyyXkm2a2Ywq/HmqjInu3ug2ArAD5hInn2WaBvo6hpjg/Uui2sap4IRJ6pVT9g93lv/CnKPKqpXL zF2uNkEplddXyDgcEHROLnb32c3cH14JmFchpb+IaDSAvHiBIHm1LfbOBDRzYNtg4yHeJjp1DHcgz NULmv6uFJpSL4c/gXkuw6ijlHXvw/g5SphCcXkSVvpPwCaLLpmuWi2tq0UoLs+rX7skOet08IXv5l MKAut9yog/F2VThNPmbzKOVYEsRUCYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qUV5Q-00B7Wh-1D; Fri, 11 Aug 2023 16:34:04 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qUV5N-00B7VX-1a for linux-arm-kernel@lists.infradead.org; Fri, 11 Aug 2023 16:34:02 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d41fa6e7103so2120083276.0 for ; Fri, 11 Aug 2023 09:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691771637; x=1692376437; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=M6OUxq5MRWIwo820poqhdTN5aSCn+WIZs4Yefeliges=; b=00HrlpQ2j2D+1J0rPvMY83N/fK7KS2utURj8FbcclPYbcpqVBq52Dyk5uoyPNEq1yH 1ZldUo/0x4j6NTq4J/0TDa1V+wTrRwphrZFwH94vWHMBV62uY6j/Hw2Ey1W+RQiGx8mK cp1Q1JgV5qpzuX4Y95uJbjq+92tS9ZLe6FVfLDgLOTrEyIbz8Mnh/e800N3pi5WIkkIe q/qcVSqisR1EBTG++6AKQDTO2ic+T6ES/Hh00r1/knUlHk5lrFGCHKt0mzv5MmHMEfEX 7PP1eV6xk5W+fB5Kokb+exOQbz1vVpLLSm4cETk7vLdfiw566tPv3hxHUjDQXZBizGPY 1oBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691771637; x=1692376437; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=M6OUxq5MRWIwo820poqhdTN5aSCn+WIZs4Yefeliges=; b=KH5W2MIMA5mQaavVpQjtk62TvI/V8+Z21tPeiBm8GahQ2G7oePWRJHu8JEYcsiM85H UKnUt5m8m3eYFfBh6bqAug2wCtgRjtpUfWwvMmST7jPsvGrVrtAbKTIcKfa3KLaz6VX7 +tcLffypC7tACMg/zECX4AJTLk4b7TETnsKnH3suA2nIMWar/k4OrYaTTTnyYbYOqaih nfyqV3Onp8fbqxT4/I3IhP47xF5lcdBMEJ17ONXyc6Rws3dmkBC/I1Jmaw2b9iEbuWia WkY+RtA8AWmkdDzT3qT3E/68wwbK27zfb9UMNQ/0s94ALNYMmSR0gMpuLBcPz5Zmeq9Y bRDw== X-Gm-Message-State: AOJu0Yzy55NwTztSnCt4vBODRlAwIMGw7RgaE0kPRRYtztft44Ia87os ggzB4DS0Eu0+yyMkw6aK4cop5o7zpiZj9fAZZQ== X-Google-Smtp-Source: AGHT+IG2n+cZRj98+MXV3Srxy52M5SH6ZVNgiFpcuwQQnUPFy5Ohyjbx9lT+2aYY08RyVtB7NWtTaVQRb9zBRtWOdA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:4ca:0:b0:d47:f09c:cc8e with SMTP id 193-20020a2504ca000000b00d47f09ccc8emr35802ybe.10.1691771637249; Fri, 11 Aug 2023 09:33:57 -0700 (PDT) Date: Fri, 11 Aug 2023 16:33:51 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAO5i1mQC/32NSwqDMBiEryL/uil52DR11XsUF5o31EQSCRXJ3 Zt6gG4GvoH55oCsk9cZhu6ApIvPPoYG9NKBdFOwGnnVGCimDAuCUd5SkOuOpiRdi4X3iBimGBW zeswC2nBN2vjPKX2NjZ3PW0z7+VHIr/2rKwQRZPjdMC5uUvH+aWO0b32VcYGx1voFUfuvfLUAA AA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1691771636; l=2211; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=6DzOCDhVK8QrlXFdb8qJxWTXZA/HZ69ASisG5d0HdUE=; b=UluTDfd96B3b7YGEMVMhWFJDv4OqE5Gk8RMCyISg7TZSa7p9jrkSHI8K6SYDWfn34DYlMDcXA OEQdku5MxTVBe7+kmDLsEXusbKTldqz+v6/B2F2mQ2c4CpxFm3hpWwL X-Mailer: b4 0.12.3 Message-ID: <20230811-strncpy-arch-arm64-v2-1-ba84eabffadb@google.com> Subject: [PATCH v2] 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-20230811_093401_527923_730287E1 X-CRM114-Status: GOOD ( 14.42 ) 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, we can simplify the logic and also check for any silent truncation by using `strscpy`'s return value. 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 Suggested-by: Kees Cook Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- Changes in v2: - Utilize return value from strscpy and check for truncation (thanks Kees) - Link to v1: https://lore.kernel.org/r/20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com --- 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 | 6 +++--- 1 file changed, 3 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..aee12c75b738 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -262,9 +262,9 @@ 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 = strscpy(buf, cmdline, ARRAY_SIZE(buf)); + if (len == -E2BIG) + len = ARRAY_SIZE(buf) - 1; if (strcmp(buf, "--") == 0) return;