Skip to main content

Python interface to the Landlock Linux Security Module.

Project description

🔒🐍 Landlock for Python

PyPI - Status PyPI - License PyPI - Python Versions PyPI - Latest Project Version GitHub Workflow Status (main) pre-commit.ci status

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:

$ python3 test.py &
[1] ...
$ curl localhost:8000
...
$ curl localhost:8000/oops/
127.0.0.1 - - [DD/MMM/YYYY HH:MM:SS] code 404, message No permission to list directory
...
$ kill $!
[1]+  Terminated              python3 test.py

Success! Instead of dumping the password file, we instead get a permission error!

Landlock is great for hardening applications against both accidental programming mistakes, and attacks. It won't prevent an exploited application from all malicious behavior, but it can stop it reading with the filesystem and interacting with device files.

Developer Information

Testing

Tests are run using pytest. Each test is run in a separate subprocess using pytest-forked so Landlock rules don't conflict.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

landlock-1.0.0.dev3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

landlock-1.0.0.dev3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file landlock-1.0.0.dev3.tar.gz.

File metadata

  • Download URL: landlock-1.0.0.dev3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for landlock-1.0.0.dev3.tar.gz
Algorithm Hash digest
SHA256 9d92eaaf5044faadc596e83398ecc157c10a7c7e470102b8d3317d44f88c3c92
MD5 f96517db9a3e7ad7bca5d7e97628e850
BLAKE2b-256 4dad49faf5d39110662921e3011b341c5ea86a5b6c8a99fc6ae85f40865af403

See more details on using hashes here.

File details

Details for the file landlock-1.0.0.dev3-py3-none-any.whl.

File metadata

File hashes

Hashes for landlock-1.0.0.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 9287f9d03072a1f05d3580c817538c6adcb0c4b0c674e17652339d24ba628633
MD5 6ecf9a631b9e57ac3999ab0ddd491784
BLAKE2b-256 caeec605f624b8fe0a943011d1fc40119cc8217c8ebe8c327debbbe108a2d9d1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page