gVisor

repository·master·Indexed 12 days ago

https://github.com/google/gvisor

An application kernel written in Go that provides a strong isolation layer between applications and the host OS. It implements a Linux-like interface in userspace and provides a secure sandbox for containers via the runsc OCI runtime, utilizing components like Sentry for system call handling and Gofer for file system proxying.

Tokens
143.9K
Snippets
381
Records
633
Agent score
97%

What's inside gVisor

  1. What is gVisor?

    master

    gVisor is an application kernel that provides strong isolation between running applications and the host operating system. It implements a Linux-like interface in a memory-safe language (Go) and runs in userspace.

    Key components:

    • runsc: An Open Container Initiative (OCI) runtime that allows gVisor to integrate with existing container tooling like Docker and Kubernetes.
    • Isolation Model: Unlike syscall filters (seccomp) or VMs (QEMU), gVisor acts as a third approach, providing VM-like security benefits with a lower resource footprint and faster startup times by running as a normal userspace process.
  2. Overview of the Nvidia Driver Differ Tool

    master
    The Nvidia Driver Differ Tool is a utility designed to automate the process of supporting new Nvidia driver versions within nvproxy. When a new driver version introduces changes to ioctl structs, nvproxy must be updated to match. This tool automates the identification of differences in struct definitions (type, number, and ordering of fields) between a supported ancestor version and a new target version.
  3. Overview of gVisor Python Bindings

    master

    The gVisor Python bindings provide a programmable interface to interact with gVisor.

    gVisor is an application kernel written in Go that implements a significant portion of the Linux system surface. It includes the runsc OCI runtime, which establishes an isolation boundary between applications and the host kernel, providing strong sandbox isolation while maintaining standard application behavior.

  4. What is the Ioctl Sniffer?

    master

    The ioctl_sniffer tool is used to profile GPU workloads by intercepting Nvidia ioctl(2) calls. It identifies which calls are currently unsupported by nvproxy.

    It works by using a shared library, libioctl_hook.so, which is injected into the workload via LD_PRELOAD. The tool intercepts calls to known Nvidia device files and compares them against nvproxy's supported ioctl(2) numbers for the specific driver version. For specific calls like NV_ESC_RM_CONTROL and NV_ESC_RM_ALLOC, it also extracts and validates the control command and allocation class.

  5. Embed precompiled seccomp-bpf programs in Go binaries

    master

    The precompiledseccomp package provides build tooling to embed precompiled seccomp-bpf programs directly into Go binaries. This is primarily used within gVisor to minimize startup latency.

    It supports runtime-modifiable uint32 variables within the embedded programs. This allows you to use values that are only known at runtime (such as file descriptor numbers) within your seccomp filtering rules.

  6. Use go_marshal for high-performance data marshalling

    master

    The go_marshal utility is a code generation tool designed to marshal Go data structures to and from memory. It improves performance over binary.Write or binary.Marshal by moving expensive reflection operations from runtime to compile-time.

    It automatically generates implementations for the marshal.Marshallable interface. To trigger code generation for a struct, add the // +marshal comment at the struct level.

  7. What is gVisor and how does it provide isolation?

    master

    gVisor is an open-source workload isolation solution designed to safely run untrusted code, containers, and applications. Unlike traditional isolation methods, gVisor acts as an application kernel that runs in userspace.

    Core Architecture

    • gVisor Sentry: The kernel-like component that intercepts and handles system calls and page faults from the sandboxed workload. It is a Go-based reimplementation of the Linux system call interface, memory management, filesystems, network stack, and more.
    • Gofer: A sidecar process that acts as a slightly-more-trusted companion running in a slightly-more-privileged context to handle requests that the Sentry cannot service within its restricted environment.

    Key Security Properties

    • No System Call Passthrough: The Sentry never passes a sandboxed system call directly to the host kernel. If a feature is not reimplemented in gVisor, the workload cannot use it.
    • Defense-in-Depth: While gVisor uses Linux kernel security primitives (like seccomp-bpf, namespaces, and cgroups), it uses them to protect the Sentry itself from the host, rather than using them as the primary way to sandbox the workload.
    • Memory Safety: The Sentry is written in Go, which eliminates many common memory-safety vulnerabilities found in traditional guest kernels.
  8. What is bufferv2 and how does it work?

    master

    In gVisor's Netstack, bufferv2 is a high-performance, non-contiguous, reference-counted, pooled, and copy-on-write buffer abstraction. It was designed to replace standard Go byte slice allocations to reduce Garbage Collection (GC) pressure in networking workloads.

    Internal Structure

    A Buffer is implemented as a linked list of Views. Each View contains start/end indices and points to a Chunk. A Chunk is a reference-counted structure allocated from a tiered pool of byte slices.

    Key Features

    • Zero-cost copies & Copy-on-Write (CoW): Cloning a Buffer only increments the reference count of the underlying chunks. If a buffer is modified, only the specific chunk being changed is cloned, rather than the entire buffer.
    • Fast Transformations: Operations like truncating, merging, appending, or prepending views are efficient because they primarily involve linked-list manipulations or index changes.
    • Tiered Pools: To handle varying packet sizes without excessive memory waste or constant reallocations, bufferv2 uses multiple pools with different chunk sizes (starting at 64 bytes and doubling up to 64k).
  9. What is the checkpoint gofer?

    master

    The checkpoint gofer is a specialized binary used to save and restore checkpoint files stored in Google Cloud Storage (GCS).

    It is implemented as a separate binary from the main runsc binary to prevent dependency conflicts. Specifically, separating it avoids pulling the net/http package into the main runsc binary, which would otherwise cause netpoll to fail in fsgofers because it cannot locate /etc/hosts.

  10. What is the LInux SAndbox FileSystem (LISAFS) Protocol?

    master

    LISAFS (LInux SAndbox FileSystem) is a protocol designed for sandboxed (untrusted) applications to communicate with a trusted RPC server. It allows an untrusted client to perform filesystem operations on the server via RPC calls.

    Unlike traditional path-based APIs (like standard Linux syscalls) which are susceptible to symlink-based attacks because they require re-walking paths on the server, LISAFS uses a file-descriptor (FD) based approach. Operations are performed directly on FD abstractions provided by the protocol, which eliminates the need for the server to perform re-walks and improves security against malicious symlinks.

  11. What is Directfs and how does it work in runsc?

    master

    Directfs is a filesystem access mode in runsc that provides gVisor's application kernel (the Sentry) with secure, direct access to the container filesystem.

    How it works

    Historically, gVisor used a "gofer" (a trusted filesystem proxy) to handle all filesystem operations via RPCs. This added significant overhead.

    With Directfs, the gofer still exists but acts as a setup process rather than a proxy for every operation:

    1. The gofer enters a new mount namespace and sets up appropriate bind mounts to create the container filesystem.
    2. The gofer uses pivot_root(2) to enter that directory.
    3. The sandbox (Sentry) enters its own user and mount namespaces and pivot_root(2)s into an empty directory to prevent path traversal escapes.
    4. Instead of making RPCs, the gofer sends file descriptors (FDs) for all mount points to the sandbox using SCM_RIGHTS messages.
    5. The sandbox then performs filesystem operations directly using file-descriptor-relative syscalls (e.g., fstatat(2), openat(2), mkdirat(2)).

    Security Model

    Directfs relies on Linux filesystem isolation primitives (mount namespaces, pivot_root, and detached bind mounts) rather than just syscall filtering. The sandbox is restricted to the FDs provided by the gofer and cannot walk backwards (using ..) or follow malicious symlinks to escape the container filesystem.