From patchwork Tue Apr 4 06:58:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 96697 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp58068qgd; Mon, 3 Apr 2017 23:58:10 -0700 (PDT) X-Received: by 10.99.56.20 with SMTP id f20mr22126210pga.167.1491289089996; Mon, 03 Apr 2017 23:58:09 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e70si16535190pfc.37.2017.04.03.23.58.09; Mon, 03 Apr 2017 23:58:09 -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=@linaro.org; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752584AbdDDG6I (ORCPT + 25 others); Tue, 4 Apr 2017 02:58:08 -0400 Received: from mail-pg0-f42.google.com ([74.125.83.42]:33475 "EHLO mail-pg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750785AbdDDG6H (ORCPT ); Tue, 4 Apr 2017 02:58:07 -0400 Received: by mail-pg0-f42.google.com with SMTP id x125so143486459pgb.0 for ; Mon, 03 Apr 2017 23:58:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=nghcAvAYLVvJiHNZkq9/MJxigPmm2wjQrcw9U7PZfUo=; b=g9PMkaW1QcEltjbwQCPZJXV4hjeWOuJznmYBARQJeHxr9AAyDfYwtXj9MNFdsOpRfY 9WUDu9cInPkQ62Y1h9WUT8/RV8YA8/2ujeU3B0mUPR0tCJurRCvlCn+RrMJvwVVugHhy SzzVqL9RTDRRdzYFjlzrVL6MLNyl4Rcx5TFL0= 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; bh=nghcAvAYLVvJiHNZkq9/MJxigPmm2wjQrcw9U7PZfUo=; b=PCojzxq8AOJjjpq9HOIzuF6iW/ln+PJ3qnIgN+4BV/HSdsONFQWtUNbh/PiqsXISVi eyVblQ645wWfQ47DuL2bl03aOOrQP2rKm5M57uNhJ0tYeJ0g0XljvLPJ2Vw5SPNkq6x7 5EuBg+gql1U2+ydnw/9TXRaoSY7tDuq2J4Ai2eo0g2zNRuRFuw4nNNetRLl41N0QCKPN 7bJCMwnz3TlfEAZ2XLn4/IHw6k7i+AmwdLyAxSTNGk+D5zxc5X4gwazvA2XyLMqYf75L 7XkEve3pM0VgexKAdffUuKSHWDR/7AUGhsgEZ+Xirs7xk0tVL7dbSa/7D52K0r7NDqNA LreQ== X-Gm-Message-State: AFeK/H3IUyzxz6Hux2Z+RqDueaIPYYqCBj+K5KZhX/ioCHUqfo9eA8AxqeJ8GgCl+7gKA523 X-Received: by 10.84.217.149 with SMTP id p21mr26204612pli.59.1491289086241; Mon, 03 Apr 2017 23:58:06 -0700 (PDT) Received: from localhost.localdomain (ip72-199-124-160.sd.sd.cox.net. [72.199.124.160]) by smtp.gmail.com with ESMTPSA id r67sm29462358pfb.125.2017.04.03.23.58.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 03 Apr 2017 23:58:05 -0700 (PDT) From: Stephen Boyd To: Catalin Marinas , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, James Morse , Laura Abbott , Mark Rutland Subject: [PATCH v3] arm64: print a fault message when attempting to write RO memory Date: Mon, 3 Apr 2017 23:58:03 -0700 Message-Id: <20170404065803.4848-1-stephen.boyd@linaro.org> X-Mailer: git-send-email 2.10.0.297.gf6727b0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a page is marked read only we should print out that fact, instead of printing out that there was a page fault. Right now we get a cryptic error message that something went wrong with an unhandled fault, but we don't evaluate the esr to figure out that it was a read/write permission fault. Instead of seeing: Unable to handle kernel paging request at virtual address ffff000008e460d8 pgd = ffff800003504000 [ffff000008e460d8] *pgd=0000000083473003, *pud=0000000083503003, *pmd=0000000000000000 Internal error: Oops: 9600004f [#1] PREEMPT SMP we'll see: Unable to handle kernel write to read-only memory at virtual address ffff000008e760d8 pgd = ffff80003d3de000 [ffff000008e760d8] *pgd=0000000083472003, *pud=0000000083435003, *pmd=0000000000000000 Internal error: Oops: 9600004f [#1] PREEMPT SMP We also fold the userspace address check into is_permission_fault() instead of at the current callsite so that the function can't be abused with software PAN and a kernel space address. Cc: James Morse Cc: Laura Abbott Cc: Mark Rutland Signed-off-by: Stephen Boyd --- Changes from v2: * Fold addr check into is_permission_fault() (James Morse) Changes from v1: * Move into __do_kernel_fault() (Mark Rutland) arch/arm64/mm/fault.c | 54 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) -- 2.10.0.297.gf6727b0 Reviewed-by: James Morse Tested-by: James Morse Acked-by: Laura Abbott diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 156169c6981b..c6560cb4ef50 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -160,12 +160,32 @@ static bool is_el1_instruction_abort(unsigned int esr) return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR; } +static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs, + unsigned long addr) +{ + unsigned int ec = ESR_ELx_EC(esr); + unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE; + + if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR) + return false; + + if (fsc_type == ESR_ELx_FSC_PERM) + return true; + + if (addr < USER_DS && system_uses_ttbr0_pan()) + return fsc_type == ESR_ELx_FSC_FAULT && + (regs->pstate & PSR_PAN_BIT); + + return false; +} + /* * The kernel tried to access some page that wasn't present. */ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int esr, struct pt_regs *regs) { + const char *msg; /* * Are we prepared to handle this kernel fault? * We are almost certainly not prepared to handle instruction faults. @@ -177,9 +197,20 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, * No handler, we'll have to terminate things with extreme prejudice. */ bust_spinlocks(1); - pr_alert("Unable to handle kernel %s at virtual address %08lx\n", - (addr < PAGE_SIZE) ? "NULL pointer dereference" : - "paging request", addr); + + if (is_permission_fault(esr, regs, addr)) { + if (esr & ESR_ELx_WNR) + msg = "write to read-only memory"; + else + msg = "read from unreadable memory"; + } else if (addr < PAGE_SIZE) { + msg = "NULL pointer dereference"; + } else { + msg = "paging request"; + } + + pr_alert("Unable to handle kernel %s at virtual address %08lx\n", msg, + addr); show_pte(mm, addr); die("Oops", regs, esr); @@ -269,21 +300,6 @@ static int __do_page_fault(struct mm_struct *mm, unsigned long addr, return fault; } -static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs) -{ - unsigned int ec = ESR_ELx_EC(esr); - unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE; - - if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR) - return false; - - if (system_uses_ttbr0_pan()) - return fsc_type == ESR_ELx_FSC_FAULT && - (regs->pstate & PSR_PAN_BIT); - else - return fsc_type == ESR_ELx_FSC_PERM; -} - static bool is_el0_instruction_abort(unsigned int esr) { return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW; @@ -321,7 +337,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr, mm_flags |= FAULT_FLAG_WRITE; } - if (addr < USER_DS && is_permission_fault(esr, regs)) { + if (is_permission_fault(esr, regs, addr)) { /* regs->orig_addr_limit may be 0 if we entered from EL0 */ if (regs->orig_addr_limit == KERNEL_DS) die("Accessing user space memory with fs=KERNEL_DS", regs, esr);