From: Nadav Amit <nadav.amit@gmail.com>
To: Andy Lutomirski <luto@kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe@intel.com>,
Ingo Molnar <mingo@redhat.com>,
LKML <linux-kernel@vger.kernel.org>, X86 ML <x86@kernel.org>,
"H. Peter Anvin" <hpa@zytor.com>,
Thomas Gleixner <tglx@linutronix.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Damian Tometzki <linux_dti@icloud.com>,
linux-integrity <linux-integrity@vger.kernel.org>,
LSM List <linux-security-module@vger.kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Kernel Hardening <kernel-hardening@lists.openwall.com>,
Linux-MM <linux-mm@kvack.org>, Will Deacon <will.deacon@arm.com>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Kristen Carlson Accardi <kristen@linux.intel.com>,
"Dock, Deneen T" <deneen.t.dock@intel.com>,
Kees Cook <keescook@chromium.org>,
Dave Hansen <dave.hansen@intel.com>,
Borislav Petkov <bp@alien8.de>
Subject: Re: [PATCH v4 03/23] x86/mm: Introduce temporary mm structs
Date: Thu, 25 Apr 2019 10:37:35 -0700 [thread overview]
Message-ID: <B7809434-CEBE-4664-ACE7-BA2412163CC4@gmail.com> (raw)
In-Reply-To: <20190425162620.GA5199@zn.tnic>
> On Apr 25, 2019, at 9:26 AM, Borislav Petkov <bp@alien8.de> wrote:
>
> On Mon, Apr 22, 2019 at 11:57:45AM -0700, Rick Edgecombe wrote:
>> From: Andy Lutomirski <luto@kernel.org>
>>
>> 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 <keescook@chromium.org>
>> Cc: Dave Hansen <dave.hansen@intel.com>
>> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
>> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
>> Tested-by: Masami Hiramatsu <mhiramat@kernel.org>
>> Signed-off-by: Andy Lutomirski <luto@kernel.org>
>> Signed-off-by: Nadav Amit <namit@vmware.com>
>> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
>> ---
>> 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..d684b954f3c0 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 *prev;
>> +} temp_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
>
> s/cores/CPUs/g
>
> Yeah, the concept of a thread of execution we call a CPU in the kernel,
> I'd say. No matter if it is one of the hyperthreads or a single thread
> in core.
>
>> + * 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.
>
> Ditto.
>
>> Using temporary mm also allows to avoid TLB shootdowns when the
>
> Using a ..
>
>> + * mapping is torn down.
>> + *
>
> Nice commenting.
>
>> + * 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 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(temp_mm_state_t prev)
>> +{
>> + lockdep_assert_irqs_disabled();
>> + switch_mm_irqs_off(NULL, prev.prev, current);
>
> I think this code would be more readable if you call that
> temp_mm_state_t variable "temp_state" and the mm_struct pointer "mm" and
> then you have:
>
> switch_mm_irqs_off(NULL, temp_state.mm, current);
>
> And above you'll have:
>
> temp_state.mm = ...
Andy, please let me know whether you are fine with this change and I’ll
incorporate it.
next prev parent reply other threads:[~2019-04-25 17:37 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-22 18:57 [PATCH v4 00/23] Merge text_poke fixes and executable lockdowns Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 01/23] Fix "x86/alternatives: Lockdep-enforce text_mutex in text_poke*()" Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 02/23] x86/jump_label: Use text_poke_early() during early init Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 03/23] x86/mm: Introduce temporary mm structs Rick Edgecombe
2019-04-25 16:26 ` Borislav Petkov
2019-04-25 17:37 ` Nadav Amit [this message]
2019-04-25 17:49 ` Andy Lutomirski
2019-04-22 18:57 ` [PATCH v4 04/23] x86/mm: Save DRs when loading a temporary mm Rick Edgecombe
2019-04-25 16:36 ` Borislav Petkov
2019-04-25 18:17 ` Peter Zijlstra
2019-04-22 18:57 ` [PATCH v4 05/23] fork: Provide a function for copying init_mm Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 06/23] x86/alternative: Initialize temporary mm for patching Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 07/23] x86/alternative: Use temporary mm for text poking Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 08/23] x86/kgdb: Avoid redundant comparison of patched code Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 09/23] x86/ftrace: Set trampoline pages as executable Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 10/23] x86/kprobes: Set instruction page " Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 11/23] x86/module: Avoid breaking W^X while loading modules Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 12/23] x86/jump-label: Remove support for custom poker Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 13/23] x86/alternative: Remove the return value of text_poke_*() Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 14/23] x86/mm/cpa: Add set_direct_map_ functions Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 15/23] mm: Make hibernate handle unmapped pages Rick Edgecombe
2019-04-22 18:57 ` [PATCH v4 16/23] vmalloc: Add flag for free of special permsissions Rick Edgecombe
2019-04-25 20:38 ` Peter Zijlstra
2019-04-25 21:22 ` Edgecombe, Rick P
2019-04-22 18:57 ` [PATCH v4 17/23] modules: Use vmalloc special flag Rick Edgecombe
2019-04-22 18:58 ` [PATCH v4 18/23] bpf: " Rick Edgecombe
2019-04-22 18:58 ` [PATCH v4 19/23] x86/ftrace: " Rick Edgecombe
2019-04-25 18:28 ` Steven Rostedt
2019-04-25 19:19 ` Edgecombe, Rick P
2019-04-22 18:58 ` [PATCH v4 20/23] x86/kprobes: " Rick Edgecombe
2019-04-22 18:58 ` [PATCH v4 21/23] x86/alternative: Comment about module removal races Rick Edgecombe
2019-04-22 18:58 ` [PATCH v4 22/23] tlb: provide default nmi_uaccess_okay() Rick Edgecombe
2019-04-22 18:58 ` [PATCH v4 23/23] bpf: Fail bpf_probe_write_user() while mm is switched Rick Edgecombe
2019-04-25 20:48 ` [PATCH v4 00/23] Merge text_poke fixes and executable lockdowns Peter Zijlstra
2019-04-25 20:49 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=B7809434-CEBE-4664-ACE7-BA2412163CC4@gmail.com \
--to=nadav.amit@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=ard.biesheuvel@linaro.org \
--cc=bp@alien8.de \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=deneen.t.dock@intel.com \
--cc=hpa@zytor.com \
--cc=keescook@chromium.org \
--cc=kernel-hardening@lists.openwall.com \
--cc=kristen@linux.intel.com \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux_dti@icloud.com \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@linutronix.de \
--cc=will.deacon@arm.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).