Skip to content

Commit 96cd8d2

Browse files
Qian Caigregkh
authored andcommitted
powerpc/64s/pgtable: fix an undefined behaviour
[ Upstream commit c2e929b ] Booting a power9 server with hash MMU could trigger an undefined behaviour because pud_offset(p4d, 0) will do, 0 >> (PAGE_SHIFT:16 + PTE_INDEX_SIZE:8 + H_PMD_INDEX_SIZE:10) Fix it by converting pud_index() and friends to static inline functions. UBSAN: shift-out-of-bounds in arch/powerpc/mm/ptdump/ptdump.c:282:15 shift exponent 34 is too large for 32-bit type 'int' CPU: 6 PID: 1 Comm: swapper/0 Not tainted 5.6.0-rc4-next-20200303+ #13 Call Trace: dump_stack+0xf4/0x164 (unreliable) ubsan_epilogue+0x18/0x78 __ubsan_handle_shift_out_of_bounds+0x160/0x21c walk_pagetables+0x2cc/0x700 walk_pud at arch/powerpc/mm/ptdump/ptdump.c:282 (inlined by) walk_pagetables at arch/powerpc/mm/ptdump/ptdump.c:311 ptdump_check_wx+0x8c/0xf0 mark_rodata_ro+0x48/0x80 kernel_init+0x74/0x194 ret_from_kernel_thread+0x5c/0x74 Suggested-by: Christophe Leroy <christophe.leroy@c-s.fr> Signed-off-by: Qian Cai <cai@lca.pw> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Reviewed-by: Christophe Leroy <christophe.leroy@c-s.fr> Link: https://lore.kernel.org/r/20200306044852.3236-1-cai@lca.pw Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a7ba81f commit 96cd8d2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

arch/powerpc/include/asm/book3s/64/pgtable.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,25 @@ extern struct page *pgd_page(pgd_t pgd);
998998
#define pud_page_vaddr(pud) __va(pud_val(pud) & ~PUD_MASKED_BITS)
999999
#define pgd_page_vaddr(pgd) __va(pgd_val(pgd) & ~PGD_MASKED_BITS)
10001000

1001-
#define pgd_index(address) (((address) >> (PGDIR_SHIFT)) & (PTRS_PER_PGD - 1))
1002-
#define pud_index(address) (((address) >> (PUD_SHIFT)) & (PTRS_PER_PUD - 1))
1003-
#define pmd_index(address) (((address) >> (PMD_SHIFT)) & (PTRS_PER_PMD - 1))
1004-
#define pte_index(address) (((address) >> (PAGE_SHIFT)) & (PTRS_PER_PTE - 1))
1001+
static inline unsigned long pgd_index(unsigned long address)
1002+
{
1003+
return (address >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1);
1004+
}
1005+
1006+
static inline unsigned long pud_index(unsigned long address)
1007+
{
1008+
return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
1009+
}
1010+
1011+
static inline unsigned long pmd_index(unsigned long address)
1012+
{
1013+
return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
1014+
}
1015+
1016+
static inline unsigned long pte_index(unsigned long address)
1017+
{
1018+
return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
1019+
}
10051020

10061021
/*
10071022
* Find an entry in a page-table-directory. We combine the address region

0 commit comments

Comments
 (0)