Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp697822rdb; Sun, 3 Sep 2023 06:42:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFu8M1cPpgiibW71NCyB0HbfvWxm+PdoGYpJfD4876siStyYN1E+Tp/OgqE6NTjFVLWvMGy X-Received: by 2002:a17:906:220f:b0:9a2:c5a:6c9d with SMTP id s15-20020a170906220f00b009a20c5a6c9dmr5609945ejs.62.1693748531845; Sun, 03 Sep 2023 06:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693748531; cv=none; d=google.com; s=arc-20160816; b=mm9Ff7Q3Ne1z+8QCeO+yUn8FYQRfA25+r1Fo7kSbK7n8Kk+tX6g/U5dum/TFfBuNdG rvagEXdyWx7Jbn+ehBki61iyBB0QvFrEhGbmX2XlN0lNWVmFkIoFdYgxAjD9TiVT7wkB 6QsWIXmqKIfPHjKKXov8P+BBhOkCIO/qziNCVVjVnMWYOpUapuYvly7A8SYNidtvazj2 IlFFTZXXK1cq09optoQG739EshcihKmOWB2onVi2NeWUOE/koycJ8S6DpPKf2zWEoGKp EiUyqPruTHxW0HTioMnS7qE5pbjWK8oUVHJ8nMvqkYFz8OxbcvYZ4iWqYpZAtIwtlSUE O6WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=CQkHQuj1a4eY9/o6+vyyM4TUlC3RfmMDw1pmj0PkdsE=; fh=VmZpAhXzKQEzx2LE/2x+oowl0OWutroknfGs2+nWuPA=; b=VdwiBd9quUjlv7EIqpFRTpiUGxEQRJF2+wbKnKExS13jVCk4ybErxEUxjtzOKqqgJ2 5LVceK8MSVKU5HE9l54/bcq5b1N7bzbD39+UhXQanyUfWbX6FbU9jXp9cK79Dc7xw+AM 4VTW7UxinCdjSe2PRz+O+InRVQfKu2p8xB6Xg+uGEm4r/O3GyTW0BjuuAl8LFTRgA7/h u++vVFPYj62xHfuAJnn9Q1QrXldfDJCGywQV6Fhjg/LtKDyTyFsmF+v1pRedKtx+7oAG 8xiAecAH2CizYZpx5d6nGHUsc4W1lD7d11X1YVpLEiKeQSAriDqSiFzE8oAyPLuBzO7y Esag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AuDK154A; 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 f19-20020a170906561300b00992a9bc7432si5115807ejq.936.2023.09.03.06.41.46; Sun, 03 Sep 2023 06:42:11 -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=AuDK154A; 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 S1349684AbjIANEo (ORCPT + 99 others); Fri, 1 Sep 2023 09:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349675AbjIANEm (ORCPT ); Fri, 1 Sep 2023 09:04:42 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 252F410D7 for ; Fri, 1 Sep 2023 06:04:36 -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)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 79A1ECE22C9 for ; Fri, 1 Sep 2023 13:04:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2463C433C8; Fri, 1 Sep 2023 13:04:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1693573472; bh=ianlJYs3YdSH85UBoj+wspIhbGLauzc+UZvcFAKV3M0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=AuDK154A5mN38XmZcqBSsdt7b45OCeP/L7FMPgXrQ2FJr8v8DrDZ+wtPSDG0e/7mo DHbexUmbSjjiD6EzwTJld7lOwk0OdS8oqtvH02Z9Ei9ydEMi1QaGGNr7dDR0FmLEb0 xwr3JwJjmOgbdryrcv5laspvHyIdVF++NqAKNCzOnq4CzgcT4oCrFOpduGVxCcZIiG eXF4H/OshyOQoeClRFpE9Vm/GsrqVnwelPulZehbpXDwQ3eKF1Y8dzsnUnzxgZ+nXy W0S2znRA9gAQn9rlR6kzbC7ak5ssSE8yIEqEL1rWP5AC9ETIN8qNVM5OzBnryiVO8p Z+0gqfx1fKD3w== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1qc3p8-009xKP-6M; Fri, 01 Sep 2023 14:04:30 +0100 Date: Fri, 01 Sep 2023 14:04:29 +0100 Message-ID: <86ttsedoyq.wl-maz@kernel.org> From: Marc Zyngier To: Mostafa Saleh , justinstitt@google.com Cc: catalin.marinas@arm.com, will@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, kristina.martsenko@arm.com, broonie@kernel.org, quic_pkondeti@quicinc.com Subject: Re: [PATCH] Revert "arm64/sysreg: refactor deprecated strncpy" In-Reply-To: References: <20230831162227.2307863-1-smostafa@google.com> <86v8cudtky.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/28.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: smostafa@google.com, justinstitt@google.com, catalin.marinas@arm.com, will@kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, kristina.martsenko@arm.com, broonie@kernel.org, quic_pkondeti@quicinc.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 Fri, 01 Sep 2023 12:57:10 +0100, Mostafa Saleh wrote: > > Hi Marc, > > On Fri, Sep 01, 2023 at 12:24:45PM +0100, Marc Zyngier wrote: > > Hi Mostafa, > > > > On Thu, 31 Aug 2023 17:22:27 +0100, > > Mostafa Saleh wrote: > > > > > > This reverts commit d232606773a0b09ec7f1ffc25f63abe801d011fd. > > > > > > Using strscpy is not correct in this context and the commit > > > assumption is not right "strncpy is deprecated for use on > > > NUL-terminated destination strings". > > > > > > strncpy is used here to copy parts of the string(cmdline) separated > > > by spaces into the buffer and not a NULL terminated string. > > > > > > This breaks the arm options "kvm-arm.mode=protected, arm64.nobti ..." > > > > > > Signed-off-by: Mostafa Saleh > > > --- > > > arch/arm64/kernel/idreg-override.c | 6 +++--- > > > 1 file changed, 3 insertions(+), 3 deletions(-) > > > > > > diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c > > > index aee12c75b738..2fe2491b692c 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 = strscpy(buf, cmdline, ARRAY_SIZE(buf)); > > > - if (len == -E2BIG) > > > - len = ARRAY_SIZE(buf) - 1; > > > + len = min(len, ARRAY_SIZE(buf) - 1); > > > + strncpy(buf, cmdline, len); > > > + buf[len] = 0; > > > > > > if (strcmp(buf, "--") == 0) > > > return; > > > > Instead of completely reverting the patch, maybe something like the > > hack below (completely untested), so that we can still get rid of > > another instance of strncpy(), and yet bring back some sanity in the > > logic? > I was thinking of something similar, but I see we limit the len anyway > by the buffer size - 1 and force the NUL at the end so it should be > safe, unless the goal is to get rid of strncpy all the way, in this > case we can do it as you proposed. > > There is also a V3 of the original patch which uses memcpy instead. > https://lore.kernel.org/all/20230831-strncpy-arch-arm64-v3-1-cdbb1e7ea5e1@google.com/ Ah, that one would actually get my vote, because it does exactly what I actually intended this piece of code to do. Justin, do you mind rebasing your v3 on top of to restore the currently broken functionality? > > > Thanks, > > > > M. > > > > diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c > > index aee12c75b738..be5c778a3f14 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 = strscpy(buf, cmdline, ARRAY_SIZE(buf)); > > - if (len == -E2BIG) > > - len = ARRAY_SIZE(buf) - 1; > > + len = min(len, ARRAY_SIZE(buf) - 1); > > + strscpy(buf, cmdline, len); > > > > if (strcmp(buf, "--") == 0) > > return; > > > > > > -- > > > > Tested-by: Mostafa Saleh Cheers Mostafa. You could also just pick it, and repost it as a v2 (I really don't mind). It should give Will and Catalin a choice of implementations (and an opportunity for some additional bike-shedding ;-)). Thanks, M. -- Without deviation from the norm, progress is not possible.