Skip to content

Commit 54c88f3

Browse files
committed
[WIP] Use landlock
1 parent ade0e38 commit 54c88f3

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

build_system/Cargo.lock

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

build_system/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ path = "main.rs"
1111
unstable-features = [] # for rust-analyzer
1212

1313
# Do not add any dependencies
14+
[dependencies]
15+
landlock = "0.4"
1416

1517
[profile.dev]
1618
debug = 1

build_system/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ enum CodegenBackend {
5454
}
5555

5656
fn main() {
57+
use landlock::{Access, Compatible, RulesetAttr, RulesetCreatedAttr};
58+
let abi = landlock::ABI::V2;
59+
let access_all = landlock::AccessFs::from_all(abi);
60+
let access_read = landlock::AccessFs::from_read(abi);
61+
landlock::Ruleset::default()
62+
.set_compatibility(landlock::CompatLevel::BestEffort)
63+
.handle_access(access_all)
64+
.unwrap()
65+
.create()
66+
.unwrap()
67+
.add_rules(landlock::path_beneath_rules(&["/"], access_read))
68+
.unwrap()
69+
.add_rules(landlock::path_beneath_rules(&["/tmp", "/dev/null"], access_all))
70+
.unwrap()
71+
.add_rules(landlock::path_beneath_rules(
72+
&[
73+
std::env::current_dir().unwrap().join("build"),
74+
std::env::current_dir().unwrap().join("dist"),
75+
],
76+
access_all,
77+
))
78+
.unwrap()
79+
.add_rules(landlock::path_beneath_rules(
80+
&[std::env::home_dir().unwrap().join(".cargo/registry")],
81+
access_all,
82+
))
83+
.unwrap()
84+
.restrict_self()
85+
.unwrap();
86+
5787
if env::var_os("RUST_BACKTRACE").is_none() {
5888
env::set_var("RUST_BACKTRACE", "1");
5989
}

build_system/utils.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,20 @@ pub(crate) fn retry_spawn_and_wait(tries: u64, mut cmd: Command) {
176176
}
177177

178178
pub(crate) fn remove_dir_if_exists(path: &Path) {
179-
match fs::remove_dir_all(&path) {
180-
Ok(()) => {}
181-
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
182-
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
179+
for entry in fs::read_dir(&path).unwrap() {
180+
let entry = entry.unwrap();
181+
if entry.file_type().unwrap().is_dir() {
182+
fs::remove_dir_all(entry.path()).unwrap();
183+
} else {
184+
fs::remove_file(entry.path()).unwrap();
185+
}
183186
}
187+
188+
//match fs::remove_dir_all(&path) {
189+
// Ok(()) => {}
190+
// Err(err) if err.kind() == io::ErrorKind::NotFound => {}
191+
// Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
192+
//}
184193
}
185194

186195
pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) {

0 commit comments

Comments
 (0)