bianbu-linux-6.6/arch/x86
Aruna Ramakrishna 1905912820 x86/pkeys: Add PKRU as a parameter in signal handling functions
[ Upstream commit 24cf2bc982ffe02aeffb4a3885c71751a2c7023b ]

Assume there's a multithreaded application that runs untrusted user
code. Each thread has its stack/code protected by a non-zero PKEY, and the
PKRU register is set up such that only that particular non-zero PKEY is
enabled. Each thread also sets up an alternate signal stack to handle
signals, which is protected by PKEY zero. The PKEYs man page documents that
the PKRU will be reset to init_pkru when the signal handler is invoked,
which means that PKEY zero access will be enabled.  But this reset happens
after the kernel attempts to push fpu state to the alternate stack, which
is not (yet) accessible by the kernel, which leads to a new SIGSEGV being
sent to the application, terminating it.

Enabling both the non-zero PKEY (for the thread) and PKEY zero in
userspace will not work for this use case. It cannot have the alt stack
writeable by all - the rationale here is that the code running in that
thread (using a non-zero PKEY) is untrusted and should not have access
to the alternate signal stack (that uses PKEY zero), to prevent the
return address of a function from being changed. The expectation is that
kernel should be able to set up the alternate signal stack and deliver
the signal to the application even if PKEY zero is explicitly disabled
by the application. The signal handler accessibility should not be
dictated by whatever PKRU value the thread sets up.

The PKRU register is managed by XSAVE, which means the sigframe contents
must match the register contents - which is not the case here. It's
required that the signal frame contains the user-defined PKRU value (so
that it is restored correctly from sigcontext) but the actual register must
be reset to init_pkru so that the alt stack is accessible and the signal
can be delivered to the application. It seems that the proper fix here
would be to remove PKRU from the XSAVE framework and manage it separately,
which is quite complicated. As a workaround, do this:

        orig_pkru = rdpkru();
        wrpkru(orig_pkru & init_pkru_value);
        xsave_to_user_sigframe();
        put_user(pkru_sigframe_addr, orig_pkru)

In preparation for writing PKRU to sigframe, pass PKRU as an additional
parameter down the call chain from get_sigframe().

No functional change.

Signed-off-by: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/all/20240802061318.2140081-2-aruna.ramakrishna@oracle.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-10-10 11:57:29 +02:00
..
boot x86/boot: Don't add the EFI stub to targets, again 2024-06-21 14:38:37 +02:00
coco x86/tdx: Fix "in-kernel MMIO" check 2024-10-04 16:30:05 +02:00
configs - The first, cleanup part of the microcode loader reorg tglx has been 2023-08-28 15:55:20 -07:00
crypto crypto: x86/sha256 - Add parentheses around macros' single arguments 2024-10-10 11:57:21 +02:00
entry x86/bhi: Avoid warning in #DB handler due to BHI mitigation 2024-07-18 13:21:27 +02:00
events perf/x86/intel/pt: Fix sampling synchronization 2024-10-04 16:29:52 +02:00
hyperv x86/hyperv: fix kexec crash due to VP assist page corruption 2024-09-18 19:24:07 +02:00
ia32
include x86/pkeys: Add PKRU as a parameter in signal handling functions 2024-10-10 11:57:29 +02:00
kernel x86/pkeys: Add PKRU as a parameter in signal handling functions 2024-10-10 11:57:29 +02:00
kvm KVM: x86: Move x2APIC ICR helper above kvm_apic_write_nodecode() 2024-10-04 16:29:47 +02:00
lib x86/kmsan: Fix hook for unaligned accesses 2024-09-12 11:11:29 +02:00
math-emu x86/fpu: Include asm/fpu/regset.h 2023-05-18 11:56:18 -07:00
mm x86/mm: Use IPIs to synchronize LAM enablement 2024-10-04 16:29:04 +02:00
net Revert "bpf: Take return from set_memory_rox() into account with bpf_jit_binary_lock_ro()" 2024-07-09 11:44:29 +02:00
pci x86/PCI: Check pcie_find_root_port() return for NULL 2024-10-04 16:29:29 +02:00
platform x86/platform/iosf_mbi: Convert PCIBIOS_* return codes to errnos 2024-08-03 08:53:21 +02:00
power mm/treewide: replace pud_large() with pud_leaf() 2024-04-10 16:35:46 +02:00
purgatory x86/purgatory: Switch to the position-independent small code model 2024-06-12 11:11:43 +02:00
ras
realmode x86/realmode: Make stack lock work in trampoline_compat() 2023-05-30 14:11:47 +02:00
tools x86/boot: Ignore relocations in .notes sections in walk_relocs() too 2024-06-12 11:11:36 +02:00
um um: Fix the -Wmissing-prototypes warning for get_thread_reg 2024-06-12 11:12:42 +02:00
video Merge drm/drm-next into drm-misc-next 2023-07-24 15:44:47 +02:00
virt/vmx/tdx
xen xen: allow mapping ACPI data using a different physical address 2024-10-04 16:29:43 +02:00
.gitignore
Kbuild
Kconfig cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n 2024-06-12 11:11:24 +02:00
Kconfig.assembler x86/kconfig: Add as-instr64 macro to properly evaluate AS_WRUSS 2024-08-03 08:53:21 +02:00
Kconfig.cpu x86/Kconfig: Transmeta Crusoe is CPU family 5, not 6 2024-02-23 09:25:10 +01:00
Kconfig.debug x86/kconfig: Select ARCH_WANT_FRAME_POINTERS again when UNWINDER_FRAME_POINTER=y 2024-06-12 11:12:46 +02:00
Makefile kbuild: unify vdso_install rules 2024-06-12 11:12:32 +02:00
Makefile.postlink x86/build: Avoid relocation information in final vmlinux 2023-06-14 19:54:40 +02:00
Makefile.um um: Only disable SSE on clang to work around old GCC bugs 2023-04-04 09:57:05 +02:00
Makefile_32.cpu