mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 13:55:32 +00:00
![Sean Christopherson](/assets/img/avatar_default.png)
Signals are a horrid little mechanism. They are especially nasty in multi-threaded environments because signal state like handlers is global across the entire process. But, signals are basically the only way that userspace can “gracefully” handle and recover from exceptions. The kernel generally does not like exceptions to occur during execution. But, exceptions are a fact of life and must be handled in some circumstances. The kernel handles them by keeping a list of individual instructions which may cause exceptions. Instead of truly handling the exception and returning to the instruction that caused it, the kernel instead restarts execution at a *different* instruction. This makes it obvious to that thread of execution that the exception occurred and lets *that* code handle the exception instead of the handler. This is not dissimilar to the try/catch exceptions mechanisms that some programming languages have, but applied *very* surgically to single instructions. It effectively changes the visible architecture of the instruction. Problem ======= SGX generates a lot of signals, and the code to enter and exit enclaves and muck with signal handling is truly horrid. At the same time, an approach like kernel exception fixup can not be easily applied to userspace instructions because it changes the visible instruction architecture. Solution ======== The vDSO is a special page of kernel-provided instructions that run in userspace. Any userspace calling into the vDSO knows that it is special. This allows the kernel a place to legitimately rewrite the user/kernel contract and change instruction behavior. Add support for fixing up exceptions that occur while executing in the vDSO. This replaces what could traditionally only be done with signal handling. This new mechanism will be used to replace previously direct use of SGX instructions by userspace. Just introduce the vDSO infrastructure. Later patches will actually replace signal generation with vDSO exception fixup. Suggested-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Borislav Petkov <bp@suse.de> Acked-by: Jethro Beekman <jethro@fortanix.com> Link: https://lkml.kernel.org/r/20201112220135.165028-17-jarkko@kernel.org
46 lines
1.2 KiB
C
46 lines
1.2 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/err.h>
|
|
#include <linux/mm.h>
|
|
#include <asm/current.h>
|
|
#include <asm/traps.h>
|
|
#include <asm/vdso.h>
|
|
|
|
struct vdso_exception_table_entry {
|
|
int insn, fixup;
|
|
};
|
|
|
|
bool fixup_vdso_exception(struct pt_regs *regs, int trapnr,
|
|
unsigned long error_code, unsigned long fault_addr)
|
|
{
|
|
const struct vdso_image *image = current->mm->context.vdso_image;
|
|
const struct vdso_exception_table_entry *extable;
|
|
unsigned int nr_entries, i;
|
|
unsigned long base;
|
|
|
|
/*
|
|
* Do not attempt to fixup #DB or #BP. It's impossible to identify
|
|
* whether or not a #DB/#BP originated from within an SGX enclave and
|
|
* SGX enclaves are currently the only use case for vDSO fixup.
|
|
*/
|
|
if (trapnr == X86_TRAP_DB || trapnr == X86_TRAP_BP)
|
|
return false;
|
|
|
|
if (!current->mm->context.vdso)
|
|
return false;
|
|
|
|
base = (unsigned long)current->mm->context.vdso + image->extable_base;
|
|
nr_entries = image->extable_len / (sizeof(*extable));
|
|
extable = image->extable;
|
|
|
|
for (i = 0; i < nr_entries; i++) {
|
|
if (regs->ip == base + extable[i].insn) {
|
|
regs->ip = base + extable[i].fixup;
|
|
regs->di = trapnr;
|
|
regs->si = error_code;
|
|
regs->dx = fault_addr;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|