Skip to main content

reverie_process/
spawn.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9use std::io;
10use std::io::Write;
11
12use syscalls::Errno;
13
14use super::Child;
15use super::Command;
16use super::clone::clone;
17use super::container::ChildContext;
18use super::error::Context;
19use super::error::Error;
20use super::fd::Fd;
21use super::fd::pipe;
22use super::id_map::make_id_map;
23use super::launch_window::Launch;
24use super::launch_window::TransientOpen;
25use super::seccomp::SeccompNotif;
26use super::stdio::ChildStderr;
27use super::stdio::ChildStdin;
28use super::stdio::ChildStdout;
29use super::util::CStringArray;
30use super::util::SharedValue;
31
32/// The allocated parts of a launch, made before its lock (`Command::prepare`).
33struct Prepared {
34    env: CStringArray,
35    uid_map: Vec<u8>,
36    gid_map: Vec<u8>,
37}
38
39impl Command {
40    /// Executes the command as a child process, returning a handle to it.
41    ///
42    /// By default, stdin, stdout and stderr are inherited from the parent.
43    pub fn spawn(&mut self) -> Result<Child, Error> {
44        self.container.validate_pinned_clone()?;
45        let prepared = self.prepare();
46
47        // Every descriptor the child may inherit is made under the launch
48        // lock, which ends when clone returns (see `launch_window`). On an
49        // early return the lock, declared later, ends before `prepared` is
50        // freed.
51        let launch = Launch::begin();
52
53        // Create a pipe to send back errors to the parent process if `execve`
54        // fails.
55        let (reader, mut writer) = launch.allocate(pipe, Errno::into_raw)?;
56
57        let child = self.spawn_launched(prepared, launch, |err| {
58            send_error(&mut writer, err);
59            1
60        })?;
61
62        // Close the writer end. Otherwise, the following read will hang
63        // forever.
64        drop(writer);
65
66        recv_error(reader)?;
67
68        Ok(child)
69    }
70
71    /// Spawn the child with helper functions. The `onfail` callback runs in the
72    /// child process if an error occurs during execution of the process. The
73    /// `wait` function can be used to wait for the child to fully start up and
74    /// to transform it into another type.
75    pub fn spawn_with<F>(&mut self, onfail: F) -> Result<Child, Error>
76    where
77        F: FnMut(Error) -> i32,
78    {
79        self.container.validate_pinned_clone()?;
80        let prepared = self.prepare();
81        self.spawn_launched(prepared, Launch::begin(), onfail)
82    }
83
84    /// Builds what a launch needs that allocates, before the launch lock is
85    /// taken. Nothing may allocate or free under that lock: an allocator
86    /// can hold its own lock, or call code that needs a transient open, while
87    /// it waits for the launch to end.
88    fn prepare(&self) -> Prepared {
89        Prepared {
90            env: self.container.env.array(),
91            uid_map: make_id_map(&self.container.uid_map),
92            gid_map: make_id_map(&self.container.gid_map),
93        }
94    }
95
96    fn spawn_launched<F>(
97        &mut self,
98        prepared: Prepared,
99        launch: Launch,
100        mut onfail: F,
101    ) -> Result<Child, Error>
102    where
103        F: FnMut(Error) -> i32,
104    {
105        let Prepared {
106            env,
107            uid_map,
108            gid_map,
109        } = prepared;
110
111        let clone_flags = self.container.namespace.bits() | libc::SIGCHLD;
112
113        // Under the lock: the child's descriptors, its shared page and the
114        // clone, all system calls. On failure only descriptors and that page
115        // are released, which frees no memory, and the error is plain data.
116        let launched = (|| {
117            // Set up IO pipes
118            let (stdin, child_stdin) =
119                launch.allocate(|| self.container.stdin.pipes(true), Errno::into_raw)?;
120            let (stdout, child_stdout) =
121                launch.allocate(|| self.container.stdout.pipes(false), Errno::into_raw)?;
122            let (stderr, child_stderr) =
123                launch.allocate(|| self.container.stdout.pipes(false), Errno::into_raw)?;
124
125            let seccomp_fd = if self.container.seccomp_notify {
126                Some(SharedValue::new(core::sync::atomic::AtomicI32::new(0))?)
127            } else {
128                None
129            };
130
131            let context = ChildContext {
132                stdin: child_stdin.as_ref(),
133                stdout: child_stdout.as_ref(),
134                stderr: child_stderr.as_ref(),
135                uid_map: &uid_map,
136                gid_map: &gid_map,
137                seccomp_fd: seccomp_fd.as_ref().map(|x| x.as_ref()),
138            };
139
140            let pid = clone(
141                || {
142                    let code = onfail(self.do_exec(&context, &env));
143                    unsafe { libc::_exit(code) }
144                },
145                clone_flags,
146            )?;
147            Ok::<_, Error>((
148                pid,
149                [stdin, stdout, stderr],
150                [child_stdin, child_stdout, child_stderr],
151                seccomp_fd,
152            ))
153        })();
154        // The child exists, so it cannot inherit what the parent opens from
155        // here on. The waits below for the child's startup must not hold the
156        // lock: the child's setup may wait on a thread that needs it.
157        drop(launch);
158        let (pid, [stdin, stdout, stderr], [child_stdin, child_stdout, child_stderr], seccomp_fd) =
159            launched?;
160
161        drop(child_stdin);
162        drop(child_stdout);
163        drop(child_stderr);
164        drop(self.container.pty.take());
165
166        let seccomp_notif = match seccomp_fd {
167            Some(shared_fd) => {
168                use core::sync::atomic::Ordering;
169
170                // Spin until the value changes in the child.
171                let mut targetfd = 0;
172                while targetfd == 0 {
173                    targetfd = shared_fd.as_ref().load(Ordering::Relaxed);
174                    std::thread::yield_now();
175                }
176
177                // Use pidfd_getfd to copy the file descriptor
178                let fd = {
179                    let _open = TransientOpen::begin();
180                    let pidfd = Fd::pidfd_open(pid.into(), 0)?;
181                    pidfd.pidfd_getfd(targetfd, 0)?
182                };
183
184                // We've successfully duplicated the file descriptor. Let the
185                // child continue on to execve.
186                shared_fd.as_ref().store(0, Ordering::Relaxed);
187
188                Some(SeccompNotif::new(fd)?)
189            }
190            None => None,
191        };
192
193        let stdin = stdin.map(ChildStdin::new).transpose()?;
194        let stdout = stdout.map(ChildStdout::new).transpose()?;
195        let stderr = stderr.map(ChildStderr::new).transpose()?;
196
197        Ok(Child {
198            pid,
199            exit_status: None,
200            seccomp_notif,
201            stdin,
202            stdout,
203            stderr,
204        })
205    }
206
207    /// Note: This function MUST NOT allocate or deallocate any memory. Doing so
208    /// can cause deadlocks.
209    ///
210    /// Only returns if an error occurs, thus it is only possible for it to
211    /// return an error.
212    fn do_exec(&mut self, context: &ChildContext, env: &CStringArray) -> Error {
213        // Refuse before child setup can publish an irreversible filter.
214        if self.terminal_execve.is_some() && !self.program.to_bytes().starts_with(b"/") {
215            return Error::new(Errno::EINVAL, Context::Exec);
216        }
217        if let Err(err) = self.container.setup(context, &mut self.pre_exec) {
218            return err;
219        }
220        if let Some(entry) = self.terminal_execve {
221            // SAFETY: installer supplied the trusted terminal entry. The
222            // actual prepared vectors are borrowed unchanged until exec.
223            unsafe { entry(self.program.as_ptr(), self.args.as_ptr(), env.as_ptr()) }
224        }
225
226        Error::result(
227            unsafe { execvpe_zeroed_tail(&self.program, self.args.as_ptr(), env.as_ptr()) },
228            Context::Exec,
229        )
230        .unwrap_err()
231    }
232}
233
234/// Issues `execve` with the three argument registers `execve` ignores
235/// (arg3..arg5) set to zero.
236///
237/// `libc::execvpe` reaches the `execve` instruction with whatever the launcher
238/// last left in those registers. The kernel ignores them and clears them in the
239/// new image, but a ptrace tracer records all six argument registers at the
240/// seccomp stop, so leftover launcher state would enter the recorded launch.
241/// glibc's `syscall(3)` moves its fifth, sixth and seventh arguments into r10,
242/// r8 and r9, so passing explicit zeros defines them.
243///
244/// Returns -1 with `errno` set, like `execve(2)`.
245unsafe fn execve_zeroed_tail(
246    path: *const libc::c_char,
247    argv: *const *const libc::c_char,
248    envp: *const *const libc::c_char,
249) -> libc::c_int {
250    let zero: libc::c_long = 0;
251    unsafe { libc::syscall(libc::SYS_execve, path, argv, envp, zero, zero, zero) as libc::c_int }
252}
253
254/// Behaves like glibc `execvpe(3)`, but issues every `execve` through
255/// [`execve_zeroed_tail`]. The `PATH` search and its error precedence follow
256/// glibc: `EACCES` is remembered, and `ENOENT`, `ESTALE`, `ENOTDIR`, `ENODEV`
257/// and `ETIMEDOUT` move on to the next entry. A candidate that fails with
258/// `ENOEXEC` is handed to `libc::execvpe`, which runs it through `/bin/sh`.
259///
260/// MUST NOT allocate: this runs in the child between `clone` and `execve`.
261unsafe fn execvpe_zeroed_tail(
262    program: &std::ffi::CStr,
263    argv: *const *const libc::c_char,
264    envp: *const *const libc::c_char,
265) -> libc::c_int {
266    const NAME_MAX: usize = libc::NAME_MAX as usize;
267    const PATH_MAX: usize = libc::PATH_MAX as usize;
268    let errno = || io::Error::last_os_error().raw_os_error().unwrap_or(0);
269    let set_errno = |value| unsafe { *libc::__errno_location() = value };
270
271    let file = program.to_bytes();
272    if file.is_empty() {
273        set_errno(libc::ENOENT);
274        return -1;
275    }
276    if file.contains(&b'/') {
277        unsafe { execve_zeroed_tail(program.as_ptr(), argv, envp) };
278        if errno() == libc::ENOEXEC {
279            return unsafe { libc::execvpe(program.as_ptr(), argv, envp) };
280        }
281        return -1;
282    }
283    if file.len() > NAME_MAX {
284        set_errno(libc::ENAMETOOLONG);
285        return -1;
286    }
287
288    let path = unsafe { libc::getenv(c"PATH".as_ptr()) };
289    let path = if path.is_null() {
290        &b"/bin:/usr/bin"[..]
291    } else {
292        unsafe { std::ffi::CStr::from_ptr(path) }.to_bytes()
293    };
294
295    let mut buffer = [0u8; PATH_MAX + NAME_MAX + 2];
296    let mut got_eacces = false;
297    for dir in path.split(|byte| *byte == b':') {
298        if dir.len() >= PATH_MAX {
299            continue;
300        }
301        // An empty entry means the current directory, as in glibc.
302        let mut len = dir.len();
303        buffer[..len].copy_from_slice(dir);
304        if !dir.is_empty() {
305            buffer[len] = b'/';
306            len += 1;
307        }
308        buffer[len..len + file.len()].copy_from_slice(file);
309        buffer[len + file.len()] = 0;
310        let candidate = buffer.as_ptr() as *const libc::c_char;
311
312        unsafe { execve_zeroed_tail(candidate, argv, envp) };
313        match errno() {
314            libc::ENOEXEC => return unsafe { libc::execvpe(candidate, argv, envp) },
315            libc::EACCES => got_eacces = true,
316            libc::ENOENT | libc::ESTALE | libc::ENOTDIR | libc::ENODEV | libc::ETIMEDOUT => {}
317            _ => return -1,
318        }
319    }
320    if got_eacces {
321        set_errno(libc::EACCES);
322    }
323    -1
324}
325
326/// Sends an error and closes the pipe. Ignore any errors if this fails.
327pub fn send_error(fd: &mut Fd, err: Error) {
328    // Writes up to PIPE_BUF (4096) should be atomic. There's also nothing we
329    // can do with an error if this fails.
330    let bytes: [u8; 8] = err.into();
331    let _ = fd.write(&bytes);
332}
333
334/// Tries to receive an error code from the pipe. If the other end of the
335/// pipe is closed before sending an error, then `Ok(())` is returned.
336pub fn recv_error(mut fd: Fd) -> Result<(), Error> {
337    use std::io::Read;
338    let mut err = [0u8; 8];
339    loop {
340        match fd.read(&mut err) {
341            Ok(0) => return Ok(()),
342            Ok(8) => return Err(Error::from(err)),
343            Ok(n) => {
344                // Sends up to PIPE_BUF (4096) should be atomic.
345                panic!("execve pipe: got unexpected number of bytes {}", n);
346            }
347            Err(err) if err.kind() == io::ErrorKind::Interrupted => {}
348            Err(err) => {
349                panic!("execve pipe: read returned unexpected error {}", err);
350            }
351        }
352    }
353}