Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp58353imu; Tue, 27 Nov 2018 09:00:48 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xim/T7Ip6T0sLk0cJ408d1Myc1zFtMzNl2k4DDJRZAQdrRvoywT7bj/CCOMl2+NdKV7IYN X-Received: by 2002:a63:ff16:: with SMTP id k22mr30045477pgi.244.1543338047929; Tue, 27 Nov 2018 09:00:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543338047; cv=none; d=google.com; s=arc-20160816; b=I7JvFw3EsnN37zWyAmJnQmdpcuemocebJYRdwX7TmPs0qrORSO+cy+dXWW4+taXBIw q2IeYG3Uvw8/zeZFI2Ab1iLSdzMF9HkHIwu2ITAl0nd+qVa3PGSaUrYqIiYaaamJ1O2A wlPaKCU8c3v4Dfo8qcwQVOy4mgN8kFjutoI36000+lU4MwBstd1NuECxBVVy+wVVX3gL lPXt5+/CIa7RicZhhwjGyZBOrApFflasn5BpARYwWdnUYMDhw0hcj0+BWuptjrRKBgCk O7y6Rr4YQA78j1AanYSCGW4uNsVGmVIySXveuMokeKG0k2VcTQKNNP64PQ78xaKCKKtm hHEA== 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=DWpBc9Aeyl2K6ftokSjq3RmCgQaem7DwUgJr7g7CaTw=; b=ESGYBW+eIz0xJVmrIMGx/vGmQIdkhZYwyMqBd4zRy3B/h9Q7zxR/SzD+15AhEtkGKp VFYPevY6ObtAyBkvg+T70kkG95635OTJMbwUd/JZA+rT13W8V+2+W7p4cO2prWbGI1oP A0nMEnkqHn0QEoiMVX9CXkCZyctL2I+NE3gq4n17wzQ4b9YmBewfTYqeVuy2g0bq1bbA sAfsMw1FXCaIJtsGp7vY9/S2z5ie7fASX79gKictRFUOhgbZb8KU5JyQJLqHUF89u9vj lmROvsom7apjPEbQZt3Y/8Xm8qD6gRCKXCNOPzCk2YjPKMBR0SKKJK2AQXj4iX125FDb GroA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=CU8JL4KP; 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 t4si4300317pga.83.2018.11.27.09.00.26; Tue, 27 Nov 2018 09:00:47 -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=CU8JL4KP; 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 S1731713AbeK1Dyp (ORCPT + 99 others); Tue, 27 Nov 2018 22:54:45 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:46702 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731460AbeK1Dyp (ORCPT ); Tue, 27 Nov 2018 22:54:45 -0500 Received: by mail-wr1-f68.google.com with SMTP id l9so23424349wrt.13 for ; Tue, 27 Nov 2018 08:56:13 -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=DWpBc9Aeyl2K6ftokSjq3RmCgQaem7DwUgJr7g7CaTw=; b=CU8JL4KPkewTdTLRjln1dlwqsvsiyrscJZOuR7rFVNXaIZbugslXoYSzN6maTJ3REU WnFZV9E5LNn91F/uCnW2q9c7gLjwsvrb6QytrWNhqn3j9K5G0nZhbhKSSSbYf4EPL5mx EfK84ABAj2pc5LpSKnh6gopYytxoe6kGOmp6cDFbAvqmDYk6K9Q9wpPKQGFzusCXUqIZ k/nrPeE+U6O8pixdoT7CMphwn6LrHgyLZxD50OG/n0MhDI9y15pYB+1ASa4z5G8dDM36 J4qlbijrEz7EyL8E8n9G6w1zONJEmEJlxwfqNZYYLFzetj/LJptLT/0D0492p53sRBbD P74w== 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=DWpBc9Aeyl2K6ftokSjq3RmCgQaem7DwUgJr7g7CaTw=; b=UTEdn/N8EOF8hOe2TlOKn92auGPPmXv4Rn4aiJTXEQet6qgYmvYn6r2C06rcmLx6gd k5E1lo7nvaSkrJMiGy9f1Vr0gS/uds8RpnFE7Bm5QQLYvf2mKxHJeAu4h3dto2o1MZ9e yPerEmi5U/5s4jSxY4U6XPUh1o4ZW2saEMKx3TKxdNagzpgdy7wRcYPjf103Jdj+bNXQ Jim8vI1H4E45iLhXLwYihEPgratMMKiNLycRVZh7H8KRLXsuSkaGs2NZucKRcygZShAK Stf/mY9PaN1EI/F3mV87Ad72wuKE+slZnSSmLWlMK1FwReaxrsassRmbQ+yeNoNiono8 yWzw== X-Gm-Message-State: AA+aEWaC526zZMgnaY9PnnniFf8Av53iPVuQnCrFm4eSJpJZkIiz67ar nQIRKpN58f5q1Efw+YDrMkqC7Q== X-Received: by 2002:a5d:4acb:: with SMTP id y11mr27674359wrs.281.1543337772547; Tue, 27 Nov 2018 08:56:12 -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.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 08:56:11 -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 13/25] kasan, arm64: fix up fault handling logic Date: Tue, 27 Nov 2018 17:55:31 +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 Right now arm64 fault handling code removes pointer tags from addresses covered by TTBR0 in faults taken from both EL0 and EL1, but doesn't do that for pointers covered by TTBR1. This patch adds two helper functions is_ttbr0_addr() and is_ttbr1_addr(), where the latter one accounts for the fact that TTBR1 pointers might be tagged when tag-based KASAN is in use, and uses these helper functions to perform pointer checks in arch/arm64/mm/fault.c. Suggested-by: Mark Rutland Signed-off-by: Andrey Konovalov --- arch/arm64/mm/fault.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 7d9571f4ae3d..c1d98f0a3086 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -132,6 +133,18 @@ static void mem_abort_decode(unsigned int esr) data_abort_decode(esr); } +static inline bool is_ttbr0_addr(unsigned long addr) +{ + /* entry assembly clears tags for TTBR0 addrs */ + return addr < TASK_SIZE; +} + +static inline bool is_ttbr1_addr(unsigned long addr) +{ + /* TTBR1 addresses may have a tag if KASAN_SW_TAGS is in use */ + return arch_kasan_reset_tag(addr) >= VA_START; +} + /* * Dump out the page tables associated with 'addr' in the currently active mm. */ @@ -141,7 +154,7 @@ void show_pte(unsigned long addr) pgd_t *pgdp; pgd_t pgd; - if (addr < TASK_SIZE) { + if (is_ttbr0_addr(addr)) { /* TTBR0 */ mm = current->active_mm; if (mm == &init_mm) { @@ -149,7 +162,7 @@ void show_pte(unsigned long addr) addr); return; } - } else if (addr >= VA_START) { + } else if (is_ttbr1_addr(addr)) { /* TTBR1 */ mm = &init_mm; } else { @@ -254,7 +267,7 @@ static inline bool is_el1_permission_fault(unsigned long addr, unsigned int esr, if (fsc_type == ESR_ELx_FSC_PERM) return true; - if (addr < TASK_SIZE && system_uses_ttbr0_pan()) + if (is_ttbr0_addr(addr) && system_uses_ttbr0_pan()) return fsc_type == ESR_ELx_FSC_FAULT && (regs->pstate & PSR_PAN_BIT); @@ -319,7 +332,7 @@ static void set_thread_esr(unsigned long address, unsigned int esr) * type", so we ignore this wrinkle and just return the translation * fault.) */ - if (current->thread.fault_address >= TASK_SIZE) { + if (!is_ttbr0_addr(current->thread.fault_address)) { switch (ESR_ELx_EC(esr)) { case ESR_ELx_EC_DABT_LOW: /* @@ -455,7 +468,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr, mm_flags |= FAULT_FLAG_WRITE; } - if (addr < TASK_SIZE && is_el1_permission_fault(addr, esr, regs)) { + if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) { /* regs->orig_addr_limit may be 0 if we entered from EL0 */ if (regs->orig_addr_limit == KERNEL_DS) die_kernel_fault("access to user memory with fs=KERNEL_DS", @@ -603,7 +616,7 @@ static int __kprobes do_translation_fault(unsigned long addr, unsigned int esr, struct pt_regs *regs) { - if (addr < TASK_SIZE) + if (is_ttbr0_addr(addr)) return do_page_fault(addr, esr, regs); do_bad_area(addr, esr, regs); @@ -758,7 +771,7 @@ asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr, * re-enabled IRQs. If the address is a kernel address, apply * BP hardening prior to enabling IRQs and pre-emption. */ - if (addr > TASK_SIZE) + if (!is_ttbr0_addr(addr)) arm64_apply_bp_hardening(); local_daif_restore(DAIF_PROCCTX); @@ -771,7 +784,7 @@ asmlinkage void __exception do_sp_pc_abort(unsigned long addr, struct pt_regs *regs) { if (user_mode(regs)) { - if (instruction_pointer(regs) > TASK_SIZE) + if (!is_ttbr0_addr(instruction_pointer(regs))) arm64_apply_bp_hardening(); local_daif_restore(DAIF_PROCCTX); } @@ -825,7 +838,7 @@ asmlinkage int __exception do_debug_exception(unsigned long addr, if (interrupts_enabled(regs)) trace_hardirqs_off(); - if (user_mode(regs) && instruction_pointer(regs) > TASK_SIZE) + if (user_mode(regs) && !is_ttbr0_addr(instruction_pointer(regs))) arm64_apply_bp_hardening(); if (!inf->fn(addr, esr, regs)) { -- 2.20.0.rc0.387.gc7a69e6b6c-goog