From nobody Sat Feb 7 09:42:43 2026 Received: from mail-yx1-f49.google.com (mail-yx1-f49.google.com [74.125.224.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33CE12D8DB8 for ; Mon, 3 Nov 2025 14:10:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762179010; cv=none; b=MP4doX4WhhuwfZgfHvhMmbWq3AQ1WaukKmCF9NIUTx3b/n6VQ7/kfGHFdKZZCDXJCeSAcHUebgG4gPVkaLJnZZTocMhqWIm/DJLB8JDCtA10+gWvuKPzLjlTmtJJgckxV/pDMQbFrYmDmYtvWkon6ciAVU9RB3CHzDwupSsh320= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762179010; c=relaxed/simple; bh=nU7SRhisp51jqv6vpBzE4wByVuhH4GMe1baLCOf5/no=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PlPNqi+MHY9c5mCp7uAT6i54RiuTuSHZ1hI9lxwXR5PGrHzKNP8297Wop/sdn/nADgBNf582re2rVY+D0sP7CbVoT1PgrOJeU7Gr87LXC6g8O0rvG7Mn2HLYzZuMkOULSKQrHQ7IlU7mn8XBFWIQmzgHC4MtlRSelhxG5NKxX5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=c4QrUr0k; arc=none smtp.client-ip=74.125.224.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c4QrUr0k" Received: by mail-yx1-f49.google.com with SMTP id 956f58d0204a3-63e393c49f1so3987169d50.0 for ; Mon, 03 Nov 2025 06:10:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762179008; x=1762783808; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RffJKsZYwfIeClOJutqJXe8g4YN0czoTWsjwNOQNhJQ=; b=c4QrUr0kvtiEEvbenCUDibongxs2H9HPXrYxaf4YD4Ufa8hvjEbGS2EO6sKH3dx7Ug 2VtpYSbFpGWGt488uCK/HRzjP40kEpgPBCajBOJg+/fY3lJ71AWpN9vfDvvxxQkDM77q l5AzGy/xw8f4Bcx4AeJhNQrdcODna5houEPJNXhnuKBNP6ntiPt+K3AEjREyL0LN8/ai eyfBKkAj3l+gNvz6ev5X+KswyDMn2c1aw1bEo0Wj2DpDx7VBJj06MNc52O8SqTqsAsDN bnCtCxMVP7jJZBQ+ceyc+JELUavAUDxP8WfoWp26pOgDf63wk2yWx5s2/aedwmnZ2zJ1 sb3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762179008; x=1762783808; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RffJKsZYwfIeClOJutqJXe8g4YN0czoTWsjwNOQNhJQ=; b=p4vvcWSR11GlStotCUa054X3QihCX2tEsywguUX9gH+d+4bNX6mM87gvNjZgUswHYj 5ExG/VmzjJhSTLUGj/0eCTFjkKGAdl+XyXkJ7kL5N2mi1xHqcfZpcLWHV3utpJHKP2cc HPKuC5p2JmupXEMuCIMZkAIrKbrWmOOC0yifUAkmK9m5NxUfDTJ3sCKx86TA2ilW/4su jVdH1u31gwx8AwxmiZT6bgV8V7rf/c6GEtl82LyMScY2rcntkwhBJ7puKxtxsb+aPL4w ooyqTKoM+UV0N2mcb7oTHMF+XPoV9Ws6vkEfSbcN0Qjb8XYfhLcDT85VS6u1c819Ng24 Mijg== X-Forwarded-Encrypted: i=1; AJvYcCVCPMJVZ9KZhJb011w3l3uT53Z2fQDvNSVxeMWidNsAsuXmpzepwt1teP5uQyr2OVgU62A2QUCkri1/irk=@vger.kernel.org X-Gm-Message-State: AOJu0Yxcw/0czWpOUuUQ9R/0VN7BjMCTD2rVLLUDWE254AkIxel62Fsd ZF1gvs3PQAPUqRm+S9lzzywG+JDQoNLr0H5En1rTdfICca5OTekonuNn X-Gm-Gg: ASbGnctpwe24S+Iks7l1gXEaZxqXVFzIWm5j5CJOVckdlDYcDaSHzLgflQP7lGzxzQL YZxvtVgXJ/2Kk9GEhfblSjJy24VraUbrThi2V/PR/OgRhjLHu098ryTwKhL/w+MTvo1UEDvpICP kvUYlCW/uU5hvRDoJ7YiQzeB/xyHsLT4SlOEUuhP3X/4vaGbd4W+MNESaRwgGVtXtcoF0zvgq9J xQDi3LJjewqMoA652RHfZJSqRe2XJDKED8bngTKR6G61iqMXwfwyPWlhxOybI3JWgpZEmtp+6Oi bPrKaSwB/J1oyzoGEL2Hb5dcKuODVUJeqSedMjquR5CsIpaXNbQ+U1TVzhoI/MxG2D/8aryH351 C563KjO+Xcr+caiA9Cx4oIibfqhl93RgiC4+rxnLXPrsmrj2DZ+qOMAkBJSDiAk0XDEgIOgaWv7 saZDaiLE5ozQ== X-Google-Smtp-Source: AGHT+IEtG1QUz22cT4bcrn+HqayTfLH7U7XmUZhV5JmnvGzOafUB3S8cEy747cA45LmQRirnSkaaQA== X-Received: by 2002:a05:690e:d8d:b0:63f:96d7:a350 with SMTP id 956f58d0204a3-63f96d7a4f4mr7403217d50.66.1762179007954; Mon, 03 Nov 2025 06:10:07 -0800 (PST) Received: from localhost ([2a03:2880:25ff:50::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78691db135esm935267b3.14.2025.11.03.06.10.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Nov 2025 06:10:07 -0800 (PST) From: Usama Arif To: dwmw@amazon.co.uk, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, ardb@kernel.org, hpa@zytor.com Cc: x86@kernel.org, apopple@nvidia.com, thuth@redhat.com, nik.borisov@suse.com, kas@kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, kernel-team@meta.com, Usama Arif , Michael van der Westhuizen , Tobias Fleig Subject: [PATCH v3 2/2] efi/libstub: Fix page table access in 5-level to 4-level paging transition Date: Mon, 3 Nov 2025 14:09:23 +0000 Message-ID: <20251103141002.2280812-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251103141002.2280812-1-usamaarif642@gmail.com> References: <20251103141002.2280812-1-usamaarif642@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When transitioning from 5-level to 4-level paging, the existing code incorrectly accesses page table entries by directly dereferencing CR3 and applying PAGE_MASK. This approach has several issues: - __native_read_cr3() returns the raw CR3 register value, which on x86_64 includes not just the physical address but also flags Bits above the physical address width of the system (i.e. above __PHYSICAL_MASK_SHIFT) are also not masked. - The pgd value is masked by PAGE_SIZE which doesn't take into account the higher bits such as _PAGE_BIT_NOPTISHADOW. Replace this with proper accessor functions: - native_read_cr3_pa(): Uses CR3_ADDR_MASK to additionally mask metadata out of CR3 (like SME or LAM bits). All remaining bits are real address bits or reserved and must be 0. - mask pgd value with PTE_PFN_MASK instead of PAGE_MASK, accounting for flags above bit 51 (_PAGE_BIT_NOPTISHADOW in particular). Bits below 51, but above the max physical address are reserved and must be 0. Fixes: cb1c9e02b0c1 ("x86/efistub: Perform 4/5 level paging switch from the= stub") Co-developed-by: Kiryl Shutsemau Signed-off-by: Kiryl Shutsemau Signed-off-by: Usama Arif Reported-by: Michael van der Westhuizen Reported-by: Tobias Fleig Reviewed-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/x86-5lvl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-5lvl.c b/drivers/firmware/efi= /libstub/x86-5lvl.c index f1c5fb45d5f7c..c00d0ae7ed5d5 100644 --- a/drivers/firmware/efi/libstub/x86-5lvl.c +++ b/drivers/firmware/efi/libstub/x86-5lvl.c @@ -66,7 +66,7 @@ void efi_5level_switch(void) bool have_la57 =3D native_read_cr4() & X86_CR4_LA57; bool need_toggle =3D want_la57 ^ have_la57; u64 *pgt =3D (void *)la57_toggle + PAGE_SIZE; - u64 *cr3 =3D (u64 *)__native_read_cr3(); + pgd_t *cr3 =3D (pgd_t *)native_read_cr3_pa(); u64 *new_cr3; =20 if (!la57_toggle || !need_toggle) @@ -82,7 +82,7 @@ void efi_5level_switch(void) new_cr3[0] =3D (u64)cr3 | _PAGE_TABLE_NOENC; } else { /* take the new root table pointer from the current entry #0 */ - new_cr3 =3D (u64 *)(cr3[0] & PAGE_MASK); + new_cr3 =3D (u64 *)(native_pgd_val(cr3[0]) & PTE_PFN_MASK); =20 /* copy the new root table if it is not 32-bit addressable */ if ((u64)new_cr3 > U32_MAX) --=20 2.47.3