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 landlockVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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).
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.
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.
Install the library using pip: `pip install landlock`
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'.
Add `ruleset.allow(...)` calls for all necessary paths and access types (e.g., `allow_read`, `allow_write`, `allow_execute`) *before* calling `ruleset.apply()`.
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.
Ensure you are importing the correct classes from the `landlock` module, for example: `from landlock import Ruleset, AccessFs`.