From patchwork Mon Feb 27 20:43:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9594183 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 839E860471 for ; Mon, 27 Feb 2017 20:44:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 767C027F9F for ; Mon, 27 Feb 2017 20:44:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6B0692832D; Mon, 27 Feb 2017 20:44:12 +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 A2D6B27F9F for ; Mon, 27 Feb 2017 20:44:11 +0000 (UTC) Received: (qmail 7837 invoked by uid 550); 27 Feb 2017 20:43:55 -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 6069 invoked from network); 27 Feb 2017 20:43:44 -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=Gn9qVmBvu5mqedtWW6cryiAtJNrIVvfbv7yIcSA6lHU=; b=nyHgU9VU8kgqAW+cuMyLIWgi8Ko4Kfp6O6+s5ZCDh9epSZ/EurLi/1C2eyMrBXxkpO Bi5IngzgziHiCmAgTpiZO/fB+rajHIdJOfRLYXR6bW/0fdJi3SCLv7rR+mB3DK0eBOFH CGVf4SCeTW/TilYReiL5O+YC/hUyIWLtWyVRg= 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=Gn9qVmBvu5mqedtWW6cryiAtJNrIVvfbv7yIcSA6lHU=; b=L5x2IwMux1ItOEG2WCxwaDJ/+shikUKH0pzcmq15McUZs4oU52SsxTPE5/ewtpnD09 icbDzCaxKinWYfQyu1hmcrTMEyOxGmIqPCdA8xyzOlK0Is6KHFgf5At5sYn9U2PrhUHp TDVWfyI1elEq0TnqsmbiFME8r6U1TqAJqGxNjaGbXyHnHJCJzAtTjG+dcuNQscjJqhEy jqzqDQdXt5aQAgaPKRB23+C2WNhk/KZikiU7IHjdvZ73iErK7xII7y8kXPvDpD+bOxPM osKxu/Bmic0PfiK1H5NERNVk/z7PUwpB2DvZ0sVwo+T7/+osD1Fc8lugq3YZAW68CB+D WHNw== X-Gm-Message-State: AMke39mHRYuOWdEECutgc9l7rdoK4pt2FcHQe87zERJLDGOG9A0NlJGE4wx+YZAQA4Rc+jDP X-Received: by 10.84.141.129 with SMTP id 1mr27001351plv.166.1488228213166; Mon, 27 Feb 2017 12:43:33 -0800 (PST) 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 Date: Mon, 27 Feb 2017 12:43:02 -0800 Message-Id: <1488228186-110679-5-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488228186-110679-1-git-send-email-keescook@chromium.org> References: <1488228186-110679-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [RFC][PATCH 4/8] x86: Implement __arch_rare_write_map/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 e487493bbd47..6d4d6f73d4b8 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -102,6 +102,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 437feb436efa..86e78e97738f 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -90,6 +90,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 inline unsigned long __arch_rare_write_map(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 inline unsigned long __arch_rare_write_unmap(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..