Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp711397rdg; Thu, 10 Aug 2023 18:00:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEa1WRGMMD8vikCneb/gJFAyQtenIvBy/Upt9v7Ph0jjcrcpf6XT6cacT8ka6EzJK75XQJX X-Received: by 2002:a05:6a00:39a5:b0:687:1604:39eb with SMTP id fi37-20020a056a0039a500b00687160439ebmr404781pfb.25.1691715623218; Thu, 10 Aug 2023 18:00:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691715623; cv=none; d=google.com; s=arc-20160816; b=0KHXiKTJsANDM79IQdEcuuLQS/FkIrXZPJbjsNUamRvTyRKkFmVEj7/3i7nfo5A9GT emTP9gDPi7ILOOhYez7FXQu4sfMYIP2Ni4vS09lGKSOiO1ifyTckE3X/x3K66l0+ryf6 T8FHdTYlvOctINUa5KOzPzj/pcf5++HuBxk7LiBhW3SUWfEEyQzF/WCysN0zTbKtkrRd USD4h4+n724n/eEHuMuy1UEIpfiY9D38MeEde8H8ZKS91NgFtThVQVifOx9IGdFcrhPI 73ex2i9z8ziYLiNTUtg/lEDlE2qQGR6+FdOf7KqNRpBZVcmwDlzehtODr9ispz5ABUah R9rA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature; bh=BLWNOCEQ9tXZVM2oKDYebVrUTzqiIK3nvYNaVLOhCJg=; fh=B3GOxvc1D0kiDENTwmSSuWzykSbI2zBkNNwttpUOUJs=; b=TOi4c9vGj6pRq/yKglaQ/5kazLawMMvLb6PE2KXMNgXVdr9IyzhlRBvtmBZtXKTOKi 3t8ydN+GGKJTpFHqLAvbfaTIFr4AATcbBr4IhizDmzf0K5zYQs+XXuRDYPjv7MYymMBe SvvM9WrmuFswiFogebiYlVbQ+ZOLzjLpwfy8Q9uqsFS224W9CpG4b+PVvIruDkh28au8 6qYwfPZfSj5D+ZSSj025qkMXT5pDyjhavBVWaifcup2daDlODAeIOF0rIP3dZKBkymLD mnrMnOdYdWotbLEpzzsMLQ9ZZiTJyzgcM2nuIQW2B8SZHOX7jQ81Rsdl6LzTGlh3NCp4 ZOjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r9jqh2h0; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bx40-20020a056a02052800b00545a181d157si2830848pgb.51.2023.08.10.18.00.10; Thu, 10 Aug 2023 18:00:23 -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=@kernel.org header.s=k20201202 header.b=r9jqh2h0; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231285AbjHKAmd (ORCPT + 99 others); Thu, 10 Aug 2023 20:42:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229605AbjHKAmc (ORCPT ); Thu, 10 Aug 2023 20:42:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4053926AB; Thu, 10 Aug 2023 17:42:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A984E65BB7; Fri, 11 Aug 2023 00:42:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA8E6C433C8; Fri, 11 Aug 2023 00:42:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691714551; bh=BLVBWqO0aRuEYlrB8Ztjvwvzg8thlHpFoerbknuafcs=; h=Date:From:To:CC:Subject:In-Reply-To:References:From; b=r9jqh2h0KZZYZDtige5DCcjtRdrHmZkgGSr8rZMOFWKpJy1OdDEi7LOi2yZ3k5zxU pX49HvM6qmrYYrktUFzgmf25B8sOQRcP2waURBl3xafOLof6sYReACF9Fj1+p9llRU idlDYn4fyWUYJTfs9sSyEosdV28AaeeUrJhiYQXJ/Vxh6lRbPWP2sNoBs0fLLmQOgT vk9/kSZHMH6u7WOrHm8UXq2xIjNeFdyC77dPnjWOQnOwOqlk4OvjpTUkLodqyVtzl5 83TkrtHmk+GprKipP+hWghNkre09QTCOZS3nR5X/OYsgCE9uVNrt4DfwMz7rwI9pMG wSzsLl261S5PQ== Date: Thu, 10 Aug 2023 17:42:26 -0700 From: Kees Cook To: Justin Stitt , Kees Cook 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 User-Agent: K-9 Mail for Android In-Reply-To: References: <20230810-strncpy-arch-arm64-v1-1-f67f3685cd64@google.com> <202308101155.81497C5B@keescook> <202308101257.47E6ACBD5@keescook> Message-ID: <4EF40BD8-9FBA-4D01-B2F4-154D32A503EB@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 On August 10, 2023 2:17:41 PM PDT, Justin Stitt = wrote: >On Thu, Aug 10, 2023 at 12:58=E2=80=AFPM Kees Cook wrote: >> >> On Thu, Aug 10, 2023 at 12:25:37PM -0700, Justin Stitt wrote: >> > On Thu, Aug 10, 2023 at 12:00=E2=80=AFPM Kees Cook wrote: >> > > >> > > On Thu, Aug 10, 2023 at 06:39:03PM +0000, Justin Stitt wrote: >> > > > `strncpy` is deprecated for use on NUL-terminated destination str= ings >> > > > [1]=2E Which seems to be the case here due to the forceful settin= g of `buf`'s >> > > > tail to 0=2E >> > > >> > > Another note to include in these evaluations would be "does the >> > > destination expect to be %NUL padded?"=2E Here, it looks like no, a= s all >> > > the routines "buf" is passed to expect a regular C string (padding >> > > doesn't matter)=2E >> > > >> > > > >> > > > A suitable replacement is `strscpy` [2] due to the fact that it >> > > > guarantees NUL-termination on its destination buffer argument whi= ch is >> > > > _not_ the case for `strncpy`! >> > > > >> > > > In this case, there is some behavior being used in conjunction wi= th >> > > > `strncpy` that `strscpy` already implements=2E This means we can = drop some >> > > > of the extra stuff like `=2E=2E=2E -1` and `buf[len] =3D 0` >> > > > >> > > > This should have no functional change and yet uses a more robust = and >> > > > less ambiguous interface whilst reducing code complexity=2E >> > > > >> > > > Link: www=2Ekernel=2Eorg/doc/html/latest/process/deprecated=2Ehtm= l#strncpy-on-nul-terminated-strings[1] >> > > > Link: https://manpages=2Edebian=2Eorg/testing/linux-manual-4=2E8/= strscpy=2E9=2Een=2Ehtml [2] >> > > > Link: https://github=2Ecom/KSPP/linux/issues/90 >> > > > Cc: linux-hardening@vger=2Ekernel=2Eorg >> > > > >> > > > Signed-off-by: Justin Stitt >> > > > --- >> > > > For reference, see a part of `strscpy`'s implementation here: >> > > > >> > > > | /* Hit buffer length without finding a NUL; force NUL-termi= nation=2E */ >> > > > | if (res) >> > > > | dest[res-1] =3D '\0'; >> > > > >> > > > Note: compile tested >> > > > --- >> > > > arch/arm64/kernel/idreg-override=2Ec | 5 ++--- >> > > > 1 file changed, 2 insertions(+), 3 deletions(-) >> > > > >> > > > diff --git a/arch/arm64/kernel/idreg-override=2Ec b/arch/arm64/ke= rnel/idreg-override=2Ec >> > > > index 2fe2491b692c=2E=2E482dc5c71e90 100644 >> > > > --- a/arch/arm64/kernel/idreg-override=2Ec >> > > > +++ b/arch/arm64/kernel/idreg-override=2Ec >> > > > @@ -262,9 +262,8 @@ static __init void __parse_cmdline(const char= *cmdline, bool parse_aliases) >> > > > if (!len) >> > > > return; >> > > > >> > > > - len =3D min(len, ARRAY_SIZE(buf) - 1); >> > > > - strncpy(buf, cmdline, len); >> > > > - buf[len] =3D 0; >> > > > + len =3D min(len, ARRAY_SIZE(buf)); >> > > > + strscpy(buf, cmdline, len); >Perhaps keeping the `=2E=2E=2E - 1` is good because we then don't have t= o >check strlen immediately after=2E This does still silently truncate but >didn't the previous `strncpy` also do that? Ah, actually there's no need to get too tricky here=2E This should be beha= viorally identical: len =3D strscpy(buf, cmdline, ARRAY_SIZE(buf)); if (len =3D=3D -E2BIG) len =3D ARRAY_SIZE(buf) - 1; -Kees --=20 Kees Cook