| Crates.io | rstrict |
| lib.rs | rstrict |
| version | 0.1.14 |
| created_at | 2025-04-10 06:07:32.846831+00 |
| updated_at | 2025-05-15 19:28:57.964958+00 |
| description | A lightweight CLI to securely exec Linux processes inside the Kernels Landlock LSM sandbox for filesystem and network access control |
| homepage | |
| repository | https://github.com/creslinux/rstrict |
| max_upload_size | |
| id | 1627681 |
| size | 79,933 |
A lightweight, secure sandbox for running Linux processes using the Linux kernel's Landlock LSM, implemented in Rust with the excellent landlock-rs crate.
rstrict leverages the Linux Landlock security module to sandbox processes, allowing you to run commands with restricted access to the filesystem and network, reducing the potential impact of vulnerabilities or unintended actions.
Website | Guides | Docs | Crate
For detailed information about the underlying Landlock security module, see the official Linux kernel documentation.
Quote:
The goal of Landlock is to enable restriction of ambient rights (e.g. global filesystem or network access) for a set of processes. Because Landlock is a stackable LSM, it makes it possible to create safe security sandboxes as new security layers in addition to the existing system-wide access-controls. This kind of sandbox is expected to help mitigate the security impact of bugs or unexpected/malicious behaviors in user space applications. Landlock empowers any process, including unprivileged ones, to securely restrict themselves.
The two existing types of rules are:
Filesystem rules For these rules, the object is a file hierarchy, and the related filesystem actions are defined with filesystem access rights.
Network rules (since ABI v4) For these rules, the object is a TCP port, and the related actions are defined with network access rights. -- Landlock
The basic command structure is:
rstrict [OPTIONS] -- <COMMAND> [COMMAND_ARGS...]
Example Sandboxing
# allow `ls` and its linked libraries to run, allow access to read /tmp
rstrict --log-level debug --ro /tmp --ldd --add-exec -- ls -l /tmp
# Curl sandbox example
# --add-exec Allow executing curl binary (optional helper)
# --ldd Allow executing curl's libraries (optional helper)
# --ro Read DNS configuration
# --ro Read Name service configuration
# --ro Read Hosts file
# --ro Read SSL certificates
# --connect-tcp <port> Allow connections to HTTPS Port
rstrict --log-level info \
--add-exec \
--ldd \
--ro /etc/resolv.conf \
--ro /etc/nsswitch.conf \
--ro /etc/hosts \
--ro /etc/ssl/certs \
--connect-tcp 443 \
-- \
curl https://example.com
** --ldd and --add-exec are optional convenience flags, including the executables path and any dependencies with --rox will have the same effect.
rstrict follows the fundamental security principle of "deny by default, allow explicitly." When a process is sandboxed with rstrict:
rstrict provides user-friendly flags that directly map to Landlock's underlying access control mechanisms:
| Feature Type | rstrict Flags | Landlock Access Rights | Available Since |
|---|---|---|---|
| Filesystem Access | |||
--ro <PATH> |
LANDLOCK_ACCESS_FS_READ_FILE, LANDLOCK_ACCESS_FS_READ_DIR (applied to PATH; recursively if PATH is a directory) |
ABI v1 | |
--rw <PATH> |
--ro rights + LANDLOCK_ACCESS_FS_WRITE_FILE, LANDLOCK_ACCESS_FS_TRUNCATE, etc. (applied to PATH; recursively if PATH is a directory) |
ABI v1+ | |
--rox <PATH> |
--ro rights + LANDLOCK_ACCESS_FS_EXECUTE (applied to PATH; recursively if PATH is a directory) |
ABI v1 | |
--rwx <PATH> |
--rw rights + LANDLOCK_ACCESS_FS_EXECUTE (applied to PATH; recursively if PATH is a directory) |
ABI v1 | |
| Network Control | |||
--bind-tcp <PORT> |
LANDLOCK_ACCESS_NET_BIND_TCP |
ABI v4+ | |
--connect-tcp <PORT> |
LANDLOCK_ACCESS_NET_CONNECT_TCP |
ABI v4+ | |
| Helper Functions | |||
--add-exec |
Automatically adds command executable to --rox |
N/A | |
--ldd |
Automatically adds libraries to --rox |
N/A | |
--env |
Environment variable management | N/A |
rstrict's filesystem flags provide intuitive access control that maps to Landlock's more granular permissions:
--ro <PATH>: Allow read-only access to the specified path
LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_READ_DIR--rw <PATH>: Allow read-write access to the specified path
--ro rights plus write operations like:
LANDLOCK_ACCESS_FS_WRITE_FILELANDLOCK_ACCESS_FS_TRUNCATE (ABI v3+)LANDLOCK_ACCESS_FS_REMOVE_FILE/LANDLOCK_ACCESS_FS_REMOVE_DIRLANDLOCK_ACCESS_FS_MAKE_REG/LANDLOCK_ACCESS_FS_MAKE_DIR, etc.--rox <PATH>: Allow read and execute access to the specified path
--ro rights plus LANDLOCK_ACCESS_FS_EXECUTE--rwx <PATH>: Allow read, write, and execute access to the specified path
--rw and --rox permissionsrstrict's network flags directly correspond to Landlock's TCP socket controls (available since ABI v4):
--bind-tcp <PORT>: Allow binding to the specified TCP port
LANDLOCK_ACCESS_NET_BIND_TCP--connect-tcp <PORT>: Allow outgoing TCP connections to the specified port
LANDLOCK_ACCESS_NET_CONNECT_TCPImportant Note: Landlock network rules currently only restrict TCP bind/connect operations. UDP, ICMP, and other protocols are NOT restricted by these rules.
rstrict provides convenience flags to simplify common sandboxing tasks:
--add-exec: Automatically find <COMMAND> in $PATH and add it to the --rox list
--ldd: Run ldd on <COMMAND> to find and add shared library dependencies
/lib, /usr/lib) to the --rox list--env <VAR>: Manage environment variables for the sandboxed process
--env KEY=VALUE: Sets an environment variable--env KEY: Inherits a value from the current environmentLANDLOCK_ACCESS_FS_REFER support (ABI v2).LANDLOCK_ACCESS_FS_TRUNCATE (ABI v3).LANDLOCK_ACCESS_NET_* (ABI v4).LANDLOCK_ACCESS_FS_IOCTL_DEV (ABI v5).ldd command: Required only if using the --ldd helper flag.cargo install rstrict
git clone https://github.com/creslinux/rstrict.git
cd rstrict
cargo build --release
target/release/rstrict. You can copy it to a location in your $PATH:
sudo cp target/release/rstrict /usr/local/bin/
The basic command structure is:
rstrict [OPTIONS] -- <COMMAND> [COMMAND_ARGS...]
The basic command structure is:
rstrict [OPTIONS] -- <COMMAND> [COMMAND_ARGS...]
[OPTIONS]: Flags to configure the sandbox rules (see table above).--: Required: Separates rstrict options from the command you want to run.<COMMAND>: The command to execute inside the sandbox.[COMMAND_ARGS...]: Arguments for the command being executed.Filesystem Access:
--ro <PATH>: Allow read-only access to the specified path. If PATH is a directory, permissions apply recursively to everything beneath it. If PATH is a file, permissions apply only to that specific file. Can be used multiple times.--rw <PATH>: Allow read-write access to the specified path. If PATH is a directory, permissions apply recursively to everything beneath it. If PATH is a file, permissions apply only to that specific file. Can be used multiple times.--rox <PATH>: Allow read + execute access to the specified path. If PATH is a directory, permissions apply recursively to everything beneath it. If PATH is a file, permissions apply only to that specific file. Can be used multiple times.--rwx <PATH>: Allow read-write + execute access to the specified path. If PATH is a directory, permissions apply recursively to everything beneath it. If PATH is a file, permissions apply only to that specific file. Can be used multiple times.Network Access (TCP Only):
--bind-tcp <PORT>: Allow binding to the specified TCP port. Can be used multiple times.--connect-tcp <PORT>: Allow outgoing TCP connections to the specified port. Can be used multiple times.Helper Flags:
--add-exec: Automatically find <COMMAND> in $PATH and add it to the --rox list.--ldd: Run ldd on <COMMAND> to find shared library dependencies and add them to --rox.--env <VAR>: Specify environment variables for the sandboxed process.
--env KEY=VALUE: Sets the variable KEY to VALUE.--env KEY: Inherits the value of KEY from the current environment.Unrestricted Access:
--unrestricted-filesystem: Disable Landlock filesystem rules.--unrestricted-network: Disable Landlock network (TCP) rules.Logging & Meta:
--log-level <LEVEL>: Set logging verbosity. Options: error (default), warn, info, debug, trace.--help: Show help message and exit.--version: Show version information and exit.--ldd is used, shared libraries are automatically discoveredrestrict_self()execvpe to replace itself with the target commandThis approach ensures the security boundary is established before the target program begins execution.
1. Running ls with minimal read access:
# Basic filesystem sandbox
rstrict --log-level info \
--ro /home \
--add-exec \
--ldd \
-- \
ls -l /home
Output should show details for /bin/bash. Trying ls -l /tmp would fail with a permission error.
2. Running curl to fetch a webpage (HTTPS):
rstrict --log-level info \
--add-exec \
--ldd \
--ro /etc/resolv.conf \
--ro /etc/nsswitch.conf \
--ro /etc/hosts \
--ro /etc/ssl/certs \
--connect-tcp 443 \
-- \
curl https://example.com
--add-exec, --ldd: Allow curl and its libraries to run--ro /etc/resolv.conf, etc.: Allow DNS resolver configuration access--ro /etc/ssl/certs: Allow TLS certificate verification--connect-tcp 443: Allow HTTPS connections3. Allowing write access to a specific directory:
# Create a temporary directory first
mkdir ./my_temp_data
# Run touch with write access to only that directory
rstrict --log-level info \
--rw ./my_temp_data \
--add-exec \
--ldd \
-- \
touch ./my_temp_data/test_file.txt
4. Running a web server on port 8080 can connect to MySQL on 3306:
rstrict --log-level info \
--ro /app/static \
--rw /app/logs \
--bind-tcp 8080 \
--connect-tcp 3306 \
--add-exec \
--ldd \
-- \
/app/myserver --port 8080
rstrict adapts to the available Landlock features on your kernel at runtime. It will use the highest supported ABI version and adjust its behavior accordingly:
| ABI Version | Kernel | Features Added |
|---|---|---|
| v1 | 5.13+ | Basic filesystem controls (read, write, execute) |
| v2 | 5.15+ | File linking/renaming between directories (LANDLOCK_ACCESS_FS_REFER) |
| v3 | 5.16+ | File truncation control (LANDLOCK_ACCESS_FS_TRUNCATE) |
| v4 | 5.19+ | TCP network controls (bind, connect) |
| v5 | 6.2+ | Device IOCTL control (LANDLOCK_ACCESS_FS_IOCTL_DEV) |
| v6 | 6.5+ | IPC scoping (signals, abstract UNIX sockets) |
Contributions (bug reports, feature requests, pull requests) are welcome! Please open an issue on the GitHub repository to discuss changes.
This project is licensed under the MIT License.