Code Monkey home page Code Monkey logo

landlock's Introduction

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.

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.

landlock's People

Contributors

edward-knight avatar pre-commit-ci[bot] avatar

Stargazers

 avatar KOLANICH avatar Ashish Bijlani avatar KokaKiwi avatar Mickaël Salaün avatar

Watchers

Mickaël Salaün avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.