Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 95bfa0f

Browse files
authored
Merge pull request #11 from onekey-sec/landlock
Expose landlock API
2 parents 371aa58 + e2763ef commit 95bfa0f

File tree

14 files changed

+351
-20
lines changed

14 files changed

+351
-20
lines changed

.github/workflows/CI.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
key: pytest-${{ matrix.os }}
8787
- name: Install dependencies
8888
run: |
89-
pdm sync -d
89+
pdm sync -v -d
9090
- name: Run Tests
9191
run: |
9292
pdm pytest
@@ -109,7 +109,7 @@ jobs:
109109
- uses: actions-rust-lang/setup-rust-toolchain@v1
110110
- name: Install dependencies
111111
run: |
112-
pdm sync -d
112+
pdm sync -v -d
113113
- name: Type-Check
114114
run: |
115115
pdm pyright
@@ -156,7 +156,7 @@ jobs:
156156
with:
157157
target: ${{ matrix.target }}
158158
container: ${{ env.CONTAINER }}
159-
args: --release --out dist
159+
args: --verbose --release --out dist
160160
sccache: ${{ matrix.target == 'musllinux_1_1' }}
161161
manylinux: auto
162162
docker-options: -e CARGO_NET_GIT_FETCH_WITH_CLI=true
@@ -171,7 +171,7 @@ jobs:
171171
cd /usr/src
172172
curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python3.10 -
173173
export PATH=/root/.local/bin:$PATH
174-
pdm sync -d --no-self -G test
174+
pdm sync -v -d --no-self -G test
175175
pdm run python -m ensurepip
176176
pdm run python -m pip install dist/*.whl
177177
pdm pytest
@@ -196,7 +196,7 @@ jobs:
196196
uses: PyO3/maturin-action@v1
197197
with:
198198
target: ${{ matrix.target }}
199-
args: --release --out dist
199+
args: --verbose --release --out dist
200200
sccache: "true"
201201
- name: Upload wheels
202202
uses: actions/upload-artifact@v3
@@ -208,7 +208,7 @@ jobs:
208208
- name: Test wheels
209209
if: ${{ matrix.target == 'x86_64' }}
210210
run: |
211-
pdm sync -d --no-self -G test
211+
pdm sync -v -d --no-self -G test
212212
pdm run python -m ensurepip
213213
pdm run python -m pip install dist/*.whl
214214
pdm pytest

Cargo.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ crate-type = [
1212
]
1313

1414
[dependencies]
15+
log = "0.4.18"
1516
pyo3 = "0.18.3"
17+
pyo3-log = "0.8.1"
18+
19+
[target.'cfg(target_os = "linux")'.dependencies]
20+
landlock = "0.2.0"
1621

1722
[dev-dependencies]
1823
approx = "0.5.0"

benches/benches_main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn shannon_entropy(c: &mut Criterion) {
1919
BenchmarkId::from_parameter(sample_size),
2020
&sample_size,
2121
|b, &size| {
22-
b.iter(|| unblob_native::math::shannon_entropy(&sample[0..size]));
22+
b.iter(|| unblob_native::math_tools::shannon_entropy(&sample[0..size]));
2323
},
2424
);
2525
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ ignore = [
9393
"D203", # one-blank-line-before-class: D211 (no-blank-line-before-class) is used instead
9494
"D213", # multi-line-summary-second-line: D212 (multi-line-summary-first-line) is used instead
9595
"E501", # line-too-long: Let black handle line length violations
96+
"UP007", # non-pep604-annotation: Python 3.8 support needs legacy annotations
9697
]
9798

9899
[tool.ruff.per-file-ignores]

python/unblob_native/_native/__init__.pyi

Lines changed: 0 additions & 3 deletions
This file was deleted.
File renamed without changes.

python/unblob_native/sandbox.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import typing
3+
4+
import typing_extensions
5+
6+
_Path: typing_extensions.TypeAlias = typing.Union[os.PathLike, str]
7+
8+
class AccessFS:
9+
@staticmethod
10+
def read(access_dir: _Path) -> AccessFS: ...
11+
@staticmethod
12+
def read_write(access_dir: _Path) -> AccessFS: ...
13+
@staticmethod
14+
def make_reg(access_dir: _Path) -> AccessFS: ...
15+
@staticmethod
16+
def make_dir(access_dir: _Path) -> AccessFS: ...
17+
18+
def restrict_access(*args: AccessFS) -> None: ...
19+
20+
class SandboxError(Exception): ...

src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
pub mod math;
1+
pub mod math_tools;
2+
pub mod sandbox;
23

34
use pyo3::prelude::*;
45

5-
/// Calculates Shannon entropy of data
6-
#[pyfunction(text_signature = "(data)")]
7-
pub fn shannon_entropy(py: Python, data: &[u8]) -> PyResult<f64> {
8-
py.allow_threads(|| Ok(math::shannon_entropy(data)))
9-
}
10-
116
/// Performance-critical functionality
127
#[pymodule]
138
fn _native(py: Python, m: &PyModule) -> PyResult<()> {
14-
let math_module = PyModule::new(py, "math_tools")?;
15-
math_module.add_function(wrap_pyfunction!(shannon_entropy, math_module)?)?;
9+
math_tools::init_module(py, m)?;
10+
sandbox::init_module(py, m)?;
11+
12+
pyo3_log::init();
1613

17-
m.add_submodule(math_module)?;
1814
Ok(())
1915
}

src/math.rs renamed to src/math_tools.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use pyo3::prelude::*;
2+
13
pub fn shannon_entropy(data: &[u8]) -> f64 {
24
let mut entropy = 0.0;
35
let mut counts = [0; 256];
@@ -17,6 +19,24 @@ pub fn shannon_entropy(data: &[u8]) -> f64 {
1719

1820
entropy
1921
}
22+
/// Calculates Shannon entropy of data
23+
#[pyfunction(name = "shannon_entropy")]
24+
pub fn py_shannon_entropy(py: Python, data: &[u8]) -> PyResult<f64> {
25+
py.allow_threads(|| Ok(shannon_entropy(data)))
26+
}
27+
28+
pub fn init_module(py: Python, root_module: &PyModule) -> PyResult<()> {
29+
let module = PyModule::new(py, "math_tools")?;
30+
module.add_function(wrap_pyfunction!(py_shannon_entropy, module)?)?;
31+
32+
root_module.add_submodule(module)?;
33+
34+
py.import("sys")?
35+
.getattr("modules")?
36+
.set_item("unblob_native.math", module)?;
37+
38+
Ok(())
39+
}
2040

2141
#[cfg(test)]
2242
mod tests {

0 commit comments

Comments
 (0)