linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Vyukov <dvyukov@google.com>
To: Paul Lawrence <paullawrence@google.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Alexander Potapenko <glider@google.com>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	LKML <linux-kernel@vger.kernel.org>,
	kasan-dev <kasan-dev@googlegroups.com>,
	Linux-MM <linux-mm@kvack.org>,
	"open list:KERNEL BUILD + fi..." <linux-kbuild@vger.kernel.org>,
	Matthias Kaehlcke <mka@chromium.org>,
	Michael Davidson <md@google.com>,
	Greg Hackmann <ghackmann@google.com>
Subject: Re: [PATCH v3 3/5] kasan: support alloca() poisoning
Date: Sun, 3 Dec 2017 13:23:00 +0100	[thread overview]
Message-ID: <CACT4Y+a-k7rc228ash9pf5UrH=uJHw9J_XyZH68BxysDdoZaww@mail.gmail.com> (raw)
In-Reply-To: <20171201213643.2506-4-paullawrence@google.com>

On Fri, Dec 1, 2017 at 10:36 PM, Paul Lawrence <paullawrence@google.com> wrote:
> clang's AddressSanitizer implementation adds redzones on either side of
> alloca()ed buffers.  These redzones are 32-byte aligned and at least 32
> bytes long.
>
> __asan_alloca_poison() is passed the size and address of the allocated
> buffer, *excluding* the redzones on either side.  The left redzone will
> always be to the immediate left of this buffer; but AddressSanitizer may
> need to add padding between the end of the buffer and the right redzone.
> If there are any 8-byte chunks inside this padding, we should poison
> those too.
>
> __asan_allocas_unpoison() is just passed the top and bottom of the
> dynamic stack area, so unpoisoning is simpler.
>
> Signed-off-by: Greg Hackmann <ghackmann@google.com>
> Signed-off-by: Paul Lawrence <paullawrence@google.com>
> ---
>  mm/kasan/kasan.c       | 34 ++++++++++++++++++++++++++++++++++
>  mm/kasan/kasan.h       |  8 ++++++++
>  mm/kasan/report.c      |  4 ++++
>  scripts/Makefile.kasan |  3 ++-
>  4 files changed, 48 insertions(+), 1 deletion(-)
>
> diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c
> index 405bba487df5..d96b36088b2f 100644
> --- a/mm/kasan/kasan.c
> +++ b/mm/kasan/kasan.c
> @@ -736,6 +736,40 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size)
>  }
>  EXPORT_SYMBOL(__asan_unpoison_stack_memory);
>
> +/* Emitted by compiler to poison alloca()ed objects. */
> +void __asan_alloca_poison(unsigned long addr, size_t size)
> +{
> +       size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
> +       size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
> +                       rounded_up_size;
> +       size_t rounded_down_size = round_down(size, KASAN_SHADOW_SCALE_SIZE);
> +
> +       const void *left_redzone = (const void *)(addr -
> +                       KASAN_ALLOCA_REDZONE_SIZE);
> +       const void *right_redzone = (const void *)(addr + rounded_up_size);
> +
> +       WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE));
> +
> +       kasan_unpoison_shadow((const void *)(addr + rounded_down_size),
> +                             size - rounded_down_size);
> +       kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
> +                       KASAN_ALLOCA_LEFT);
> +       kasan_poison_shadow(right_redzone,
> +                       padding_size + KASAN_ALLOCA_REDZONE_SIZE,
> +                       KASAN_ALLOCA_RIGHT);
> +}
> +EXPORT_SYMBOL(__asan_alloca_poison);
> +
> +/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */
> +void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
> +{
> +       if (unlikely(!stack_top || stack_top > stack_bottom))
> +               return;
> +
> +       kasan_unpoison_shadow(stack_top, stack_bottom - stack_top);
> +}
> +EXPORT_SYMBOL(__asan_allocas_unpoison);
> +
>  #ifdef CONFIG_MEMORY_HOTPLUG
>  static int __meminit kasan_mem_notifier(struct notifier_block *nb,
>                         unsigned long action, void *data)
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index c70851a9a6a4..7c0bcd1f4c0d 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -24,6 +24,14 @@
>  #define KASAN_STACK_PARTIAL     0xF4
>  #define KASAN_USE_AFTER_SCOPE   0xF8
>
> +/*
> + * alloca redzone shadow values
> + */
> +#define KASAN_ALLOCA_LEFT      0xCA
> +#define KASAN_ALLOCA_RIGHT     0xCB
> +
> +#define KASAN_ALLOCA_REDZONE_SIZE      32
> +
>  /* Don't break randconfig/all*config builds */
>  #ifndef KASAN_ABI_VERSION
>  #define KASAN_ABI_VERSION 1
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 410c8235e671..eff12e040498 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -102,6 +102,10 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)
>         case KASAN_USE_AFTER_SCOPE:
>                 bug_type = "use-after-scope";
>                 break;
> +       case KASAN_ALLOCA_LEFT:
> +       case KASAN_ALLOCA_RIGHT:
> +               bug_type = "alloca-out-of-bounds";
> +               break;
>         }
>
>         return bug_type;
> diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
> index 7c00be9216f4..b4983cf8a9d4 100644
> --- a/scripts/Makefile.kasan
> +++ b/scripts/Makefile.kasan
> @@ -32,7 +32,8 @@ else
>         $(call cc-param,asan-globals=1) \
>         $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
>         $(call cc-param,asan-stack=1) \
> -       $(call cc-param,asan-use-after-scope=1)
> +       $(call cc-param,asan-use-after-scope=1) \
> +       $(call cc-param,asan-instrument-allocas=1)
>     endif
>
>  endif


Reviewed-by: Dmitry Vyukov <dvyukov@google.com>

  reply	other threads:[~2017-12-03 12:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-01 21:36 [PATCH v3 0/5] kasan: support alloca, LLVM Paul Lawrence
2017-12-01 21:36 ` [PATCH v3 1/5] kasan: add compiler support for clang Paul Lawrence
2017-12-01 21:36 ` [PATCH v3 2/5] kasan/Makefile: Support LLVM style asan parameters Paul Lawrence
2017-12-04 16:14   ` Andrey Ryabinin
     [not found]     ` <CAL=UVf7LO5BDWVEeLXLkrLDBxwV0aO2sLv_htkpcL_Gp7sT07Q@mail.gmail.com>
2017-12-04 16:55       ` Andrey Ryabinin
2017-12-01 21:36 ` [PATCH v3 3/5] kasan: support alloca() poisoning Paul Lawrence
2017-12-03 12:23   ` Dmitry Vyukov [this message]
2017-12-04 16:42   ` Christoph Hellwig
2017-12-04 16:55     ` Andrey Ryabinin
2017-12-04 17:09       ` Andrey Ryabinin
2017-12-01 21:36 ` [PATCH v3 4/5] kasan: Add tests for alloca poisonong Paul Lawrence
2017-12-01 21:36 ` [PATCH v3 5/5] kasan: added functions for unpoisoning stack variables Paul Lawrence
2017-12-03 12:24 ` [PATCH v3 0/5] kasan: support alloca, LLVM Dmitry Vyukov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACT4Y+a-k7rc228ash9pf5UrH=uJHw9J_XyZH68BxysDdoZaww@mail.gmail.com' \
    --to=dvyukov@google.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=ghackmann@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=md@google.com \
    --cc=mka@chromium.org \
    --cc=paullawrence@google.com \
    --cc=yamada.masahiro@socionext.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).