Skip to content

Commit 69c0ec9

Browse files
committed
Separate landlock rules for prepare and build
1 parent 54c88f3 commit 69c0ec9

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

build_system/landlock.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use landlock::{
2+
path_beneath_rules, Access, AccessFs, Compatible, RulesetAttr, RulesetCreated,
3+
RulesetCreatedAttr, ABI,
4+
};
5+
6+
/// Base landlock ruleset
7+
///
8+
/// This allows access to various essential system locations.
9+
pub(super) fn base_ruleset() -> RulesetCreated {
10+
let abi = ABI::V2;
11+
let access_all = AccessFs::from_all(abi);
12+
let access_read = AccessFs::from_read(abi);
13+
landlock::Ruleset::default()
14+
.set_compatibility(landlock::CompatLevel::BestEffort)
15+
.handle_access(access_all)
16+
.unwrap()
17+
.create()
18+
.unwrap()
19+
.add_rules(path_beneath_rules(&["/"], access_read))
20+
.unwrap()
21+
.add_rules(path_beneath_rules(&["/tmp", "/dev/null"], access_all))
22+
.unwrap()
23+
.add_rules(landlock::path_beneath_rules(
24+
&[std::env::home_dir().unwrap().join(".cargo/registry")],
25+
access_all,
26+
))
27+
.unwrap()
28+
}
29+
30+
pub(super) fn lock_fetch() {
31+
let abi = landlock::ABI::V2;
32+
let access_all = landlock::AccessFs::from_all(abi);
33+
base_ruleset()
34+
.add_rules(landlock::path_beneath_rules(
35+
&[
36+
std::env::current_dir().unwrap().join("build"), // FIXME only enable during ./y.rs build
37+
],
38+
access_all,
39+
))
40+
.unwrap()
41+
.add_rules(path_beneath_rules(
42+
[std::env::current_dir().unwrap().join("download")],
43+
access_all,
44+
))
45+
.unwrap()
46+
.restrict_self()
47+
.unwrap();
48+
}
49+
50+
pub(super) fn lock_build() {
51+
let abi = landlock::ABI::V2;
52+
let access_all = landlock::AccessFs::from_all(abi);
53+
base_ruleset()
54+
.add_rules(landlock::path_beneath_rules(
55+
&[
56+
std::env::current_dir().unwrap().join("build"),
57+
std::env::current_dir().unwrap().join("dist"),
58+
],
59+
access_all,
60+
))
61+
.unwrap()
62+
.restrict_self()
63+
.unwrap();
64+
}

build_system/main.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod bench;
1212
mod build_backend;
1313
mod build_sysroot;
1414
mod config;
15+
mod landlock;
1516
mod path;
1617
mod prepare;
1718
mod rustc_info;
@@ -54,36 +55,6 @@ enum CodegenBackend {
5455
}
5556

5657
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-
8758
if env::var_os("RUST_BACKTRACE").is_none() {
8859
env::set_var("RUST_BACKTRACE", "1");
8960
}
@@ -160,15 +131,21 @@ fn main() {
160131
out_dir = current_dir.join(out_dir);
161132

162133
if command == Command::Prepare {
163-
prepare::prepare(&path::Dirs {
134+
let dirs = path::Dirs {
164135
source_dir: current_dir.clone(),
165136
download_dir: download_dir
166137
.map(|dir| current_dir.join(dir))
167138
.unwrap_or_else(|| out_dir.join("download")),
168139
build_dir: PathBuf::from("dummy_do_not_use"),
169140
dist_dir: PathBuf::from("dummy_do_not_use"),
170141
frozen,
171-
});
142+
};
143+
144+
path::RelPath::DOWNLOAD.ensure_exists(&dirs);
145+
146+
landlock::lock_fetch();
147+
148+
prepare::prepare(&dirs);
172149
process::exit(0);
173150
}
174151

@@ -216,6 +193,7 @@ fn main() {
216193
};
217194

218195
path::RelPath::BUILD.ensure_exists(&dirs);
196+
path::RelPath::DIST.ensure_exists(&dirs);
219197

220198
{
221199
// Make sure we always explicitly specify the target dir
@@ -226,6 +204,8 @@ fn main() {
226204
std::fs::File::create(target).unwrap();
227205
}
228206

207+
landlock::lock_build();
208+
229209
env::set_var("RUSTC", "rustc_should_be_set_explicitly");
230210
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
231211

0 commit comments

Comments
 (0)