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: 9652249 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 C5C75602C8 for ; Wed, 29 Mar 2017 18:21:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF1122851D for ; Wed, 29 Mar 2017 18:21:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A0C2A2851F; Wed, 29 Mar 2017 18:21: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=-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 136A928517 for ; Wed, 29 Mar 2017 18:21:12 +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:References: In-Reply-To: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:List-Owner; bh=F24yBz2Ewvi74riG7HUO1WAOf6fs+UhuSlHQLxGK++E=; b=N77HhSl1Z9R4ryL+XMlzo/bZ20 IPlEneqv3fIyS92Bn8jIFeQfiIGwqA2wZ6IWDsnm65iWgviIDciMcycwo0iF91CcZbusZfUnNBdFZ 9aeY/3M5ks8ZXoSSlXYFIcsPBi59YWA69mpZBdh5Qg/zgy2lA8qlGp+7ORjdY2dT5k0aGyp2bKlwO aGn42Rc2c1ti0+4YeopsSrOxmcchCOpiUgC8lMLS2COOpAINy5bPsmIZTnQvgZE/G3b6Eg7ZqG1ez cubQyV6e4drf/HxmNDjN6od5IFUlGR9/hdj2H3MVhgCS6vDv7sm4MJMk4fYKvOT8WtjOVA0wYBxGa 5djUBc4g==; 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 1ctIDU-0001ks-Qg; Wed, 29 Mar 2017 18:21:08 +0000 Received: from mail-pg0-x229.google.com ([2607:f8b0:400e:c05::229]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctI9D-00058F-9W for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 18:16:47 +0000 Received: by mail-pg0-x229.google.com with SMTP id 81so14644769pgh.2 for ; Wed, 29 Mar 2017 11:16:22 -0700 (PDT) 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=ujBTH5+Bb+C0/JOOzUMDKJ7Ci4W98t4/wDISQZ7edt+GCIyqThRQwjHQXiHb+0I9xU keVXFvlcsrjUUG4N/19mxBaTjNRM4fHTZ7dxoKG2uc+r/c6r+Q25/8bh8R7wcLLmEA/9 Rgks4Z0ptHZNB5D+LedkAn9SPjzcgA4OubQFqm7eyx7RGiYtgOXosxYCpQ/RgqQ38qud F9Yc5DNIusOpgD8E2roM8g/773jdpbRWFMgQMBW2rgujax11GXZioNe//0KTwinSoMZG cRFpadoe5wU/l0nmx6QtQwl4MwlKjzDMFG0ycAZu4FZnp7e1f+0NjGrDoWZKbXLITixu lzkw== X-Gm-Message-State: AFeK/H0XuhaCeZZ+TEc/Kc2l4Q5pYRDkZ8TocSWaKPGMDhoFZ0jDCc9UL4TDFZxbmN6xhomK X-Received: by 10.98.220.65 with SMTP id t62mr1927512pfg.0.1490811382182; Wed, 29 Mar 2017 11:16:22 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id u75sm14842327pgc.31.2017.03.29.11.16.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Mar 2017 11:16:20 -0700 (PDT) From: Kees Cook To: kernel-hardening@lists.openwall.com Subject: [RFC v2][PATCH 04/11] x86: Implement __arch_rare_write_begin/unmap() 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170329_111643_477957_6FBD543A X-CRM114-Status: GOOD ( 11.61 ) 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 , Hoeun Ryu , Kees Cook , x86@kernel.org, Russell King , linux-kernel@vger.kernel.org, Emese Revfy , Andy Lutomirski , PaX Team , 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 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..