gvisor/pkg/sentry/kernel
Nicolas Lacasse f4ce43e1f4 Allow and document bug ids in gVisor codebase.
PiperOrigin-RevId: 245818639
Change-Id: I03703ef0fb9b6675955637b9fe2776204c545789
2019-04-29 14:04:14 -07:00
..
auth Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
contexttest Decouple filemem from platform and move it to pgalloc.MemoryFile. 2019-03-14 08:12:48 -07:00
epoll Use open fids when fstat()ing gofer files. 2019-04-11 00:43:04 -07:00
eventfd Use open fids when fstat()ing gofer files. 2019-04-11 00:43:04 -07:00
fasync Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
futex Priority-inheritance futex implementation 2019-03-05 23:40:18 -08:00
g3doc Use png for the run states diagram 2018-05-02 03:43:41 -07:00
kdefs Remove license comments 2019-01-31 11:12:53 -08:00
memevent Decouple filemem from platform and move it to pgalloc.MemoryFile. 2019-03-14 08:12:48 -07:00
pipe Use open fids when fstat()ing gofer files. 2019-04-11 00:43:04 -07:00
sched Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
semaphore Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
shm Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
time Remove license comments 2019-01-31 11:12:53 -08:00
BUILD Save/restore simple devices. 2019-04-01 15:39:16 -07:00
README.md Format documentation 2018-07-12 10:37:21 -07:00
abstract_socket_namespace.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
context.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
fd_map.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
fd_map_test.go Allow threads with CAP_SYS_RESOURCE to raise hard rlimits. 2019-04-10 12:36:45 -07:00
fs_context.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
ipc_namespace.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
kernel.go Perform explicit CPUID and FP state compatibility checks on restore 2019-04-25 17:47:05 -07:00
kernel_state.go Save/restore simple devices. 2019-04-01 15:39:16 -07:00
pending_signals.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
pending_signals_state.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
posixtimer.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
ptrace.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
ptrace_amd64.go Make some ptrace commands x86-only 2019-02-20 15:10:59 -08:00
ptrace_arm64.go Make some ptrace commands x86-only 2019-02-20 15:10:59 -08:00
rseq.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
seccomp.go Set rax to syscall number on SECCOMP_RET_TRAP. 2019-02-19 15:49:37 -08:00
sessions.go Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
signal.go Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
signal_handlers.go Don't allow writing or reading to TTY unless process group is in foreground. 2019-02-14 15:47:31 -08:00
syscalls.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
syscalls_state.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
syslog.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
table_test.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task.go Use FD limit and file size limit from host 2019-04-17 12:57:40 -07:00
task_acct.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task_block.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task_clone.go gvisor: don't allocate a new credential object on fork 2019-03-20 18:41:00 -07:00
task_context.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_exec.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_exit.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_futex.go Priority-inheritance futex implementation 2019-03-05 23:40:18 -08:00
task_identity.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_log.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task_net.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task_run.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_sched.go Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
task_signals.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_start.go Cache ThreadGroups in PIDNamespace 2019-04-03 16:22:43 -07:00
task_stop.go Allow and document bug ids in gVisor codebase. 2019-04-29 14:04:14 -07:00
task_syscall.go Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
task_test.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
task_usermem.go syscalls: sendfile: limit the count to MAX_RW_COUNT 2019-04-09 14:57:05 -07:00
thread_group.go Export kernel.SignalInfoPriv. 2019-04-08 16:32:11 -07:00
threads.go Remove defer from trivial ThreadID methods 2019-04-04 17:14:27 -07:00
timekeeper.go Decouple filemem from platform and move it to pgalloc.MemoryFile. 2019-03-14 08:12:48 -07:00
timekeeper_state.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
timekeeper_test.go Decouple filemem from platform and move it to pgalloc.MemoryFile. 2019-03-14 08:12:48 -07:00
uncaught_signal.proto Add EventChannel messages for uncaught signals. 2018-12-26 11:26:28 -08:00
uts_namespace.go Use correct company name in copyright header 2018-10-19 16:35:11 -07:00
vdso.go Decouple filemem from platform and move it to pgalloc.MemoryFile. 2019-03-14 08:12:48 -07:00
version.go Create stubs for syscalls upto Linux 4.4. 2018-11-08 11:09:46 -08:00

README.md

