Skip to main content

Ruleset

Struct Ruleset 

Source
pub struct Ruleset { /* private fields */ }
Expand description

A Landlock policy under construction.

Rules are added with the allow_* methods and the policy is applied with Ruleset::restrict_self, which consumes the builder. Building never fails: errors (invalid paths, unsupported rights, …) are remembered and reported by restrict_self, and nothing is enforced when it returns them.

The builder is Send and Sync, and every method takes self by value, so a policy is never shared or mutated by two threads at once.

Implementations§

Source§

impl Ruleset

Source

pub fn allow_filesystem( self, access: Access, paths: impl IntoIterator<Item = impl AsRef<Path>>, ) -> Self

Grants access on each path of paths.

The same access rights are granted on every path. Paths are opened with O_PATH, which requires no permission on the target, and rules are attached to the objects the paths resolve to (symlinks are followed).

Rights that do not apply to the object are dropped (for example Access::RemoveFile on a regular file). Requesting access that grants nothing on the object (for example Access::ReadDir on a regular file) is an invalid rule and is reported by Ruleset::restrict_self.

Failure modes are deferred to Ruleset::restrict_self; this method never returns an error.

Source

pub fn allow_file_descriptor( self, access: Access, fds: impl IntoIterator<Item = impl AsFd>, ) -> Self

Grants access on each of fds, which must identify file hierarchies.

This is the equivalent of Ruleset::allow_filesystem for callers that want to control how the path is resolved (for example with openat2 and RESOLVE_NO_SYMLINKS). The descriptors may be closed once Ruleset::restrict_self returned: the kernel keeps a reference to the underlying objects.

Failure modes are deferred to Ruleset::restrict_self; this method never returns an error.

Source

pub fn allow_network( self, access: NetworkAccess, ports: impl IntoIterator<Item = u16>, ) -> Self

Grants access on each of ports.

Port 0 covers the ephemeral port range for binding. Landlock has no wildcard port, so every port has to be listed. Only TCP and UDP are restricted.

Failure modes are deferred to Ruleset::restrict_self; this method never returns an error.

Source

pub fn allow_scope(self, scope: Scope) -> Self

Lifts the IPC restriction described by scope.

Scopes are active by default. Unlike filesystem and network access, Landlock cannot grant exceptions to a scope: it can only be lifted as a whole.

Failure modes are deferred to Ruleset::restrict_self; this method never returns an error.

Source

pub fn restrict_self(self) -> Result<u32, Error>

Applies the policy to the calling thread (and, on Landlock ABI 8 and newer, to every thread of the process).

This consumes the ruleset, which is final: restrictions can never be relaxed afterwards, only added to. On success, the returned ABI version is the version reported by the running kernel, and the restrictions stay in place when the builder is dropped.

PR_SET_NO_NEW_PRIVS is set on the calling thread first (on Landlock ABI 11 and newer the kernel does it as part of the enforcement). It is never unset, and it prevents the process from gaining privileges through set-user-ID programs.

§Errors

Returns the first error encountered while building the policy (see Error) without applying it, or an error reported by the kernel.

Trait Implementations§

Source§

impl Debug for Ruleset

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.