NAME

Linux::Landlock - A higher level interface to the Linux Landlock API

DESCRIPTION

Landlock is a sandboxing feature specific to Linux that allows a process to restrict its own access to the file system. Once set, restrictions cannot be undone and they are inherited by all future child processes.

Since the restrictions are set at runtime, from within the process itself, you can take into account dynamic information from your configuration.

For example, a server that is supposed to serve files from a specific directory can restrict itself to that directory and its subdirectories to mitigate any bugs allowing directory traversal attacks. This is much less intrusive than chroot and does not require root privileges.

This module provides an object-oriented interface to the Linux Landlock API. It uses the lower-level interface provided by Linux::Landlock::Direct.

See https://docs.kernel.org/userspace-api/landlock.html for more information about Landlock.

METHODS

SYNOPSIS

  use Linux::Landlock;

  my $ruleset = Linux::Landlock->new(supported_abi_version => 4); # this can die
  $ruleset->add_path_rule('/etc/fstab', qw(read_file));
  $ruleset->add_net_rule(22222, qw(bind_tcp));
  $ruleset->apply();

  print -r '/etc/fstab' ? "allowed\n" : "not allowed\n"; # allowed ...
  IO::File->new('/etc/fstab', 'r') and print "succeeded: $!\n"; # ... and opening works
  print -r '/etc/passwd' ? "allowed\n" : "not allowed\n"; # allowed ...
  IO::File->new('/etc/passwd', 'r') or print "failed\n"; # ... but opening fails because of Landlock

  system('/usr/bin/cat /etc/fstab') and print "failed: $!\n"; # this fails, because we cannot execute cat

  IO::Socket::INET->new(LocalPort => 33333, Proto => 'tcp') or print "failed: $!\n"; # failed
  IO::Socket::INET->new(LocalPort => 22222, Proto => 'tcp') and print "succeeded\n"; # succeeded

LIMITATIONS

This module requires a Linux system supporting the Landlock functionality. As of 2024, this is the case for almost all distributions, however, the version of the available Landlock ABI varies.

Notably, the TRUNCATE access right is only supported by the kernel since ABI version 3 (kernel version 6.2 or newer, unless backported).

Network functionality is only available since ABI version 4.

Also keep in mind, that some Perl modules can implicitly rely on operations that are restricted by the Landlock rules you apply, so test carefully.

AUTHOR

Marc Ballarin, ballarin.marc@gmx.de

COPYRIGHT AND LICENSE

Copyright (C) 2024-2025 by Marc Ballarin

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.