Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp339415yba; Fri, 26 Apr 2019 00:35:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwd8Ax/HBtrhHd+p/GfSJXQOxUdKcHYKW0NXVKHp/MyPTZYzOo5y8xoKPGdXDbzn5c3i7w4 X-Received: by 2002:a17:902:e208:: with SMTP id ce8mr30194457plb.99.1556264100849; Fri, 26 Apr 2019 00:35:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556264100; cv=none; d=google.com; s=arc-20160816; b=WaEOC/SJMsYr0p48Dp8Kn3F89V/svaIOc3RPInSNz770L8TAc3WyR2NdgYCoUNEtZM Z02Y/+1S12iY5ue2vKvA1IqYJSPOx5F3zrFUr3muZIjVa8mqUSw9kcQzvFTIxl/4X/1Z eIBcUh8cuzA7FtmwU9iP28AGTIbQ0GtmZ0OnentYo9yLUbt8toaZJS59rgcbh+vrIqou cXVdBh1splpRQweP4UfOzHTBUfTEceV6711OX8ZFLCyZ35CDSeO8AnB8BzFUn6LwSXPp mx8uUPdNk8hb6/YMJf8+6Rgiv8JKtvG72HJqI3tEG8R4OqMPF1Dr8vEaAsHhrg38cwIK jAFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=fM02CdP3EHsEznyJ/zI3DVkvywjW2Y0W5QE/m/kGqS8=; b=psbkG1su5K6596DtxiXTC/6DGof2+VuBkSmh0Hgf/Zst3O8DLQ2K/3l1tn13njcmMc ngj0MfV+4I5mGh4Na5gYQD0j1U7RwXFpY6UA+qWdVxUCbv8m+ZinxvvVMiIvYWeW4aKD aYBYp0VpdcFCb6SMrA8q9mUfywCIJpImYeF7dzDDuQHyyKKlqs4+tJqdppk43w8HUl0v 42DhwhhANGAFEJ6zKXqE0Fl9lfKbBxcCcZ4TB8ctgk83xzdftz5E3DjR8e57YIDhlX+D qBqbEMTBV1RbiQS0HP/RQ+ztYfK+Xsva+f3rh8I1C4bGyC0ZSJxYXpE+OzcNgueDbq/J A+aA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r77si13877311pgr.140.2019.04.26.00.34.45; Fri, 26 Apr 2019 00:35:00 -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; 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=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726193AbfDZHbx (ORCPT + 99 others); Fri, 26 Apr 2019 03:31:53 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:29121 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725912AbfDZHbr (ORCPT ); Fri, 26 Apr 2019 03:31:47 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Fri, 26 Apr 2019 00:31:44 -0700 Received: from sc2-haas01-esx0118.eng.vmware.com (sc2-haas01-esx0118.eng.vmware.com [10.172.44.118]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 77C0D412A1; Fri, 26 Apr 2019 00:31:45 -0700 (PDT) From: Nadav Amit To: Peter Zijlstra , Borislav Petkov , Andy Lutomirski , Ingo Molnar CC: , , , Thomas Gleixner , Nadav Amit , Dave Hansen , , , , , , , , , , , Rick Edgecombe , Kees Cook , Dave Hansen , Nadav Amit Subject: [PATCH v5 03/23] x86/mm: Introduce temporary mm structs Date: Thu, 25 Apr 2019 17:11:23 -0700 Message-ID: <20190426001143.4983-4-namit@vmware.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190426001143.4983-1-namit@vmware.com> References: <20190426001143.4983-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-002.vmware.com: namit@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Lutomirski 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 ] Cc: Kees Cook Cc: Dave Hansen Acked-by: Peter Zijlstra (Intel) 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 | 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 */ -- 2.17.1