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 71D47C48BC4 for ; Tue, 20 Feb 2024 11:57:24 +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:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=40BmPauyvqDJKZc+OGqvABSNJ+mB13/4LHqg61jxhuU=; b=j8OI98okYGBG+j pfhjXOaTuJdX+rStMKBR3ManTB3KFVNh/uw1EbhO1qrla6ccNn+GaROdYsxD5Dl+pYB72mhe+TdIn FzanQgb7VygyzvDo/+aU/VnEcJMst0h5Tr11XpXApAYQZzIkoZZxrjeRkoVGYR9uVHbND2sr0Ay2R EDTRTl6UXAA/Y8Z8xypc8fMECxqP+0Iav4eni6AWvSJ1DbOVwjkJjPIAWH6nyt812/Rej8wapZTDk yGvlb9vIw7e0/Ckn7b8wfWN0lIkyuDbDEMSoqt12tSL/+dL9VWlUKTANM4vakpuPIPoGkOlXwI1YZ sAi3HdntuWxMq/bIqxoQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rcOkM-0000000EZ3P-40Cu; Tue, 20 Feb 2024 11:57:14 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rcOkK-0000000EZ2u-0HHy for linux-arm-kernel@lists.infradead.org; Tue, 20 Feb 2024 11:57:13 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 55765CE188C; Tue, 20 Feb 2024 11:57:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 886DEC433C7; Tue, 20 Feb 2024 11:57:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708430227; bh=8Nce/i/DtSEnqU882m4uZZHEkogE/ADUKlPVWKkOfMA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=LU0/vz4jnuMEn3+iUqSfhnqtybp2jz5W0KN9su+Ia5mbHqncFMmO0svfhV0Sw3HxW S3V6gfpMzbv+z4r7DBtvZmqX0eSot78SW0ea+vX6OAl5TuwVJBGN2MgLqUoZcJbjZ9 70ghZfyUfic68hX+g1qaWNJFCtRxIU4zfYHAS1SK88repj0DhsoPmxewWxr52121pA lfdAFxBxNesfTxZizDVRKe89WiN6aQT40GmujdwlfuC4ECQoKvcPQFcX2cHscsYMU1 WRYrQ1U0vcdyCkPKgusEiI96xG2XS93Lb2a0S/uJ1lvkWICJPKt8WYonQz4g7KxIKV FNTuA8vYxnz8Q== 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 1rcOkD-004uFu-CO; Tue, 20 Feb 2024 11:57:05 +0000 Date: Tue, 20 Feb 2024 11:57:04 +0000 Message-ID: <8634tn4acv.wl-maz@kernel.org> From: Marc Zyngier To: Joey Gouly Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Will Deacon , Catalin Marinas Subject: Re: [PATCH 01/13] KVM: arm64: Harden __ctxt_sys_reg() against out-of-range values In-Reply-To: <20240220112031.GA16168@e124191.cambridge.arm.com> References: <20240219092014.783809-1-maz@kernel.org> <20240219092014.783809-2-maz@kernel.org> <20240220112031.GA16168@e124191.cambridge.arm.com> 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/29.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: joey.gouly@arm.com, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, will@kernel.org, catalin.marinas@arm.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240220_035712_469708_E358D9DB X-CRM114-Status: GOOD ( 29.90 ) 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 Tue, 20 Feb 2024 11:20:31 +0000, Joey Gouly wrote: > > On Mon, Feb 19, 2024 at 09:20:02AM +0000, Marc Zyngier wrote: > > The unsuspecting kernel tinkerer can be easily confused into > > writing something that looks like this: > > > > ikey.lo = __vcpu_sys_reg(vcpu, SYS_APIAKEYLO_EL1); > > > > which seems vaguely sensible, until you realise that the second > > parameter is the encoding of a sysreg, and not the index into > > the vcpu sysreg file... Debugging what happens in this case is > > type safety :( Are you advocating for making everything a struct? Or something else? > > > an interesting exercise in head<->wall interactions. > > > > As they often say: "Any resemblance to actual persons, living > > or dead, or actual events is purely coincidental". > > > > In order to save people's time, add some compile-time hardening > > that will at least weed out the "stupidly out of range" values. > > This will *not* catch anything that isn't a compile-time constant. > > > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/include/asm/kvm_host.h | 9 ++++++++- > > 1 file changed, 8 insertions(+), 1 deletion(-) > > > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > > index 181fef12e8e8..a5ec4c7d3966 100644 > > --- a/arch/arm64/include/asm/kvm_host.h > > +++ b/arch/arm64/include/asm/kvm_host.h > > @@ -895,7 +895,7 @@ struct kvm_vcpu_arch { > > * Don't bother with VNCR-based accesses in the nVHE code, it has no > > * business dealing with NV. > > */ > > -static inline u64 *__ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) > > +static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) > > When in doubt, add more underscores! That's the one true way. > > > { > > #if !defined (__KVM_NVHE_HYPERVISOR__) > > if (unlikely(cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) && > > @@ -905,6 +905,13 @@ static inline u64 *__ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) > > return (u64 *)&ctxt->sys_regs[r]; > > } > > > > +#define __ctxt_sys_reg(c,r) \ > > + ({ \ > > + BUILD_BUG_ON(__builtin_constant_p(r) && \ > > + (r) >= NR_SYS_REGS); \ > > + ___ctxt_sys_reg(c, r); \ > > + }) > > I'm assuming the extra macro layer is to try make __builtin_constant_p() as > effective as possible? Otherwise maybe it relies on the compiler inling the > ___ctxt_sys_reg() function? It's not about efficiency. It's about making it *work*. Otherwise, lack of inlining will screw you over, and you may not check anything. Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel