Skip to content

Commit a31e184

Browse files
hansendcKAGA-KOKO
authored andcommitted
x86/pkeys: Properly copy pkey state at fork()
Memory protection key behavior should be the same in a child as it was in the parent before a fork. But, there is a bug that resets the state in the child at fork instead of preserving it. The creation of new mm's is a bit convoluted. At fork(), the code does: 1. memcpy() the parent mm to initialize child 2. mm_init() to initalize some select stuff stuff 3. dup_mmap() to create true copies that memcpy() did not do right For pkeys two bits of state need to be preserved across a fork: 'execute_only_pkey' and 'pkey_allocation_map'. Those are preserved by the memcpy(), but mm_init() invokes init_new_context() which overwrites 'execute_only_pkey' and 'pkey_allocation_map' with "new" values. The author of the code erroneously believed that init_new_context is *only* called at execve()-time. But, alas, init_new_context() is used at execve() and fork(). The result is that, after a fork(), the child's pkey state ends up looking like it does after an execve(), which is totally wrong. pkeys that are already allocated can be allocated again, for instance. To fix this, add code called by dup_mmap() to copy the pkey state from parent to child explicitly. Also add a comment above init_new_context() to make it more clear to the next poor sod what this code is used for. Fixes: e8c24d3 ("x86/pkeys: Allocation/free syscalls") Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: bp@alien8.de Cc: hpa@zytor.com Cc: peterz@infradead.org Cc: mpe@ellerman.id.au Cc: will.deacon@arm.com Cc: luto@kernel.org Cc: jroedel@suse.de Cc: stable@vger.kernel.org Cc: Borislav Petkov <bp@alien8.de> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Will Deacon <will.deacon@arm.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Joerg Roedel <jroedel@suse.de> Link: https://lkml.kernel.org/r/20190102215655.7A69518C@viggo.jf.intel.com
1 parent 7e6fc2f commit a31e184

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

arch/x86/include/asm/mmu_context.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
178178

179179
void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk);
180180

181+
/*
182+
* Init a new mm. Used on mm copies, like at fork()
183+
* and on mm's that are brand-new, like at execve().
184+
*/
181185
static inline int init_new_context(struct task_struct *tsk,
182186
struct mm_struct *mm)
183187
{
@@ -228,8 +232,22 @@ do { \
228232
} while (0)
229233
#endif
230234

235+
static inline void arch_dup_pkeys(struct mm_struct *oldmm,
236+
struct mm_struct *mm)
237+
{
238+
#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
239+
if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
240+
return;
241+
242+
/* Duplicate the oldmm pkey state in mm: */
243+
mm->context.pkey_allocation_map = oldmm->context.pkey_allocation_map;
244+
mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
245+
#endif
246+
}
247+
231248
static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm)
232249
{
250+
arch_dup_pkeys(oldmm, mm);
233251
paravirt_arch_dup_mmap(oldmm, mm);
234252
return ldt_dup_context(oldmm, mm);
235253
}

0 commit comments

Comments
 (0)