pub struct Ruleset { /* private fields */ }
Expand description
Landlock ruleset builder.
Ruleset
enables to create a Landlock ruleset in a flexible way
following the builder pattern.
Most build steps return a Result
with RulesetError
.
You should probably not create more than one ruleset per application. Creating multiple rulesets is only useful when gradually restricting an application (e.g., a first set of generic restrictions before reading any file, then a second set of tailored restrictions after reading the configuration).
§Simple example
Simple helper handling only Landlock-related errors.
use landlock::{
Access, AccessFs, PathBeneath, PathFd, RestrictionStatus, Ruleset, RulesetAttr,
RulesetCreatedAttr, RulesetError, ABI,
};
use std::os::unix::io::AsFd;
fn restrict_fd<T>(hierarchy: T) -> Result<RestrictionStatus, RulesetError>
where
T: AsFd,
{
// The Landlock ABI should be incremented (and tested) regularly.
let abi = ABI::V1;
let access_all = AccessFs::from_all(abi);
let access_read = AccessFs::from_read(abi);
Ok(Ruleset::default()
.handle_access(access_all)?
.create()?
.add_rule(PathBeneath::new(hierarchy, access_read))?
.restrict_self()?)
}
let fd = PathFd::new("/home").expect("failed to open /home");
let status = restrict_fd(fd).expect("failed to build the ruleset");
§Generic example
More generic helper handling a set of file hierarchies
and multiple types of error (i.e. RulesetError
and PathFdError
.
use landlock::{
Access, AccessFs, PathBeneath, PathFd, PathFdError, RestrictionStatus, Ruleset,
RulesetAttr, RulesetCreatedAttr, RulesetError, ABI,
};
use thiserror::Error;
#[derive(Debug, Error)]
enum MyRestrictError {
#[error(transparent)]
Ruleset(#[from] RulesetError),
#[error(transparent)]
AddRule(#[from] PathFdError),
}
fn restrict_paths(hierarchies: &[&str]) -> Result<RestrictionStatus, MyRestrictError> {
// The Landlock ABI should be incremented (and tested) regularly.
let abi = ABI::V1;
let access_all = AccessFs::from_all(abi);
let access_read = AccessFs::from_read(abi);
Ok(Ruleset::default()
.handle_access(access_all)?
.create()?
.add_rules(
hierarchies
.iter()
.map::<Result<_, MyRestrictError>, _>(|p| {
Ok(PathBeneath::new(PathFd::new(p)?, access_read))
}),
)?
.restrict_self()?)
}
let status = restrict_paths(&["/usr", "/home"]).expect("failed to build the ruleset");
Implementations§
source§impl Ruleset
impl Ruleset
pub fn new() -> Self
sourcepub fn create(self) -> Result<RulesetCreated, RulesetError>
pub fn create(self) -> Result<RulesetCreated, RulesetError>
Attempts to create a real Landlock ruleset (if supported by the running kernel).
The returned RulesetCreated
is also a builder.
On error, returns a wrapped CreateRulesetError
.
Trait Implementations§
source§impl Compatible for &mut Ruleset
impl Compatible for &mut Ruleset
source§fn set_compatibility(self, level: CompatLevel) -> Self
fn set_compatibility(self, level: CompatLevel) -> Self
set_compatibility()
. Read moresource§fn set_best_effort(self, best_effort: bool) -> Selfwhere
Self: Sized,
fn set_best_effort(self, best_effort: bool) -> Selfwhere
Self: Sized,
source§impl Compatible for Ruleset
impl Compatible for Ruleset
source§fn set_compatibility(self, level: CompatLevel) -> Self
fn set_compatibility(self, level: CompatLevel) -> Self
set_compatibility()
. Read moresource§fn set_best_effort(self, best_effort: bool) -> Selfwhere
Self: Sized,
fn set_best_effort(self, best_effort: bool) -> Selfwhere
Self: Sized,
source§impl Default for Ruleset
impl Default for Ruleset
source§fn default() -> Self
fn default() -> Self
Returns a new Ruleset
.
This call automatically probes the running kernel to know if it supports Landlock.
To be able to successfully call create()
,
it is required to set the handled accesses with
handle_access()
.
source§impl RulesetAttr for &mut Ruleset
impl RulesetAttr for &mut Ruleset
source§fn handle_access<T, U>(self, access: T) -> Result<Self, RulesetError>
fn handle_access<T, U>(self, access: T) -> Result<Self, RulesetError>
handle_access()
will be interpreted as logical ORs
with the previous handled accesses. Read moresource§impl RulesetAttr for Ruleset
impl RulesetAttr for Ruleset
source§fn handle_access<T, U>(self, access: T) -> Result<Self, RulesetError>
fn handle_access<T, U>(self, access: T) -> Result<Self, RulesetError>
handle_access()
will be interpreted as logical ORs
with the previous handled accesses. Read more