Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp466544imm; Wed, 29 Aug 2018 04:37:51 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdaupk90xH9ApqdUUj5CK3J/Oxpx3/OdOAJA5pEzSq+GI82+oOwLsFegEiVhyDcExdebL3Sc X-Received: by 2002:a17:902:9045:: with SMTP id w5-v6mr5669482plz.10.1535542670961; Wed, 29 Aug 2018 04:37:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535542670; cv=none; d=google.com; s=arc-20160816; b=R8clYWQ6MyzisXexxXpp0pj/4QmlJl4I0obDK3b3uiSZbSN77E6OyT/XAtcqhioayW 99fabm8G8qzG9U18TJ79g2e6L2/xjCLRYmREq0n/ZN3blpYna1bd9yAkeBJbnrzr+q87 uYFz89u8K41088QFvI7AnpcslWcrN4BtUVmYqsDlJHle3FvqfF7zY/NPtf4C6QfIK69r Q5tyLTeKhLZaGjJu1HFt/Al+3npRW07ChDYits55Jt5EO0fYqjPAqBB7//fNEHgaPlC4 XHrJNMwVnT5/VRCe1bQRMgJSHGDoA6ooE6xpFD0QBtKw4iga0yHweU8KrPxy0lHn2cgn 1glQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=CCBGVbMzHdvCWnsB/Yhk2lUOwxfWB4ZppCmhRRUM9JA=; b=o6P9+YKjzrZtLZRxbUBlKqXJ+fg3aP1h/MAtYNPQZAjWdLJWoIyAAQCKy3TVZSfkb8 eM037d0fjYD0D772dNGsfQWCdqx8OUHLgA2bSKZbCFZB6ThlISkybklfTDnLKL8LTryU dYDWLlL0I8s8JrTODUO8Yf5ZvJ2nc0q4CoZFprtYAcUju3zZGjaLxRt8GdG7rplrVZv+ 0oHQ1TKlIFmC+5yepU9cGqZNLgK3+bXUXFZ8qLjj0NZkOOPPZyUPWUalNLRjfDvj3JAk zTP4JO3IYoiRnCHtz4TGAzQj67SFDIdozdMzYrFmN4LBAlWeEQv2BbW9FQznATY41uV1 mbDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=P+4HLBVN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j15-v6si3491962pgi.478.2018.08.29.04.37.35; Wed, 29 Aug 2018 04:37:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=P+4HLBVN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728690AbeH2PcY (ORCPT + 99 others); Wed, 29 Aug 2018 11:32:24 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:38463 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728655AbeH2PcX (ORCPT ); Wed, 29 Aug 2018 11:32:23 -0400 Received: by mail-wr1-f68.google.com with SMTP id w11-v6so4487420wrc.5 for ; Wed, 29 Aug 2018 04:35:53 -0700 (PDT) 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=CCBGVbMzHdvCWnsB/Yhk2lUOwxfWB4ZppCmhRRUM9JA=; b=P+4HLBVNOxPKN9sWnmTedOcFE/v3k7mqmCD4VSAgFSMl7of3OTjZlSlH3AnkitKzKX JAU41HXyYVWcUHQAIswpIi1IcmwpgAEdWg2AJBHPz3piJeWXckqIhM5+FxSDEMK4gYbP Jqhj6ue6k2WMJZrncYGU96Fx+RXGfhyWuPXz0Oe8l7GHkmQnmvh5ZRx9KTsdzO65tUQg bqgezWXIEtPPcAp9SKFZNI8q8MTba6UZXLfbPIDXEr23IYXon4oTWUDX/O78Yt1INXVl fKdJjPeFsXUUxxufErLlE5iMPDNfHZtEA3hpjNEMu12w1uep9wsis+IuSqkZ7PZamP9M J8PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CCBGVbMzHdvCWnsB/Yhk2lUOwxfWB4ZppCmhRRUM9JA=; b=KYz2w89GBxHNc4knbgEHslWlA7ERmC3JPzssFRf236JB1eV2XoZAWs+Hkuj+ZSvvZH 4zE10eQaA6E4lsFSB1OLjcx/IOzOX9BJr9/qHCjwrQncy9oqNmtmusDL1Aeg44lkVBLM psnXuLHbHFKrJM/o9tdum91m0SOQW2SDvLnuGV32LHG5epfGzvxlAYYQw5/4RmIkinQ3 xdwnyPygcnV8CvGemBuBUKJ0cqUYn9B6Wuda8LZhWp/I2GvFwK0gX8i7kWqLVvRg4UkH gDotRnffDeR6XjLk0oGZ2yyzhyutlKA9z5xguf8EMuZ6O/t6e3rhnabV0sbxb7vxXaFW FRyg== X-Gm-Message-State: APzg51CoHU4fpMAzg9yYLtQZffM0Xu3poAjZP3hps6kx/sbBdUSBsDax rlvZvW6qXUq4/TEj8LCbZZ0bwg== X-Received: by 2002:a5d:4648:: with SMTP id j8-v6mr3974471wrs.52.1535542552181; Wed, 29 Aug 2018 04:35:52 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id s10-v6sm7800454wmd.22.2018.08.29.04.35.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 04:35:51 -0700 (PDT) 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 v6 15/18] khwasan, arm64: add brk handler for inline instrumentation Date: Wed, 29 Aug 2018 13:35:19 +0200 Message-Id: X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 039e9ff379cc..fd70347d1ce7 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. @@ -775,7 +780,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; } @@ -785,6 +790,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(). @@ -792,6 +850,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; } @@ -799,4 +861,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 } -- 2.19.0.rc0.228.g281dcd1b4d0-goog