Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 1 | /* |
| 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 Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 22 | //! 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 Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 28 | use anyhow::Result; |
| 29 | use log::error; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 30 | use minijail::{self, Minijail}; |
| 31 | use std::path::PathBuf; |
| 32 | |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 33 | use crate::signer::Signer; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 34 | use compos_aidl_interface::aidl::com::android::compos::ICompService::{ |
| 35 | BnCompService, ICompService, |
| 36 | }; |
| 37 | use compos_aidl_interface::aidl::com::android::compos::Metadata::Metadata; |
| 38 | use compos_aidl_interface::binder::{ |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 39 | BinderFeatures, Interface, Result as BinderResult, Status, StatusCode, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Victor Hsieh | b5f465a | 2021-05-11 13:45:15 -0700 | [diff] [blame] | 42 | const WORKER_BIN: &str = "/apex/com.android.compos/bin/compsvc_worker"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 43 | |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 44 | // TODO: Replace with a valid directory setup in the VM. |
Victor Hsieh | ccdfa0d | 2021-06-11 13:54:02 -0700 | [diff] [blame] | 45 | const AUTHFS_MOUNTPOINT: &str = "/data/local/tmp"; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 46 | |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 47 | /// Constructs a binder object that implements ICompService. task_bin is the path to the binary that will |
| 48 | /// be run when execute() is called. If debuggable is true then stdout/stderr from the binary will be |
| 49 | /// available for debugging. |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 50 | pub fn new_binder( |
| 51 | task_bin: String, |
| 52 | debuggable: bool, |
| 53 | signer: Option<Box<dyn Signer>>, |
| 54 | ) -> Strong<dyn ICompService> { |
| 55 | let service = CompService { |
| 56 | worker_bin: PathBuf::from(WORKER_BIN.to_owned()), |
| 57 | task_bin, |
| 58 | debuggable, |
| 59 | signer, |
| 60 | }; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 61 | BnCompService::new_binder(service, BinderFeatures::default()) |
| 62 | } |
| 63 | |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 64 | struct CompService { |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 65 | task_bin: String, |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 66 | worker_bin: PathBuf, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 67 | debuggable: bool, |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 68 | #[allow(dead_code)] // TODO: Make use of this |
| 69 | signer: Option<Box<dyn Signer>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | impl CompService { |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 73 | fn run_worker_in_jail_and_wait(&self, args: &[String]) -> Result<(), minijail::Error> { |
| 74 | let mut jail = Minijail::new()?; |
| 75 | |
| 76 | // TODO(b/185175567): New user and uid namespace when supported. Run as nobody. |
| 77 | // New mount namespace to isolate the FUSE mount. |
| 78 | jail.namespace_vfs(); |
| 79 | |
| 80 | let inheritable_fds = if self.debuggable { |
| 81 | vec![1, 2] // inherit/redirect stdout/stderr for debugging |
| 82 | } else { |
| 83 | vec![] |
| 84 | }; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 85 | let _pid = jail.run(&self.worker_bin, &inheritable_fds, args)?; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 86 | jail.wait() |
| 87 | } |
| 88 | |
| 89 | fn build_worker_args(&self, args: &[String], metadata: &Metadata) -> Vec<String> { |
| 90 | let mut worker_args = vec![ |
| 91 | WORKER_BIN.to_string(), |
| 92 | "--authfs-root".to_string(), |
| 93 | AUTHFS_MOUNTPOINT.to_string(), |
| 94 | ]; |
| 95 | for annotation in &metadata.input_fd_annotations { |
| 96 | worker_args.push("--in-fd".to_string()); |
| 97 | worker_args.push(format!("{}:{}", annotation.fd, annotation.file_size)); |
| 98 | } |
| 99 | for annotation in &metadata.output_fd_annotations { |
| 100 | worker_args.push("--out-fd".to_string()); |
| 101 | worker_args.push(annotation.fd.to_string()); |
| 102 | } |
| 103 | if self.debuggable { |
| 104 | worker_args.push("--debug".to_string()); |
| 105 | } |
| 106 | worker_args.push("--".to_string()); |
| 107 | |
| 108 | // Do not accept arbitrary code execution. We want to execute some specific task of this |
| 109 | // service. Use the associated executable. |
| 110 | worker_args.push(self.task_bin.clone()); |
| 111 | worker_args.extend_from_slice(&args[1..]); |
| 112 | worker_args |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl Interface for CompService {} |
| 117 | |
| 118 | impl ICompService for CompService { |
| 119 | fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> { |
| 120 | let worker_args = self.build_worker_args(args, metadata); |
| 121 | |
| 122 | match self.run_worker_in_jail_and_wait(&worker_args) { |
| 123 | Ok(_) => Ok(0), // TODO(b/161471326): Sign the output on succeed. |
| 124 | Err(minijail::Error::ReturnCode(exit_code)) => { |
| 125 | error!("Task failed with exit code {}", exit_code); |
| 126 | Err(Status::from(StatusCode::FAILED_TRANSACTION)) |
| 127 | } |
| 128 | Err(e) => { |
| 129 | error!("Unexpected error: {}", e); |
| 130 | Err(Status::from(StatusCode::UNKNOWN_ERROR)) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |