From patchwork Tue Nov 6 17:30:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10670977 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0B59A13BF for ; Tue, 6 Nov 2018 17:31:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E9DBF2ABF5 for ; Tue, 6 Nov 2018 17:31:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E71582ABF9; Tue, 6 Nov 2018 17:31:52 +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=unavailable 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 581DD2ABE9 for ; Tue, 6 Nov 2018 17:31:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 270906B0375; Tue, 6 Nov 2018 12:31:14 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 220D86B0377; Tue, 6 Nov 2018 12:31:14 -0500 (EST) 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 0C15B6B0378; Tue, 6 Nov 2018 12:31:14 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by kanga.kvack.org (Postfix) with ESMTP id A88326B0375 for ; Tue, 6 Nov 2018 12:31:13 -0500 (EST) Received: by mail-wm1-f71.google.com with SMTP id c13-v6so11429304wmb.8 for ; Tue, 06 Nov 2018 09:31:13 -0800 (PST) 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:mime-version :content-transfer-encoding; bh=qG7GGQswG6z6qbF8z455/sUNiXtUU10lOp1Oj7JJWlA=; b=nrE/rTWBdheGZqDS/o/eR7CuiHVuCnfJ1y5hMP6BZiaXhofdMzyoUNvVShbInLfg46 0Oc/nOkkt4WWs0e4qFzu07AhG6seuWUg8dYyydN8Ju5/Z8q+CuYarKI6/bVeE2ovm8tz 0p/iD8x69iyBWP2EdjhvIfcXJALNikPNN7uIsHRl/YdGTej43F7FTKDYMpX1x4H1u6hA xCJYR9isWxvzpzTY1aD6oTCo0e23xiyW3p6eWHRHDl8psOWoCwYTIqvhr+svtMVHrX6l 1QXdRjSE7QcoCXhuJjM/VPY2pvARJKgyCKcpvXwsXjb7orv70AiBaIW1xkL2BX8KCAMs ScnQ== X-Gm-Message-State: AGRZ1gJi0Xcs6dqybbyDFLsbPgMGWV3ta1dt1ZVOPRxBwR6TjI04wzpy kAw2clj7jnakwUxIgonIJJT9PpKoe83xydQwDC5MP8yxuzcNI5JwdX8H8tpfPh7nzzuJ84/MkFJ xwpwYHaQ7DObB2LNGd184iuDdQ0dFmKNGcAAqAsklVHFJVtywPRHN4D6Ta5MdkM3LY+jthmXFcT QE+YMeBLS4QRunICD+cHc904fQXh8hJ8M4YFKsqRxSxW7gx817KH2G24rnPlq7OyeiVitAIpIRC p5En/lPsVioEUoKNYYhX2KeGv6pAU7S/iHg93o7SOyt3lIhO90+aEp/gZq0CnfiXid3GDrhQKpi YtGj/B+XvXt4Y07Xvx63XroGkaFh+MKldtA0VyjgBb8ykeO86wjvD1R3yW9SkXHn2G7FnyvVn6f u X-Received: by 2002:a5d:680c:: with SMTP id w12-v6mr22943537wru.248.1541525473125; Tue, 06 Nov 2018 09:31:13 -0800 (PST) X-Received: by 2002:a5d:680c:: with SMTP id w12-v6mr22943457wru.248.1541525472072; Tue, 06 Nov 2018 09:31:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541525472; cv=none; d=google.com; s=arc-20160816; b=DwjW+4iw8nal57iC2712L6EKphI6EtopAZ79ZoYPoSW9nTsuuYUKxYodlQH6irn6S4 XWBgf36DvuoMv2QP7VOaivq3xppPQc5Pug9BLUXmTroWeOdiQ5RsADqGDZYrhAM+I2U8 56dn6cVtb7m3y5JTK3zWwSDGC3CnQqJPpoaD/yN+fH19xxZYcg0li7xXvBU7iVlu8SHl 37fXIuePstACV2ZualYrOvv1E5zvavVnD0Rn+AbRBQrDUsh6h5s4pGpgfOSfmSAfI2LL TTH9DGMkgalf6axGN/9QQkZkOCinUFQZQc+Bn33/ya8unBSJQzc26hD7cUDGNYOtSlPG gyOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=qG7GGQswG6z6qbF8z455/sUNiXtUU10lOp1Oj7JJWlA=; b=mKTlEBED11SOGeWVeDra1H27F3nyjMht/7OPwZum/EDxm/EyyGODHr51FWw9dkyDld t3HpN0FdIz4awrgZDHEWVMOuKZgtbhnEfOHB/imYDqWwpJWNN7XFyjYATVNKvfqgjqLO eheS+2pcdp8Bmr7DfZMirGJr7Zf9Cs5+lVw3DyCWn4yDPt7E+qPmrdEGK4ZQYot92ksM 6thnV2R/SxyZDGvtYQ8YMyVN6uR4c/mVkuO9j20NzDI+1ryDAE6s13t7m8fre1xADSPp 7j6KTFO90ZR4cxanfsnXr22wML3khpaxSUar7Vln4UtaFlaPHBXx+Y1soFtgH5sOL3wY 9CSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=usrC+MmM; 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 r23-v6sor1711074wmh.19.2018.11.06.09.31.11 for (Google Transport Security); Tue, 06 Nov 2018 09:31:12 -0800 (PST) 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=usrC+MmM; 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 :mime-version:content-transfer-encoding; bh=qG7GGQswG6z6qbF8z455/sUNiXtUU10lOp1Oj7JJWlA=; b=usrC+MmM1aDZFeALAY/qucJYeM4DBspv73RKgp/1or3/pITpA9tPm07GhuzXp4K/qJ NnundVooazUik2QJ9SkARp1FgSFa4am9Bx7wzJf5fUmyTdq83QfnHlZIk1Ck2yg1XoRt 89ehgwWlydiqqniexC0JAVha4GEz1HJ8sVb0k7qPLQWbzm/KfQhzI73ZABvu6g79Rvtb JpcFj8ttwmrcGpijLGtYLl4vOUEFHDLJxMAvCsfsXf0p8MM9X8+NDsjYzRmljOBKMbB5 BE2quiA40/7eoG51sKOUETR5fhtyj66KeEoDmG0+yxrD3y9mvPBBEyBO/dYCigFCFC/D v+Tw== X-Google-Smtp-Source: AJdET5d3fUoOAj9jJzdwBL4RII3aygkS9jrUMcIOVUis2WZl2TDUzOuau/0A2MWlKD+3Rw/caxIKfA== X-Received: by 2002:a1c:6c09:: with SMTP id h9-v6mr2715568wmc.131.1541525471472; Tue, 06 Nov 2018 09:31:11 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id z17-v6sm16328416wrm.65.2018.11.06.09.31.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 09:31:10 -0800 (PST) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , 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 , Jann Horn , Mark Brand , Chintan Pandya , Vishwath Mohan , Andrey Konovalov Subject: [PATCH v10 18/22] kasan, arm64: add brk handler for inline instrumentation Date: Tue, 6 Nov 2018 18:30:33 +0100 Message-Id: X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-goog In-Reply-To: References: MIME-Version: 1.0 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 Tag-based KASAN 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 adds a tag-based KASAN specific 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. Reviewed-by: Andrey Ryabinin Reviewed-by: Dmitry Vyukov Signed-off-by: Andrey Konovalov --- arch/arm64/include/asm/brk-imm.h | 2 + arch/arm64/kernel/traps.c | 68 +++++++++++++++++++++++++++++++- include/linux/kasan.h | 3 ++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/brk-imm.h b/arch/arm64/include/asm/brk-imm.h index ed693c5bcec0..2945fe6cd863 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: tag-based KASAN 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 KASAN_BRK_IMM 0x900 #endif diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 5f4d9acb32f5..04bdc53716ef 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -284,10 +285,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. @@ -959,7 +964,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; } @@ -969,6 +974,58 @@ static struct break_hook bug_break_hook = { .fn = bug_handler, }; +#ifdef CONFIG_KASAN_SW_TAGS + +#define KASAN_ESR_RECOVER 0x20 +#define KASAN_ESR_WRITE 0x10 +#define KASAN_ESR_SIZE_MASK 0x0f +#define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK)) + +static int kasan_handler(struct pt_regs *regs, unsigned int esr) +{ + bool recover = esr & KASAN_ESR_RECOVER; + bool write = esr & KASAN_ESR_WRITE; + size_t size = KASAN_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. KASAN reporting is disabled in some contexts (for example when + * the allocator accesses slab object metadata; 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 - KASAN", 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 KASAN_ESR_VAL (0xf2000000 | KASAN_BRK_IMM) +#define KASAN_ESR_MASK 0xffffff00 + +static struct break_hook kasan_break_hook = { + .esr_val = KASAN_ESR_VAL, + .esr_mask = KASAN_ESR_MASK, + .fn = kasan_handler, +}; +#endif + /* * Initial handler for AArch64 BRK exceptions * This handler only used until debug_traps_init(). @@ -976,6 +1033,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_SW_TAGS + if ((esr & KASAN_ESR_MASK) == KASAN_ESR_VAL) + return kasan_handler(regs, esr) != DBG_HOOK_HANDLED; +#endif return bug_handler(regs, esr) != DBG_HOOK_HANDLED; } @@ -983,4 +1044,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_SW_TAGS + register_break_hook(&kasan_break_hook); +#endif } diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 4c9d6f9029f2..d5a2a7f1f72c 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -173,6 +173,9 @@ void kasan_init_tags(void); void *kasan_reset_tag(const void *addr); +void kasan_report(unsigned long addr, size_t size, + bool is_write, unsigned long ip); + #else /* CONFIG_KASAN_SW_TAGS */ static inline void kasan_init_tags(void) { }