From patchwork Thu Jan 17 00:32:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 10767233 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2E61414E5 for ; Thu, 17 Jan 2019 00:33:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1ECA72FD1B for ; Thu, 17 Jan 2019 00:33:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1CE512FD2C; Thu, 17 Jan 2019 00:33:43 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 933C12FD35 for ; Thu, 17 Jan 2019 00:33:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CCDA8E0002; Wed, 16 Jan 2019 19:33:39 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 155EC8E0003; Wed, 16 Jan 2019 19:33:39 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 074358E0002; Wed, 16 Jan 2019 19:33:39 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id B1AC98E0003 for ; Wed, 16 Jan 2019 19:33:38 -0500 (EST) Received: by mail-pg1-f198.google.com with SMTP id r13so5003872pgb.7 for ; Wed, 16 Jan 2019 16:33:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=ezRmX4cq7Q+xV7duJ1xb3mXnXLtmokQHa4CGkRg5SRM=; b=FhHkah2D464d4oOB5Q2JNFGlNwkOpIo5qvQCWUHdJ/UmtI3rXv4NaB8fDgNc72J1pb QXN82ZmLv8exKA8q157mfUotdoIkTTEM6i+haHnm1ueaWsdGge6pV8CEaicVYIUeDZ11 0Ph0mjghThbg7KEn/JClm6ETSxWzeY5P7mrMO4tRfvU5q6qwPi4FvVRO/mQw13PMEmy3 kLiLlDRunVUGs+kQwZjM5txvBZbkdoVstKzBJ7D6hGH8fmRBeitekmd7vS6WkgQMScFQ 2XNuXEEtFJNAihyEq/CdKFSIPQEkXrYrB67BY15BRrDBomt/0J7axKQAiFD00FXr/WIs WDng== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: AJcUukckv2/O1wxzG566R/I6yue/97gQ6FyID+EKOnogYYruZey9jJ06 QrDNWQ+Dn1/HGdEvJpsc+sUFKUC4etpdvmNOmJ0YstpETW6ACwVnvWc5AzUo0+86Pui5F4juB0v 7a4P6ynK+CTh9O4oY7mBTPF5L8Np5wczGMX9esIG2cVB6NkwAnRLi7mJHLafEwYVmRQ== X-Received: by 2002:a63:30c8:: with SMTP id w191mr11834927pgw.120.1547685218336; Wed, 16 Jan 2019 16:33:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN55lywrvWGxYvvFHt5GBMxdPQ0mwOdfFR/kwuig5Arrv6rDoFMNYa/gmPUUIqLy5eb1cOoZ X-Received: by 2002:a63:30c8:: with SMTP id w191mr11834852pgw.120.1547685217353; Wed, 16 Jan 2019 16:33:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547685217; cv=none; d=google.com; s=arc-20160816; b=ypMeh0vKX/Im3NBqD2OVIchbMh45ekMa2TU+DQQSzMpDOWtuaNEQJL539EQBphBQRH EJENobUBqnSxbCYCb0d220KA5KrJZuLUVs9A+1rNvbpKH31/u7yla0JusSNoe+NiZO0r mjONelHtzVHQB+1UYZOU09aoSQwCFdMkJH4XrjKfhlrrVeKkc9jQEMG0cK7LRwPF6ViH YeGS6xQ1dt0mtY1i1LQj21E50Gb93O+PzjkcGZxuxzqL2Ya34WP9qo1YvZ8RCp2UnvEk xCM2RBggLxt7DSyDfSKG7VP1M/sb53Q2n2mqb68yePHcFQRx5jBb96t65RlTYlwvMbI9 bB2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=ezRmX4cq7Q+xV7duJ1xb3mXnXLtmokQHa4CGkRg5SRM=; b=Pyp7NVSQrCkP6+vyCZDlflWbSRISg0HcuYjGMCKc1pXkI/S2/OuG8ylVZYJTbnLR/X 15IE362oFSIHeAq056NzyU11ZpcaxXf+RiUE+pkkt2vAZHiK8CH3AwtA2BZ6hqAshteo DEUKUkhMmefVsxuX23dyzMyxV/YBfdD+NDHUeJzCSx5GZz8mGQrrKv8HJfPkeqYPQ7NX 4adiDxYF9n5RkglRBoOwuiRmTApoF562W89aIeDGWkZel0LGyivVHo9p5U344q0chx+M ZdOehIz+rnj/Qxsj/WmLNLGegG5Bte5ekU8QrzOWiam7V4EdZI2JAB4RjlxOoeiNXS8Z ffTQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga04.intel.com (mga04.intel.com. [192.55.52.120]) by mx.google.com with ESMTPS id y10si7582915plt.406.2019.01.16.16.33.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jan 2019 16:33:37 -0800 (PST) Received-SPF: pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.120 as permitted sender) client-ip=192.55.52.120; Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Jan 2019 16:33:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,488,1539673200"; d="scan'208";a="292166015" Received: from rpedgeco-desk5.jf.intel.com ([10.54.75.79]) by orsmga005.jf.intel.com with ESMTP; 16 Jan 2019 16:33:35 -0800 From: Rick Edgecombe To: Andy Lutomirski , Ingo Molnar Cc: linux-kernel@vger.kernel.org, x86@kernel.org, hpa@zytor.com, Thomas Gleixner , Borislav Petkov , Nadav Amit , Dave Hansen , Peter Zijlstra , linux_dti@icloud.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, akpm@linux-foundation.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, will.deacon@arm.com, ard.biesheuvel@linaro.org, kristen@linux.intel.com, deneen.t.dock@intel.com, Kees Cook , Dave Hansen , Nadav Amit , Rick Edgecombe Subject: [PATCH 03/17] x86/mm: temporary mm struct Date: Wed, 16 Jan 2019 16:32:45 -0800 Message-Id: <20190117003259.23141-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190117003259.23141-1-rick.p.edgecombe@intel.com> References: <20190117003259.23141-1-rick.p.edgecombe@intel.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Andy Lutomirski Sometimes we want to set a temporary page-table entries (PTEs) in one of the cores, without allowing other cores to use - even speculatively - these mappings. There are two benefits for doing so: (1) Security: if sensitive PTEs are set, temporary mm prevents their use in other cores. This hardens the security as it prevents exploding a dangling pointer to overwrite sensitive data using the sensitive PTE. (2) Avoiding TLB shootdowns: the PTEs do not need to be flushed in remote page-tables. To do so a temporary mm_struct can be used. Mappings which are private for this mm can be set in the userspace part of the address-space. During the whole time in which the temporary mm is loaded, interrupts must be disabled. The first use-case for temporary PTEs, which will follow, is for poking the kernel text. [ Commit message was written by Nadav ] Cc: Kees Cook Cc: Peter Zijlstra Cc: Dave Hansen Reviewed-by: Masami Hiramatsu Tested-by: Masami Hiramatsu Signed-off-by: Andy Lutomirski Signed-off-by: Nadav Amit Signed-off-by: Rick Edgecombe --- arch/x86/include/asm/mmu_context.h | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 0ca50611e8ce..0141b7fa6d01 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -338,4 +338,36 @@ static inline unsigned long __get_current_cr3_fast(void) return cr3; } +typedef struct { + struct mm_struct *prev; +} temporary_mm_state_t; + +/* + * Using a temporary mm allows to set temporary mappings that are not accessible + * by other cores. Such mappings are needed to perform sensitive memory writes + * that override the kernel memory protections (e.g., W^X), without exposing the + * temporary page-table mappings that are required for these write operations to + * other cores. + * + * Context: The temporary mm needs to be used exclusively by a single core. To + * harden security IRQs must be disabled while the temporary mm is + * loaded, thereby preventing interrupt handler bugs from override the + * kernel memory protection. + */ +static inline temporary_mm_state_t use_temporary_mm(struct mm_struct *mm) +{ + temporary_mm_state_t state; + + lockdep_assert_irqs_disabled(); + state.prev = this_cpu_read(cpu_tlbstate.loaded_mm); + switch_mm_irqs_off(NULL, mm, current); + return state; +} + +static inline void unuse_temporary_mm(temporary_mm_state_t prev) +{ + lockdep_assert_irqs_disabled(); + switch_mm_irqs_off(NULL, prev.prev, current); +} + #endif /* _ASM_X86_MMU_CONTEXT_H */