From patchwork Tue May 8 17:21:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10386639 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 723796053E for ; Tue, 8 May 2018 17:22:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 60115290B3 for ; Tue, 8 May 2018 17:22:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53E7A290C2; Tue, 8 May 2018 17:22:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A5F96290B9 for ; Tue, 8 May 2018 17:22:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64D4E6B02D5; Tue, 8 May 2018 13:21:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5AB416B02D7; Tue, 8 May 2018 13:21:43 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D43B6B02D8; Tue, 8 May 2018 13:21:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f198.google.com (mail-wr0-f198.google.com [209.85.128.198]) by kanga.kvack.org (Postfix) with ESMTP id D2E366B02D5 for ; Tue, 8 May 2018 13:21:42 -0400 (EDT) Received: by mail-wr0-f198.google.com with SMTP id c56-v6so22218997wrc.5 for ; Tue, 08 May 2018 10:21:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:in-reply-to:references; bh=vS/KcpwGXBxG8BJLX2GFFP0lc8l1Zpo/Yq8CpDQW390=; b=n9Ye2hXspu1FykzyyOI4ACNJEdv0apzUCyHS6IcBZEYkclO388GudlxyjaGJIqbw0U 84MiZj2Vdfwx4Yw+unBQfaVeQVztuiLmwQt0kGrxnPAWccJ+K9IsNgU+covcAUQ+vivJ gvwI6OfH+6CdeBewDJPGTmYld4wpLn+TI/somjz2uWj+LoYNTBkczL5iCQEJr92M5b63 AeCUgWCfPT3sYbz8i63/+PNWAEnWEctQlgd+6rKadIiyOoPcp2rr+jUHoAZkluLif+tU i5gaJdPQ7aM7oM0LY1UNkzM/TzqXMOgYDhLdak9aSOpCmXQu0pqBhpe16tEGzxmiWuvB 4kbg== X-Gm-Message-State: ALQs6tC539JPp21IxmjViQgPoHhs2gnboH9ntriEDxzThztgr8atjIQ2 cc9BhtRcK6QXGcZ/pZ4LS/kmY3tCwxL6uMWYOzQnj7eDRFVXXhfUHBlM26UJJ1tnNQ6W52bQb4/ hMJIfPaI9s9ozW056R3+uN8ghRxb1fV7ZMcj/RxaYROvNYhbokPB/nJZ5yHsQbgYOVED6s1Z4aE PoP1i0wIgCM1AyEimObRJQjahBPEeyH0RcFKLqNa1BVWxjn/8mIo4GYgwhiMg5B/EY7ZPmUbLge bTg6bkW6FYImVa6X3HZ68h6gpaG5WU2ovclzL8B/oepuxmDrAzbw168wFEpaZjchzVgo+O3xMi7 mSKMbVPiUHk+4WZEixzrzncpynrdIahVFajWOkC2Clyx/qfz1TPx+Z620dJpETwlmGIcpltUEvc O X-Received: by 2002:adf:b065:: with SMTP id g34-v6mr34162549wra.25.1525800102415; Tue, 08 May 2018 10:21:42 -0700 (PDT) X-Received: by 2002:adf:b065:: with SMTP id g34-v6mr34162491wra.25.1525800101542; Tue, 08 May 2018 10:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525800101; cv=none; d=google.com; s=arc-20160816; b=J9wTm0FHtIvqfH5x1cB9RL7IwDuTaeLGD74oJEFUi9z4waCjHjt93x0+yxtzZwthVV ZXHTZHE2cF4J9OrOonQB2BjghA4eiH3tR7soCsuGXZSueEtAoznJcjKS21D/N7uMCOWv bfe9aASYRX87qP5c8B0XjcDUTvNSdghznkch9XIgNGHgTL7iP4lwZRDV7uXvUjiiM3el WworzYdG/9gh7VPWgemmET2nQV7bp86IU6E6LULNjAi7Z4WoMbUTaUVAgX5KvMN82PZ2 RRg8n9F0SivhkVqDBY+jMM1uQj1P4xP8nXY9g/Zln//8rtHv5BUSK7caJaKmYCYN/Bgx fSnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=vS/KcpwGXBxG8BJLX2GFFP0lc8l1Zpo/Yq8CpDQW390=; b=QYimWUYo+C8Bv6SfLHbDaNdHq39CnWEqWMvvZ2h+RAv9M+3BmTLcDbWaSbkO9oS2rt +9nxnr2xmx2av7E1VfTA3J3mDdDj/s+G2wVzf81JskbeNn/5IcecfDpE2TpREPSswxLt KM7jmKa71t0PNWyq4Wp2llCTYhroNKwf2gOuwctkAtURoaBg1cPoJiHkn8JemfpRlyQl PFLdDZvH7bWxeAW8Tc0tsOgxn531Irni6NvTLvjyUfO7Wtp8stAQnFLz7TowCZ19+cfn l2AMD1tFCPAEHFEBr4KDuFMecNVSn3RtisRac4rPGymgu4bUC2LEjO3EYqtsctIk/8yY v+MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ul6S41tq; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id p15-v6sor11641192wrh.25.2018.05.08.10.21.41 for (Google Transport Security); Tue, 08 May 2018 10:21:41 -0700 (PDT) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ul6S41tq; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=vS/KcpwGXBxG8BJLX2GFFP0lc8l1Zpo/Yq8CpDQW390=; b=ul6S41tqBrZjpbzPyxRAtYldoguLA17jqQf7zzdNeZjrWMX1SFwCvFlbmPE3KigU18 i5LCw1ML8pVy48d3WiD3HVIlV8eis+C72+M4aZ8Txv+bkulCl5wo2UDMv4blUPijO/w2 bTYuS79LYtjd1msP2IPy/9MxMbszIufM8qo6QW3JySuo+uHVAUGyB63C7pEUyyPDKUNS D+9aVbCAZfJ6Dkat+juSqWbBqAZm5hwbFqigFe7dAPnWUubSbszC6E6QnBHpL7qBbO5K Ywku7D0hXoD6Xg351KjHfCg/c1htU4K86QvojuW+ocbjrDOMeXWQtiu6me+QQsVs3tob JDOg== X-Google-Smtp-Source: AB8JxZpKN6RBJ8oAtKBSfCe/3cQvTikZbk+KQJzz8zRceOVeNe6OFwmu1XkjceChXUjz9vZWBj1tHQ== X-Received: by 2002:adf:ce03:: with SMTP id p3-v6mr32868868wrn.110.1525800100674; Tue, 08 May 2018 10:21:40 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id m134sm14178311wmg.4.2018.05.08.10.21.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 10:21:40 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Jonathan Corbet , Catalin Marinas , Will Deacon , Christopher Li , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Michal Marek , Andrey Konovalov , Mark Rutland , Nick Desaulniers , Yury Norov , Marc Zyngier , Kristina Martsenko , Suzuki K Poulose , Punit Agrawal , Dave Martin , Ard Biesheuvel , James Morse , Michael Weiser , Julien Thierry , Tyler Baicar , "Eric W . Biederman" , Thomas Gleixner , Ingo Molnar , Kees Cook , Sandipan Das , David Woodhouse , Paul Lawrence , Herbert Xu , Josh Poimboeuf , Geert Uytterhoeven , Tom Lendacky , Arnd Bergmann , Dan Williams , Michal Hocko , Jan Kara , Ross Zwisler , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Matthew Wilcox , "Kirill A . Shutemov" , Souptick Joarder , Hugh Dickins , Davidlohr Bueso , Greg Kroah-Hartman , Philippe Ombredanne , Kate Stewart , Laura Abbott , Boris Brezillon , Vlastimil Babka , Pintu Agarwal , Doug Berger , Anshuman Khandual , Mike Rapoport , Mel Gorman , Pavel Tatashin , Tetsuo Handa , kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sparse@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Kees Cook , Jann Horn , Mark Brand , Chintan Pandya Subject: [PATCH v1 14/16] khwasan, arm64: add brk handler for inline instrumentation Date: Tue, 8 May 2018 19:21:00 +0200 Message-Id: <1259ed7ecd6e47925184964db90139bc9ef29426.1525798754.git.andreyknvl@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: References: In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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. 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. Signed-off-by: Andrey Konovalov --- arch/arm64/include/asm/brk-imm.h | 2 + arch/arm64/kernel/traps.c | 69 +++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) 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 8bbdc17e49df..2234e3a4cc35 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -269,10 +270,14 @@ void arm64_notify_die(const char *str, struct pt_regs *regs, } } -void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size) +void __arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size) { regs->pc += size; +} +void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size) +{ + __arm64_skip_faulting_instruction(regs, size); /* * If we were single stepping, we want to get the step exception after * we return from the trap. @@ -790,7 +795,7 @@ static int bug_handler(struct pt_regs *regs, unsigned int esr) } /* If thread survives, skip over the BUG instruction and continue: */ - arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); + __arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); return DBG_HOOK_HANDLED; } @@ -800,6 +805,59 @@ static struct break_hook bug_break_hook = { .fn = bug_handler, }; +#ifdef CONFIG_KASAN_HW + +#define KHWASAN_ESR_RECOVER 0x20 +#define KHWASAN_ESR_WRITE 0x10 +#define KHWASAN_ESR_SIZE_MASK 0x0f +#define KHWASAN_ESR_SIZE(esr) (1 << ((esr) & KHWASAN_ESR_SIZE_MASK)) + +static int khwasan_handler(struct pt_regs *regs, unsigned int esr) +{ + bool recover = esr & KHWASAN_ESR_RECOVER; + bool write = esr & KHWASAN_ESR_WRITE; + size_t size = KHWASAN_ESR_SIZE(esr); + u64 addr = regs->regs[0]; + u64 pc = regs->pc; + + if (user_mode(regs)) + return DBG_HOOK_ERROR; + + kasan_report(addr, size, write, pc); + + /* + * 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. + */ + if (!recover) + die("Oops - KHWASAN", regs, 0); + + /* If thread survives, skip over the brk instruction and continue: */ + __arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); + return DBG_HOOK_HANDLED; +} + +#define KHWASAN_ESR_VAL (0xf2000000 | KHWASAN_BRK_IMM) +#define KHWASAN_ESR_MASK 0xffffff00 + +static struct break_hook khwasan_break_hook = { + .esr_val = KHWASAN_ESR_VAL, + .esr_mask = KHWASAN_ESR_MASK, + .fn = khwasan_handler, +}; +#endif + /* * Initial handler for AArch64 BRK exceptions * This handler only used until debug_traps_init(). @@ -807,6 +865,10 @@ static struct break_hook bug_break_hook = { int __init early_brk64(unsigned long addr, unsigned int esr, struct pt_regs *regs) { +#ifdef CONFIG_KASAN_HW + if ((esr & KHWASAN_ESR_MASK) == KHWASAN_ESR_VAL) + return khwasan_handler(regs, esr) != DBG_HOOK_HANDLED; +#endif return bug_handler(regs, esr) != DBG_HOOK_HANDLED; } @@ -814,4 +876,7 @@ int __init early_brk64(unsigned long addr, unsigned int esr, void __init trap_init(void) { register_break_hook(&bug_break_hook); +#ifdef CONFIG_KASAN_HW + register_break_hook(&khwasan_break_hook); +#endif }