Skip to content

std::process::Command does not work properly with less privileged children accessing stdin/stdout/stderr file descriptors #119113

Open
@TaaviE

Description

@TaaviE

I tried this code:

use std::io::Write;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};

fn main() {
    let mut command = Command::new("/bin/cat");
    command.arg("/dev/fd/0")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .uid(35565)
        .gid(35565);

    let mut child = command.spawn().unwrap();
    child.stdin.take().unwrap().write_all("foo".as_ref()).unwrap();
}
Tokio version:

use std::process::Stdio;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::Command;

#[tokio::main]
async fn main() {
    tokio::task::spawn(async move {
        let mut command = Command::new("/bin/cat");
        command.arg("/dev/fd/0")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .uid(35565)
            .gid(35565);

        let mut child = command.spawn().unwrap();
        child.stdin.take().unwrap().write_all("foo".as_ref()).await.unwrap();
    });
}

I expected to see this happen:
The child process opens and reads it's stdin.

Instead, this happened:
The child process was unable to read it's stdin fd.

Meta

I've asked around about this issue in other places and it seems that it's Linux quirk that the Rust standard library does not handle. Neither does there seem to be a proper way to handle it (nor do I know how to safely work around stdlib).

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-processArea: `std::process` and `std::env`C-bugCategory: This is a bug.O-unixOperating system: Unix-likeT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions