Skip to content

saw_nul is never reset for std::process::Command on Linux #165316

Description

@qwaz

Parameters for std::proces::Command, such as the program name, args, current_dir, and env variables (the list is not exhaustive), set saw_nul flag when the value contains a null byte. However, this flag is not cleared even when the offending value is cleared, leaving the command in unusable state.

use std::process::Command;

fn main() {
    let mut cmd = Command::new("/bin/true");
    println!("{:?}", cmd.status());
    cmd.env("BAD\0", "");
    println!("{:?}", cmd.status());
    cmd.env_clear();
    // Doesn't work even after clearing the env
    println!("{:?}", cmd.status());
}

Output:

Ok(ExitStatus(unix_wait_status(0)))
Err(Error { kind: InvalidInput, message: "nul byte found in provided data" })
Err(Error { kind: InvalidInput, message: "nul byte found in provided data" })

Tested environment:

rustc 1.99.0 (b940084d7 2026-09-28)
binary: rustc
commit-hash: b940084d7eb6a299eb4bfeb8e34901bc051e7ac4
commit-date: 2026-09-28
host: x86_64-unknown-linux-gnu
release: 1.99.0
LLVM version: 23.1.1

Reporting on behalf of Microsoft FORGE Lab.

Activity

  1. added
    C-bug
    on Nov 2, 2026
  2. added
    needs-triage
    on Nov 2, 2026
  3. added
    T-libs
    A-process
    and removed
    needs-triage
    on Nov 2, 2026
  4. maxdexh commented on Nov 2, 2026

    @maxdexh
    Member

    We should be able to solve this fairly cheaply by checking the strings again only if saw_nul is set. Since this is going to be the cold case, it shouldn't affect the perf of the type for regular use.

  5. asder8215 commented on Nov 4, 2026

    @asder8215
    Contributor

    Could do a fix for this, been tinkering around with the unix Command stuff for a different bug (that qwaz pointed out in a separate issue a while back ironically) in #157144. This particular issue qwaz is pointing out seems to be occurring here:

    let envp = self.capture_env();
    if self.saw_nul() {
    return Err(io::const_error!(
    ErrorKind::InvalidInput,
    "nul byte found in provided data",
    ));
    }

    From qwaz's demo, the issue could also occur if we called Command::exec after the Command::env_clear since it has the same if self.saw_nul() { ... } (ngl, I'm really not fond of the saw_nul flag which is only seen in the unix Command inner struct, and it tries to encapsulate if we saw an interior nul byte in arg, env, program name, etc., which makes it hard to differentiate if the saw_nul flag was set to true due to arg, env, program name, etcs and we can't just mark it false on Command::env_clear as a result).

    We should be able to solve this fairly cheaply by checking the strings again only if saw_nul is set.

    I don't think I'm following with what you're suggesting. However, since the only place we set saw_nul to true when we look into env variables is in Command::capture_env + construct_envp:

    pub fn capture_env(&mut self) -> Option<CStringArray> {
    let maybe_env = self.env.capture_if_changed();
    maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
    }

    fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
    let mut result = CStringArray::with_capacity(env.len());
    for (mut k, v) in env {
    // Reserve additional space for '=' and null terminator
    k.reserve_exact(v.len() + 2);
    k.push("=");
    k.push(&v);
    // Add the new entry into the array
    if let Ok(item) = CString::new(k.into_vec()) {
    result.push(item);
    } else {
    *saw_nul = true;
    }
    }
    result
    }

    I think the easier and cleaner thing to do is to have each of those function propagate a result, i.e. io::Result<Option<CStringArray>>/io::Result<CStringArray>, where we'd just return the same error that the saw_nul check does below. We probably want to deviate any shared reliance on saw_nul flag for the env variables because we are able to clear/remove our environment variables.

    On the plus side, we can exit early in construct_envp instead of just going through the rest of the BTreeMap<OsString, OsString>.

    @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-processC-bugT-libs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions