-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from onekey-sec/landlock
Expose landlock API
- Loading branch information
Showing
14 changed files
with
351 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
import typing | ||
|
||
import typing_extensions | ||
|
||
_Path: typing_extensions.TypeAlias = typing.Union[os.PathLike, str] | ||
|
||
class AccessFS: | ||
@staticmethod | ||
def read(access_dir: _Path) -> AccessFS: ... | ||
@staticmethod | ||
def read_write(access_dir: _Path) -> AccessFS: ... | ||
@staticmethod | ||
def make_reg(access_dir: _Path) -> AccessFS: ... | ||
@staticmethod | ||
def make_dir(access_dir: _Path) -> AccessFS: ... | ||
|
||
def restrict_access(*args: AccessFS) -> None: ... | ||
|
||
class SandboxError(Exception): ... |
This file contains 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 |
---|---|---|
@@ -1,19 +1,15 @@ | ||
pub mod math; | ||
pub mod math_tools; | ||
pub mod sandbox; | ||
|
||
use pyo3::prelude::*; | ||
|
||
/// Calculates Shannon entropy of data | ||
#[pyfunction(text_signature = "(data)")] | ||
pub fn shannon_entropy(py: Python, data: &[u8]) -> PyResult<f64> { | ||
py.allow_threads(|| Ok(math::shannon_entropy(data))) | ||
} | ||
|
||
/// Performance-critical functionality | ||
#[pymodule] | ||
fn _native(py: Python, m: &PyModule) -> PyResult<()> { | ||
let math_module = PyModule::new(py, "math_tools")?; | ||
math_module.add_function(wrap_pyfunction!(shannon_entropy, math_module)?)?; | ||
math_tools::init_module(py, m)?; | ||
sandbox::init_module(py, m)?; | ||
|
||
pyo3_log::init(); | ||
|
||
m.add_submodule(math_module)?; | ||
Ok(()) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use landlock::{ | ||
path_beneath_rules, Access, AccessFs, Ruleset, RulesetAttr, RulesetCreatedAttr, ABI, | ||
}; | ||
use log; | ||
|
||
use std::path::Path; | ||
|
||
use crate::sandbox::AccessFS; | ||
|
||
impl AccessFS { | ||
fn read(&self) -> Option<&Path> { | ||
if let Self::Read(path) = self { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn read_write(&self) -> Option<&Path> { | ||
if let Self::ReadWrite(path) = self { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn make_reg(&self) -> Option<&Path> { | ||
if let Self::MakeReg(path) = self { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn make_dir(&self) -> Option<&Path> { | ||
if let Self::MakeDir(path) = self { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
pub fn restrict_access(access_rules: &[AccessFS]) -> Result<(), Box<dyn std::error::Error>> { | ||
let abi = ABI::V2; | ||
|
||
let read_only: Vec<&Path> = access_rules.iter().filter_map(AccessFS::read).collect(); | ||
|
||
let read_write: Vec<&Path> = access_rules | ||
.iter() | ||
.filter_map(AccessFS::read_write) | ||
.collect(); | ||
|
||
let create_file: Vec<&Path> = access_rules.iter().filter_map(AccessFS::make_reg).collect(); | ||
|
||
let create_directory: Vec<&Path> = access_rules.iter().filter_map(AccessFS::make_dir).collect(); | ||
|
||
let status = Ruleset::new() | ||
.handle_access(AccessFs::from_all(abi))? | ||
.create()? | ||
.add_rules(path_beneath_rules(read_write, AccessFs::from_all(abi)))? | ||
.add_rules(path_beneath_rules(create_file, AccessFs::MakeReg))? | ||
.add_rules(path_beneath_rules(create_directory, AccessFs::MakeDir))? | ||
.add_rules(path_beneath_rules(read_only, AccessFs::from_read(abi)))? | ||
.restrict_self()?; | ||
|
||
log::info!( | ||
"Activated FS access restrictions; rules={:?}, status={:?}", | ||
access_rules, | ||
status.ruleset | ||
); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.