Skip to main content

starnix_kernel_runner/
component_runner.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::{MountAction, run_component_features};
6use anyhow::{Context, Error, anyhow, bail};
7use fidl::AsyncChannel;
8use fidl::endpoints::{ControlHandle, RequestStream, ServerEnd};
9use fidl_fuchsia_component_runner::{
10    ComponentControllerMarker, ComponentControllerRequest, ComponentControllerRequestStream,
11    ComponentStartInfo,
12};
13use fuchsia_runtime::{HandleInfo, HandleType};
14use futures::channel::oneshot;
15use futures::{FutureExt, StreamExt};
16use rand::distr::Alphanumeric;
17use rand::{Rng, rng};
18use serde::Deserialize;
19use serde::de::Error as _;
20use starnix_core::execution::{create_init_child_process, execute_task_with_prerun_result};
21use starnix_core::fs::fuchsia::{RemoteFs, SyslogFile, create_file_from_handle};
22use starnix_core::task::{CurrentTask, ExitStatus, Task};
23use starnix_core::vfs::fs_args::MountParams;
24use starnix_core::vfs::{
25    FdNumber, FdTable, FileSystemOptions, FsString, LookupContext, NamespaceNode, WhatToMount,
26};
27use starnix_core::{security, signals};
28use starnix_logging::{log_debug, log_error, log_info, log_warn};
29use starnix_sync::{FileOpsCore, LockEqualOrBefore, Locked, Mutex, Unlocked};
30use starnix_task_command::TaskCommand;
31use starnix_types::ownership::WeakRef;
32use starnix_uapi::auth::{Capabilities, Credentials};
33use starnix_uapi::device_type::DeviceType;
34use starnix_uapi::errno;
35use starnix_uapi::errors::{EEXIST, ENOTDIR, Errno};
36use starnix_uapi::file_mode::mode;
37use starnix_uapi::mount_flags::MountFlags;
38use starnix_uapi::open_flags::OpenFlags;
39use starnix_uapi::signals::{SIGINT, SIGKILL};
40use starnix_uapi::unmount_flags::UnmountFlags;
41use std::ffi::CString;
42use std::ops::DerefMut;
43use std::os::unix::ffi::OsStrExt;
44use std::path::Path;
45use std::sync::Arc;
46use {
47    fidl_fuchsia_component as fcomponent, fidl_fuchsia_io as fio, fidl_fuchsia_process as fprocess,
48    fuchsia_async as fasync,
49};
50
51/// Component controller epitaph value used as the base value to pass non-zero error
52/// codes to the calling component.
53///
54/// TODO(https://fxbug.dev/42081234): Cleanup this once we have a proper mechanism to
55/// get Linux exit code from component runner.
56const COMPONENT_EXIT_CODE_BASE: i32 = 1024;
57
58#[derive(Debug, Deserialize)]
59#[serde(deny_unknown_fields)]
60struct ComponentProgram {
61    binary: CString,
62
63    #[serde(default)]
64    args: Vec<String>,
65
66    #[serde(default)]
67    environ: Vec<String>,
68
69    #[serde(default)]
70    cwd: Option<String>,
71
72    #[serde(default)]
73    uid: Option<runner::serde::StoreAsString<u32>>,
74
75    #[serde(default)]
76    component_mounts: Vec<String>,
77
78    #[serde(default)]
79    features: Vec<String>,
80
81    #[serde(default, deserialize_with = "parse_capabilities")]
82    capabilities: Option<Capabilities>,
83
84    #[serde(default)]
85    seclabel: Option<CString>,
86
87    #[serde(default, rename(deserialize = "test_target_kernel"))]
88    _test_target_kernel: Option<String>,
89}
90
91impl ComponentProgram {
92    fn resolve_templates(&mut self, component_path: &str, pkg_path: &str) {
93        let resolve_template = |values: &mut Vec<String>| {
94            for val in values {
95                *val = val
96                    .replace("{pkg_path}", &pkg_path)
97                    .replace("{component_path}", &component_path);
98            }
99        };
100
101        resolve_template(&mut self.args);
102        resolve_template(&mut self.environ);
103    }
104}
105
106fn parse_capabilities<'de, D>(deserializer: D) -> Result<Option<Capabilities>, D::Error>
107where
108    D: serde::Deserializer<'de>,
109{
110    let mut capabilities = Capabilities::empty();
111    for cap in Vec::<String>::deserialize(deserializer)? {
112        capabilities |= cap.parse().map_err(D::Error::custom)?;
113    }
114    Ok(Some(capabilities))
115}
116
117/// Starts a component inside the given container.
118///
119/// The component's `binary` can either:
120///   - an absolute path, in which case the path is treated as a path into the root filesystem that
121///     is mounted by the container's configuration
122///   - relative path, in which case the binary is read from the component's package (which is
123///     mounted at /container/component/{random}/pkg.)
124///
125/// The directories in the component's namespace are mounted at /container/component/{random}.
126pub async fn start_component(
127    mut start_info: ComponentStartInfo,
128    controller: ServerEnd<ComponentControllerMarker>,
129    system_task: &CurrentTask,
130) -> Result<(), Error> {
131    let url = start_info.resolved_url.clone().unwrap_or_else(|| "<unknown>".to_string());
132
133    let (task_complete_sender, task_complete) = oneshot::channel::<TaskResult>();
134
135    let weak_task = system_task.override_creds(
136        security::creds_start_internal_operation(system_task),
137        || {
138            // TODO(https://fxbug.dev/42076551): We leak the directory created by this function.
139            let component_path = generate_component_path(
140                system_task.kernel().kthreads.unlocked_for_async().deref_mut(),
141                system_task,
142            )?;
143            let pkg_path = format!("{component_path}/pkg");
144
145            let mount_record = Arc::new(Mutex::new(MountRecord::default()));
146
147            let ns = start_info.ns.take().ok_or_else(|| anyhow!("Missing namespace"))?;
148
149            let program = start_info.program.as_ref().context("reading program block")?;
150            let mut program: ComponentProgram =
151                runner::serde::deserialize_program(program).context("parsing program block")?;
152            program.resolve_templates(&component_path, &pkg_path);
153            log_debug!("start_component: {}\n{:#?}", url, program);
154
155            let ns_mount_options = system_task.kernel().features.default_ns_mount_options.as_ref();
156            let mut maybe_pkg = None;
157            let mut maybe_svc = None;
158            for entry in ns {
159                if let (Some(dir_path), Some(dir_handle)) = (entry.path, entry.directory) {
160                    let dir_path_str = dir_path.as_str();
161                    let mount_options = ns_mount_options
162                        .and_then(|mount_options| mount_options.get(dir_path_str).cloned());
163
164                    match dir_path_str {
165                        "/svc" => {
166                            maybe_svc = Some(fio::DirectoryProxy::new(AsyncChannel::from_channel(
167                                dir_handle.into_channel(),
168                            )));
169                        }
170                        "/custom_artifacts" => {
171                            // Mount custom_artifacts directory at root of container
172                            // We may want to transition to have this directory unique per component
173                            let dir_proxy =
174                                fio::DirectorySynchronousProxy::new(dir_handle.into_channel());
175                            mount_record
176                                .lock()
177                                .mount_remote(
178                                    system_task.kernel().kthreads.unlocked_for_async().deref_mut(),
179                                    system_task,
180                                    &dir_proxy,
181                                    &dir_path,
182                                    mount_options.as_ref(),
183                                )
184                                .with_context(|| {
185                                    format!("failed to mount_remote on path {}", &dir_path)
186                                })?;
187                        }
188                        _ => {
189                            let dir_proxy =
190                                fio::DirectorySynchronousProxy::new(dir_handle.into_channel());
191                            mount_record
192                                .lock()
193                                .mount_remote(
194                                    system_task.kernel().kthreads.unlocked_for_async().deref_mut(),
195                                    system_task,
196                                    &dir_proxy,
197                                    &format!("{component_path}/{dir_path}"),
198                                    mount_options.as_ref(),
199                                )
200                                .with_context(|| {
201                                    format!(
202                                        "failed to mount_remote on path {component_path}/{dir_path}"
203                                    )
204                                })?;
205                            if dir_path == "/pkg" {
206                                maybe_pkg = Some(dir_proxy);
207                            }
208                        }
209                    }
210                }
211            }
212
213            let pkg = maybe_pkg.ok_or_else(|| anyhow!("Missing /pkg entry in namespace"))?;
214
215            let uid = program
216                .uid
217                .map(|uid| uid.0)
218                .unwrap_or_else(|| system_task.kernel().features.default_uid);
219
220            let mut credentials = Credentials::with_ids(uid, uid);
221            if let Some(capabilities) = program.capabilities {
222                credentials.cap_permitted = capabilities;
223                credentials.cap_effective = capabilities;
224                credentials.cap_inheritable = capabilities;
225                credentials.cap_ambient = capabilities;
226            }
227
228            run_component_features(system_task.kernel(), &program.features, maybe_svc)
229                .unwrap_or_else(|e| {
230                    log_error!("failed to set component features for {} - {:?}", url, e);
231                });
232
233            let current_task = create_init_child_process(
234                system_task.kernel().kthreads.unlocked_for_async().deref_mut(),
235                system_task.kernel(),
236                TaskCommand::new(program.binary.as_bytes()),
237                credentials,
238                program.seclabel.as_ref(),
239            )?;
240
241            execute_task_with_prerun_result(
242                system_task.kernel().kthreads.unlocked_for_async().deref_mut(),
243                current_task,
244                {
245                    let mount_record = mount_record.clone();
246                    move |locked, current_task| {
247                        let cwd_path = FsString::from(program.cwd.unwrap_or(pkg_path));
248                        let cwd = current_task.lookup_path(
249                            locked,
250                            &mut LookupContext::default(),
251                            current_task.fs().root(),
252                            cwd_path.as_ref(),
253                        )?;
254                        current_task.fs().chdir(locked, current_task, cwd)?;
255
256                        for mount in &program.component_mounts {
257                            let action = MountAction::from_spec(locked, current_task, &pkg, mount)
258                                .map_err(|e| {
259                                    log_error!("Error while mounting the filesystems: {e:?}");
260                                    errno!(EINVAL)
261                                })?;
262                            let mount_point =
263                                current_task.lookup_path_from_root(locked, action.path.as_ref())?;
264                            mount_record.lock().mount(
265                                mount_point,
266                                WhatToMount::Fs(action.fs),
267                                action.flags,
268                            )?;
269                        }
270
271                        parse_numbered_handles(
272                            locked,
273                            current_task,
274                            start_info.numbered_handles,
275                            &current_task.files,
276                        )
277                        .map_err(|e| {
278                            log_error!("Error while parsing the numbered handles: {e:?}");
279                            errno!(EINVAL)
280                        })?;
281
282                        let mut argv = vec![program.binary.clone()];
283                        for arg in program.args {
284                            argv.push(CString::new(arg).map_err(|_| errno!(EINVAL))?);
285                        }
286
287                        let mut environ = vec![];
288                        for env in program.environ {
289                            environ.push(CString::new(env).map_err(|_| errno!(EINVAL))?);
290                        }
291
292                        let executable = current_task.open_file(
293                            locked,
294                            program.binary.as_bytes().into(),
295                            OpenFlags::RDONLY,
296                        )?;
297                        current_task.exec(locked, executable, program.binary, argv, environ)?;
298
299                        Ok(WeakRef::from(&current_task.task))
300                    }
301                },
302                move |result| {
303                    // Unmount all the directories for this component.
304                    std::mem::drop(mount_record);
305
306                    // If the component controller server has gone away, there is nobody for us to
307                    // report the result to.
308                    let _ = task_complete_sender.send(result);
309                },
310                None,
311            )
312            .map_err(anyhow::Error::from)
313        },
314    )?;
315
316    let controller = controller.into_stream();
317    fasync::Task::local(serve_component_controller(controller, weak_task, task_complete)).detach();
318
319    Ok(())
320}
321
322type TaskResult = Result<ExitStatus, Error>;
323
324/// Translates [ComponentControllerRequest] messages to signals on the `task`.
325///
326/// When a `Stop` request is received, it will send a `SIGINT` to the task.
327/// When a `Kill` request is received, it will send a `SIGKILL` to the task and close the component
328/// controller channel regardless if/how the task responded to the signal. Due to Linux's design,
329/// this may not reliably cleanup everything that was started as a result of running the component.
330///
331/// If the task has completed, it will also close the controller channel.
332async fn serve_component_controller(
333    controller: ComponentControllerRequestStream,
334    task: WeakRef<Task>,
335    task_complete: oneshot::Receiver<TaskResult>,
336) {
337    let controller_handle = controller.control_handle();
338
339    enum Event<T, U> {
340        Controller(T),
341        Completion(U),
342    }
343
344    let mut stream = futures::stream::select(
345        controller.map(Event::Controller),
346        task_complete.into_stream().map(Event::Completion),
347    );
348
349    while let Some(event) = stream.next().await {
350        match event {
351            Event::Controller(request) => match request {
352                Ok(ComponentControllerRequest::Stop { .. }) => {
353                    if let Some(task) = task.upgrade() {
354                        signals::send_standard_signal(
355                            task.kernel().kthreads.unlocked_for_async().deref_mut(),
356                            task.as_ref(),
357                            signals::SignalInfo::kernel(SIGINT),
358                        );
359                        log_info!("Sent SIGINT to program {}", task.command());
360                    }
361                }
362                Ok(ComponentControllerRequest::Kill { .. }) => {
363                    if let Some(task) = task.upgrade() {
364                        signals::send_standard_signal(
365                            task.kernel().kthreads.unlocked_for_async().deref_mut(),
366                            &task,
367                            signals::SignalInfo::kernel(SIGKILL),
368                        );
369                        log_info!("Sent SIGKILL to program {}", task.command());
370                        controller_handle.shutdown_with_epitaph(zx::Status::from_raw(
371                            fcomponent::Error::InstanceDied.into_primitive() as i32,
372                        ));
373                    }
374                    return;
375                }
376                Ok(ComponentControllerRequest::_UnknownMethod { ordinal, .. }) => {
377                    log_warn!("Unknown ComponentController request: {ordinal}");
378                }
379                Err(_) => {
380                    return;
381                }
382            },
383            Event::Completion(result) => match result {
384                Ok(Ok(ExitStatus::Exit(0))) => {
385                    controller_handle.shutdown_with_epitaph(zx::Status::OK)
386                }
387                Ok(Ok(ExitStatus::Exit(n))) => controller_handle.shutdown_with_epitaph(
388                    zx::Status::from_raw(COMPONENT_EXIT_CODE_BASE + n as i32),
389                ),
390                _ => controller_handle.shutdown_with_epitaph(zx::Status::from_raw(
391                    fcomponent::Error::InstanceDied.into_primitive() as i32,
392                )),
393            },
394        }
395    }
396}
397
398/// Returns /container/component/{random} that doesn't already exist
399fn generate_component_path<L>(
400    locked: &mut Locked<L>,
401    system_task: &CurrentTask,
402) -> Result<String, Error>
403where
404    L: LockEqualOrBefore<FileOpsCore>,
405{
406    // Checking container directory already exists.
407    // If this lookup fails, the container might not have the "container" feature enabled.
408    let mount_point = system_task.lookup_path_from_root(locked, "/container/component/".into())?;
409
410    // Find /container/component/{random} that doesn't already exist
411    let component_path = loop {
412        let random_string: String =
413            rng().sample_iter(&Alphanumeric).take(10).map(char::from).collect();
414
415        // This returns EEXIST if /container/component/{random} already exists.
416        // If so, try again with another {random} string.
417        match mount_point.create_node(
418            locked,
419            system_task,
420            random_string.as_str().into(),
421            mode!(IFDIR, 0o755),
422            DeviceType::NONE,
423        ) {
424            Ok(_) => break format!("/container/component/{random_string}"),
425            Err(errno) if errno == EEXIST => {}
426            Err(e) => bail!(e),
427        };
428    };
429
430    Ok(component_path)
431}
432
433/// Adds the given startup handles to a CurrentTask.
434///
435/// The `numbered_handles` of type `HandleType::FileDescriptor` are used to
436/// create files, and the handles are required to be of type `zx::Socket`.
437///
438/// If there is a `numbered_handles` of type `HandleType::User0`, that is
439/// interpreted as the server end of the ShellController protocol.
440pub fn parse_numbered_handles(
441    locked: &mut Locked<Unlocked>,
442    current_task: &CurrentTask,
443    numbered_handles: Option<Vec<fprocess::HandleInfo>>,
444    files: &FdTable,
445) -> Result<(), Error> {
446    if let Some(numbered_handles) = numbered_handles {
447        for numbered_handle in numbered_handles {
448            let info = HandleInfo::try_from(numbered_handle.id)?;
449            if info.handle_type() == HandleType::FileDescriptor {
450                let file = create_file_from_handle(locked, current_task, numbered_handle.handle)?;
451                files.insert(locked, current_task, FdNumber::from_raw(info.arg().into()), file)?;
452            }
453        }
454    }
455
456    let stdio = SyslogFile::new_file(locked, current_task);
457    // If no numbered handle is provided for each stdio handle, default to syslog.
458    for i in [0, 1, 2] {
459        if files.get(FdNumber::from_raw(i)).is_err() {
460            files.insert(locked, current_task, FdNumber::from_raw(i), stdio.clone())?;
461        }
462    }
463
464    Ok(())
465}
466
467/// A record of the mounts created when starting a component.
468///
469/// When the record is dropped, the mounts are unmounted.
470#[derive(Default)]
471struct MountRecord {
472    /// The namespace nodes at which we have crated mounts for this component.
473    mounts: Vec<NamespaceNode>,
474}
475
476impl MountRecord {
477    fn mount(
478        &mut self,
479        mount_point: NamespaceNode,
480        what: WhatToMount,
481        flags: MountFlags,
482    ) -> Result<(), Errno> {
483        mount_point.mount(what, flags)?;
484        self.mounts.push(mount_point);
485        Ok(())
486    }
487
488    fn mount_remote<L>(
489        &mut self,
490        locked: &mut Locked<L>,
491        system_task: &CurrentTask,
492        directory: &fio::DirectorySynchronousProxy,
493        path: &str,
494        mount_options: Option<&String>,
495    ) -> Result<(), Error>
496    where
497        L: LockEqualOrBefore<FileOpsCore>,
498    {
499        // The incoming dir_path might not be top level, e.g. it could be /foo/bar.
500        // Iterate through each component directory starting from the parent and
501        // create it if it doesn't exist.
502        let mut current_node =
503            system_task.lookup_path_from_root(locked, ".".into()).context("looking up '.'")?;
504        let mut context = LookupContext::default();
505
506        // Extract each component using Path::new(path).components(). For example,
507        // Path::new("/foo/bar").components() will return [RootDir, Normal("foo"), Normal("bar")].
508        // We're not interested in the RootDir, so we drop the prefix "/" if it exists.
509        let path = if let Some(path) = path.strip_prefix('/') { path } else { path };
510
511        for sub_dir in Path::new(path).components() {
512            let sub_dir_bytes = sub_dir.as_os_str().as_bytes();
513            current_node = match current_node.create_node(
514                locked,
515                system_task,
516                sub_dir_bytes.into(),
517                mode!(IFDIR, 0o755),
518                DeviceType::NONE,
519            ) {
520                Ok(node) => node,
521                Err(errno) if errno == EEXIST || errno == ENOTDIR => current_node
522                    .lookup_child(locked, system_task, &mut context, sub_dir_bytes.into())
523                    .with_context(|| format!("looking up {sub_dir:?}"))?,
524                Err(e) => bail!(e),
525            };
526        }
527
528        let flags = directory
529            .get_flags(zx::MonotonicInstant::INFINITE)
530            .context("transport error")?
531            .map_err(zx::Status::from_raw)
532            .context("get_flags")?;
533        let rights = flags.intersection(fio::MASK_KNOWN_PERMISSIONS);
534
535        let (client_end, server_end) = zx::Channel::create();
536        directory.clone(ServerEnd::new(server_end)).context("cloning directory")?;
537
538        // If a filesystem security label argument was provided then apply it to all files via
539        // mountpoint-labeling, with a "context=..." mount option.
540        let params = if let Some(mount_options) = mount_options {
541            MountParams::parse(mount_options.as_str().into())
542                .expect("failed to parse default_ns_mount_options")
543        } else {
544            MountParams::default()
545        };
546
547        let fs = RemoteFs::new_fs(
548            locked,
549            system_task.kernel(),
550            client_end,
551            FileSystemOptions { source: path.into(), params, ..Default::default() },
552            rights,
553        )
554        .context("making remote fs")?;
555
556        security::file_system_resolve_security(locked, system_task, &fs)
557            .context("resolving security")?;
558
559        // Fuchsia doesn't specify mount flags in the incoming namespace, so we need to make
560        // up some flags.
561        let flags = MountFlags::NOSUID | MountFlags::NODEV | MountFlags::RELATIME;
562        current_node.mount(WhatToMount::Fs(fs), flags).context("mounting fs")?;
563        self.mounts.push(current_node);
564
565        Ok(())
566    }
567
568    fn unmount(&mut self) -> Result<(), Errno> {
569        while let Some(node) = self.mounts.pop() {
570            node.unmount(UnmountFlags::DETACH)?;
571        }
572        Ok(())
573    }
574}
575
576impl Drop for MountRecord {
577    fn drop(&mut self) {
578        match self.unmount() {
579            Ok(()) => {}
580            Err(e) => log_error!("failed to unmount during component exit: {:?}", e),
581        }
582    }
583}