linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: Andrey Konovalov <andreyknvl@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Alexander Potapenko <glider@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>, Theodore Ts'o <tytso@mit.edu>,
	Jan Kara <jack@suse.com>, Christopher Li <sparse@chrisli.org>,
	Christoph Lameter <cl@linux.com>,
	Pekka Enberg <penberg@kernel.org>,
	David Rientjes <rientjes@google.com>,
	Joonsoo Kim <iamjoonsoo.kim@lge.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Michal Marek <michal.lkml@markovi.net>,
	Yury Norov <ynorov@caviumnetworks.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Marc Zyngier <marc.zyngier@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Kristina Martsenko <kristina.martsenko@arm.com>,
	Punit Agrawal <punit.agrawal@arm.com>,
	Dave Martin <Dave.Martin@arm.com>,
	James Morse <james.morse@arm.com>,
	Julien Thierry <julien.thierry@arm.com>,
	Michael Weiser <michael.weiser@gmx.de>,
	Steve Capper <steve.capper@arm.com>,
	Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Sandipan Das <sandipan@linux.vnet.ibm.com>,
	Paul Lawrence <paullawrence@google.com>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Kees Cook <keescook@chromium.org>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Arnd Bergmann <arnd@arndb.de>,
	kasan-dev <kasan-dev@googlegroups.com>,
	Linux Doc Mailing List <linux-doc@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Linux ARM <linux-arm-kernel@lists.infradead.org>,
	linux-ext4@vger.kernel.org,
	Linux-Sparse <linux-sparse@vger.kernel.org>,
	Linux Memory Management List <linux-mm@kvack.org>,
	Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>,
	Kostya Serebryany <kcc@google.com>,
	Evgeniy Stepanov <eugenis@google.com>,
	Lee Smith <Lee.Smith@arm.com>,
	Ramana Radhakrishnan <Ramana.Radhakrishnan@arm.com>,
	Jacob Bramley <Jacob.Bramley@arm.com>,
	Ruben Ayrapetyan <Ruben.Ayrapetyan@arm.com>,
	Kees Cook <keescook@google.com>, Jann Horn <jannh@google.com>,
	Mark Brand <markbrand@google.com>
Subject: Re: [RFC PATCH 11/14] khwasan: add brk handler for inline instrumentation
Date: Sat, 24 Mar 2018 03:42:16 +0000	[thread overview]
Message-ID: <CAKv+Gu8C3z_UoPxN92JjFOUCQy+SAKT3_m189g5RyQteM_686w@mail.gmail.com> (raw)
In-Reply-To: <CAAeHK+zHfgSfZtKhOnfFVa35uB=PSsPiN65BDd9RVNK63f_G0w@mail.gmail.com>

On 23 March 2018 at 15:59, Andrey Konovalov <andreyknvl@google.com> wrote:
> On Mon, Mar 5, 2018 at 3:51 PM, Mark Rutland <mark.rutland@arm.com> wrote:
>> On Fri, Mar 02, 2018 at 08:44:30PM +0100, Andrey Konovalov wrote:
>>> KHWASAN inline instrumentation mode (which embeds checks of shadow memory
>>> into the generated code, instead of inserting a callback) generates a brk
>>> instruction when a tag mismatch is detected.
>>
>> The compiler generates the BRK?
>
> Correct.
>
>>
>> I'm a little worried about the ABI implications of that. So far we've
>> assumed that for hte kernel-side, the BRK space is completely under our
>> control.
>>

GCC already generates traps (translating to BRKs in the arm64 world)
for other things like integer divide by zero and NULL dereferences.
(Arnd may know more, I know he has looked into this in the past.) So
we should probably implement a BRK handler for compiler generated
traps and reserve it in the brk space, given that this behavior is not
specific to khwasan

