Skip to content

File descriptors passed to Command::stdout/err/in are not closed after dup2 #147624

@i509VCB

Description

@i509VCB

File descriptors passed to Command::stdout/err/in are not closed after dup2 to replace stdout/stderr/stdin. This means the file descriptors passed to Command::stdout/err/in are leaked in the child process.

Example:

use std::fs::File;
use std::process::{Command, Stdio};

fn main() -> std::io::Result<()> {
    let file = File::create("output.txt")?;

    let mut child = Command::new("echo")
        .arg("Hello from the child process!")
        .stdout(Stdio::from(file))
        .spawn()?;

    let status = child.wait()?;

    println!("Child exited with status: {}", status);

    Ok(())
}

strace -f output (full output is attached):

3903339 dup2(3, 1)                      = 1
3903339 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
3903339 execve("/home/i509vcb/.cargo/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/home/i509vcb/.local/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/home/i509vcb/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/lib64/ccache/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/local/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */ <unfinished ...>
3903338 <... clone3 resumed>)           = 3903339

This will leak fd 3 on the child process. Ideally do_exec would close the stdio file descriptors which were overriden after dup2 on the child:

) -> Result<!, io::Error> {

strace.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions