1use 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
32struct Prepared {
34 env: CStringArray,
35 uid_map: Vec<u8>,
36 gid_map: Vec<u8>,
37}
38
39impl Command {
40 pub fn spawn(&mut self) -> Result<Child, Error> {
44 self.container.validate_pinned_clone()?;
45 let prepared = self.prepare();
46
47 let launch = Launch::begin();
52
53 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 drop(writer);
65
66 recv_error(reader)?;
67
68 Ok(child)
69 }
70
71 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 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 let launched = (|| {
117 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 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 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 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 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 fn do_exec(&mut self, context: &ChildContext, env: &CStringArray) -> Error {
213 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 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
234unsafe 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
254unsafe 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 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
326pub fn send_error(fd: &mut Fd, err: Error) {
328 let bytes: [u8; 8] = err.into();
331 let _ = fd.write(&bytes);
332}
333
334pub 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 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}