x86/boot: Fix off-by-one error in sme_clear_pgd()
PATCH RESEND 1/1 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.
--- arch/x86/boot/startup/sme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/startup/sme.c +1 -1
@@ -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);
Quoted 9 lines · On Fri, 18 Sep 2026 16:27:13 +0200 Thorsten Blum <[email protected]> wrote:
The mapping helpers
__sme_map_range_pmd()and__sme_map_range_pte(), and thereforesme_encrypt_kernel(), use exclusive end addresses and stop before vaddr reachesvaddr_end.However,
sme_clear_pgd()treatsvaddr_endas inclusive when calculating the last PGD entry to clear. Ifvaddr_endis 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.
Quoted 11 lines
arch/x86/boot/startup/sme.c +1
-1@@ -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);
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Fri, 18 Sep 2026 16:27:13 +0200 Thorsten Blum <[email protected]> wrote:
The mapping helpers
__sme_map_range_pmd()and__sme_map_range_pte(), and thereforesme_encrypt_kernel(), use exclusive end addresses and stop before vaddr reachesvaddr_end.However,
sme_clear_pgd()treatsvaddr_endas inclusive when calculating the last PGD entry to clear. Ifvaddr_endis PGD-aligned, the first PGD entry outside the range is cleared too.
Quoted 6 lines
Quoted 1 line
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.
Quoted 4 lines · Thorsten Blum
The mapping helpers
__sme_map_range_pmd()and__sme_map_range_pte(), and thereforesme_encrypt_kernel(), use exclusive end addresses and stop before vaddr reachesvaddr_end.
Quoted 8 lines
However,
sme_clear_pgd()treatsvaddr_endas inclusive when calculating the last PGD entry to clear. Ifvaddr_endis 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 <[email protected]>
Quoted 17 lines
--- arch/x86/boot/startup/sme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/boot/startup/sme.c +1
-1@@ -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);
The following commit has been merged into the x86/boot branch of tip:
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 <[email protected]> AuthorDate: Fri, 18 Sep 2026 16:27:13 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> 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.
--- arch/x86/boot/startup/sme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/startup/sme.c +1 -1
@@ -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);
The following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: cb319926808b1cca522d2087a13205e840beb45b Gitweb: https://git.kernel.org/tip/cb319926808b1cca522d2087a13205e840beb45b Author: Thorsten Blum <[email protected]> AuthorDate: Fri, 18 Sep 2026 16:27:13 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 06 Nov 2026 09:05:23 -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.
--- arch/x86/boot/startup/sme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/startup/sme.c +1 -1
@@ -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);