Re: [PATCH v2] mm/kasan: Print frame description for stack bugs

From: Alexander Potapenko
Date: Tue May 21 2019 - 11:55:42 EST


On Tue, May 21, 2019 at 5:43 PM Andrey Ryabinin <aryabinin@xxxxxxxxxxxxx> wrote:
>
>
>
> On 5/20/19 6:47 PM, Marco Elver wrote:
>
> > +static void print_decoded_frame_descr(const char *frame_descr)
> > +{
> > + /*
> > + * We need to parse the following string:
> > + * "n alloc_1 alloc_2 ... alloc_n"
> > + * where alloc_i looks like
> > + * "offset size len name"
> > + * or "offset size len name:line".
> > + */
> > +
> > + char token[64];
> > + unsigned long num_objects;
> > +
> > + if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
> > + &num_objects))
> > + return;
> > +
> > + pr_err("\n");
> > + pr_err("this frame has %lu %s:\n", num_objects,
> > + num_objects == 1 ? "object" : "objects");
> > +
> > + while (num_objects--) {
> > + unsigned long offset;
> > + unsigned long size;
> > +
> > + /* access offset */
> > + if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
> > + &offset))
> > + return;
> > + /* access size */
> > + if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
> > + &size))
> > + return;
> > + /* name length (unused) */
> > + if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
> > + return;
> > + /* object name */
> > + if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
> > + NULL))
> > + return;
> > +
> > + /* Strip line number, if it exists. */
>
> Why?
>
> > + strreplace(token, ':', '\0');
> > +
>
> ...
>
> > +
> > + aligned_addr = round_down((unsigned long)addr, sizeof(long));
> > + mem_ptr = round_down(aligned_addr, KASAN_SHADOW_SCALE_SIZE);
> > + shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
> > + shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
> > +
> > + while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
> > + shadow_ptr--;
> > + mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
> > + }
> > +
> > + while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
> > + shadow_ptr--;
> > + mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
> > + }
> > +
>
> I suppose this won't work if stack grows up, which is fine because it grows up only on parisc arch.
> But "BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROUWSUP))" somewhere wouldn't hurt.
Note that KASAN was broken on parisc from day 1 because of other
assumptions on the stack growth direction hardcoded into KASAN
(e.g. __kasan_unpoison_stack() and __asan_allocas_unpoison()).
So maybe this BUILD_BUG_ON can be added in a separate patch as it's
not specific to what Marco is doing here?
>
> --
> You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@xxxxxxxxxxxxxxxxx
> To post to this group, send email to kasan-dev@xxxxxxxxxxxxxxxxx
> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/ebec4325-f91b-b392-55ed-95dbd36bbb8e%40virtuozzo.com.
> For more options, visit https://groups.google.com/d/optout.



--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-StraÃe, 33
80636 MÃnchen

GeschÃftsfÃhrer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg