Skip to content

Commit fc85278

Browse files
committed
Validate environment variable names in std::process
Make sure that they're not empty and do not contain `=` signs beyond the first character. This prevents environment variable injection, because previously, setting the `PATH=/opt:` variable to `foobar` would lead to the `PATH` variable being overridden. Fixes #122335.
1 parent c90b430 commit fc85278

File tree

6 files changed

+110
-41
lines changed

6 files changed

+110
-41
lines changed

library/std/src/process/tests.rs

+36
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,42 @@ fn test_interior_nul_in_env_value_is_error() {
378378
}
379379
}
380380

381+
#[test]
382+
fn test_empty_env_key_is_error() {
383+
match env_cmd().env("", "value").spawn() {
384+
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
385+
Ok(_) => panic!(),
386+
}
387+
}
388+
389+
#[test]
390+
fn test_interior_equals_in_env_key_is_error() {
391+
match env_cmd().env("has=equals", "value").spawn() {
392+
Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
393+
Ok(_) => panic!(),
394+
}
395+
}
396+
397+
#[test]
398+
fn test_env_leading_equals() {
399+
let mut cmd;
400+
if cfg!(not(target_os = "windows")) {
401+
cmd = env_cmd();
402+
} else {
403+
cmd = Command::new("cmd");
404+
cmd.arg("/c");
405+
cmd.arg("echo =RUN_TEST_LEADING_EQUALS=%=RUN_TEST_LEADING_EQUALS%");
406+
}
407+
cmd.env("=RUN_TEST_LEADING_EQUALS", "789=012");
408+
let result = cmd.output().unwrap();
409+
let output = String::from_utf8_lossy(&result.stdout).to_string();
410+
411+
assert!(
412+
output.contains("=RUN_TEST_LEADING_EQUALS=789=012"),
413+
"didn't find =RUN_TEST_LEADING_EQUALS inside of:\n\n{output}",
414+
);
415+
}
416+
381417
/// Tests that process creation flags work by debugging a process.
382418
/// Other creation flags make it hard or impossible to detect
383419
/// behavioral changes in the process.

