Skip to content

Commit 13904d5

Browse files
committed
Add command-line options to configure environment sandbox
The environment sandbox controls access to environment variables and which path prefixes are available for opening from source (ie, with the env!() and include!() class of macros). By default, it does nothing. However, if you configure some sandbox options then they will override the default-open mode of operation. The options are: --env-clear - clear entire environment, making all env!() operations fail --env-allow VAR - allow a specific environment variable to be used --env-define VAR=VAL - define the value of an environment variable for env!() --clear-include-prefixes - clear all valid prefixes, making all include!() operations fail --include-prefix PATH - define a path prefix that all include files must start with These options are cumulative. The environment sandboxing is different from controlling the environment that's present when rustc is invoked, say with the "env" command. Sandboxing allows the environment used by rustc - such as PATH or LD_LIBRARY_PATH (or equiv) - versus the environment that's available to the compiled Rust source itself. (This change just collects the options and sets up the EnvSandbox, but does not implement any constraints.)
1 parent 8780e3f commit 13904d5

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

src/Cargo.lock

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

src/librustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ syntax = { path = "../libsyntax" }
2727
syntax_pos = { path = "../libsyntax_pos" }
2828
backtrace = "0.3.3"
2929
byteorder = { version = "1.1", features = ["i128"]}
30+
env_sandbox = { path = "../librustc_env_sandbox" }
3031

3132
# Note that these dependencies are a lie, they're just here to get linkage to
3233
# work.

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ extern crate rustc_errors as errors;
9191
extern crate syntax_pos;
9292
extern crate jobserver;
9393
extern crate proc_macro;
94+
extern crate env_sandbox;
9495

9596
extern crate serialize as rustc_serialize; // used by deriving
9697

src/librustc/session/config.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use syntax::feature_gate::UnstableFeatures;
3838

3939
use errors::{ColorConfig, FatalError, Handler};
4040

41+
use env_sandbox::{EnvSandboxBuilder, EnvSandbox};
42+
4143
use getopts;
4244
use std::collections::{BTreeMap, BTreeSet};
4345
use std::collections::btree_map::Iter as BTreeMapIter;
@@ -413,6 +415,9 @@ top_level_options!(
413415
// Remap source path prefixes in all output (messages, object files, debug, etc)
414416
remap_path_prefix: Vec<(PathBuf, PathBuf)> [UNTRACKED],
415417
edition: Edition [TRACKED],
418+
419+
// Environment sandbox for process envvars and include path prefixes
420+
env_sb: EnvSandbox [UNTRACKED],
416421
}
417422
);
418423

@@ -593,6 +598,7 @@ pub fn basic_options() -> Options {
593598
cli_forced_thinlto_off: false,
594599
remap_path_prefix: Vec::new(),
595600
edition: DEFAULT_EDITION,
601+
env_sb: EnvSandbox::default(),
596602
}
597603
}
598604

@@ -1653,6 +1659,34 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
16531659
"Remap source names in all output (compiler messages and output files)",
16541660
"FROM=TO",
16551661
),
1662+
opt::multi_s(
1663+
"",
1664+
"env-allow",
1665+
"Allow a specific environment variable to be accessed with an env!() macro",
1666+
"ENVVAR",
1667+
),
1668+
opt::multi_s(
1669+
"",
1670+
"env-define",
1671+
"Define an environment variable for reading with an env!() macro",
1672+
"ENVVAR=VALUE",
1673+
),
1674+
opt::flag_s(
1675+
"",
1676+
"env-clear",
1677+
"Clear all environment, and prevent access to process environment",
1678+
),
1679+
opt::multi_s(
1680+
"",
1681+
"include-prefix",
1682+
"Define a valid prefix for include!() macros",
1683+
"PATH",
1684+
),
1685+
opt::flag_s(
1686+
"",
1687+
"clear-include-prefixes",
1688+
"Clear all path prefixes, disallowing access to all files",
1689+
),
16561690
]);
16571691
opts
16581692
}
@@ -2161,6 +2195,32 @@ pub fn build_session_options_and_crate_config(
21612195
})
21622196
.collect();
21632197

2198+
let mut env_sb = EnvSandboxBuilder::new();
2199+
2200+
if matches.opt_present("env-clear") {
2201+
env_sb.env_clear();
2202+
}
2203+
for env in matches.opt_strs("env-allow") {
2204+
env_sb.env_allow(env);
2205+
}
2206+
for envvar in matches.opt_strs("env-define") {
2207+
let envvar: Vec<_> = envvar.splitn(2, '=').collect();
2208+
if envvar.len() != 2 {
2209+
early_error(error_format, "--env-define must contain '=' between ENVVAR and VALUE");
2210+
}
2211+
env_sb.env_define(envvar[0], envvar[1]);
2212+
}
2213+
2214+
if matches.opt_present("clear-include-prefixes") {
2215+
env_sb.paths_clear();
2216+
}
2217+
for pathpfx in matches.opt_strs("include-prefix") {
2218+
if let Err(err) = env_sb.path_add(pathpfx) {
2219+
early_error(error_format, &format!("--include-prefix path error: {}", err));
2220+
}
2221+
}
2222+
let env_sb = env_sb.build();
2223+
21642224
(
21652225
Options {
21662226
crate_types,
@@ -2191,6 +2251,7 @@ pub fn build_session_options_and_crate_config(
21912251
cli_forced_thinlto_off: disable_thinlto,
21922252
remap_path_prefix,
21932253
edition,
2254+
env_sb,
21942255
},
21952256
cfg,
21962257
)

0 commit comments

Comments
 (0)