This package contains:

  • A (partial) emulation of the "core Linux kernel", which governs task execution and scheduling, system call dispatch, and signal handling. See below for details.

  • The top-level interface for the sentry's Linux kernel emulation in general, used by the main function of all versions of the sentry. This interface revolves around the Env type (defined in kernel.go).

Background

In Linux, each schedulable context is referred to interchangeably as a "task" or "thread". Tasks can be divided into userspace and kernel tasks. In the sentry, scheduling is managed by the Go runtime, so each schedulable context is a goroutine; only "userspace" (application) contexts are referred to as tasks, and represented by Task objects. (From this point forward, "task" refers to the sentry's notion of a task unless otherwise specified.)

At a high level, Linux application threads can be thought of as repeating a "run loop":

  • Some amount of application code is executed in userspace.

  • A trap (explicit syscall invocation, hardware interrupt or exception, etc.) causes control flow to switch to the kernel.

  • Some amount of kernel code is executed in kernelspace, e.g. to handle the cause of the trap.

  • The kernel "returns from the trap" into application code.

Analogously, each task in the sentry is associated with a task goroutine that executes that task's run loop (Task.run in task_run.go). However, the sentry's task run loop differs in structure in order to support saving execution state to, and resuming execution from, checkpoints.

While in kernelspace, a Linux thread can be descheduled (cease execution) in a variety of ways:

  • It can yield or be preempted, becoming temporarily descheduled but still runnable. At present, the sentry delegates scheduling of runnable threads to the Go runtime.

  • It can exit, becoming permanently descheduled. The sentry's equivalent is returning from Task.run, terminating the task goroutine.

  • It can enter interruptible sleep, a state in which it can be woken by a caller-defined wakeup or the receipt of a signal. In the sentry, interruptible sleep (which is ambiguously referred to as blocking) is implemented by making all events that can end blocking (including signal notifications) communicated via Go channels and using select to multiplex wakeup sources; see task_block.go.

  • It can enter uninterruptible sleep, a state in which it can only be woken by a caller-defined wakeup. Killable sleep is a closely related variant in which the task can also be woken by SIGKILL. (These definitions also include Linux's "group-stopped" (TASK_STOPPED) and "ptrace-stopped" (TASK_TRACED) states.)

To maximize compatibility with Linux, sentry checkpointing appears as a spurious signal-delivery interrupt on all tasks; interrupted system calls return EINTR or are automatically restarted as usual. However, these semantics require that uninterruptible and killable sleeps do not appear to be interrupted. In other words, the state of the task, including its progress through the interrupted operation, must be preserved by checkpointing. For many such sleeps, the wakeup condition is application-controlled, making it infeasible to wait for the sleep to end before checkpointing. Instead, we must support checkpointing progress through sleeping operations.

Implementation

We break the task's control flow graph into states, delimited by:

  1. Points where uninterruptible and killable sleeps may occur. For example, there exists a state boundary between signal dequeueing and signal delivery because there may be an intervening ptrace signal-delivery-stop.

  2. Points where sleep-induced branches may "rejoin" normal execution. For example, the syscall exit state exists because it can be reached immediately following a synchronous syscall, or after a task that is sleeping in execve() or vfork() resumes execution.

  3. Points containing large branches. This is strictly for organizational purposes. For example, the state that processes interrupt-signaled conditions is kept separate from the main "app" state to reduce the size of the latter.

  4. SyscallReinvoke, which does not correspond to anything in Linux, and exists solely to serve the autosave feature.

dot -Tpng -Goverlap=false -orun_states.png run_states.dot

States before which a stop may occur are represented as implementations of the taskRunState interface named run(state), allowing them to be saved and restored. States that cannot be immediately preceded by a stop are simply Task methods named do(state).

Conditions that can require task goroutines to cease execution for unknown lengths of time are called stops. Stops are divided into internal stops, which are stops whose start and end conditions are implemented within the sentry, and external stops, which are stops whose start and end conditions are not known to the sentry. Hence all uninterruptible and killable sleeps are internal stops, and the existence of a pending checkpoint operation is an external stop. Internal stops are reified into instances of the TaskStop type, while external stops are merely counted. The task run loop alternates between checking for stops and advancing the task's state. This allows checkpointing to hold tasks in a stopped state while waiting for all tasks in the system to stop.