Skip to content

Commit e1a5413

Browse files
committed
Better restrict ./y.sh prepare
1 parent 69c0ec9 commit e1a5413

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

build_system/landlock.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use std::env;
2+
use std::path::Path;
3+
14
use landlock::{
25
path_beneath_rules, Access, AccessFs, Compatible, RulesetAttr, RulesetCreated,
36
RulesetCreatedAttr, ABI,
47
};
58

9+
use crate::rustc_info::get_cargo_home;
10+
611
/// Base landlock ruleset
712
///
813
/// This allows access to various essential system locations.
@@ -20,42 +25,29 @@ pub(super) fn base_ruleset() -> RulesetCreated {
2025
.unwrap()
2126
.add_rules(path_beneath_rules(&["/tmp", "/dev/null"], access_all))
2227
.unwrap()
23-
.add_rules(landlock::path_beneath_rules(
24-
&[std::env::home_dir().unwrap().join(".cargo/registry")],
25-
access_all,
26-
))
27-
.unwrap()
2828
}
2929

3030
pub(super) fn lock_fetch() {
31-
let abi = landlock::ABI::V2;
32-
let access_all = landlock::AccessFs::from_all(abi);
31+
let abi = ABI::V2;
32+
let access_all = AccessFs::from_all(abi);
3333
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-
))
34+
.add_rules(path_beneath_rules([env::current_dir().unwrap().join("download")], access_all))
4535
.unwrap()
4636
.restrict_self()
4737
.unwrap();
4838
}
4939

50-
pub(super) fn lock_build() {
51-
let abi = landlock::ABI::V2;
52-
let access_all = landlock::AccessFs::from_all(abi);
40+
pub(super) fn lock_build(cargo: &Path) {
41+
let abi = ABI::V2;
42+
let access_all = AccessFs::from_all(abi);
5343
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-
],
44+
.add_rules(path_beneath_rules(
45+
&[get_cargo_home(cargo).join("git"), get_cargo_home(cargo).join("registry")],
46+
access_all,
47+
))
48+
.unwrap()
49+
.add_rules(path_beneath_rules(
50+
&[env::current_dir().unwrap().join("build"), env::current_dir().unwrap().join("dist")],
5951
access_all,
6052
))
6153
.unwrap()

build_system/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn main() {
204204
std::fs::File::create(target).unwrap();
205205
}
206206

207-
landlock::lock_build();
207+
landlock::lock_build(&bootstrap_host_compiler.cargo);
208208

209209
env::set_var("RUSTC", "rustc_should_be_set_explicitly");
210210
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");

build_system/rustc_info.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ pub(crate) fn get_toolchain_name() -> String {
2727
String::from_utf8(active_toolchain).unwrap().trim().split_once(' ').unwrap().0.to_owned()
2828
}
2929

30+
pub(crate) fn get_cargo_home(cargo: &Path) -> PathBuf {
31+
let cargo_home = Command::new(cargo)
32+
.stderr(Stdio::inherit())
33+
.args(&["-Zunstable-options", "config", "get", "--format=json-value", "home"])
34+
.output()
35+
.unwrap()
36+
.stdout;
37+
PathBuf::from(
38+
String::from_utf8(cargo_home)
39+
.unwrap()
40+
.trim()
41+
.strip_prefix('"')
42+
.unwrap()
43+
.strip_suffix('"')
44+
.unwrap(),
45+
)
46+
}
47+
3048
pub(crate) fn get_cargo_path() -> PathBuf {
3149
if let Ok(cargo) = std::env::var("CARGO") {
3250
return PathBuf::from(cargo);

0 commit comments

Comments
 (0)