>> How much does this save, compared to having a callback?
>
> Around 7% of code size is what I see (you can have the same single
> instruction for a call, but it may cost some register allocation
> troubles).
>
>>
>>> This commit add a KHWASAN brk handler, that decodes the immediate value
>>> passed to the brk instructions (to extract information about the memory
>>> access that triggered the mismatch), reads the register values (x0 contains
>>> the guilty address) and reports the bug.
>>> ---
>>>  arch/arm64/include/asm/brk-imm.h |  2 ++
>>>  arch/arm64/kernel/traps.c        | 40 ++++++++++++++++++++++++++++++++
>>>  2 files changed, 42 insertions(+)
>>>
>>> diff --git a/arch/arm64/include/asm/brk-imm.h b/arch/arm64/include/asm/brk-imm.h
>>> index ed693c5bcec0..e4a7013321dc 100644
>>> --- a/arch/arm64/include/asm/brk-imm.h
>>> +++ b/arch/arm64/include/asm/brk-imm.h
>>> @@ -16,10 +16,12 @@
>>>   * 0x400: for dynamic BRK instruction
>>>   * 0x401: for compile time BRK instruction
>>>   * 0x800: kernel-mode BUG() and WARN() traps
>>> + * 0x9xx: KHWASAN trap (allowed values 0x900 - 0x9ff)
>>>   */
>>>  #define FAULT_BRK_IMM                        0x100
>>>  #define KGDB_DYN_DBG_BRK_IMM         0x400
>>>  #define KGDB_COMPILED_DBG_BRK_IMM    0x401
>>>  #define BUG_BRK_IMM                  0x800
>>> +#define KHWASAN_BRK_IMM                      0x900
>>>
>>>  #endif
>>> diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c
>>> index eb2d15147e8d..5df8cdf5af13 100644
>>> --- a/arch/arm64/kernel/traps.c
>>> +++ b/arch/arm64/kernel/traps.c
>>> @@ -35,6 +35,7 @@
>>>  #include <linux/sizes.h>
>>>  #include <linux/syscalls.h>
>>>  #include <linux/mm_types.h>
>>> +#include <linux/kasan.h>
>>>
>>>  #include <asm/atomic.h>
>>>  #include <asm/bug.h>
>>> @@ -771,6 +772,38 @@ static struct break_hook bug_break_hook = {
>>>       .fn = bug_handler,
>>>  };
>>>
>>> +#ifdef CONFIG_KASAN_TAGS
>>> +static int khwasan_handler(struct pt_regs *regs, unsigned int esr)
>>> +{
>>> +     bool recover = esr & 0x20;
>>> +     bool write = esr & 0x10;
>>
>> Can you please add mnemonics for these, e.g.
>>
>> #define KHWASAN_ESR_RECOVER             0x20
>> #define KHWASAN_ESR_WRITE               0x10
>>
>>> +     size_t size = 1 << (esr & 0xf);
>>
>> #define KHWASAN_ESR_SIZE_MASK           0x4
>> #define KHWASAN_ESR_SIZE(esr)   (1 << (esr) & KHWASAN_ESR_SIZE_MASK)
>
> Will do!
>
>>
>>> +     u64 addr = regs->regs[0];
>>> +     u64 pc = regs->pc;
>>> +
>>> +     if (user_mode(regs))
>>> +             return DBG_HOOK_ERROR;
>>> +
>>> +     khwasan_report(addr, size, write, pc);
>>> +
>>> +     if (!recover)
>>> +             die("Oops - KHWASAN", regs, 0);
>>
>> Could you elaborate on what "recover" means, and why it's up the the
>> compiler to decide if the kernel should die()?
>
> The instrumentation allows to control whether we can proceed after a
> crash was detected. This is done by passing the -recover flag to the
> compiler. Disabling recovery allows to generate more compact code.
>
> Unfortunately disabling recovery doesn't work for the kernel right
> now. KHWASAN reporting is disabled in some contexts (for example when
> the allocator accesses slab object metadata; same is true for KASAN;
> this is controlled by current->kasan_depth). All these accesses are
> detected by the tool, even though the reports for them are not
> printed.
>
> This is something that might be fixed at some point in the future, so
> I think it makes sense to leave this check as is.
>
> I'll add a comment with explanations though.
>
>>
>>> +
>>> +     /* If thread survives, skip over the BUG instruction and continue: */
>>> +     arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
>>
>> This is for fast-forwarding user instruction streams, and isn't correct
>> to call for kernel faults (as it'll mess up the userspace single step
>> logic).
>
> I saw BUG handler using this (which also inserts a brk), so I used it
> as well. What should I do instead to jump over the faulting brk
> instruction?
>
> Thanks!
>
>>
>> Thanks,
>> Mark.

  reply	other threads:[~2018-03-24  3:42 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-02 19:44 [RFC PATCH 00/14] khwasan: kernel hardware assisted address sanitizer Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 01/14] khwasan: change kasan hooks signatures Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 02/14] khwasan: move common kasan and khwasan code to common.c Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 03/14] khwasan: add CONFIG_KASAN_CLASSIC and CONFIG_KASAN_TAGS Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 04/14] khwasan: adjust shadow size for CONFIG_KASAN_TAGS Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 05/14] khwasan: initialize shadow to 0xff Andrey Konovalov
