Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4777760yba; Tue, 30 Apr 2019 04:20:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqz3TpbzgghC39C9F5iTO+6Hz8R3Y6Ghrb/JsMSbShTrNyfPcmjE6CWY0pvk+1nQZE1KreId X-Received: by 2002:a63:f843:: with SMTP id v3mr61544902pgj.69.1556623212783; Tue, 30 Apr 2019 04:20:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556623212; cv=none; d=google.com; s=arc-20160816; b=PELp9WuOgSg63fqXOB0MQgvNVoAcm7E7VieAhoNRdgQ8FRwPBjHezU+KXGm9HzjSs9 zIo1z6PkQZeBixSIcpPncod1tZXnYveiUfpHbSXtaIkAG0fVPFpdkELVCm9syOtDmP70 zwnEIyLzLytutUJFcWa6yKYGwLaksPWK5XPJcsNqvcq/HtMAak70ZQn2ab3cJS37PU1E BNgAtysmC8dUkoE83p2y2PIt+ylmYc+uonJuAaAjTBZJcnAvUANkH1n5vvmvwvVw4Mgp COyW7ShHXHr0nqjMr2qlKzXUJlvwWi27WcY6GmghTy6IOHOeuIv1qgDQUZhkhSJyilmK nWlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:dkim-signature:dkim-filter; bh=PWl7pxRpmZ4dL+XaeosBXcqK53avqKxDFfAff4vSNNI=; b=wYlcEb53qiTkIiPTXfCadnSze+pv/hlRc+srtCMq7NWb9fJI3pDTnJ7Tlb52l2c/qp WRbAWEoDoFMv4+6BahH66eU0BAnDuPaWOo4RmBXDHcd9Tbq28N7w6dBfB3Y4OB2wFUZc AQRqxr57TrhEHR9PqQh/WPQyEK4+D8Pw7HB9mB0n3ZrdJ5kD7NiUNgGPQQeMAWppwWfc UppJ4ytHR+7BvWLuwjRES0D2HQ9RDtXmKPPLcBPLEQBw3S3WS2oYT3X4f/IFysEJCtbX vOrAmSVa1RR01D+sVu4F8vtU0mCVadQqx6T0kWgTNgBo4C2Mt3fEI4WyYFBONd9jcJKJ PlGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2019041745 header.b="Ki/yNoV2"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si19331358pgn.190.2019.04.30.04.19.55; Tue, 30 Apr 2019 04:20:12 -0700 (PDT) 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=@zytor.com header.s=2019041745 header.b="Ki/yNoV2"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727612AbfD3LRL (ORCPT + 99 others); Tue, 30 Apr 2019 07:17:11 -0400 Received: from terminus.zytor.com ([198.137.202.136]:49717 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726648AbfD3LRK (ORCPT ); Tue, 30 Apr 2019 07:17:10 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x3UBGavh1346716 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 30 Apr 2019 04:16:36 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 terminus.zytor.com x3UBGavh1346716 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2019041745; t=1556622997; bh=PWl7pxRpmZ4dL+XaeosBXcqK53avqKxDFfAff4vSNNI=; h=Date:From:Cc:Reply-To:In-Reply-To:References:To:Subject:From; b=Ki/yNoV24eKhYZHoKpgzCL+Ekh0ThWTqnnPdDYkVkzNcAZEGSOffwZ6FpMxMHMExr iDHhGzzmqSNDAvGXHXWdKg+v8r/NzVe36W//0lCAGjEDmAV7pvZRNSIM1AG5lem67p cnCZYW1qTSL08g7gwwbQIqB4kaoyupiahRQjTFL9im2GysVUp4Ma5MFjD0PuS9CGvp ZuJFkasAShtfS698HsdbY0shsyMsBJ0uWuLJ+Kx+qxWy5VMcuPTXx9CNNKbPPVFz5Q ux9XmTVVLPC9doEEzoEb2RCWzay3XmCF6VsFiH+jz6a8trtvvGIZYwGMQIWTwg6RYq LSo9P6jYP+nHg== Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x3UBGaGV1346710; Tue, 30 Apr 2019 04:16:36 -0700 Date: Tue, 30 Apr 2019 04:16:36 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Andy Lutomirski Message-ID: Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org, will.deacon@arm.com, mhiramat@kernel.org, hpa@zytor.com, torvalds@linux-foundation.org, peterz@infradead.org, mingo@kernel.org, kernel-hardening@lists.openwall.com, dave.hansen@intel.com, kristen@linux.intel.com, riel@surriel.com, linux_dti@icloud.com, namit@vmware.com, ard.biesheuvel@linaro.org, luto@kernel.org, keescook@chromium.org, rick.p.edgecombe@intel.com, bp@alien8.de, akpm@linux-foundation.org, deneen.t.dock@intel.com Reply-To: torvalds@linux-foundation.org, hpa@zytor.com, mhiramat@kernel.org, will.deacon@arm.com, linux-kernel@vger.kernel.org, tglx@linutronix.de, dave.hansen@intel.com, kernel-hardening@lists.openwall.com, mingo@kernel.org, peterz@infradead.org, luto@kernel.org, ard.biesheuvel@linaro.org, namit@vmware.com, linux_dti@icloud.com, riel@surriel.com, kristen@linux.intel.com, akpm@linux-foundation.org, deneen.t.dock@intel.com, bp@alien8.de, rick.p.edgecombe@intel.com, keescook@chromium.org In-Reply-To: <20190426001143.4983-4-namit@vmware.com> References: <20190426001143.4983-4-namit@vmware.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/mm] x86/mm: Introduce temporary mm structs Git-Commit-ID: cefa929c034eb5d9c15c50088235a0093a219687 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, FREEMAIL_FORGED_REPLYTO,T_DATE_IN_FUTURE_96_Q autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: cefa929c034eb5d9c15c50088235a0093a219687 Gitweb: https://git.kernel.org/tip/cefa929c034eb5d9c15c50088235a0093a219687 Author: Andy Lutomirski AuthorDate: Thu, 25 Apr 2019 17:11:23 -0700 Committer: Ingo Molnar CommitDate: Tue, 30 Apr 2019 12:37:50 +0200 x86/mm: Introduce temporary mm structs Using a dedicated page-table for temporary PTEs prevents other cores from using - even speculatively - these PTEs, thereby providing two benefits: (1) Security hardening: an attacker that gains kernel memory writing abilities cannot easily overwrite sensitive data. (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 mm struct, which will follow, is for poking the kernel text. [ Commit message was written by Nadav Amit ] Tested-by: Masami Hiramatsu Signed-off-by: Andy Lutomirski Signed-off-by: Nadav Amit Signed-off-by: Rick Edgecombe Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Masami Hiramatsu Cc: Cc: Cc: Cc: Cc: Cc: Cc: Cc: Borislav Petkov Cc: Dave Hansen Cc: H. Peter Anvin Cc: Kees Cook Cc: Linus Torvalds Cc: Rik van Riel Cc: Thomas Gleixner Link: https://lkml.kernel.org/r/20190426001143.4983-4-namit@vmware.com Signed-off-by: Ingo Molnar --- arch/x86/include/asm/mmu_context.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 19d18fae6ec6..24dc3b810970 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -356,4 +356,37 @@ static inline unsigned long __get_current_cr3_fast(void) return cr3; } +typedef struct { + struct mm_struct *mm; +} temp_mm_state_t; + +/* + * Using a temporary mm allows to set temporary mappings that are not accessible + * by other CPUs. 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 CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the + * mapping is torn down. + * + * 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 overriding + * the kernel memory protection. + */ +static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm) +{ + temp_mm_state_t temp_state; + + lockdep_assert_irqs_disabled(); + temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm); + switch_mm_irqs_off(NULL, mm, current); + return temp_state; +} + +static inline void unuse_temporary_mm(temp_mm_state_t prev_state) +{ + lockdep_assert_irqs_disabled(); + switch_mm_irqs_off(NULL, prev_state.mm, current); +} + #endif /* _ASM_X86_MMU_CONTEXT_H */