Skip to content

Commit a11a496

Browse files
lecopzertorvalds
authored andcommitted
kasan: fix unaligned address is unhandled in kasan_remove_zero_shadow
During testing kasan_populate_early_shadow and kasan_remove_zero_shadow, if the shadow start and end address in kasan_remove_zero_shadow() is not aligned to PMD_SIZE, the remain unaligned PTE won't be removed. In the test case for kasan_remove_zero_shadow(): shadow_start: 0xffffffb802000000, shadow end: 0xffffffbfbe000000 3-level page table: PUD_SIZE: 0x40000000 PMD_SIZE: 0x200000 PAGE_SIZE: 4K 0xffffffbf80000000 ~ 0xffffffbfbdf80000 will not be removed because in kasan_remove_pud_table(), kasan_pmd_table(*pud) is true but the next address is 0xffffffbfbdf80000 which is not aligned to PUD_SIZE. In the correct condition, this should fallback to the next level kasan_remove_pmd_table() but the condition flow always continue to skip the unaligned part. Fix by correcting the condition when next and addr are neither aligned. Link: https://lkml.kernel.org/r/20210103135621.83129-1-lecopzer@gmail.com Fixes: 0207df4 ("kernel/memremap, kasan: make ZONE_DEVICE with work with KASAN") Signed-off-by: Lecopzer Chen <lecopzer.chen@mediatek.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Alexander Potapenko <glider@google.com> Cc: YJ Chiang <yj.chiang@mediatek.com> Cc: Andrey Konovalov <andreyknvl@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5c447d2 commit a11a496

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

mm/kasan/init.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ static void kasan_remove_pmd_table(pmd_t *pmd, unsigned long addr,
373373

374374
if (kasan_pte_table(*pmd)) {
375375
if (IS_ALIGNED(addr, PMD_SIZE) &&
376-
IS_ALIGNED(next, PMD_SIZE))
376+
IS_ALIGNED(next, PMD_SIZE)) {
377377
pmd_clear(pmd);
378-
continue;
378+
continue;
379+
}
379380
}
380381
pte = pte_offset_kernel(pmd, addr);
381382
kasan_remove_pte_table(pte, addr, next);
@@ -398,9 +399,10 @@ static void kasan_remove_pud_table(pud_t *pud, unsigned long addr,
398399

399400
if (kasan_pmd_table(*pud)) {
400401
if (IS_ALIGNED(addr, PUD_SIZE) &&
401-
IS_ALIGNED(next, PUD_SIZE))
402+
IS_ALIGNED(next, PUD_SIZE)) {
402403
pud_clear(pud);
403-
continue;
404+
continue;
405+
}
404406
}
405407
pmd = pmd_offset(pud, addr);
406408
pmd_base = pmd_offset(pud, 0);
@@ -424,9 +426,10 @@ static void kasan_remove_p4d_table(p4d_t *p4d, unsigned long addr,
424426

425427
if (kasan_pud_table(*p4d)) {
426428
if (IS_ALIGNED(addr, P4D_SIZE) &&
427-
IS_ALIGNED(next, P4D_SIZE))
429+
IS_ALIGNED(next, P4D_SIZE)) {
428430
p4d_clear(p4d);
429-
continue;
431+
continue;
432+
}
430433
}
431434
pud = pud_offset(p4d, addr);
432435
kasan_remove_pud_table(pud, addr, next);
@@ -457,9 +460,10 @@ void kasan_remove_zero_shadow(void *start, unsigned long size)
457460

458461
if (kasan_p4d_table(*pgd)) {
459462
if (IS_ALIGNED(addr, PGDIR_SIZE) &&
460-
IS_ALIGNED(next, PGDIR_SIZE))
463+
IS_ALIGNED(next, PGDIR_SIZE)) {
461464
pgd_clear(pgd);
462-
continue;
465+
continue;
466+
}
463467
}
464468

465469
p4d = p4d_offset(pgd, addr);

0 commit comments

Comments
 (0)