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: 134632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1277069vqi; Fri, 11 Aug 2023 11:24:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELI0Jt/K2mziR8CNIKuWYDLMhRqU/ooFkCDTu9UCoCF1NLYrlHlg7aZycAXE5iG1jqB0Re X-Received: by 2002:aa7:d292:0:b0:523:b133:5c7e with SMTP id w18-20020aa7d292000000b00523b1335c7emr2681644edq.1.1691778258209; Fri, 11 Aug 2023 11:24:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691778258; cv=none; d=google.com; s=arc-20160816; b=CyoYTjoQlCGE7xGFttcdYW2bOpeW4IKO32q2346B3lwwnAxxAzeR5+SDkEm+2Of93R Uue11cM+gxe2mcEf9hFV8EtcrX4sbtsA6y8lKhiTEi0uURZ4Aju4Tkf1UneTQS0uRxFg Qo0KBdZqAd9bc91BgRlTcdj0TnAzhLtF0XvdA4p2R+ab2+yY6iBatbYa7Q1YFT/6g4wC 6VcHM7m51LjpikeQ4nyjLotmkKqDGvVJ663tbWCcJDoa1wSpeTWqN2xXxfRmcEXtTFVW YKT+grUXxqQtCj/ZzOS4pdrgxAbq6mohDeSx3Kgb+5mRZcAPgZhfgh31E8toFoh+Dcwk 1YoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=M6OUxq5MRWIwo820poqhdTN5aSCn+WIZs4Yefeliges=; fh=nNXYygaKJ6LRO/a0CU3PHMpEN/pMNbEu8LYkZu7Yivs=; b=Xy5ioLLFI9EsEjhZ8dlutUiQxStrUFai6TEtmD+R+rKEgjiySV5xlpXaw5oWsDyu5f J/D5DaLv+XUsDey+zly7u8t1Z2MbmTl35+VktCuPl+G5ZhOVvuvId+lJUF7pkLrd1p/r aBU0ysGiTmJj5C1VvcnWYmwN8WvJIWHMwt0yZ3JnB92zhUH39kSE3YDf+bRtL73yyrFh TDuJHEFcdRhAo7vvKp3/6m0BecjuidlIKcyU1xmC5Wzm5pYZL2u2PtynJIsuA6IkAZ/y 7M6YcRn7n/g9a5vPeBcxXuB7eIhBaTGHFRXGLnC7Lbk84vIzAtYN7mLjeeyDuQjkwwaE N7Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=00HrlpQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m14-20020a50ef0e000000b005230d2bec19si3683199eds.178.2023.08.11.11.23.53; Fri, 11 Aug 2023 11:24:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=00HrlpQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235256AbjHKQd7 (ORCPT + 99 others); Fri, 11 Aug 2023 12:33:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229535AbjHKQd6 (ORCPT ); Fri, 11 Aug 2023 12:33:58 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F32C218F for ; Fri, 11 Aug 2023 09:33:57 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-cf4cb742715so2096614276.2 for ; Fri, 11 Aug 2023 09:33:57 -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=KaZROSotXsLewBi8I5UawiGyhYbyixl4O8tmOg0KlQ54d+ARHxmepeEgoO62W/xKil qAQLTPeIGxdYuBZnCPUPsviKWx67wsf4RhoKJuwR8MPWPkss3XrMmxzoRxNGKk8eMvVB Hrb/bOtkJRGOHteQhRxdmSjE/XKLkD27jvn5c/OWXlV1I5RjlGZ1ghUZGCBtdUb9lSFV bKxL85Agd7abTRxvCcUEQZwEd0Zid629XoLDdTORDMOchI7Kuj5wAtDie3M8px4wi+0n gHM09PwGa1HUCnrjklhlGLqwC241I1ekO9qdSLffirtdjKQXSYoqjVMhDvNtQK9h+i+Z ZMzA== X-Gm-Message-State: AOJu0YxaXTFx35faPzuOkpGwJy150zqs41+Vy8yK3ga5CO3h3i0//aXX frIKLnY9zVTea+wp8H7x3WX4WJcA+wg2nwmiJA== 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773958078978041986 X-GMAIL-MSGID: 1773958078978041986 `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;