library/std/src/sys/pal/unix/process/process_common.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct Command {
100100
uid: Option<uid_t>,
101101
gid: Option<gid_t>,
102102
saw_nul: bool,
103+
saw_invalid_env_key: bool,
103104
closures: Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>>,
104105
groups: Option<Box<[gid_t]>>,
105106
stdin: Option<Stdio>,
@@ -193,6 +194,7 @@ impl Command {
193194
uid: None,
194195
gid: None,
195196
saw_nul,
197+
saw_invalid_env_key: false,
196198
closures: Vec::new(),
197199
groups: None,
198200
stdin: None,
@@ -217,6 +219,7 @@ impl Command {
217219
uid: None,
218220
gid: None,
219221
saw_nul,
222+
saw_invalid_env_key: false,
220223
closures: Vec::new(),
221224
groups: None,
222225
stdin: None,
@@ -279,8 +282,17 @@ impl Command {
279282
self.create_pidfd
280283
}
281284

282-
pub fn saw_nul(&self) -> bool {
283-
self.saw_nul
285+
pub fn validate_input(&self) -> io::Result<()> {
286+
if self.saw_invalid_env_key {
287+
Err(io::const_io_error!(
288+
io::ErrorKind::InvalidInput,
289+
"env key empty or equals sign found in env key",
290+
))
291+
} else if self.saw_nul {
292+
Err(io::const_io_error!(io::ErrorKind::InvalidInput, "nul byte found in provided data"))
293+
} else {
294+
Ok(())
295+
}
284296
}
285297

286298
pub fn get_program(&self) -> &OsStr {
@@ -361,7 +373,7 @@ impl Command {
361373

362374
pub fn capture_env(&mut self) -> Option<CStringArray> {
363375
let maybe_env = self.env.capture_if_changed();
364-
maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
376+
maybe_env.map(|env| construct_envp(env, &mut self.saw_nul, &mut self.saw_invalid_env_key))
365377
}
366378

367379
#[allow(dead_code)]
@@ -426,9 +438,18 @@ impl CStringArray {
426438
}
427439
}
428440

429-
fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
441+
fn construct_envp(
442+
env: BTreeMap<OsString, OsString>,
443+
saw_nul: &mut bool,
444+
saw_invalid_env_key: &mut bool,
445+
) -> CStringArray {
430446
let mut result = CStringArray::with_capacity(env.len());
431447
for (mut k, v) in env {
448+
if k.is_empty() || k.as_bytes()[1..].contains(&b'=') {
449+
*saw_invalid_env_key = true;
450+
continue;
451+
}
452+
432453
// Reserve additional space for '=' and null terminator
433454
k.reserve_exact(v.len() + 2);
434455
k.push("=");

library/std/src/sys/pal/unix/process/process_fuchsia.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ impl Command {
2121
) -> io::Result<(Process, StdioPipes)> {
2222
let envp = self.capture_env();
2323

24-
if self.saw_nul() {
25-
return Err(io::const_io_error!(
26-
io::ErrorKind::InvalidInput,
27-
"nul byte found in provided data",
28-
));
29-
}
24+
self.validate_input()?;
3025

3126
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
3227

@@ -41,11 +36,8 @@ impl Command {
4136
}
4237

4338
pub fn exec(&mut self, default: Stdio) -> io::Error {
44-
if self.saw_nul() {
45-
return io::const_io_error!(
46-
io::ErrorKind::InvalidInput,
47-
"nul byte found in provided data",
48-
);
39+
if let Err(err) = self.validate_input() {
40+
return err;
4941
}
5042

5143
match self.setup_io(default, true) {

library/std/src/sys/pal/unix/process/process_unix.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fmt;
2-
use crate::io::{self, Error, ErrorKind};
2+
use crate::io::{self, Error};
33
use crate::mem;
44
use crate::num::NonZero;
55
use crate::sys;
@@ -64,12 +64,7 @@ impl Command {
6464

6565
let envp = self.capture_env();
6666

67-
if self.saw_nul() {
68-
return Err(io::const_io_error!(
69-
ErrorKind::InvalidInput,
70-
"nul byte found in provided data",
71-
));
72-
}
67+
self.validate_input()?;
7368

7469
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
7570

@@ -180,7 +175,7 @@ impl Command {
180175
// way to avoid that all-together.
181176
#[cfg(any(target_os = "tvos", target_os = "watchos"))]
182177
const ERR_APPLE_TV_WATCH_NO_FORK_EXEC: Error = io::const_io_error!(
183-
ErrorKind::Unsupported,
178+
io::ErrorKind::Unsupported,
184179
"`fork`+`exec`-based process spawning is not supported on this target",
185180
);
186181

@@ -221,7 +216,7 @@ impl Command {
221216
thread::sleep(delay);
222217
} else {
223218
return Err(io::const_io_error!(
224-
ErrorKind::WouldBlock,
219+
io::ErrorKind::WouldBlock,
225220
"forking returned EBADF too often",
226221
));
227222
}
@@ -236,8 +231,8 @@ impl Command {
236231
pub fn exec(&mut self, default: Stdio) -> io::Error {
237232
let envp = self.capture_env();
238233

239-
if self.saw_nul() {
240-
return io::const_io_error!(ErrorKind::InvalidInput, "nul byte found in provided data",);
234+
if let Err(err) = self.validate_input() {
235+
return err;
241236
}
242237

243238
match self.setup_io(default, true) {
@@ -497,7 +492,7 @@ impl Command {
497492
thread::sleep(delay);
498493
} else {
499494
return Err(io::const_io_error!(
500-
ErrorKind::WouldBlock,
495+
io::ErrorKind::WouldBlock,
501496
"posix_spawnp returned EBADF too often",
502497
));
503498
}
@@ -1084,7 +1079,6 @@ mod linux_child_ext {
10841079
use crate::mem;
10851080
use crate::os::linux::process as os;
10861081
use crate::sys::pal::unix::linux::pidfd as imp;
1087-
use crate::sys::pal::unix::ErrorKind;
10881082
use crate::sys_common::FromInner;
10891083

10901084
#[unstable(feature = "linux_pidfd", issue = "82971")]
@@ -1095,7 +1089,9 @@ mod linux_child_ext {
10951089
.as_ref()
10961090
// SAFETY: The os type is a transparent wrapper, therefore we can transmute references
10971091
.map(|fd| unsafe { mem::transmute::<&imp::PidFd, &os::PidFd>(fd) })
1098-
.ok_or_else(|| io::Error::new(ErrorKind::Uncategorized, "No pidfd was created."))
1092+
.ok_or_else(|| {
1093+
io::Error::new(io::ErrorKind::Uncategorized, "No pidfd was created.")
1094+
})
10991095
}
11001096

11011097
fn into_pidfd(mut self) -> Result<os::PidFd, Self> {

library/std/src/sys/pal/unix/process/process_vxworks.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ impl Command {
2121
use crate::sys::cvt_r;
2222
let envp = self.capture_env();
2323

24-
if self.saw_nul() {
25-
return Err(io::const_io_error!(
26-
ErrorKind::InvalidInput,
27-
"nul byte found in provided data",
28-
));
29-
}
24+
self.validate_input()?;
25+
3026
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
3127
let mut p = Process { pid: 0, status: None };
3228

library/std/src/sys/pal/windows/process.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,40 @@ impl AsRef<OsStr> for EnvKey {
149149
}
150150
}
151151

152-
pub(crate) fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> {
153-
if str.as_ref().encode_wide().any(|b| b == 0) {
154-
Err(io::const_io_error!(ErrorKind::InvalidInput, "nul byte found in provided data"))
155-
} else {
156-
Ok(str)
152+
/// Returns an error if the provided string has a NUL byte anywhere or a `=`
153+
/// after the first character.
154+
fn ensure_env_var_name<T: AsRef<OsStr>>(s: T) -> io::Result<T> {
155+
fn inner(s: &OsStr) -> io::Result<()> {
156+
let err = || {
157+
Err(io::const_io_error!(
158+
ErrorKind::InvalidInput,
159+
"nul or '=' byte found in provided data",
160+
))
161+
};
162+
let mut iter = s.as_encoded_bytes().iter();
163+
if iter.next() == Some(&0) {
164+
return err();
165+
}
166+
if iter.any(|&b| b == 0 || b == b'=') {
167+
return err();
168+
}
169+
Ok(())
170+
}
171+
inner(s.as_ref())?;
172+
Ok(s)
173+
}
174+
175+
/// Returns an error if the provided string has a NUL byte anywhere.
176+
pub(crate) fn ensure_no_nuls<T: AsRef<OsStr>>(s: T) -> io::Result<T> {
177+
fn inner(s: &OsStr) -> io::Result<()> {
178+
if s.as_encoded_bytes().iter().any(|&b| b == 0) {
179+
Err(io::const_io_error!(ErrorKind::InvalidInput, "nul byte found in provided data"))
180+
} else {
181+
Ok(())
182+
}
157183
}
184+
inner(s.as_ref())?;
185+
Ok(s)
158186
}
159187

160188
pub struct Command {
@@ -867,7 +895,7 @@ fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>) -> io::Result<(*mut
867895
}
868896

869897
for (k, v) in env {
870-
ensure_no_nuls(k.os_string)?;
898+
ensure_env_var_name(k.os_string)?;
871899
blk.extend(k.utf16);
872900
blk.push('=' as u16);
873901
blk.extend(ensure_no_nuls(v)?.encode_wide());

0 commit comments

Comments
 (0)