Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp55356imu; Tue, 27 Nov 2018 08:58:41 -0800 (PST) X-Google-Smtp-Source: AFSGD/UwpfOcJRIdfINuET/OdaxcaIoXCt6fWEiXPQxsqhFybhgVHY5Jo4wOf0Lz5yIf1UIpmk+V X-Received: by 2002:a62:1e87:: with SMTP id e129mr20677889pfe.221.1543337921169; Tue, 27 Nov 2018 08:58:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543337921; cv=none; d=google.com; s=arc-20160816; b=oIHjn06SREKMK9P6R20ToXQ6qHmmFyFr5xFJBTkXoUstJ8qeJ1usBUz2hXu282aIwe PpH4PC8L0d+uAZGrFVuwY7oZ3XaaPczSnMRLnGWVJfD4cQWLuKcFyl9KzU1jGD4iHte8 kAS6agQwKB7R9s0+tdgVW0WkWk++IK3rz92NGh0Nu+hgzm+SWhJEXGLvOe/vcpIPDrup FFVDvPDu6Y8L3xhMvpUE0H6Hg5+nUP1ViBexRvJHQqx6M8zfhcUddVLDUZ0zK8SOoWdi kR4GyQGko2XA7Ehp3OfOFSRN1I7CVauHHF2zcfetJ0DDdHCY1Xw6hx7y+ORY6rDqukdI mwXA== 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; bh=zstYfFUM8JCpnxw1Tbdi2+5o2Ofx4Ch1nUlOE/Rv2dg=; b=whZy4gaQmik/R45haqHeFKSxOGB8W4OkT5JyM1ftnx/XSqXjRBYqQw8qjNELb+dr3k LOt0srt4K6QQfuJTEZoaNMX5HeP5pVpzQh+15bDNwuPtv884w9BJAeQryBq00RN1pBsq Y4fjOPv7BCEu0Urawp06J1bMpKjjnLHj5bd5ZtUWvOCUUEVE8gThwtqUwLQxRPLBzH5Y P+cD4EUTqAMlXmthCMEL3VRdUbz/Y5WHANFcOoPOF9vyuDQOvLTqP4+LMAZP+dgkaq0R Fs1NQau2EjulchiGYDyHVKPeCeoy9vG/efaSgscWWrEUPIBlYEyxQuoJRAINED8ekDHa 3oeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aR4pRpym; 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 c5si4090742pgq.434.2018.11.27.08.58.25; Tue, 27 Nov 2018 08:58:41 -0800 (PST) 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=aR4pRpym; 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 S1731896AbeK1Dzj (ORCPT + 99 others); Tue, 27 Nov 2018 22:55:39 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:42612 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731784AbeK1Dy7 (ORCPT ); Tue, 27 Nov 2018 22:54:59 -0500 Received: by mail-wr1-f68.google.com with SMTP id q18so23437657wrx.9 for ; Tue, 27 Nov 2018 08:56:26 -0800 (PST) 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=zstYfFUM8JCpnxw1Tbdi2+5o2Ofx4Ch1nUlOE/Rv2dg=; b=aR4pRpymE+CP9XTk9XMcEkb4x8W0Sjx8bq24P8tbJj9npfknYJBXYh+ERBr61KUf75 Q/qLV5uDTb/q0AezDQJJHyH796cuqqbtPY5/dAwSDXQ35uaHW0DXZs2NHdyHYnBDdIJA NLwl1lcyglLSnayHUj/Nt/+G5ly+BlUblicnl2Ho9dyrLl1lwN+9IpEvq1qxG1RXo3+z 6QMNRROEeuEkhzU+Pbp5vhT9RM5U4Gr2rgfpLUZh+e5nckbcsgQaWeLJgp6ftpu8lgTR DVR/JZKcQkEp6rOpkks2FA/pOkD3p1lnaiG4hZ+9mKcowCDGVf7hyv7pQo1Gx0RXnlLt MiuA== 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=zstYfFUM8JCpnxw1Tbdi2+5o2Ofx4Ch1nUlOE/Rv2dg=; b=bxVsyva1CZKC7vLNLxhOnvzHsnMFV27BPZOAWSJW0jUwrkZWs8rWpbB3lCVQMsxqnZ AnpXQ1A1/0eQd7+KUFlq3mtTyXWBm1BYH2hkVSGtjbWU0OSnn9BguriMnprkV4EtZaT8 3nJvaspTpXIFZKxlp3fafn8zoWqqG7XVX2Skk2fSI6iVI3P30Wz94LWWlGVvvL/e8uJ7 dgCP403/Vg7sTDrH6P9DqmqXHiJij3b0nd98SxpNVtk5z1uLOirBB4WKnKjZ/c6eJMpY jLPgb2AnMk7MyzQvC2Oq9HftLUWOWA0TwECHcGGGLeecqWsSy4wZmz3COwyOjjimVewZ VKNw== X-Gm-Message-State: AA+aEWYcR5l4zatYOxYXW+FccQIMa0IuWT7M2dAWa6Tn5gbEHC70nFUl RyyrWbCZGFsXR4okpokoiM4rsA== X-Received: by 2002:a05:6000:1287:: with SMTP id f7mr29808746wrx.302.1543337785909; Tue, 27 Nov 2018 08:56:25 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:3180:41f8:3010:ff61]) by smtp.gmail.com with ESMTPSA id k73sm6383099wmd.36.2018.11.27.08.56.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 08:56:24 -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 v12 20/25] kasan, arm64: add brk handler for inline instrumentation Date: Tue, 27 Nov 2018 17:55:38 +0100 Message-Id: X-Mailer: git-send-email 2.20.0.rc0.387.gc7a69e6b6c-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 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 a477ce2abdc9..8da7b7a4397a 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) { } -- 2.20.0.rc0.387.gc7a69e6b6c-goog