Re: [PATCH] kasan: Add explicit preconditions to kasan_report()

From: Catalin Marinas
Date: Wed Jan 20 2021 - 12:58:02 EST


On Wed, Jan 20, 2021 at 04:16:02PM +0000, Vincenzo Frascino wrote:
> On 1/20/21 4:04 PM, Catalin Marinas wrote:
> > On Tue, Jan 19, 2021 at 08:35:49PM +0000, Vincenzo Frascino wrote:
> >> On 1/19/21 6:52 PM, Catalin Marinas wrote:
> >>> On Tue, Jan 19, 2021 at 07:27:43PM +0100, Andrey Konovalov wrote:
> >>>> On Tue, Jan 19, 2021 at 6:26 PM Vincenzo Frascino
> >>>> <vincenzo.frascino@xxxxxxx> wrote:
> >>>>>
> >>>>> With the introduction of KASAN_HW_TAGS, kasan_report() dereferences
> >>>>> the address passed as a parameter.
> >>>>>
> >>>>> Add a comment to make sure that the preconditions to the function are
> >>>>> explicitly clarified.
> >>>>>
> >>>>> Note: An invalid address (e.g. NULL pointer address) passed to the
> >>>>> function when, KASAN_HW_TAGS is enabled, leads to a kernel panic.
> >>>>>
> >>>>> Cc: Andrey Ryabinin <aryabinin@xxxxxxxxxxxxx>
> >>>>> Cc: Alexander Potapenko <glider@xxxxxxxxxx>
> >>>>> Cc: Dmitry Vyukov <dvyukov@xxxxxxxxxx>
> >>>>> Cc: Leon Romanovsky <leonro@xxxxxxxxxxxx>
> >>>>> Cc: Andrey Konovalov <andreyknvl@xxxxxxxxxx>
> >>>>> Signed-off-by: Vincenzo Frascino <vincenzo.frascino@xxxxxxx>
> >>>>> ---
> >>>>> mm/kasan/report.c | 11 +++++++++++
> >>>>> 1 file changed, 11 insertions(+)
> >>>>>
> >>>>> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> >>>>> index c0fb21797550..2485b585004d 100644
> >>>>> --- a/mm/kasan/report.c
> >>>>> +++ b/mm/kasan/report.c
> >>>>> @@ -403,6 +403,17 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write,
> >>>>> end_report(&flags);
> >>>>> }
> >>>>>
> >>>>> +/**
> >>>>> + * kasan_report - report kasan fault details
> >>>>> + * @addr: valid address of the allocation where the tag fault was detected
> >>>>> + * @size: size of the allocation where the tag fault was detected
> >>>>> + * @is_write: the instruction that caused the fault was a read or write?
> >>>>> + * @ip: pointer to the instruction that cause the fault
> >>>>> + *
> >>>>> + * Note: When CONFIG_KASAN_HW_TAGS is enabled kasan_report() dereferences
> >>>>> + * the address to access the tags, hence it must be valid at this point in
> >>>>> + * order to not cause a kernel panic.
> >>>>> + */
> >>>>
> >>>> It doesn't dereference the address, it just checks the tags, right?
> >>>>
> >>>> Ideally, kasan_report() should survive that with HW_TAGS like with the
> >>>> other modes. The reason it doesn't is probably because of a blank
> >>>> addr_has_metadata() definition for HW_TAGS in mm/kasan/kasan.h. I
> >>>> guess we should somehow check that the memory comes from page_alloc or
> >>>> kmalloc. Or otherwise make sure that it has tags. Maybe there's an arm
> >>>> instruction to check whether the memory has tags?
> >>>
> >>> There isn't an architected way to probe whether a memory location has a
> >>> VA->PA mapping. The tags are addressed by PA but you can't reach them if
> >>> you get a page fault on the VA. So we either document the kasan_report()
> >>> preconditions or, as you suggest, update addr_has_metadata() for the
> >>> HW_TAGS case. Something like:
> >>>
> >>> return is_vmalloc_addr(virt) || virt_addr_valid(virt));
> >>>
> >>
> >> This seems not working on arm64 because according to virt_addr_valid 0 is a
> >> valid virtual address, in fact:
> >>
> >> __is_lm_address(0) == true && pfn_valid(virt_to_pfn(0)) == true.
> >
> > Ah, so __is_lm_address(0) is true. Maybe we should improve this since
> > virt_to_pfn(0) doesn't make much sense.
>
> How do you propose to improve it?

Check that it's actually a kernel address starting at PAGE_OFFSET. The
current __is_lm_address() check just masks out the top 12 bits but if
they were 0, this still yields a true result. Maybe extending the
current definition as:

#define __is_lm_address(addr) ((u64)(addr) >= PAGE_OFFSET && \
((u64)(addr) & ~PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))

Which basically means:

#define __is_lm_address(addr) ((u64)(addr) >= PAGE_OFFSET && \
(u64)(addr) < PAGE_END)

I think we could write the above as:

#define __is_lm_address(addr) (((u64)(addr) ^ PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))

This way we catch any 0 bits in the top 12 (or 16 with a 48-bit VA
configuration).

--
Catalin