blob: 3903cd0b3fce6730db67154cca615fd10c46c186 [file] [log] [blame]
Victor Hsieh272aa242021-02-01 14:19:20 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! compsvc is a service to run computational tasks in a PVM upon request. It is able to set up
18//! file descriptors backed by fd_server and pass the file descriptors to the actual tasks for
19//! read/write. The service also attempts to sandbox the execution so that one task cannot leak or
20//! impact future tasks.
21//!
Victor Hsieh272aa242021-02-01 14:19:20 -080022//! The current architecture / process hierarchy looks like:
23//! - compsvc (handle requests)
24//! - compsvc_worker (for environment setup)
25//! - authfs (fd translation)
26//! - actual task
27
Alan Stokes9e2c5d52021-07-21 11:29:10 +010028use anyhow::Result;
29use log::error;
Victor Hsieh272aa242021-02-01 14:19:20 -080030use minijail::{self, Minijail};
31use std::path::PathBuf;
32
33use compos_aidl_interface::aidl::com::android::compos::ICompService::{
34 BnCompService, ICompService,
35};
36use compos_aidl_interface::aidl::com::android::compos::Metadata::Metadata;
37use compos_aidl_interface::binder::{
Alan Stokes9e2c5d52021-07-21 11:29:10 +010038 BinderFeatures, Interface, Result as BinderResult, Status, StatusCode, Strong,
Victor Hsieh272aa242021-02-01 14:19:20 -080039};
40
Victor Hsiehb5f465a2021-05-11 13:45:15 -070041const WORKER_BIN: &str = "/apex/com.android.compos/bin/compsvc_worker";
Alan Stokes9e2c5d52021-07-21 11:29:10 +010042
Victor Hsieh272aa242021-02-01 14:19:20 -080043// TODO: Replace with a valid directory setup in the VM.
Victor Hsiehccdfa0d2021-06-11 13:54:02 -070044const AUTHFS_MOUNTPOINT: &str = "/data/local/tmp";
Victor Hsieh272aa242021-02-01 14:19:20 -080045
Alan Stokes9e2c5d52021-07-21 11:29:10 +010046/// Constructs a binder object that implements ICompService. task_bin is the path to the binary that will
47/// be run when execute() is called. If debuggable is true then stdout/stderr from the binary will be
48/// available for debugging.
49pub fn new_binder(task_bin: String, debuggable: bool) -> Strong<dyn ICompService> {
50 let service = CompService { worker_bin: PathBuf::from(WORKER_BIN), task_bin, debuggable };
51 BnCompService::new_binder(service, BinderFeatures::default())
52}
53
Victor Hsieh272aa242021-02-01 14:19:20 -080054struct CompService {
Victor Hsieh272aa242021-02-01 14:19:20 -080055 task_bin: String,
Alan Stokes9e2c5d52021-07-21 11:29:10 +010056 worker_bin: PathBuf,
Victor Hsieh272aa242021-02-01 14:19:20 -080057 debuggable: bool,
58}
59
60impl CompService {
Victor Hsieh272aa242021-02-01 14:19:20 -080061 fn run_worker_in_jail_and_wait(&self, args: &[String]) -> Result<(), minijail::Error> {
62 let mut jail = Minijail::new()?;
63
64 // TODO(b/185175567): New user and uid namespace when supported. Run as nobody.
65 // New mount namespace to isolate the FUSE mount.
66 jail.namespace_vfs();
67
68 let inheritable_fds = if self.debuggable {
69 vec![1, 2] // inherit/redirect stdout/stderr for debugging
70 } else {
71 vec![]
72 };
73 let _pid = jail.run(&self.worker_bin, &inheritable_fds, &args)?;
74 jail.wait()
75 }
76
77 fn build_worker_args(&self, args: &[String], metadata: &Metadata) -> Vec<String> {
78 let mut worker_args = vec![
79 WORKER_BIN.to_string(),
80 "--authfs-root".to_string(),
81 AUTHFS_MOUNTPOINT.to_string(),
82 ];
83 for annotation in &metadata.input_fd_annotations {
84 worker_args.push("--in-fd".to_string());
85 worker_args.push(format!("{}:{}", annotation.fd, annotation.file_size));
86 }
87 for annotation in &metadata.output_fd_annotations {
88 worker_args.push("--out-fd".to_string());
89 worker_args.push(annotation.fd.to_string());
90 }
91 if self.debuggable {
92 worker_args.push("--debug".to_string());
93 }
94 worker_args.push("--".to_string());
95
96 // Do not accept arbitrary code execution. We want to execute some specific task of this
97 // service. Use the associated executable.
98 worker_args.push(self.task_bin.clone());
99 worker_args.extend_from_slice(&args[1..]);
100 worker_args
101 }
102}
103
104impl Interface for CompService {}
105
106impl ICompService for CompService {
107 fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> {
108 let worker_args = self.build_worker_args(args, metadata);
109
110 match self.run_worker_in_jail_and_wait(&worker_args) {
111 Ok(_) => Ok(0), // TODO(b/161471326): Sign the output on succeed.
112 Err(minijail::Error::ReturnCode(exit_code)) => {
113 error!("Task failed with exit code {}", exit_code);
114 Err(Status::from(StatusCode::FAILED_TRANSACTION))
115 }
116 Err(e) => {
117 error!("Unexpected error: {}", e);
118 Err(Status::from(StatusCode::UNKNOWN_ERROR))
119 }
120 }
121 }
122}