Skip to content

Commit f666c92

Browse files
kirylIngo Molnar
authored andcommitted
x86/mm/ident_map: Fix theoretical virtual address overflow to zero
The current calculation of the 'next' virtual address in the page table initialization functions in arch/x86/mm/ident_map.c doesn't protect against wrapping to zero. This is a theoretical issue that cannot happen currently, the problematic case is possible only if the user sets a high enough x86_mapping_info::offset value - which no current code in the upstream kernel does. ( The wrapping to zero only occurs if the top PGD entry is accessed. There are no such users upstream. Only hibernate_64.c uses x86_mapping_info::offset, and it operates on the direct mapping range, which is not the top PGD entry. ) Should such an overflow happen, it can result in page table corruption and a hang. To future-proof this code, replace the manual 'next' calculation with p?d_addr_end() which handles wrapping correctly. [ Backporter's note: there's no need to backport this patch. ] Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Kai Huang <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 775d37d commit f666c92

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

arch/x86/mm/ident_map.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page,
101101
pmd_t *pmd;
102102
bool use_gbpage;
103103

104-
next = (addr & PUD_MASK) + PUD_SIZE;
105-
if (next > end)
106-
next = end;
104+
next = pud_addr_end(addr, end);
107105

108106
/* if this is already a gbpage, this portion is already mapped */
109107
if (pud_leaf(*pud))
@@ -154,10 +152,7 @@ static int ident_p4d_init(struct x86_mapping_info *info, p4d_t *p4d_page,
154152
p4d_t *p4d = p4d_page + p4d_index(addr);
155153
pud_t *pud;
156154

157-
next = (addr & P4D_MASK) + P4D_SIZE;
158-
if (next > end)
159-
next = end;
160-
155+
next = p4d_addr_end(addr, end);
161156
if (p4d_present(*p4d)) {
162157
pud = pud_offset(p4d, 0);
163158
result = ident_pud_init(info, pud, addr, next);
@@ -199,10 +194,7 @@ int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
199194
pgd_t *pgd = pgd_page + pgd_index(addr);
200195
p4d_t *p4d;
201196

202-
next = (addr & PGDIR_MASK) + PGDIR_SIZE;
203-
if (next > end)
204-
next = end;
205-
197+
next = pgd_addr_end(addr, end);
206198
if (pgd_present(*pgd)) {
207199
p4d = p4d_offset(pgd, 0);
208200
result = ident_p4d_init(info, p4d, addr, next);

0 commit comments

Comments
 (0)