mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd()
@ 2026-09-18 14:27 Thorsten Blum
  2026-10-31  1:46 ` Andrew Morton
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Thorsten Blum @ 2026-09-18 14:27 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Ahmed S. Darwish, Kevin Brodsky, Andreas Larsson,
	Yeoreum Yun, Tom Lendacky
  Cc: Thorsten Blum, Andrew Morton, Ritesh Harjani (IBM),
	Ingo Molnar, linux-kernel

The mapping helpers __sme_map_range_pmd() and __sme_map_range_pte(), and
therefore sme_encrypt_kernel(), use exclusive end addresses and stop
before vaddr reaches vaddr_end.

However, sme_clear_pgd() treats vaddr_end as inclusive when calculating
the last PGD entry to clear. If vaddr_end is PGD-aligned, the first PGD
entry outside the range is cleared too.

Use vaddr_end - 1 to calculate the last PGD entry within the range.

Fixes: 6ebcb060713f ("x86/mm: Add support to encrypt the kernel in-place")
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 arch/x86/boot/startup/sme.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
index c07a2c381ed1..2d5cb3f7eee6 100644
--- a/arch/x86/boot/startup/sme.c
+++ b/arch/x86/boot/startup/sme.c
@@ -99,7 +99,7 @@ static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
 	pgd_t *pgd_p;
 
 	pgd_start = ppd->vaddr & PGDIR_MASK;
-	pgd_end = ppd->vaddr_end & PGDIR_MASK;
+	pgd_end = (ppd->vaddr_end - 1) & PGDIR_MASK;
 
 	pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd()
  2026-09-18 14:27 [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd() Thorsten Blum
@ 2026-10-31  1:46 ` Andrew Morton
  2026-11-04 20:14   ` Thorsten Blum
  2026-11-05 14:12 ` Tom Lendacky
  2026-11-05 19:07 ` [tip: x86/boot] " tip-bot2 for Thorsten Blum
  2 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2026-10-31  1:46 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Ahmed S. Darwish, Kevin Brodsky, Andreas Larsson,
	Yeoreum Yun, Tom Lendacky, Ritesh Harjani (IBM),
	Ingo Molnar, linux-kernel

On Fri, 18 Sep 2026 16:27:13 +0200 Thorsten Blum <blum@kernel.org> wrote:

> The mapping helpers __sme_map_range_pmd() and __sme_map_range_pte(), and
> therefore sme_encrypt_kernel(), use exclusive end addresses and stop
> before vaddr reaches vaddr_end.
> 
> However, sme_clear_pgd() treats vaddr_end as inclusive when calculating
> the last PGD entry to clear. If vaddr_end is PGD-aligned, the first PGD
> entry outside the range is cleared too.
> 
> Use vaddr_end - 1 to calculate the last PGD entry within the range.

When fixing a bug, please always fully describe the worst-case
userspace-visible runtime effects of that bug.

I'm guessing "none known, this is from code inspection"?

Thanks.

> --- a/arch/x86/boot/startup/sme.c
> +++ b/arch/x86/boot/startup/sme.c
> @@ -99,7 +99,7 @@ static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
>  	pgd_t *pgd_p;
>  
>  	pgd_start = ppd->vaddr & PGDIR_MASK;
> -	pgd_end = ppd->vaddr_end & PGDIR_MASK;
> +	pgd_end = (ppd->vaddr_end - 1) & PGDIR_MASK;
>  
>  	pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
>  

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd()
  2026-10-31  1:46 ` Andrew Morton
@ 2026-11-04 20:14   ` Thorsten Blum
  0 siblings, 0 replies; 5+ messages in thread
From: Thorsten Blum @ 2026-11-04 20:14 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Ahmed S. Darwish, Kevin Brodsky, Andreas Larsson,
	Yeoreum Yun, Tom Lendacky, Ritesh Harjani (IBM),
	Ingo Molnar, linux-kernel

On Fri, Oct 30, 2026 at 06:46:42PM -0700, Andrew Morton wrote:
> On Fri, 18 Sep 2026 16:27:13 +0200 Thorsten Blum <blum@kernel.org> wrote:
> 
> > The mapping helpers __sme_map_range_pmd() and __sme_map_range_pte(), and
> > therefore sme_encrypt_kernel(), use exclusive end addresses and stop
> > before vaddr reaches vaddr_end.
> > 
> > However, sme_clear_pgd() treats vaddr_end as inclusive when calculating
> > the last PGD entry to clear. If vaddr_end is PGD-aligned, the first PGD
> > entry outside the range is cleared too.
> > 
> > Use vaddr_end - 1 to calculate the last PGD entry within the range.
> 
> When fixing a bug, please always fully describe the worst-case
> userspace-visible runtime effects of that bug.
> 
> I'm guessing "none known, this is from code inspection"?

Correct, this was found by code inspection.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd()
  2026-09-18 14:27 [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd() Thorsten Blum
  2026-10-31  1:46 ` Andrew Morton
