From patchwork Tue Sep 5 20:10:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13375025 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 414DACA1001 for ; Tue, 5 Sep 2023 20:10:53 +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=v5N5xiXUQAE6qRydCeNLrebMXZwNMnIOCn3kEx5YgvM=; b=hZU oHRhRwttBHcXoJ0gxDicQf9wgeZIE4/z0GjMoRyTyVYYQB3WiVWHxPYy2td9KRxBHsq9Wu5FAwUBG klDLxsdlZJAlX0Ai0fmthUjh/yUKiG817RgjifG1HEjEp7MQAI402UhzMwIYcuND+7mInoHwjuDiY sktK7oHj60sH8lZeIgtEItPhl2RsClxjJNHHqulteRdGH2o82ewnlN6+ds9KJukgGOdSU1pEOHoSh tKpnYG7L55r9jeaqomqteY8HipWnz+G/8uU1zdv1wq3CPJa/yjOvvG5rD7IOaVpsg1/0J/RGLFg0P KbTqgqlJttBGsVFpU3hsSkoR0aFYvHw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdcNZ-006erh-14; Tue, 05 Sep 2023 20:10:29 +0000 Received: from mail-oa1-x49.google.com ([2001:4860:4864:20::49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdcNW-006eqe-1U for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 20:10:27 +0000 Received: by mail-oa1-x49.google.com with SMTP id 586e51a60fabf-1c8f14ed485so3311050fac.1 for ; Tue, 05 Sep 2023 13:10:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693944623; x=1694549423; darn=lists.infradead.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=4pmzv+LLCuNutmWDx5XVz6vIet3weuj1okr6x6iIDqs=; b=edcjEcP9VFKzynxUO3pYn95BxX5a7u4p3rfR5S+nCJ3kxWvr5DV+bS6RHH2sWpsKRw 2RacS23+4mKBE0WDuehWSkeKnWUWN7/t25jRvbcJkva1PXZgoVjGL/2F5wS+75YdW6sa 9Y5P/30WjQpmoAZgUErn/m4wOcFyg94vV1xJxBOgL2dnh/apx+bK+8Ocs++U4o2ZJzsp Mgk9bce/9PaY7mxCwZe6bkXXAi1wyOIoVYZylpaRw//OrCh02oaHKgMas/Evv/uM5e1/ Qf2UnBhUxwNAoeKxECToOZXvGjz3mDptA9qVf24r/WjN6G6mTm685X/0ycMh0ICwWUMF FPDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693944623; x=1694549423; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=4pmzv+LLCuNutmWDx5XVz6vIet3weuj1okr6x6iIDqs=; b=NDVJMPEplK0xzpCtZkxDWdtQIghzM9+RjOrqTW8DShj9fHchMkcZM/4qWYVoyE2s5t 9mzviJCeHNOMiLAiho+HHmsLDUNaUUBKU27r0qmUyPxRrFz2s2XYSAN2m9YKSLrR1xQh N1CAKCwz2o9jgx2laEifT+wwBI2VcE8+H1Xzi4WTnT0Bu1Cl+Q9/PkvBRjXgNIsX0Me9 L0lpJ1mrIqmn1+qzJpMSNxPrHRjtToRyeBrFnEZWw4yKbyGGkntaqn8ltSVBbUW94eFy zA1MgyeZOhBNf4WedXttvVmfE20aTP6sq0pbfQnFjsKbtmBVfvg4+tTaND8K155yHWsb 2ONA== X-Gm-Message-State: AOJu0YyQu3zggktqRgSHrfORgPCl7PI5JdNnYlyas0tyW/Jqs5Re0qU4 GzK7wQGHft2uyyhPnGUWuSWylu8GELfx7DSs3Q== X-Google-Smtp-Source: AGHT+IH0UAhU/aEXaLYwKV529bYDq2+IrLYFkojxcQXZZrpNvgRPipYWy3B0aEIwkrHJxkLEyoHB42gmo8sADZGiSQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6870:98b3:b0:1c6:7d66:d47 with SMTP id eg51-20020a05687098b300b001c67d660d47mr350044oab.3.1693944623730; Tue, 05 Sep 2023 13:10:23 -0700 (PDT) Date: Tue, 05 Sep 2023 20:10:21 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAC2L92QC/33NSwqDMBCA4atI1k0xDzXtqvcoXeQx0UBrJJFQE e/e6MpS6WbgH5hvZhQhOIjoWswoQHLR+T4HPxVId7JvATuTG9GSslKQEscx9HqYsAy6y+NVc0w sM4wKZS5KoHw4BLDuvaH3R+7OxdGHafuRyLr9yyWCCbZ1Y1ktKm1qfmu9b59w1v6FVi/RvUEOD ZoNJQUHqayVRv0YbGewY4NlQxulCDQgKyBfxrIsH3BtrsE9AQAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1693944622; l=2237; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=t/5OxQrRWKo7ANfBmo93SLcVLgmcGRCwdycJFf2MN74=; b=3u23MHg/jPWsU7x0jAafSrxxG4WZqrLPGQs34EinIy6nacmqUYB0HuRg2pkyCwTWc6oyA62E7 wNCh34KUVbPDkbS3QVngsClDPBUK9kMKubaBgFgZcoSnQY/jTofVttO X-Mailer: b4 0.12.3 Message-ID: <20230905-strncpy-arch-arm64-v4-1-bc4b14ddfaef@google.com> Subject: [PATCH v4] arm64/sysreg: refactor deprecated strncpy From: Justin Stitt To: Catalin Marinas , Will Deacon , Mostafa Saleh 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-20230905_131026_502537_80208DF5 X-CRM114-Status: GOOD ( 15.11 ) 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 [1] and should not be used if the src string is not NUL-terminated. When dealing with `cmdline` we are counting the number of characters until a space then copying these over into `buf`. Let's not use any of the str*() functions since the src string is not necessarily NUL-terminated. Prefer `memcpy()` alongside a forced NUL-termination as it more accurately describes what is going on within this function, i.e: copying from non NUL-terminated buffer into a NUL-terminated buffer. 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 Cc: linux-hardening@vger.kernel.org Suggested-by: Kees Cook Signed-off-by: Justin Stitt --- Changes in v4: - rebase onto mainline (thanks Will) - Link to v3: https://lore.kernel.org/r/20230831-strncpy-arch-arm64-v3-1-cdbb1e7ea5e1@google.com Changes in v3: - Fix faulty logic and use memcpy over strscpy (thanks Mostafa and Kees) - Use '\0' instead of 0 to make it abundantly clear that `buf` is a NUL-terminated string - Link to v2: https://lore.kernel.org/r/20230811-strncpy-arch-arm64-v2-1-ba84eabffadb@google.com 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 --- arch/arm64/kernel/idreg-override.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c 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..3addc09f8746 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -263,8 +263,8 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases) return; len = min(len, ARRAY_SIZE(buf) - 1); - strncpy(buf, cmdline, len); - buf[len] = 0; + memcpy(buf, cmdline, len); + buf[len] = '\0'; if (strcmp(buf, "--") == 0) return;