2018-03-02 21:55   ` Evgenii Stepanov
2018-03-02 19:44 ` [RFC PATCH 06/14] khwasan: enable top byte ignore for the kernel Andrey Konovalov
2018-03-05 14:29   ` Mark Rutland
2018-03-09 18:15     ` Andrey Konovalov
2018-03-05 14:36   ` Mark Rutland
2018-03-06 14:24     ` Marc Zyngier
2018-03-09 18:21       ` Andrey Konovalov
2018-03-09 18:32         ` Marc Zyngier
2018-03-09 18:42           ` Andrey Konovalov
2018-03-09 19:06             ` Marc Zyngier
2018-03-09 19:16               ` Mark Rutland
2018-03-09 19:14             ` Mark Rutland
2018-03-09 18:17     ` Andrey Konovalov
2018-03-09 18:59       ` Mark Rutland
2018-03-02 19:44 ` [RFC PATCH 07/14] khwasan: add tag related helper functions Andrey Konovalov
2018-03-05 14:32   ` Mark Rutland
2018-03-06 18:31     ` Andrey Konovalov
2018-03-07 18:16       ` Christopher Lameter
2018-03-08  9:09         ` Dmitry Vyukov
2018-03-08 11:20       ` Mark Rutland
2018-03-02 19:44 ` [RFC PATCH 08/14] khwasan: perform untagged pointers comparison in krealloc Andrey Konovalov
2018-03-05 14:39   ` Mark Rutland
2018-03-06 18:33     ` Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 09/14] khwasan: add hooks implementation Andrey Konovalov
2018-03-05 14:44   ` Mark Rutland
2018-03-06 18:38     ` Andrey Konovalov
2018-03-08 11:25       ` Mark Rutland
2018-03-09 18:10         ` Andrey Konovalov
2018-03-13 15:05   ` Alexander Potapenko
2018-03-13 17:00     ` Andrey Konovalov
2018-03-15 16:52       ` Andrey Ryabinin
2018-03-16 18:09         ` Andrey Konovalov
2018-03-16 18:16           ` Evgenii Stepanov
2018-03-16 18:24             ` Andrey Konovalov
2018-03-16 18:45               ` Evgenii Stepanov
2018-03-16 19:06                 ` Andrey Konovalov
2018-03-16 20:21                   ` Evgenii Stepanov
2018-03-20  0:44   ` Anthony Yznaga
2018-03-20 13:43     ` Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 10/14] khwasan: add bug reporting routines Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 11/14] khwasan: add brk handler for inline instrumentation Andrey Konovalov
2018-03-05 14:51   ` Mark Rutland
2018-03-23 15:59     ` Andrey Konovalov
2018-03-24  3:42       ` Ard Biesheuvel [this message]
2018-03-26  9:36       ` Mark Rutland
2018-03-27 13:03         ` Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 12/14] khwasan, jbd2: add khwasan annotations Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 13/14] khwasan: update kasan documentation Andrey Konovalov
2018-03-02 19:44 ` [RFC PATCH 14/14] khwasan: default the instrumentation mode to inline Andrey Konovalov
2018-03-05 14:54   ` Mark Rutland
2018-03-09 18:06     ` Andrey Konovalov
2018-03-09 19:18       ` Mark Rutland
2018-03-12 13:10         ` Andrey Konovalov
2018-03-13 14:44   ` Alexander Potapenko
2018-03-13 16:49     ` Andrey Konovalov
2018-03-04  9:16 ` [RFC PATCH 00/14] khwasan: kernel hardware assisted address sanitizer Geert Uytterhoeven
2018-03-04 11:44   ` Ingo Molnar
2018-03-04 15:49     ` Geert Uytterhoeven
2018-03-06 18:21       ` Andrey Konovalov

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=CAKv+Gu8C3z_UoPxN92JjFOUCQy+SAKT3_m189g5RyQteM_686w@mail.gmail.com \
    --to=ard.biesheuvel@linaro.org \
    --cc=Dave.Martin@arm.com \
    --cc=Jacob.Bramley@arm.com \
    --cc=Lee.Smith@arm.com \
    --cc=Ramana.Radhakrishnan@arm.com \
    --cc=Ruben.Ayrapetyan@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=arnd@arndb.de \
    --cc=aryabinin@virtuozzo.com \
    --cc=catalin.marinas@arm.com \
    --cc=cl@linux.com \
    --cc=corbet@lwn.net \
    --cc=dvyukov@google.com \
    --cc=dwmw@amazon.co.uk \
    --cc=eugenis@google.com \
    --cc=geert@linux-m68k.org \
    --cc=glider@google.com \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=jack@suse.com \
    --cc=james.morse@arm.com \
    --cc=jannh@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=julien.thierry@arm.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kcc@google.com \
    --cc=keescook@chromium.org \
    --cc=keescook@google.com \
    --cc=kristina.martsenko@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=marc.zyngier@arm.com \
    --cc=mark.rutland@arm.com \
    --cc=markbrand@google.com \
    --cc=michael.weiser@gmx.de \
    --cc=michal.lkml@markovi.net \
    --cc=mingo@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=paullawrence@google.com \
    --cc=penberg@kernel.org \
    --cc=punit.agrawal@arm.com \
    --cc=rientjes@google.com \
    --cc=sandipan@linux.vnet.ibm.com \
    --cc=sparse@chrisli.org \
    --cc=steve.capper@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=tglx@linutronix.de \
    --cc=tytso@mit.edu \
    --cc=will.deacon@arm.com \
    --cc=yamada.masahiro@socionext.com \
    --cc=ynorov@caviumnetworks.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