The patch titled Subject: selftests/vm/pkeys: refill shadow register after implicit kernel write has been added to the -mm tree. Its filename is selftests-vm-pkeys-refill-shadow-register-after-implicit-kernel-write.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/selftests-vm-pkeys-refill-shadow-register-after-implicit-kernel-write.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/selftests-vm-pkeys-refill-shadow-register-after-implicit-kernel-write.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Subject: selftests/vm/pkeys: refill shadow register after implicit kernel write The pkey test code keeps a "shadow" of the pkey register around. This ensures that any bugs which might write to the register can be caught more quickly. Generally, userspace has a good idea when the kernel is going to write to the register. For instance, alloc_pkey() is passed a permission mask. The caller of alloc_pkey() can update the shadow based on the return value and the mask. But, the kernel can also modify the pkey register in a more sneaky way. For mprotect(PROT_EXEC) mappings, the kernel will allocate a pkey and write the pkey register to create an execute-only mapping. The kernel never tells userspace what key it uses for this. This can cause the test to fail with messages like: protection_keys_64.2: pkey-helpers.h:132: _read_pkey_reg: Assertion `pkey_reg == shadow_pkey_reg' failed. because the shadow was not updated with the new kernel-set value. Forcibly update the shadow value immediately after an mprotect(). Link: https://lkml.kernel.org/r/20210611164200.EF76AB73@xxxxxxxxxxxxxxxxxx Fixes: 6af17cf89e99 ("x86/pkeys/selftests: Add PROT_EXEC test") Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Ram Pai <linuxram@xxxxxxxxxx> Cc: Sandipan Das <sandipan@xxxxxxxxxxxxx> Cc: Florian Weimer <fweimer@xxxxxxxxxx> Cc: "Desnes A. Nunes do Rosario" <desnesn@xxxxxxxxxxxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: Thiago Jung Bauermann <bauerman@xxxxxxxxxxxxx> Cc: "Aneesh Kumar K.V" <aneesh.kumar@xxxxxxxxxxxxx> Cc: Michael Ellerman <mpe@xxxxxxxxxxxxxx> Cc: Michal Hocko <mhocko@xxxxxxxxxx> Cc: Michal Suchanek <msuchanek@xxxxxxx> Cc: Shuah Khan <shuah@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- tools/testing/selftests/vm/protection_keys.c | 7 +++++++ 1 file changed, 7 insertions(+) --- a/tools/testing/selftests/vm/protection_keys.c~selftests-vm-pkeys-refill-shadow-register-after-implicit-kernel-write +++ a/tools/testing/selftests/vm/protection_keys.c @@ -1448,6 +1448,13 @@ void test_implicit_mprotect_exec_only_me ret = mprotect(p1, PAGE_SIZE, PROT_EXEC); pkey_assert(!ret); + /* + * Reset the shadow, assuming that the above mprotect() + * correctly changed PKRU, but to an unknown value since + * the actual alllocated pkey is unknown. + */ + shadow_pkey_reg = __read_pkey_reg(); + dprintf2("pkey_reg: %016llx\n", read_pkey_reg()); /* Make sure this is an *instruction* fault */ _ Patches currently in -mm which might be from dave.hansen@xxxxxxxxxxxxxxx are selftests-vm-pkeys-fix-alloc_random_pkey-to-make-it-really-really-random.patch selftests-vm-pkeys-handle-negative-sys_pkey_alloc-return-code.patch selftests-vm-pkeys-refill-shadow-register-after-implicit-kernel-write.patch selftests-vm-pkeys-exercise-x86-xsave-init-state.patch