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 Coveralls branch

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.

Features

Landlock ABI Version Feature Supported
1 Initial support
2 File renaming and linking
3 File truncation
4 TCP bind and connect
5 Device IOCTL
6 Abstract UNIX socket
7 Linux audit logging

For more information about what these features are, please see the Landlock user documentation section Previous Limitations.

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.dev5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

landlock-1.0.0.dev5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: landlock-1.0.0.dev5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for landlock-1.0.0.dev5.tar.gz
Algorithm Hash digest
SHA256 e82d511992be24986c51e3c9d2bfcc956833c2f82f95004216262aa4e2d50a2e
MD5 33f6cb5b9ac90693bf9592010beca49a
BLAKE2b-256 79e2ad65eb9afcde46eb055dce9b7003b372b1de19da94acf800a2e352ba5ae1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: landlock-1.0.0.dev5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for landlock-1.0.0.dev5-py3-none-any.whl
Algorithm Hash digest
SHA256 6d72357f5dc23d64b3b3d976672b61af736281b8d34a5825c1a61b602f2df815
MD5 f8fcef744c7e1a0138aa22b32b5dae99
BLAKE2b-256 45c05cff902d2ff3df3db965e0b36208017569abf4a93ed30fe9ffafbf171c95

See more details on using hashes here.

Supported by

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