Skip to content

Only use clone3 when needed for pidfd #89930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions library/std/src/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,22 @@ impl Command {
fn clone3(cl_args: *mut clone_args, len: libc::size_t) -> libc::c_long
}

// Bypassing libc for `clone3` can make further libc calls unsafe,
// so we use it sparingly for now. See #89522 for details.
// Some tools (e.g. sandboxing tools) may also expect `fork`
// rather than `clone3`.
let want_clone3_pidfd = self.get_create_pidfd();

// If we fail to create a pidfd for any reason, this will
// stay as -1, which indicates an error.
let mut pidfd: pid_t = -1;

// Attempt to use the `clone3` syscall, which supports more arguments
// (in particular, the ability to create a pidfd). If this fails,
// we will fall through this block to a call to `fork()`
if HAS_CLONE3.load(Ordering::Relaxed) {
let mut flags = 0;
if self.get_create_pidfd() {
flags |= CLONE_PIDFD;
}

if want_clone3_pidfd && HAS_CLONE3.load(Ordering::Relaxed) {
let mut args = clone_args {
flags,
flags: CLONE_PIDFD,
pidfd: &mut pidfd as *mut pid_t as u64,
child_tid: 0,
parent_tid: 0,
Expand Down Expand Up @@ -212,8 +213,8 @@ impl Command {
}
}

// If we get here, the 'clone3' syscall does not exist
// or we do not have permission to call it
// Generally, we just call `fork`. If we get here after wanting `clone3`,
// then the syscall does not exist or we do not have permission to call it.
cvt(libc::fork()).map(|res| (res, pidfd))
}

Expand Down
25 changes: 6 additions & 19 deletions src/test/ui/command/command-pre-exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,15 @@
// ignore-sgx no processes
#![feature(process_exec, rustc_private)]

extern crate libc;

use std::env;
use std::io::Error;
use std::os::unix::process::CommandExt;
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

#[cfg(not(target_os = "linux"))]
fn getpid() -> u32 {
use std::process;
process::id()
}

/// We need to directly use the getpid syscall instead of using `process::id()`
/// because the libc wrapper might return incorrect values after a process was
/// forked.
#[cfg(target_os = "linux")]
fn getpid() -> u32 {
extern crate libc;
unsafe {
libc::syscall(libc::SYS_getpid) as _
}
}

fn main() {
if let Some(arg) = env::args().nth(1) {
match &arg[..] {
Expand Down Expand Up @@ -83,12 +68,14 @@ fn main() {
};
assert_eq!(output.raw_os_error(), Some(102));

let pid = getpid();
let pid = unsafe { libc::getpid() };
assert!(pid >= 0);
let output = unsafe {
Command::new(&me)
.arg("empty")
.pre_exec(move || {
let child = getpid();
let child = libc::getpid();
assert!(child >= 0);
assert!(pid != child);
Ok(())
})
Expand Down
17 changes: 1 addition & 16 deletions src/test/ui/process/process-panic-after-fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ use std::sync::atomic::{AtomicU32, Ordering};

use libc::c_int;

#[cfg(not(target_os = "linux"))]
fn getpid() -> u32 {
process::id()
}

/// We need to directly use the getpid syscall instead of using `process::id()`
/// because the libc wrapper might return incorrect values after a process was
/// forked.
#[cfg(target_os = "linux")]
fn getpid() -> u32 {
unsafe {
libc::syscall(libc::SYS_getpid) as _
}
}

/// This stunt allocator allows us to spot heap allocations in the child.
struct PidChecking<A> {
parent: A,
Expand All @@ -59,7 +44,7 @@ impl<A> PidChecking<A> {
fn check(&self) {
let require_pid = self.require_pid.load(Ordering::Acquire);
if require_pid != 0 {
let actual_pid = getpid();
let actual_pid = process::id();
if require_pid != actual_pid {
unsafe {
libc::raise(libc::SIGUSR1);
Expand Down