@ 2026-11-05 14:12 ` Tom Lendacky
  2026-11-05 19:07 ` [tip: x86/boot] " tip-bot2 for Thorsten Blum
  2 siblings, 0 replies; 5+ messages in thread
From: Tom Lendacky @ 2026-11-05 14:12 UTC (permalink / raw)
  To: Thorsten Blum, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Ahmed S. Darwish,
	Kevin Brodsky, Andreas Larsson, Yeoreum Yun
  Cc: Andrew Morton, Ritesh Harjani (IBM), Ingo Molnar, linux-kernel

On 9/18/26 09:27, Thorsten Blum wrote:
> The mapping helpers __sme_map_range_pmd() and __sme_map_range_pte(), and
> therefore sme_encrypt_kernel(), use exclusive end addresses and stop
> before vaddr reaches vaddr_end.
> 
> However, sme_clear_pgd() treats vaddr_end as inclusive when calculating
> the last PGD entry to clear. If vaddr_end is PGD-aligned, the first PGD
> entry outside the range is cleared too.
> 
> Use vaddr_end - 1 to calculate the last PGD entry within the range.
> 
> Fixes: 6ebcb060713f ("x86/mm: Add support to encrypt the kernel in-place")
> Signed-off-by: Thorsten Blum <blum@kernel.org>

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
> ---
>  arch/x86/boot/startup/sme.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
> index c07a2c381ed1..2d5cb3f7eee6 100644
> --- a/arch/x86/boot/startup/sme.c
> +++ b/arch/x86/boot/startup/sme.c
> @@ -99,7 +99,7 @@ static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
>  	pgd_t *pgd_p;
>  
>  	pgd_start = ppd->vaddr & PGDIR_MASK;
> -	pgd_end = ppd->vaddr_end & PGDIR_MASK;
> +	pgd_end = (ppd->vaddr_end - 1) & PGDIR_MASK;
>  
>  	pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
>  


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip: x86/boot] x86/boot: Fix off-by-one error in sme_clear_pgd()
  2026-09-18 14:27 [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd() Thorsten Blum
  2026-10-31  1:46 ` Andrew Morton
  2026-11-05 14:12 ` Tom Lendacky
@ 2026-11-05 19:07 ` tip-bot2 for Thorsten Blum
  2 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Thorsten Blum @ 2026-11-05 19:07 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Thorsten Blum, Borislav Petkov (AMD), Tom Lendacky, x86, linux-kernel

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     8523d938dce836fbe7ea33fcc98d8460b094ef81
Gitweb:        https://git.kernel.org/tip/8523d938dce836fbe7ea33fcc98d8460b094ef81
Author:        Thorsten Blum <blum@kernel.org>
AuthorDate:    Fri, 18 Sep 2026 16:27:13 +02:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Thu, 05 Nov 2026 10:21:38 -08:00

x86/boot: Fix off-by-one error in sme_clear_pgd()

The mapping helpers __sme_map_range_pmd() and __sme_map_range_pte(), and
therefore sme_encrypt_kernel(), use exclusive end addresses and stop
before vaddr reaches vaddr_end.

However, sme_clear_pgd() treats vaddr_end as inclusive when calculating
the last PGD entry to clear. If vaddr_end is PGD-aligned, the first PGD
entry outside the range is cleared too.

Use vaddr_end - 1 to calculate the last PGD entry within the range.

Fixes: 6ebcb060713f ("x86/mm: Add support to encrypt the kernel in-place")
Signed-off-by: Thorsten Blum <blum@kernel.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Link: https://patch.msgid.link/20260918142714.242711-2-blum@kernel.org
---
 arch/x86/boot/startup/sme.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
index c07a2c3..2d5cb3f 100644
--- a/arch/x86/boot/startup/sme.c
+++ b/arch/x86/boot/startup/sme.c
@@ -99,7 +99,7 @@ static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
 	pgd_t *pgd_p;
 
 	pgd_start = ppd->vaddr & PGDIR_MASK;
-	pgd_end = ppd->vaddr_end & PGDIR_MASK;
+	pgd_end = (ppd->vaddr_end - 1) & PGDIR_MASK;
 
 	pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-05 19:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-18 14:27 [PATCH RESEND] x86/boot: Fix off-by-one error in sme_clear_pgd() Thorsten Blum
2026-10-31  1:46 ` Andrew Morton
2026-11-04 20:14   ` Thorsten Blum
2026-11-05 14:12 ` Tom Lendacky
2026-11-05 19:07 ` [tip: x86/boot] " tip-bot2 for Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®