libkrun is a dynamic library that allows programs to easily acquire the ability to run processes in a partially isolated environment using KVM Virtualization on Linux and HVF on macOS/ARM64.
It integrates a VMM (Virtual Machine Monitor, the userspace side of an Hypervisor) with the minimum amount of emulated devices required to its purpose, abstracting most of the complexity that comes from Virtual Machine management, offering users a simple C API.
[!NOTE] The
mainbranch is now libkrun 2.0, which will not be backwards compatible with the 1.x API/ABI. The 2.0 API is also still under active development and may change further before the first stable release. If you are building from source for production use, please use the neweststable-*release branch instead.
This project provides the following variants of the library:
Each variant generates a dynamic library with a different name (and soname), so both can be installed at the same time in the same system.
In libkrun, networking is provided by two different, mutually exclusive techniques: virtio-vsock + TSI and virtio-net + passt/gvproxy.
This is a novel technique called Transparent Socket Impersonation which allows the VM to have network connectivity without a virtual interface. This technique supports both outgoing and incoming connections. It's possible for userspace applications running in the VM to transparently connect to endpoints outside the VM and receive connections from the outside to ports listening inside the VM.
TSI for AF_INET and AF_INET6 is automatically enabled when no network interface is added to the VM. TSI for AF_UNIX is enabled when, in addition to the previous condition, the root filesystem has been configured with / as the shared directory.
A conventional virtual interface that allows the guest to communicate with the outside through the VMM using a supporting application like passt or gvproxy.
Use krun_add_net_unixstream and/or krun_add_net_unixdgram to add a virtio-net interface connected to the userspace network proxy.
The libkrun security model is primarily defined by the consideration that both the guest and the VMM pertain to the same security context. For many operations, the VMM acts as a proxy for the guest within the host. Host resources that are accessible to the VMM can potentially be accessed by the guest through it.
While defining the security implementation of your environment, you should think about the guest and the VMM as a single entity. To prevent the guest from accessing host's resources, you need to use the host's OS security features to run the VMM inside an isolated context. On Linux, the primary mechanism to be used for this purpose is namespaces. Single-user systems may have a more relaxed security policy and just ensure the VMM runs with a particular UID/GID.
While most virtio devices allow the guest to access resources from the host, two of them require special consideration when used: virtio-fs and virtio-vsock+TSI.
When exposing a directory in a filesystem from the host to the guest through virtio-fs devices configured with krun_add_virtiofs*, libkrun does not provide any protection against the guest attempting to access other directories in the same filesystem, or even other filesystems in the host.
A mount point isolation mechanism from the host should be used in combination with virtio-fs.
In addition, when using virtio-fs, a guest may exhaust filesystem resources such as inode limits and disk capacity. Controls should be implemented on the host to mitigate this.
When TSI is enabled, the VMM acts as a proxy for AF_INET, AF_INET6 and AF_UNIX sockets, for both incoming and outgoing connections. For all that matters, the VMM and the guest should be considered to be running in the network context. As such, you should apply on the VMM whatever restrictions you want to apply on the guest.
glibc-static in Fedora)make [FEATURE_OPTIONS]
sudo make [FEATURE_OPTIONS] install
libkrunfw-sev.so library.glibc-static in Fedora)openssl-devel in Fedora).make SEV=1
sudo make SEV=1 install
libkrunfw-tdx.so library.glibc-static in Fedora)openssl-devel in Fedora).make TDX=1
sudo make TDX=1 install
The TDX flavor of libkrun only supports guests with 1 vCPU and memory less than or equal to 3072mib.
lld and xzmake [FEATURE_OPTIONS]
The init binary is cross-compiled using clang and lld. A suitable sysroot is automatically generated by the Makefile from Debian repository.
sudo make [FEATURE_OPTIONS] install
Despite being written in Rust, this library provides a simple C API defined in include/libkrun.h
This is a simple example providing chroot-like functionality using libkrun.
To be able to chroot_vm, you need need to build libkrun with the virtio-block and virtio-net optional features:
make BLK=1 NET=1
sudo make BLK=1 NET=1 install
cd examples
make
To be able to chroot_vm, you need first a directory to act as the root filesystem for your isolated program.
Use the rootfs target to get a rootfs prepared from the Fedora container image (note: you must have podman installed):
make rootfs
Now you can use chroot_vm to run a process within this new root filesystem:
./chroot_vm ./rootfs_fedora /bin/sh
If the libkrun and/or libkrunfw libraries were installed on a path that's not included in your /etc/ld.so.conf configuration, you may get an error like this one:
./chroot_vm: error while loading shared libraries: libkrun.so: cannot open shared object file: No such file or directory
To avoid this problem, use the LD_LIBRARY_PATH environment variable to point to the location where the libraries were installed. For example, if the libraries were installed in /usr/local/lib64, use something like this:
LD_LIBRARY_PATH=/usr/local/lib64 ./chroot_vm rootfs_fedora/ /bin/sh
See CONTRIBUTING.md for guidelines on submitting changes.
libkrun has achieved maturity and starting version 1.0.0 the public API is guaranteed to be stable, following SemVer.
If you think you've identified a security issue in the project, please DO NOT report the issue publicly via the GitHub issue tracker or Matrix. Instead, send an email with as many details as possible to libkrun-security@redhat.com. This is a private mailing list for the core maintainers.
The main communication channel is the libkrun Matrix channel.
libkrun incorporates code from Firecracker, rust-vmm and Cloud-Hypervisor.
(top 30 of 70)
Rust
95.7%
C
3.6%
libkrun is a dynamic library that allows programs to easily acquire the ability to run processes in a partially isolated environment using KVM Virtualization on Linux and HVF on macOS/ARM64.
It integrates a VMM (Virtual Machine Monitor, the userspace side of an Hypervisor) with the minimum amount of emulated devices required to its purpose, abstracting most of the complexity that comes from Virtual Machine management, offering users a simple C API.
[!NOTE] The
mainbranch is now libkrun 2.0, which will not be backwards compatible with the 1.x API/ABI. The 2.0 API is also still under active development and may change further before the first stable release. If you are building from source for production use, please use the neweststable-*release branch instead.
This project provides the following variants of the library:
Each variant generates a dynamic library with a different name (and soname), so both can be installed at the same time in the same system.
In libkrun, networking is provided by two different, mutually exclusive techniques: virtio-vsock + TSI and virtio-net + passt/gvproxy.
This is a novel technique called Transparent Socket Impersonation which allows the VM to have network connectivity without a virtual interface. This technique supports both outgoing and incoming connections. It's possible for userspace applications running in the VM to transparently connect to endpoints outside the VM and receive connections from the outside to ports listening inside the VM.
TSI for AF_INET and AF_INET6 is automatically enabled when no network interface is added to the VM. TSI for AF_UNIX is enabled when, in addition to the previous condition, the root filesystem has been configured with / as the shared directory.
A conventional virtual interface that allows the guest to communicate with the outside through the VMM using a supporting application like passt or gvproxy.
Use krun_add_net_unixstream and/or krun_add_net_unixdgram to add a virtio-net interface connected to the userspace network proxy.
The libkrun security model is primarily defined by the consideration that both the guest and the VMM pertain to the same security context. For many operations, the VMM acts as a proxy for the guest within the host. Host resources that are accessible to the VMM can potentially be accessed by the guest through it.
While defining the security implementation of your environment, you should think about the guest and the VMM as a single entity. To prevent the guest from accessing host's resources, you need to use the host's OS security features to run the VMM inside an isolated context. On Linux, the primary mechanism to be used for this purpose is namespaces. Single-user systems may have a more relaxed security policy and just ensure the VMM runs with a particular UID/GID.
While most virtio devices allow the guest to access resources from the host, two of them require special consideration when used: virtio-fs and virtio-vsock+TSI.
When exposing a directory in a filesystem from the host to the guest through virtio-fs devices configured with krun_add_virtiofs*, libkrun does not provide any protection against the guest attempting to access other directories in the same filesystem, or even other filesystems in the host.
A mount point isolation mechanism from the host should be used in combination with virtio-fs.
In addition, when using virtio-fs, a guest may exhaust filesystem resources such as inode limits and disk capacity. Controls should be implemented on the host to mitigate this.
When TSI is enabled, the VMM acts as a proxy for AF_INET, AF_INET6 and AF_UNIX sockets, for both incoming and outgoing connections. For all that matters, the VMM and the guest should be considered to be running in the network context. As such, you should apply on the VMM whatever restrictions you want to apply on the guest.
glibc-static in Fedora)make [FEATURE_OPTIONS]
sudo make [FEATURE_OPTIONS] install
libkrunfw-sev.so library.glibc-static in Fedora)openssl-devel in Fedora).make SEV=1
sudo make SEV=1 install
libkrunfw-tdx.so library.glibc-static in Fedora)openssl-devel in Fedora).make TDX=1
sudo make TDX=1 install
The TDX flavor of libkrun only supports guests with 1 vCPU and memory less than or equal to 3072mib.
lld and xzmake [FEATURE_OPTIONS]
The init binary is cross-compiled using clang and lld. A suitable sysroot is automatically generated by the Makefile from Debian repository.
sudo make [FEATURE_OPTIONS] install
Despite being written in Rust, this library provides a simple C API defined in include/libkrun.h
This is a simple example providing chroot-like functionality using libkrun.
To be able to chroot_vm, you need need to build libkrun with the virtio-block and virtio-net optional features:
make BLK=1 NET=1
sudo make BLK=1 NET=1 install
cd examples
make
To be able to chroot_vm, you need first a directory to act as the root filesystem for your isolated program.
Use the rootfs target to get a rootfs prepared from the Fedora container image (note: you must have podman installed):
make rootfs
Now you can use chroot_vm to run a process within this new root filesystem:
./chroot_vm ./rootfs_fedora /bin/sh
If the libkrun and/or libkrunfw libraries were installed on a path that's not included in your /etc/ld.so.conf configuration, you may get an error like this one:
./chroot_vm: error while loading shared libraries: libkrun.so: cannot open shared object file: No such file or directory
To avoid this problem, use the LD_LIBRARY_PATH environment variable to point to the location where the libraries were installed. For example, if the libraries were installed in /usr/local/lib64, use something like this:
LD_LIBRARY_PATH=/usr/local/lib64 ./chroot_vm rootfs_fedora/ /bin/sh
See CONTRIBUTING.md for guidelines on submitting changes.
libkrun has achieved maturity and starting version 1.0.0 the public API is guaranteed to be stable, following SemVer.
If you think you've identified a security issue in the project, please DO NOT report the issue publicly via the GitHub issue tracker or Matrix. Instead, send an email with as many details as possible to libkrun-security@redhat.com. This is a private mailing list for the core maintainers.
The main communication channel is the libkrun Matrix channel.
libkrun incorporates code from Firecracker, rust-vmm and Cloud-Hypervisor.
(top 30 of 70)
Rust
95.7%
C
3.6%