From mboxrd@z Thu Jan 1 00:00:00 1970 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 61855C001B0 for ; Thu, 10 Aug 2023 19:01:11 +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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=q5rnPtkOgvjqxt0oUTRQvx7BL+WVt1n8MlqclDMc/U0=; b=4wygaKh5c1+RqH nMzJLfavjZUk1E228Bb6T4j5FDO6bfNic0ElkMcx+lQNlh3ZfMGHuvLBdJbBNlUO9cHiZGg8QIOHi IDB/9LFV6m0XAxbE+ehM5r0jaPg0jOpcl3HWxLTZGmBs/G839eAeIRXKaVxXZseKBk4ue9/kpl4CU a+8l8uquTJwLkpHMpSzikyonZsQB6tsenTnWu1pDzA82dH4YvhNV6z6qX3VSrObW2r/uMNbodWR1H 8+5XadZaQBg2LF+XHirWtS9zj3Vg/YkMG2ltEf4SQOTj0KGUyReIO1qW6mLiuN/GjbLrt/YqV3Vre dzzUrQ0dbLOqRPnILocQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qUAtl-008UTc-1E; Thu, 10 Aug 2023 19:00:41 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qUAti-008URV-1e for linux-arm-kernel@lists.infradead.org; Thu, 10 Aug 2023 19:00:40 +0000 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-68706b39c4cso942801b3a.2 for ; Thu, 10 Aug 2023 12:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691694036; x=1692298836; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=2pDoIIBmfCvDO688KPmA5fczvqhk8UceQwraywNL9tc=; b=nYqpk2RC9oF28D912uGs1EAh+cTVjhhp83I9sUY8Wt7fx9Mwk2hDj4bLWCbb+xj9pN kRrMhprpPKQRNED4xixZCB3hTHHglGzHaqleHJ1B48V1ycfz73RTaWBoYw28xxgo6jnZ tj5p6NsGQBM8tVAZiAF4GKDq+7UC9tPmMKBfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691694036; x=1692298836; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2pDoIIBmfCvDO688KPmA5fczvqhk8UceQwraywNL9tc=; b=X+ccVLrJNIWE2L8fd1pnWnqWHcHdTM7daNx1ljS9cHVE6ACa6y2dAPttoPyhuL4nNW T6s7EfBfP2HUAKf8OuJwwgz7InKmSTgSp8/1PE4qc2V5qUAXDjvDtAzio//Z+DtTnsRI f+akt8Q5h4AmnECEphlrVue8JpMW+XivLfGEnVh+jX/FABFsWnM9AT/cdkX6plR2zhrV Kz64BWrFVpj7Vm/ZYUhk7BkFZDDh/tgoth47Jb8ZvKYEy0nNDAr4uMADISIiEq0MpM6i ayLPdmUpk3NtPjbdsGV8N98uNvecL0jDaaQzI+FBjdHcEeNDDN+ZZLinPKRZnmBzdgLt vE5g== X-Gm-Message-State: AOJu0YxBSBm6/7Fd3SrsDMfNxM26agbDdRTxTVPjHlc6a6S42TlIBK5q HpwpC3AtjPjT4LmdUgyvzt+PLpAqQGxk7eZSsww= X-Google-Smtp-Source: AGHT+IHq/PIN+zQMWZEQ03Gl2aHm7aN7Itbvi7T0nJbs0wWH/4fy2NhkY52HWQNhsc5WbHhRFOKFsg== X-Received: by 2002:a05:6a21:4841:b0:140:a0dc:c834 with SMTP id au1-20020a056a21484100b00140a0dcc834mr2938661pzc.24.1691694036431; Thu, 10 Aug 2023 12:00:36 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id c2-20020aa78802000000b00687087d8bc3sm1918804pfo.141.2023.08.10.12.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 12:00:35 -0700 (PDT) Date: Thu, 10 Aug 2023 12:00:35 -0700 From: Kees Cook To: Justin Stitt Cc: Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] arm64/sysreg: refactor deprecated strncpy Message-ID: <202308101155.81497C5B@keescook> References: <20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230810_120038_566965_A768EF57 X-CRM114-Status: GOOD ( 26.74 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, Aug 10, 2023 at 06:39:03PM +0000, Justin Stitt wrote: > `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. Another note to include in these evaluations would be "does the destination expect to be %NUL padded?". Here, it looks like no, as all the routines "buf" is passed to expect a regular C string (padding doesn't matter). > > 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(-) > > 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); This, however, isn't correct: "cmdline" will be incremented by "leN" later, and we want a count of the characters copied into "buf", even if they're truncated. I think this should be: strscpy(buf, cmdline, ARRAY_SIZE(buf)); len = strlen(buf); -Kees > > if (strcmp(buf, "--") == 0) > return; > > --- > base-commit: 52a93d39b17dc7eb98b6aa3edb93943248e03b2f > change-id: 20230810-strncpy-arch-arm64-1f3d328bd9b8 > > Best regards, > -- > Justin Stitt > -- Kees Cook _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel