Skip to content

Commit cefa929

Browse files
amlutoIngo Molnar
authored andcommitted
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 <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> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org> Cc: <akpm@linux-foundation.org> Cc: <ard.biesheuvel@linaro.org> Cc: <deneen.t.dock@intel.com> Cc: <kernel-hardening@lists.openwall.com> Cc: <kristen@linux.intel.com> Cc: <linux_dti@icloud.com> Cc: <will.deacon@arm.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@intel.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Rik van Riel <riel@surriel.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: https://lkml.kernel.org/r/20190426001143.4983-4-namit@vmware.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1fd8de4 commit cefa929

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

arch/x86/include/asm/mmu_context.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,37 @@ static inline unsigned long __get_current_cr3_fast(void)
356356
return cr3;
357357
}
358358

359+
typedef struct {
360+
struct mm_struct *mm;
361+
} temp_mm_state_t;
362+
363+
/*
364+
* Using a temporary mm allows to set temporary mappings that are not accessible
365+
* by other CPUs. Such mappings are needed to perform sensitive memory writes
366+
* that override the kernel memory protections (e.g., W^X), without exposing the
367+
* temporary page-table mappings that are required for these write operations to
368+
* other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the
369+
* mapping is torn down.
370+
*
371+
* Context: The temporary mm needs to be used exclusively by a single core. To
372+
* harden security IRQs must be disabled while the temporary mm is
373+
* loaded, thereby preventing interrupt handler bugs from overriding
374+
* the kernel memory protection.
375+
*/
376+
static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm)
377+
{
378+
temp_mm_state_t temp_state;
379+
380+
lockdep_assert_irqs_disabled();
381+
temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm);
382+
switch_mm_irqs_off(NULL, mm, current);
383+
return temp_state;
384+
}
385+
386+
static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
387+
{
388+
lockdep_assert_irqs_disabled();
389+
switch_mm_irqs_off(NULL, prev_state.mm, current);
390+
}
391+
359392
#endif /* _ASM_X86_MMU_CONTEXT_H */

0 commit comments

Comments
 (0)