NAME

Linux::Landlock::Ruleset - 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. Since the restrictions are set at runtime, from within the process itself, you can take into account dynamic information, like log or file system spool locations defined in your current configuration.

Once set, restrictions cannot be undone and they are inherited by all future child processes.

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.

SYNOPSIS

  use Linux::Landlock::Ruleset;

  my $ruleset = Linux::Landlock::Ruleset->new();
  $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

METHODS

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.

AUTHOR

Marc Ballarin, ballarin.marc@gmx.de

COPYRIGHT AND LICENSE

Copyright (C) 2024 by Marc Ballarin

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