pkgcore.landlock module¶
Self-confinement with the Linux Landlock LSM.
Generating metadata means sourcing ebuilds with bash, which runs code out of the
repo it came from with whatever access the invoking user has. confine()
takes away what that doesn’t need: everything stays readable and executable, but
writes are denied outside the paths the caller names, as is outgoing TCP.
Restrictions cannot be lifted and are inherited by every thread and process started afterwards, so only call this from a command’s main function, in a process meant to stay confined for the rest of its life. In particular, never call it from library code whose caller carries on working once the ebuilds have been sourced.
Landlock mediates TCP bind and connect only, leaving UDP, ICMP and unix sockets untouched, so this is not an exfiltration boundary. Access through an already open descriptor is unaffected as well, since rights are checked when a path is resolved rather than on the descriptor.
- pkgcore.landlock.confine(*writable: str, allow_net: bool = False, required: bool = False) bool[source]¶
Deny writes outside writable, and outgoing TCP unless allow_net.
The paths sourcing an ebuild needs are always granted on top of writable. A path that doesn’t exist is skipped, and rights the kernel won’t accept for a non-directory are dropped rather than failing the whole rule.
- Parameters:
required – raise rather than carry on unconfined when landlock is unavailable, too old, or disabled at boot.
- Raises:
PkgcoreUserException – if required and the restrictions couldn’t be applied.
- Returns:
whether the restrictions were applied.