-
Notifications
You must be signed in to change notification settings - Fork 26
compat,lib: add basic support for ABI v7, add support for restrict_self log flags #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,11 @@ | |
|
|
||
| use crate::compat::private::OptionCompatLevelMut; | ||
| use crate::{ | ||
| uapi, AccessFs, AccessNet, AddRuleError, AddRulesError, BitFlags, CompatLevel, CompatState, | ||
| Compatibility, Compatible, CreateRulesetError, HandledAccess, LandlockStatus, | ||
| PrivateHandledAccess, RestrictSelfError, RulesetError, Scope, ScopeError, TryCompat, | ||
| uapi, Access, AccessFs, AccessNet, AddRuleError, AddRulesError, BitFlags, CompatLevel, | ||
| CompatState, Compatibility, Compatible, CreateRulesetError, HandledAccess, LandlockStatus, | ||
| PrivateHandledAccess, RestrictSelfError, RulesetError, Scope, ScopeError, TryCompat, ABI, | ||
| }; | ||
| use enumflags2::bitflags; | ||
| use std::io::Error; | ||
| use std::mem::size_of_val; | ||
| use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd}; | ||
|
|
@@ -50,6 +51,65 @@ pub enum RulesetStatus { | |
| NotEnforced, | ||
| } | ||
|
|
||
| /// Flags to use when applying ruleset restrictions | ||
| #[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
| #[bitflags] | ||
| #[repr(u32)] | ||
| #[non_exhaustive] | ||
| pub enum RestrictSelfFlag { | ||
| /// Disables logging of denied accesses originating | ||
| /// from the thread creating the Landlock domain, as | ||
| /// well as its children, as long as they continue | ||
| /// running the same executable code (i.e., without | ||
| /// an intervening execve(2) call). This is intended | ||
| /// for programs that execute unknown code without | ||
| /// invoking execve(2), such as script interpreters. | ||
| /// Programs that only sandbox themselves should not | ||
| /// set this flag, so users can be notified of | ||
| /// unauthorized access attempts via system logs. | ||
| LogSameExecOff = uapi::LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF, | ||
| /// Enables logging of denied accesses after an | ||
| /// execve(2) call, providing visibility into | ||
| /// unauthorized access attempts by newly executed | ||
| /// programs within the created Landlock domain. | ||
| /// This flag is recommended only when all potential | ||
| /// executables in the domain are expected to comply | ||
| /// with the access restrictions, as excessive audit | ||
| /// log entries could make it more difficult to | ||
| /// identify critical events. | ||
| LogNewExecOn = uapi::LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON, | ||
| /// Disables logging of denied accesses originating | ||
| /// from nested Landlock domains created by the caller | ||
| /// or its descendants. This flag should be set | ||
| /// according to runtime configuration, not hardcoded, | ||
| /// to avoid suppressing important security events. | ||
| /// It is useful for container runtimes or sandboxing | ||
| /// tools that may launch programs which themselves | ||
| /// create Landlock domains and could otherwise | ||
| /// generate excessive logs. Unlike | ||
| /// LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF, this flag | ||
| /// only affects future nested domains, not the one | ||
| /// being created. It can also be used with a ruleset_fd | ||
| /// value of -1 to mute subdomain logs without creating | ||
| /// a domain. | ||
| LogSubdomainsOff = uapi::LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF, | ||
| } | ||
|
|
||
| impl Access for RestrictSelfFlag { | ||
| fn from_all(abi: ABI) -> BitFlags<Self> { | ||
| match abi { | ||
| ABI::Unsupported | ABI::V1 | ABI::V2 | ABI::V3 | ABI::V4 | ABI::V5 | ABI::V6 => { | ||
| BitFlags::EMPTY | ||
| } | ||
| ABI::V7 => { | ||
| RestrictSelfFlag::LogSameExecOff | ||
| | RestrictSelfFlag::LogNewExecOn | ||
| | RestrictSelfFlag::LogSubdomainsOff | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<CompatState> for RulesetStatus { | ||
| fn from(state: CompatState) -> Self { | ||
| match state { | ||
|
|
@@ -746,14 +806,17 @@ impl RulesetCreated { | |
| } | ||
| } | ||
|
|
||
| /// Attempts to restrict the calling thread with the ruleset | ||
| /// Attempts to restrict the calling thread with the ruleset and flags | ||
| /// according to the best-effort configuration | ||
| /// (see [`RulesetCreated::set_compatibility()`] and [`CompatLevel::BestEffort`]). | ||
| /// Call `prctl(2)` with the `PR_SET_NO_NEW_PRIVS` | ||
| /// according to the ruleset configuration. | ||
| /// | ||
| /// On error, returns a wrapped [`RestrictSelfError`]. | ||
| pub fn restrict_self(mut self) -> Result<RestrictionStatus, RulesetError> { | ||
| pub fn restrict_self_with_flags( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change, we need a smoother interface. |
||
| mut self, | ||
| flags: BitFlags<RestrictSelfFlag>, | ||
| ) -> Result<RestrictionStatus, RulesetError> { | ||
| let mut body = || -> Result<RestrictionStatus, RestrictSelfError> { | ||
| // Enforce no_new_privs even if something failed with SoftRequirement. The rationale is | ||
| // that no_new_privs should not be an issue on its own if it is not explicitly | ||
|
|
@@ -807,7 +870,7 @@ impl RulesetCreated { | |
| assert!(self.fd.is_some()); | ||
| // Does not consume ruleset FD, which will be automatically closed after this block. | ||
| let fd = self.fd.as_ref().map(|f| f.as_raw_fd()).unwrap_or(-1); | ||
| match unsafe { uapi::landlock_restrict_self(fd, 0) } { | ||
| match unsafe { uapi::landlock_restrict_self(fd, flags.bits()) } { | ||
| 0 => { | ||
| self.compat.update(CompatState::Full); | ||
| Ok(RestrictionStatus { | ||
|
|
@@ -827,6 +890,17 @@ impl RulesetCreated { | |
| Ok(body()?) | ||
| } | ||
|
|
||
| /// Attempts to restrict the calling thread with the ruleset | ||
| /// according to the best-effort configuration | ||
| /// (see [`RulesetCreated::set_compatibility()`] and [`CompatLevel::BestEffort`]). | ||
| /// Call `prctl(2)` with the `PR_SET_NO_NEW_PRIVS` | ||
| /// according to the ruleset configuration. | ||
| /// | ||
| /// On error, returns a wrapped [`RestrictSelfError`]. | ||
| pub fn restrict_self(self) -> Result<RestrictionStatus, RulesetError> { | ||
| return self.restrict_self_with_flags(BitFlags::<RestrictSelfFlag>::empty()); | ||
| } | ||
|
|
||
| /// Creates a new `RulesetCreated` instance by duplicating the underlying file descriptor. | ||
| /// Rule modification will affect both `RulesetCreated` instances simultaneously. | ||
| /// | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Syscall flags are not the same semantic as access rights.