Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp1123376rdb; Fri, 2 Feb 2024 14:47:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsSQSSvuAIFvAshjT4AnMc+nER+LWBOHq9emD5udGHfmDbK/lmNfiQuU9pm9xSR1Y6uCpL X-Received: by 2002:a05:6512:62:b0:511:1ed7:61bd with SMTP id i2-20020a056512006200b005111ed761bdmr2339461lfo.54.1706914067995; Fri, 02 Feb 2024 14:47:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706914067; cv=pass; d=google.com; s=arc-20160816; b=rxYK64LEnyANYO6a5cZVqPJ4ir2F5oO0erYs2dh3aXIkbXymGbFDVXd/ztMkOepnNQ 1kLZA6WIP1xCHXVA3TwH0CZpI01kvkV/lGYQoyMcZ2jL8iz959ZxqZwSQSDe9WJUSYro HT3lud+LTw1mgJVwFED/84vFYobsftJqDfkDdKb0BiDJVLjF3znhFPwnr0pjZ+XTTiMr XpJns6a5IBb4sEOgmZ7AWMBmlhG+blND6mGu3XuaTWiuIx5NSLKkq62htixICHzisdyj Y+qCu/22+L8nYn5nRx0nyDuZVYjBr/tHMOYUSoZ+qDp5zkKjD9AoVPHDb03rhfttFGr2 NekA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=MnvV6ZaJ1qKhyKwNgOnXq7N24UQkGRQN4iQInF7UuJM=; fh=2TgNFFHdZymAVJHWgYgZawS6sKu9H1rTaoAc9e+la+A=; b=Eie+9DkkpmcbDBA0xrhs6MMliimqBBuH/rSItkIOW7In5cRgH351rDSRs6YTFEkU7L HjpIWHwM3DR5EU8xmY/WD3w1kF47WrJ07gyOipcbrlBdMdJ9lnXs6paXv8FkDJFsfMnS uo9eeSeoKI657oEz+cjEgXHH+Sz0PcLgLuykUZFpBNdKlr/UbCz5ocREzLnzZ34MGiyD en21JoUR4pZR9Xj/wEiaL2t6nfbCuAk5VuF5EFpRhzuvgSy6SwukSTGilCq1OmIZFfaq qYqWO34O1HhwpnEKka+Nud3nCD6YnKKkUJmN/Tzc44l4hxTQuw3sfSJnj7HaWO6zwwmN CS/w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=n0DPef5g; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50682-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50682-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCVq26D6MBuF/R5Rrp2dhC99/JtbuT/taqTBo6+SX/W1puKEtmC96J/KS81D3gqZhFMVIdvHycVQMwmY/n3XmP1YpTy1CvZiJFGBPLbayQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i19-20020a170906115300b00a36600d8ed0si1154522eja.658.2024.02.02.14.47.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:47:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50682-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=n0DPef5g; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-50682-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50682-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8EDF31F273B5 for ; Fri, 2 Feb 2024 22:47:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EC7C126F1A; Fri, 2 Feb 2024 22:47:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n0DPef5g" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD4B25FF01 for ; Fri, 2 Feb 2024 22:47:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706914058; cv=none; b=tHrdk6rb5MxSEDXjSr8PSx/UG13GTYCW6Y5+g5I06abXh9EZPRHRaEx7Pp/uAiuE764mODid3mMOgV3rT/kYukel84lTrE02a3MAcrVbNDqV1fzi7HNyzDpuvEQEZQQBWVrzHxEtRUzXDajFvTG4dAlVRn92R7c98WtaukZMcBY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706914058; c=relaxed/simple; bh=FRA7NClfGuH+mvD0ef76LKG3VhCS7wCAZ28qMHUqieI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=YvvPsi2bQ4Go6Yjsb/ExIcDDE1D+izUMNqrwsd8i/vmN2FwiVQS8MfTwtv+5ivlTb3bkmoyXO3GSfsInoVZcuf2XnQ5jR4x8uSkvuUKRSpKi9zkotTcwIsfHwZIbpYK830fMqj8Hy5L+XPfJ7aybQ2KCmLABWCK6ISxmCpjp9OA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n0DPef5g; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29E98C433C7 for ; Fri, 2 Feb 2024 22:47:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706914058; bh=FRA7NClfGuH+mvD0ef76LKG3VhCS7wCAZ28qMHUqieI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=n0DPef5gSNxToXTohsSeZcihSpMaSpUKBj1kmxQ8Ovo7ep11yIBePTRjz5D+Lwryz a0vbIvbGQGl8DzLy4D7sA9GTwhVxhN7fxbEVxyJtmR18bQF5zWJOUPO7ylgibJRtAM bOtiZIJJBs34wQGc0i+FUULk+gksBFUAUOwmrNfxCWX7rHeYYXOqyyp3ZAS+mA9apz oXFAh7ANei2JCeeujyMqZdbkMUjmSkVysc3OrQ7AMRUI1H2hj7pAcHhex2qHptN+WB h2+zOnv4OsTzsGTmZ5llFqSWL4MrFD4611phCPcz2wUtUuY8R3vYqHw38nG7O9fhIx rW+caqU/Yuc/g== Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-50eac018059so3183198e87.0 for ; Fri, 02 Feb 2024 14:47:38 -0800 (PST) X-Gm-Message-State: AOJu0Yyn6c6n+7Ou+/TAvLRQqcVkOJ/CK0u4zYzc24L06neGG3ljgfcy 9fEZ/iSaPPEyt0zeQo7gYfEaUXiV9bhpq6G7o6agLTZzz9DCRKCSn85wIZjBp/Q8uAgeTkgEK3t mcT3j8/BTSpTjd9XFPIVzIpYyDRw= X-Received: by 2002:a05:6512:62:b0:511:1ed7:61bd with SMTP id i2-20020a056512006200b005111ed761bdmr2339301lfo.54.1706914056299; Fri, 02 Feb 2024 14:47:36 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240121182040.GBZa1geI5NxWSslvt0@fat_crate.local> <20240130220845.1978329-2-kevinloughlin@google.com> <4ljfvg7c23g2wx4hcqz6x6tx7uvp6fffayqvu4ptsmfqajlujr@zngokbd2awma> In-Reply-To: From: Ard Biesheuvel Date: Fri, 2 Feb 2024 23:47:24 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 1/2] x86/sev: enforce RIP-relative accesses in early SEV/SME code To: Kevin Loughlin Cc: "Kirill A. Shutemov" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Nick Desaulniers , Justin Stitt , Tom Lendacky , Pankaj Gupta , Hou Wenlong , Dionna Glaze , Brijesh Singh , Michael Roth , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-coco@lists.linux.dev, Ashish Kalra , Andi Kleen , Adam Dunlap , Peter Gonda , Jacob Xu , Sidharth Telang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 2 Feb 2024 at 23:00, Kevin Loughlin wrot= e: > > On Wed, Jan 31, 2024 at 12:20=E2=80=AFAM Kirill A. Shutemov > wrote: > > > > On Tue, Jan 30, 2024 at 10:08:44PM +0000, Kevin Loughlin wrote: > > > diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/as= m/mem_encrypt.h > > > index 359ada486fa9..b65e66ee79c4 100644 > > > --- a/arch/x86/include/asm/mem_encrypt.h > > > +++ b/arch/x86/include/asm/mem_encrypt.h > > > @@ -17,6 +17,20 @@ > > > > > > #include > > > > > > +/* > > > + * Like the address operator "&", evaluates to the address of a LHS = variable > > > + * "var", but also enforces the use of RIP-relative logic. This macr= o can be > > > + * used to safely access global data variables prior to kernel reloc= ation. > > > + */ > > > +#define RIP_RELATIVE_ADDR(var) \ > > > +({ \ > > > + void *rip_rel_ptr; \ > > > + asm ("lea "#var"(%%rip), %0" \ > > > + : "=3Dr" (rip_rel_ptr) \ > > > + : "p" (&var)); \ > > > + rip_rel_ptr; \ > > > +}) > > > + > > > > I don't think it is the right place for the macro. The next patch uses = for > > things unrelated to memory encryption. > > You're right; with the cleanup, I agree it becomes more general. We > can move it to arch/x86/include/asm/asm.h. > > > > > > @@ -239,14 +244,14 @@ unsigned long __head __startup_64(unsigned long= physaddr, > > > */ > > > > > > next_pgt_ptr =3D fixup_pointer(&next_early_pgt, physaddr); > > > - pud =3D fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], ph= ysaddr); > > > - pmd =3D fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], ph= ysaddr); > > > + early_dynamic_pgts_ptr =3D fixup_pointer(early_dynamic_pgts, ph= ysaddr); > > > + pud =3D (pudval_t *) early_dynamic_pgts_ptr[(*next_pgt_ptr)++]; > > > + pmd =3D (pmdval_t *) early_dynamic_pgts_ptr[(*next_pgt_ptr)++]; > > > > > > > This change doesn't belong to this patch. Maybe move it into the next > > patch and combine with removing fixup_pointer(). > > I'll put it in a separate commit even preceding this one, as it's > actually a bug in the existing fixup pointer logic that I noticed when > transitioning to the use of the RIP-relative macro. Specifically, > early_dynamic_pgts is a global variable just like next_early_pgt and > thus also needs to be fixed up to guarantee the correct address is > used across toolchains. No, this is not a bug. early_dynamic_pgts is a two dimensional array extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD]; and so this expression early_dynamic_pgts[(*next_pgt_ptr)++] produces the kernel virtual address of one of the top level elements, which itself is an array, and so this can be fixed up as usual. IOW, the [] operation does not result in pointer dereference here, only in pointer arithmetic.