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: 9660893 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5A48360352 for ; Tue, 4 Apr 2017 06:58:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F9EE2847B for ; Tue, 4 Apr 2017 06:58:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 41D3C28494; Tue, 4 Apr 2017 06:58:35 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BAAB22847B for ; Tue, 4 Apr 2017 06:58:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=zR6RW3JzJ3pbJViNjNkm5CPpzhgZ4qUpKqz3qqW0oic=; b=Chf OcCI/3BScy3K+QxiOnvmnsz0o0htTxj3VXo/W1AanzXPHi3uGnOIqMwu4GkMhrK8Wvte72WKXm01V xekoXKYdyaRdr8bBlCNfl85MMOWdB6dW+5vIdaTDMvnbuzDpTBnNzN+0ikUCAkMiatbB5Ox1Pq5zs 9Q9PT9noAFl/8dhNhL5Qi6Ib0n5eTDL5kTmBaOrXFv0GZ+mP9AolTS9+CsQuLvnCBlTeeWWSLDlkb zwAIS0KO7904Ohvtge6XTLkrv84FTHRMT9SSmhEi2XHXHz4a6+ihQQmuepwEotDY8l5aWD13RLgS2 qZ/Qv2oXhhpaolouDTF3JGye5RGAsmA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cvIQC-00033g-NL; Tue, 04 Apr 2017 06:58:32 +0000 Received: from mail-pg0-x234.google.com ([2607:f8b0:400e:c05::234]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cvIQ8-00030t-Mq for linux-arm-kernel@lists.infradead.org; Tue, 04 Apr 2017 06:58:31 +0000 Received: by mail-pg0-x234.google.com with SMTP id g2so141716457pge.3 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=XLpVK7IafiS0/g3dLFay1NHoneXcX6LHvQkdNehO/Wgk6711KKGK8apOP1qcCR0Ern pH914oj7lmU2aCaV66hzLeRTCes+lxQz4ynTdhQsnCpabD3HCTSFRqxuoqx4+BJwfORp WBhN0K0+fpmeOIvk2yZISrnZAxcc6AjMk1Iu166sQF4yOxMX/l2BrFbgGKWgtc5cgf8y TyjIQj+XegBReodshi8bQBJapQCrzb6I+j/i7E1WbIq5EIq4yy56XcFjQGzKDHWCikyq cvghmboO8N497q3pzP+HJpr857KfrRzqqWRizhZGjTFPMG7qbi0YULeKxbR3iVRHQcKk 6wJg== X-Gm-Message-State: AFeK/H3h3tnWleI44xmv5GBrJoUKKfj1KRvH7r3b0TtYvLbm+evc68+s3XuSvDHMUxS4UQYT 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170403_235828_836757_AFA60399 X-CRM114-Status: GOOD ( 18.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Laura Abbott , James Morse , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: James Morse Tested-by: James Morse Acked-by: Laura Abbott --- 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(-) 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);