Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4799446ybl; Wed, 22 Jan 2020 05:02:27 -0800 (PST) X-Google-Smtp-Source: APXvYqy2WcZcA+eO50k4OuywzG/Ka6nBY/8kXv83z9kgyLaxUk6dsAcw1hnOfcrCyDG6cS0gVENV X-Received: by 2002:a9d:c42:: with SMTP id 60mr7072341otr.182.1579698146965; Wed, 22 Jan 2020 05:02:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579698146; cv=none; d=google.com; s=arc-20160816; b=IT2Z+IVaCyxf8fo4jKQ8adIh7m1f9cE8eO/1a+u305x8+rq9Phc2agfIzuMd8wpJf/ E9txUvLkDKJt5sJOKe1kbVS9bX32u32AzYfvawBFmaPJkMa9akXqNRU6q0dO3nDzpJ4U RlLEXq3JXB6ERP/3pvc6zfFPMFN8Ph+mFYNj1AnFcb/04yfWIMSlE5abY/iHxEnQIS4w c8x+ktHiRoLys+DOX6v6VIHu4vxQRpWqVVp+2GePmu+DRJJ9WnEfWhICb6RxgMVBbcmE KwDLx52A8yHJ+F1uSRYiWhLCF38rF8Hi07o/tc3uf3Am18hrVAk2kCZB/GhvXnjbALJE JkDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:dkim-signature; bh=BF/WSUwGbAawtr7cCXMCEdJLNLqYSo2Xw7j9lE0uNk4=; b=JDI/02SbgHyncGOyDIOU0t0PgZdjdXScrwXBo1HULQf/SO2xqrSXzuoxa+QTtsMDJ2 zE/IqPhV+L6NKfuUU5WLWTNfS7k6vMaOA43Ib6J1r90XNB1U3uxexqXi5LbC+61WM3Fw K4BkccYkv8/pIsqOcoeKuVGR7xDsnpBqG1SE4AoZdNIc+i8caCtcfAuw+oLPkbIcUO5s 2lyxpd0E2C5AJyBhgqclElihBHYl5U3uKBiiF8+gJaiYwHkKS81IcgL1JGU7YpP5CfaA ldELa7KWj4uE/MiU0Nj+9nTUibyB3WgAOB4bKP0bRkqec+J5h967jA7ta0m1/dkEYQsD EaQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=lCAZ5ppu; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m19si20201631oig.91.2020.01.22.05.02.15; Wed, 22 Jan 2020 05:02:26 -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=@c-s.fr header.s=mail header.b=lCAZ5ppu; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729019AbgAVNAj (ORCPT + 99 others); Wed, 22 Jan 2020 08:00:39 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:44375 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728731AbgAVNAj (ORCPT ); Wed, 22 Jan 2020 08:00:39 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 482lrN1tMTz9v4T2; Wed, 22 Jan 2020 14:00:36 +0100 (CET) Authentication-Results: localhost; dkim=pass reason="1024-bit key; insecure key" header.d=c-s.fr header.i=@c-s.fr header.b=lCAZ5ppu; dkim-adsp=pass; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id 4B5aZk5Era2I; Wed, 22 Jan 2020 14:00:36 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 482lrN0ppMz9v4T0; Wed, 22 Jan 2020 14:00:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1579698036; bh=BF/WSUwGbAawtr7cCXMCEdJLNLqYSo2Xw7j9lE0uNk4=; h=In-Reply-To:References:From:Subject:To:Cc:Date:From; b=lCAZ5ppuGHI848C/DLKi3ORIAWjLUE+8sMxMVk4t8Nxu00wufy47pzWSQOFSjLLuy lzi66CcwzqqercRKpnD0c/9MSDxZp/FqnFh/tMHL2uBRnK9tNT1pel6SdjsOZB4X4P 13McGq9rdvcUrHJ/ePssf3KMo2FFX3mocHXIlZg4= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 337488B7FE; Wed, 22 Jan 2020 14:00:37 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id VyG-5qfjoPzz; Wed, 22 Jan 2020 14:00:37 +0100 (CET) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.230.100]) by messagerie.si.c-s.fr (Postfix) with ESMTP id BC60F8B800; Wed, 22 Jan 2020 14:00:36 +0100 (CET) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id ACF0D651E0; Wed, 22 Jan 2020 13:00:36 +0000 (UTC) Message-Id: In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v1 2/6] powerpc/32s: Fix bad_kuap_fault() To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Date: Wed, 22 Jan 2020 13:00:36 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At the moment, bad_kuap_fault() reports a fault only if a bad access to userspace occurred while access to userspace was not granted. But if a fault occurs for a write outside the allowed userspace segment(s) that have been unlocked, bad_kuap_fault() fails to detect it and the kernel loops forever in do_page_fault(). Fix it by checking that the accessed address is within the allowed range. Fixes: a68c31fc01ef ("powerpc/32s: Implement Kernel Userspace Access Protection") Cc: stable@vger.kernel.org Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/book3s/32/kup.h | 9 +++++++-- arch/powerpc/include/asm/book3s/64/kup-radix.h | 3 ++- arch/powerpc/include/asm/nohash/32/kup-8xx.h | 3 ++- arch/powerpc/mm/fault.c | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/32/kup.h b/arch/powerpc/include/asm/book3s/32/kup.h index f9dc597b0b86..d88008c8eb85 100644 --- a/arch/powerpc/include/asm/book3s/32/kup.h +++ b/arch/powerpc/include/asm/book3s/32/kup.h @@ -131,12 +131,17 @@ static inline void prevent_user_access(void __user *to, const void __user *from, kuap_update_sr(mfsrin(addr) | SR_KS, addr, end); /* set Ks */ } -static inline bool bad_kuap_fault(struct pt_regs *regs, bool is_write) +static inline bool +bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) { + unsigned long begin = regs->kuap & 0xf0000000; + unsigned long end = regs->kuap << 28; + if (!is_write) return false; - return WARN(!regs->kuap, "Bug: write fault blocked by segment registers !"); + return WARN(address < begin || address >= end, + "Bug: write fault blocked by segment registers !"); } #endif /* CONFIG_PPC_KUAP */ diff --git a/arch/powerpc/include/asm/book3s/64/kup-radix.h b/arch/powerpc/include/asm/book3s/64/kup-radix.h index f254de956d6a..dbbd22cb80f5 100644 --- a/arch/powerpc/include/asm/book3s/64/kup-radix.h +++ b/arch/powerpc/include/asm/book3s/64/kup-radix.h @@ -95,7 +95,8 @@ static inline void prevent_user_access(void __user *to, const void __user *from, set_kuap(AMR_KUAP_BLOCKED); } -static inline bool bad_kuap_fault(struct pt_regs *regs, bool is_write) +static inline bool +bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) { return WARN(mmu_has_feature(MMU_FTR_RADIX_KUAP) && (regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : AMR_KUAP_BLOCK_READ)), diff --git a/arch/powerpc/include/asm/nohash/32/kup-8xx.h b/arch/powerpc/include/asm/nohash/32/kup-8xx.h index 1006a427e99c..f2fea603b929 100644 --- a/arch/powerpc/include/asm/nohash/32/kup-8xx.h +++ b/arch/powerpc/include/asm/nohash/32/kup-8xx.h @@ -46,7 +46,8 @@ static inline void prevent_user_access(void __user *to, const void __user *from, mtspr(SPRN_MD_AP, MD_APG_KUAP); } -static inline bool bad_kuap_fault(struct pt_regs *regs, bool is_write) +static inline bool +bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) { return WARN(!((regs->kuap ^ MD_APG_KUAP) & 0xf0000000), "Bug: fault blocked by AP register !"); diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c index b5047f9b5dec..1baeb045f7f4 100644 --- a/arch/powerpc/mm/fault.c +++ b/arch/powerpc/mm/fault.c @@ -233,7 +233,7 @@ static bool bad_kernel_fault(struct pt_regs *regs, unsigned long error_code, // Read/write fault in a valid region (the exception table search passed // above), but blocked by KUAP is bad, it can never succeed. - if (bad_kuap_fault(regs, is_write)) + if (bad_kuap_fault(regs, address, is_write)) return true; // What's left? Kernel fault on user in well defined regions (extable -- 2.25.0