Python interface to the Landlock Linux Security Module.
Low maintenance activity. Last release 19 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install landlock🔒🐍 Landlock for Python
Harden your Python code by with rule-based file system access restrictions.
Example
Let's write a simple HTTP server that serves files in the local directory.
from http.server import HTTPServer, SimpleHTTPRequestHandler
server = HTTPServer(("", 8000), SimpleHTTPRequestHandler)
server.serve_forever()
But if there's a symlink in the local directory, the program can "escape".
$ ln -s /etc oops
$ python3 test.py &
[1] ...
$ curl localhost:8000
...
$ curl localhost:8000/oops/passwd
uh oh
$ kill $!
[1]+ Terminated python3 test.py
Now let's harden our server with Landlock!
from http.server import HTTPServer, SimpleHTTPRequestHandler
from landlock import Ruleset
server = HTTPServer(("", 8000), SimpleHTTPRequestHandler)
# the ruleset by default disallows all filesystem access
rs = Ruleset()
# explicitly allow access to the local directory hierarchy
rs.allow(".")
# turn on protections
rs.apply()
server.serve_forever()
And now we get a permission denied error if we try and access files outside the current directory, even via a symlink: