x86/pkeys: properly copy pkey state at fork()
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Thomas Gleixner
Reviewed-byQuoted 30 lines · Dave Hansen
From: Dave Hansen <[email protected]>
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.
Our 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 thatmemcpy()did not do right.For pkeys, we need to preserve two bits of state across a fork:
'execute_only_pkey' and 'pkey_allocation_map'. Those are preserved by thememcpy(), which I thought did the right thing. But,mm_init()callsinit_new_context(), which I thought was *only* for execve()-time and overwrites 'execute_only_pkey' and 'pkey_allocation_map' with "new" values. But, alas,init_new_context()is used atexecve()andfork().The result is that, after a
fork(), the child's pkey state ends up looking like it does after anexecve(), 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 aboveinit_new_context()to make it more clear to the next poor sod what this code is used for.
Quoted 14 lines
Fixes e8c24d3a23a ("x86/pkeys: Allocation/free syscalls")Signed-off-by Dave Hansen <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Dave Hansen <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Michael Ellerman <[email protected]>Cc Will Deacon <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Joerg Roedel <[email protected]>