{"id":3532,"library":"landlock","title":"Landlock for Python","description":"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.","status":"active","version":"1.0.0.dev5","language":"en","source_language":"en","source_url":"https://github.com/Edward-Knight/landlock","tags":["security","linux","sandboxing","filesystem","lsm"],"install":[{"cmd":"pip install landlock","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required Python interpreter version.","package":"python","version":">=3.9","optional":false},{"reason":"Landlock is a Linux kernel feature, requiring kernel version 5.13 or later with Landlock enabled.","package":"linux-kernel"}],"imports":[{"note":"This is the primary class for defining and applying Landlock rules.","symbol":"Ruleset","correct":"from landlock import Ruleset"}],"quickstart":{"code":"import os\nfrom landlock import Ruleset, LandlockError\n\ndef main():\n    # Create a ruleset, by default it disallows all filesystem access\n    rs = Ruleset()\n    \n    # Explicitly allow read access to the current directory and its contents\n    # and execute access to /usr/bin for common commands\n    rs.allow_read('.')\n    rs.allow_execute('/usr/bin/ls')\n    rs.allow_execute('/usr/bin/cat')\n    \n    try:\n        # Apply the Landlock ruleset to the current thread\n        rs.apply()\n        print(\"Landlock rules applied. Trying to access allowed paths...\")\n        \n        # This should succeed\n        print(f\"Current directory listing: {os.listdir('.')}\")\n        os.system(\"ls -l .\")\n        \n        print(\"\\nTrying to access a disallowed path (/etc/passwd)...\")\n        # This should fail with a PermissionError (or similar LandlockError)\n        try:\n            with open('/etc/passwd', 'r') as f:\n                _ = f.read()\n            print(\"Accessed /etc/passwd (unexpectedly succeeded)\")\n        except LandlockError as e:\n            print(f\"Caught expected LandlockError: {e}\")\n        except PermissionError as e:\n            print(f\"Caught expected PermissionError: {e}\")\n        \n    except LandlockError as e:\n        print(f\"Landlock is not available or failed to apply rules: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n\nif __name__ == '__main__':\n    main()","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"breaking","affected_versions":"All"},{"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.","message":"Landlock 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.","severity":"gotcha","affected_versions":"All"},{"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).","message":"This 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"Landlock 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}