freebsd-skq/sys/i386/include
Konstantin Belousov 5efe338f3d Fix handling of the segment registers on i386.
Suppose that userspace is executing with the non-standard segment
descriptors.  Then, until exception or interrupt handler executed
SET_KERNEL_SEGS, kernel is still executing with user %ds, %es and %fs.
If an interrupt occurs in this window, the interrupt handler is
executed unsafely, relying on usability of the usermode registers.  If
the interrupt results in the context switch on return, the
contamination of the kernel state spreads to the thread we switched
to.  As result, kernel data accesses might fault or, if only the base
is changed, completely messed up.

More, if the user segment was allocated in LDT, another thread might
mark the descriptor as invalid before doreti code tried to reload
them.  In this case kernel panics.

The issue exists for all exception entry points which use trap gate,
and thus do not automatically disable interrupts on entry, and for
lcall_handler.

Fix is two-fold: first, we need to disable interrupts for all kernel
entries, changing the IDT descriptor types from trap gate to interrupt
gate.  Interrupts are re-enabled not earlier than the kernel segments
are loaded into the segment registers.  Second, we only load the
segment registers from the trap frame when returning to usermode.  For
the later, all interrupt return paths must happen through the doreti
common code.

There is no way to disable interrupts on call gate, which is the
supposed mode of servicing for lcall $7,$0 syscalls.  Change the LDT
descriptor 0 into a code segment type and point it to the userspace
trampoline which redirects the syscall to int $0x80.

All the measures make the segment register handling similar to that of
amd64.  We do not apply amd64 optimizations of not reloading segment
registers on return from the syscall.

Reported by:	Maxime Villard <max@m00nbsd.net>
Tested by:	pho (the non-lcall part)
Reviewed by:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D12402
2017-09-18 20:22:42 +00:00
..
pc Add more UEFI/e820 memory types from latest specifications. 2016-07-24 09:15:11 +00:00
xen xen/i386: enable the platform hypercall for i386 2016-05-03 08:05:14 +00:00
_align.h
_bus.h
_inttypes.h
_limits.h
_stdint.h
_types.h
acpica_machdep.h
apm_bios.h
apm_segments.h
asm.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
asmacros.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
atomic.h - Remove 'struct vmmeter' from 'struct pcpu', leaving only global vmmeter 2017-04-17 17:34:47 +00:00
bootinfo.h
bus_dma.h Clean up MD pollution of bus_dma.h: 2017-07-01 05:35:29 +00:00
bus.h
clock.h
counter.h - Remove 'struct vmmeter' from 'struct pcpu', leaving only global vmmeter 2017-04-17 17:34:47 +00:00
cpu.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
cpufunc.h x86: Tag some intrinsics with __pure2 2017-08-03 22:28:30 +00:00
cputypes.h Remove 'cpu' and 'cpu_class' on amd64. 2016-09-15 17:05:54 +00:00
cserial.h Fix improper use of "its". 2016-11-08 23:59:41 +00:00
db_machdep.h Fix printing of negative offsets (typically from frame pointers) again. 2017-03-26 18:46:35 +00:00
dump.h
efi.h Fix building on i386 and arm. But 'public domain' headers on the files 2016-10-13 06:56:23 +00:00
elan_mmcr.h
elf.h
endian.h
exec.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
fdt.h
float.h
floatingpoint.h
frame.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
gdb_machdep.h
ieeefp.h
if_wl_wavelan.h
in_cksum.h
intr_machdep.h x86: Add dynamic interrupt rebalancing 2017-08-16 18:48:53 +00:00
ioctl_bt848.h
ioctl_meteor.h
iodev.h
kdb.h
limits.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
md_var.h Fix handling of the segment registers on i386. 2017-09-18 20:22:42 +00:00
memdev.h
metadata.h Move amd64 metadata.h to x86 and share with i386 2016-01-07 19:47:26 +00:00
minidump.h
mp_watchdog.h
nexusvar.h
npx.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
ofw_machdep.h
param.h Drop CACHE_LINE_SIZE to 64 bytes on x86 2017-08-28 22:28:41 +00:00
pcaudioio.h
pcb_ext.h
pcb.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
pci_cfgreg.h
pcpu.h - Remove 'struct vmmeter' from 'struct pcpu', leaving only global vmmeter 2017-04-17 17:34:47 +00:00
perfmon.h
pmap.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
pmc_mdep.h
ppireg.h
proc.h Make struct syscall_args visible to userspace compilation environment 2017-06-12 20:53:44 +00:00
profile.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
psl.h
ptrace.h
pvclock.h
reg.h
reloc.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
resource.h
runq.h
segments.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
setjmp.h
sf_buf.h
sigframe.h
signal.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
smapi.h
smp.h
speaker.h
specialreg.h
stack.h
stdarg.h
sysarch.h
timerreg.h
trap.h
tss.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
ucontext.h
varargs.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
vdso.h
vm86.h
vm.h
vmparam.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
xbox.h