Registry / auth-security / landlock

landlock

JSON →
library1.0.0.dev5pypypi✓ verified 23d ago

Landlock for Python is a library providing a Python interface to the Landlock Linux Security Module (LSM). It enables developers to apply rule-based filesystem access restrictions to Python code, enhancing application security by limiting what an unprivileged process can access. Currently at version 1.0.0.dev5, its release cadence is in active development, with periodic updates as the Landlock kernel module itself evolves.

pip install landlock
INSTALL
IMPORT
SIG · LANDLOCK
L
landlock
auth-securitypythonv1.0.0.dev5
Install
1.5s avg
Import
56ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0.dev5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.054s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.058s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Ruleset
from landlock import Ruleset
This is the primary class for defining and applying Landlock rules.

This quickstart demonstrates how to create a Landlock ruleset to restrict filesystem access. It allows reading the current directory and executing `ls` and `cat` from `/usr/bin`, while implicitly denying access to all other paths, such as `/etc/passwd`. The `apply()` method enforces these rules on the current thread and its children.

import os from landlock import Ruleset, LandlockError def main(): # Create a ruleset, by default it disallows all filesystem access rs = Ruleset() # Explicitly allow read access to the current directory and its contents # and execute access to /usr/bin for common commands rs.allow_read('.') rs.allow_execute('/usr/bin/ls') rs.allow_execute('/usr/bin/cat') try: # Apply the Landlock ruleset to the current thread rs.apply() print("Landlock rules applied. Trying to access allowed paths...") # This should succeed print(f"Current directory listing: {os.listdir('.')}") os.system("ls -l .") print("\nTrying to access a disallowed path (/etc/passwd)...") # This should fail with a PermissionError (or similar LandlockError) try: with open('/etc/passwd', 'r') as f: _ = f.read() print("Accessed /etc/passwd (unexpectedly succeeded)") except LandlockError as e: print(f"Caught expected LandlockError: {e}") except PermissionError as e: print(f"Caught expected PermissionError: {e}") except LandlockError as e: print(f"Landlock is not available or failed to apply rules: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") if __name__ == '__main__': main()
Debug
Known issues
breakingThe `landlock` library is a binding for the Linux Landlock Security Module. It will only function on Linux systems running kernel version 5.13 or newer with Landlock support enabled. It is not compatible with other operating systems or older Linux kernels.
fix
Ensure your environment is a compatible Linux distribution with a supported kernel version and Landlock enabled. Check `/proc/filesystems` for `landlock` entry or try `landlock.get_abi_version()` (if available in this specific binding) to confirm.
affects: All
gotchaLandlock rules are immutable and cannot be removed or relaxed once applied to a thread. Any child processes or threads created after `ruleset.apply()` will inherit the parent's restrictions. Children can only add *further* restrictions, not lessen existing ones.
fix
Design your application's security policy carefully. Apply the least permissive rules as late as possible in your program's execution flow, typically before processing untrusted input.
affects: All
gotchaThis specific `landlock` library (Edward-Knight's) explicitly states that it does not support Landlock ABI version 4 features, such as TCP bind and connect restrictions. Therefore, network sandboxing capabilities are limited or unavailable through this binding.
fix
If network access control is critical, you may need to explore alternative Landlock Python bindings (e.g., `py-landlock` from SebastienWae) or other Linux security mechanisms (e.g., cgroups, seccomp-bpf).
affects: All
gotchaLandlock has limitations regarding filesystem topology. Sandboxed threads cannot modify the filesystem topology (e.g., via `mount(2)` or `pivot_root(2)`). However, `chroot(2)` is permitted.
fix
Be aware of these limitations when designing your sandbox. Do not expect to remount filesystems or change the root directory (except `chroot`) within a Landlock-restricted process.
affects: All
gotchaThe kernel enforces a limit of 16 stacked Landlock rulesets per thread. Attempting to apply more rulesets than this limit will result in an `E2BIG` error.
fix
Consolidate your Landlock rules into as few rulesets as possible. If multiple granular policies are needed, consider designing them hierarchically or applying them at different stages of your application's lifecycle, rather than excessive stacking.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'landlock'
The 'landlock' Python package has not been installed in the current Python environment or is not accessible on the Python path.
fix
Install the library using pip: `pip install landlock`
LandlockError: Landlock is not available (wrong platform, architecture, or kernel)
The system running the Python code does not have Landlock enabled in its Linux kernel (requires kernel 5.13 or newer), or the operating system is not Linux.
fix
Ensure your environment is a compatible Linux distribution with a supported kernel version (5.13+) and that Landlock is enabled. Check kernel logs for 'landlock: Up and running'.
PermissionError: [Errno 13] Permission denied
After applying a Landlock ruleset, the Python code attempted to access a filesystem path or perform an action (read, write, execute) that was not explicitly allowed by the ruleset. By default, the `landlock` library disallows all filesystem access once a ruleset is applied, requiring explicit allowances.
fix
Add `ruleset.allow(...)` calls for all necessary paths and access types (e.g., `allow_read`, `allow_write`, `allow_execute`) *before* calling `ruleset.apply()`.
RulesetError: Failed to create, configure, or apply a ruleset
A general error occurred during the creation, configuration, or application of a Landlock ruleset. This can include exceeding the kernel's limit of 16 stacked rulesets (resulting in an E2BIG internal error) or other misconfigurations of access rules.
fix
Review the ruleset configuration for errors. If an E2BIG error is suspected, consolidate Landlock rules into fewer rulesets or design policies hierarchically to stay within the kernel's limit.
cannot import name 'Ruleset' from 'landlock'
The specific class `Ruleset` (or other top-level components like `AccessFs`) is not being imported correctly, potentially due to a typo or a misunderstanding of the library's structure.
fix
Ensure you are importing the correct classes from the `landlock` module, for example: `from landlock import Ruleset, AccessFs`.
Upgrade
Version history
1.0.0.dev5latest on PyPI · released May 3, 2025
Audit
Dependencies
pythonrequiredRequired Python interpreter version.
linux-kernelrequiredLandlock is a Linux kernel feature, requiring kernel version 5.13 or later with Landlock enabled.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources