From patchwork Wed Mar 29 18:15:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9652177 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 DB8CC602C8 for ; Wed, 29 Mar 2017 18:16:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C5E1428510 for ; Wed, 29 Mar 2017 18:16:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BA9792851A; Wed, 29 Mar 2017 18:16:41 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id A2B8228510 for ; Wed, 29 Mar 2017 18:16:39 +0000 (UTC) Received: (qmail 20474 invoked by uid 550); 29 Mar 2017 18:16:36 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 20364 invoked from network); 29 Mar 2017 18:16:33 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=E784mOTbHmhepBnfYqMY14LYhBrFinjD8bap/cV1Hwk=; b=k3pH/OLShRqUL5KcObte+oH5Flo3JB5ZekLa7BiMGsYTOgAjlNc9XZD02LmCyopT1H fnlITM+vD5s14/VThzIAuKfrLQpkgtUaoDJxZjWh9+kIE5mAhdfkFrSmGXxltXxSdqo6 9nDidJJB2gSF4n6/CsYs5vuI8qVAsmFm7A2aQ= 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; bh=E784mOTbHmhepBnfYqMY14LYhBrFinjD8bap/cV1Hwk=; b=iWXfkusAp4+Z4bgV0qpOVckWmUxTUegXC03lIzF9jPSUbA1uP0l3ysZQ+1jgPSeRDZ c5JqDjyGnNxGgwcUtoMJG2xGfb7gX8tIfWj/Z4LzcjdXM//FypntpXK8AXjyLk9sSD7p gWyMPu2HJtcu63+ThljX5xoUmXOnhIPGuhPlOYwtqvhqtEFofCsZ9OSgAwiWVTIEZRUA Xw5o8umEj6OXCc5E2J+sNB8BaPBR/c9Uw5aW/ekdyrs5HA0MLFNtX5Ci7SUxv+fsVQm7 aXd6bk4tb8xbUFpPNWJMr7MGkWlSBsGDCToTwl59Xmp8osVxNJ36ehQN8hfp0pDxTch/ cwHg== X-Gm-Message-State: AFeK/H2+URc3nGvo/QhdXCfngpPshYqYzeRcGN7lsCO2MIOpyxEeau/3Cdr6vFw6JPuBqkE9 X-Received: by 10.98.220.65 with SMTP id t62mr1927512pfg.0.1490811382182; Wed, 29 Mar 2017 11:16:22 -0700 (PDT) From: Kees Cook To: kernel-hardening@lists.openwall.com Cc: Kees Cook , Mark Rutland , Andy Lutomirski , Hoeun Ryu , PaX Team , Emese Revfy , Russell King , x86@kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Date: Wed, 29 Mar 2017 11:15:56 -0700 Message-Id: <1490811363-93944-5-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490811363-93944-1-git-send-email-keescook@chromium.org> References: <1490811363-93944-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [RFC v2][PATCH 04/11] x86: Implement __arch_rare_write_begin/unmap() X-Virus-Scanned: ClamAV using ClamSMTP Based on PaX's x86 pax_{open,close}_kernel() implementation, this allows HAVE_ARCH_RARE_WRITE to work on x86. There is missing work to sort out some header file issues where preempt.h is missing, though it can't be included in pg_table.h unconditionally... some other solution will be needed, perhaps an entirely separate header file for rare_write()-related defines... This patch is also missing paravirt support. Signed-off-by: Kees Cook --- arch/x86/Kconfig | 1 + arch/x86/include/asm/pgtable.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index cc98d5a294ee..2d1d707aa036 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -106,6 +106,7 @@ config X86 select HAVE_ARCH_KMEMCHECK select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT + select HAVE_ARCH_RARE_WRITE select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 1cfb36b8c024..2e6bf661bb84 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -91,6 +91,37 @@ extern struct mm_struct *pgd_page_get_mm(struct page *page); #endif /* CONFIG_PARAVIRT */ +/* TODO: Bad hack to deal with preempt macros being missing sometimes. */ +#ifndef preempt_disable +#include +#endif + +static __always_inline unsigned long __arch_rare_write_begin(void) +{ + unsigned long cr0; + + preempt_disable(); + barrier(); + cr0 = read_cr0() ^ X86_CR0_WP; + BUG_ON(cr0 & X86_CR0_WP); + write_cr0(cr0); + barrier(); + return cr0 ^ X86_CR0_WP; +} + +static __always_inline unsigned long __arch_rare_write_end(void) +{ + unsigned long cr0; + + barrier(); + cr0 = read_cr0() ^ X86_CR0_WP; + BUG_ON(!(cr0 & X86_CR0_WP)); + write_cr0(cr0); + barrier(); + preempt_enable_no_resched(); + return cr0 ^ X86_CR0_WP; +} + /* * The following only work if pte_present() is true. * Undefined behaviour if not..