From patchwork Fri May 25 14:40:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10427683 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 2AAB7602D8 for ; Fri, 25 May 2018 14:41:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 178D72977B for ; Fri, 25 May 2018 14:41:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B9CA29793; Fri, 25 May 2018 14:41:44 +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 62F1529792 for ; Fri, 25 May 2018 14:41:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 448B26B026E; Fri, 25 May 2018 10:41:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3FE5C6B026F; Fri, 25 May 2018 10:41:13 -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 1B0676B0270; Fri, 25 May 2018 10:41:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm0-f71.google.com (mail-wm0-f71.google.com [74.125.82.71]) by kanga.kvack.org (Postfix) with ESMTP id AE3F96B026E for ; Fri, 25 May 2018 10:41:12 -0400 (EDT) Received: by mail-wm0-f71.google.com with SMTP id n17-v6so3433371wmc.8 for ; Fri, 25 May 2018 07:41:12 -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; bh=IiJRqyVkBF0gPedQVRdh0iCDct+3V6NXiz8l5n3JXr0=; b=c5QPW/WrfV8Jf0gkrcvBgJ1bX5xcyNidEfATqeU45xtOizcmCJk9XkVcRU/undeIB6 DYMVPfTD9oG4pme/J4zm+DY/OR2KuGKgD+fXtaCgMri/luBLZcRCWzG0UpMAVN+Por1L /cEPFni/0+SBywcAwFlCYznqz5BGwMeDsgHB+mGgWhoPuQdHpLotwC5cqZExWoRDWOGZ xXhQuo10GG0FYqwSUgmTIr5kwJRFYSFVTrwYo3XYGkkDqnpy3YPkoCqkA5Qo8Xj3IuWP 7fNWRa5yAQJttuOwGkYqU/C20phI4KJjYTbsJiBsyGXGeDsXeAuXwLyP/5iQOv3tDArN N4pg== X-Gm-Message-State: ALKqPwfdpDqrORcvfIRLI0ql9Tmp3cqplH7IlL+6Ie1Thy2t1oI5jDPK P2Id6+4cUg1mCnnKuV9aB7uMM8G9j7I4zwbRRNlRUMbkA5RQ1iH9JlHAqXUztD0qcO1F3lU3qa3 yY4xh0229QzbSjYpumWvFEC4aZ+/x/Qoog5aGdSxdN3iLV+4xnnpuhyGuD9E8FdTjC3PBzlICVT O7M4WK4ck2G1VcwCn6nK543ZcZ/WQ5v5s4f+XkLjlBhoSecs5CZUlLYgotN+8hxBrq92vIioVgK wP612KIbpeO1UjkCA6olAR2jvBHbuxudLJTjajR1ny4pxKz64eydhWsR7Pdan1tfne2weuvAlsj i3rUrOvTQ0d6un0gqth6815JfvYOWakY1W71TS/9cIe4+3mj3HR1BPFNBplLcb+YH1MbQp+K/mU L X-Received: by 2002:a1c:15c3:: with SMTP id 186-v6mr2266817wmv.39.1527259272235; Fri, 25 May 2018 07:41:12 -0700 (PDT) X-Received: by 2002:a1c:15c3:: with SMTP id 186-v6mr2266771wmv.39.1527259271393; Fri, 25 May 2018 07:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527259271; cv=none; d=google.com; s=arc-20160816; b=mXFhnWcR2vhdn/IWtbeDBrxUz/qmBLw7HEFqPT+mx+WJKIXJCUNYT5LRPM34WzLgel ZNA9dG46eW/YNBtLtXzijW6Q5tHpqDGqLzfRyJ8PijaMUVc73sGLVNgg4avbc7SfN9Yy fa5YXnIU7vRMFhJdbM1WClPSG32JqV4MP0Dwvvyk0NCq1QErgbd9SZD11K8393P2kiyb fji9W1Ayouyg8f3AObyl5a8PTaCWr4TwaWcmzEyQjVzQC2uxIj/b1nuAaZUufoBuqGYw fVmB7xwA3wQkbDbNPXTbelIh6eKzyF42685Uw/L5vjfP/BP1ZkyeiwLQQlCVEoIY023u Uurg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=IiJRqyVkBF0gPedQVRdh0iCDct+3V6NXiz8l5n3JXr0=; b=atY7qsNHTH5kRXDRBUXuTP6/oTFcw5n0uBImLTKBGrGQha6H24XkTZt6FWBC8jWYOj OuoZnGQOIMBGwU9d8+J+6nTMYg6IAzLCzlwskKotTjtgV7xNkcVgMY9duYSEcBj4+q3I JTf601Hgb2Fo3LvUm3+oNfBHdM98CWLfsDgvp/zlC8QpamxjcpcI60Fbq8AN3iLpXnrN YoPlcaK9HCFsrNsSJZbyQeg+CiZxE8coRcC8tHUQTInuvdyCaWkmqCglBTnR4fxMMHuW X7UgJ+G8dUC4H20FNIKkRDh8PzAMKqlhFG2S/1wfQaq/zuS1CdTbYG8VY6hOKKrH7sYv Z5nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RwSMrhY/; 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 u126-v6sor2417945wmg.12.2018.05.25.07.41.11 for (Google Transport Security); Fri, 25 May 2018 07:41:11 -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=RwSMrhY/; 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; bh=IiJRqyVkBF0gPedQVRdh0iCDct+3V6NXiz8l5n3JXr0=; b=RwSMrhY/NR1sv0w++9A8/5TVB+IQrFoWvryDaWF+7UK04jfd3YO7AWslhar1Bv0iZd 0/D1gIM4W6mEU9xa9l+gWzKo4MhJkH0T9xgv4vWEPr3TTwPoPrF2n648s5nbtlXsNupz siyv/AGoNLhOFOOkvnf477LF/UBj/Urqj5FIduqmkGbTrEs98HXBpnH5b8CRK4cQiVXE +Y1Hlfx3ttyiTQX0rIHPGcUMuVzPvTyAv8mHABJxdJTpFaqWRl0LaYReOrg+zg41vuRm uPZnR9PC28kVsF5U5e8L99WhqwhZDm3K24XcEe+thZmN97ujQhzuqHXI06iduYVrI8cE Neig== X-Google-Smtp-Source: ADUXVKLm8NXXj7DIM0/yhc4kR0+at829+1Z/fzZhwUwcWhYcOBPp9mvPUn/gK4Po/FjHlRLNTChr4A== X-Received: by 2002:a1c:d495:: with SMTP id l143-v6mr1792973wmg.144.1527259269888; Fri, 25 May 2018 07:41:09 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id q2-v6sm25293885wrm.26.2018.05.25.07.41.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 07:41:09 -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 v2 14/16] khwasan, arm64: add brk handler for inline instrumentation Date: Fri, 25 May 2018 16:40:30 +0200 Message-Id: X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog 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 }