Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Microdroid Manager |
| 16 | |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 17 | mod ioutil; |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 18 | mod metadata; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 19 | |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 20 | use anyhow::{anyhow, bail, Context, Result}; |
| 21 | use apkverify::verify; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame^] | 22 | use binder::unstable_api::{new_spibinder, AIBinder}; |
| 23 | use binder::{FromIBinder, Strong}; |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 24 | use log::{error, info, warn}; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 25 | use microdroid_payload_config::{Task, TaskType, VmPayloadConfig}; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 26 | use rustutils::system_properties::PropertyWatcher; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 27 | use std::fs::{self, File}; |
| 28 | use std::os::unix::io::{FromRawFd, IntoRawFd}; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 29 | use std::path::Path; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 30 | use std::process::{Command, Stdio}; |
| 31 | use std::str; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 32 | use std::time::Duration; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 33 | use vsock::VsockStream; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 34 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame^] | 35 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService; |
| 36 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 37 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 38 | const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk"; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 39 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame^] | 40 | /// The CID representing the host VM |
| 41 | const VMADDR_CID_HOST: u32 = 2; |
| 42 | |
| 43 | /// Port number that virtualizationservice listens on connections from the guest VMs for the |
| 44 | /// VirtualMachineService binder service |
| 45 | /// Sync with virtualizationservice/src/aidl.rs |
| 46 | const PORT_VM_BINDER_SERVICE: u32 = 5000; |
| 47 | |
| 48 | fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> { |
| 49 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be |
| 50 | // safely taken by new_spibinder. |
| 51 | let ibinder = unsafe { |
| 52 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient( |
| 53 | VMADDR_CID_HOST, |
| 54 | PORT_VM_BINDER_SERVICE, |
| 55 | ) as *mut AIBinder) |
| 56 | }; |
| 57 | if let Some(ibinder) = ibinder { |
| 58 | <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service") |
| 59 | } else { |
| 60 | bail!("Invalid raw AIBinder") |
| 61 | } |
| 62 | } |
| 63 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 64 | fn main() -> Result<()> { |
Jiyong Park | 79b8801 | 2021-06-25 13:06:25 +0900 | [diff] [blame] | 65 | kernlog::init()?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 66 | info!("started."); |
| 67 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 68 | let metadata = metadata::load()?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 69 | |
| 70 | if let Err(err) = verify_payloads() { |
Jooyung Han | d4e035e | 2021-08-18 21:19:41 +0900 | [diff] [blame] | 71 | error!("failed to verify payload: {:#?}", err); |
| 72 | return Err(err); |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame^] | 75 | // TODO(b/191845268): microdroid_manager should use this binder to communicate with the host |
| 76 | if let Err(err) = get_vms_rpc_binder() { |
| 77 | error!("cannot connect to VirtualMachineService: {}", err); |
| 78 | } |
| 79 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 80 | if !metadata.payload_config_path.is_empty() { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 81 | let config = load_config(Path::new(&metadata.payload_config_path))?; |
| 82 | |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 83 | let fake_secret = "This is a placeholder for a value that is derived from the images that are loaded in the VM."; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 84 | if let Err(err) = rustutils::system_properties::write("ro.vmsecret.keymint", fake_secret) { |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 85 | warn!("failed to set ro.vmsecret.keymint: {}", err); |
| 86 | } |
| 87 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 88 | // TODO(jooyung): wait until sys.boot_completed? |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 89 | if let Some(main_task) = &config.task { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 90 | exec_task(main_task).map_err(|e| { |
| 91 | error!("failed to execute task: {}", e); |
| 92 | e |
| 93 | })?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | Ok(()) |
| 98 | } |
| 99 | |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 100 | // TODO(jooyung): v2/v3 full verification can be slow. Consider multithreading. |
| 101 | fn verify_payloads() -> Result<()> { |
| 102 | // We don't verify APEXes since apexd does. |
| 103 | |
| 104 | // should wait APK to be dm-verity mounted by apkdmverity |
| 105 | ioutil::wait_for_file(DM_MOUNTED_APK_PATH, WAIT_TIMEOUT)?; |
| 106 | verify(DM_MOUNTED_APK_PATH).context(format!("failed to verify {}", DM_MOUNTED_APK_PATH))?; |
| 107 | |
| 108 | info!("payload verification succeeded."); |
| 109 | // TODO(jooyung): collect public keys and store them in instance.img |
| 110 | Ok(()) |
| 111 | } |
| 112 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 113 | fn load_config(path: &Path) -> Result<VmPayloadConfig> { |
| 114 | info!("loading config from {:?}...", path); |
| 115 | let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?; |
| 116 | Ok(serde_json::from_reader(file)?) |
| 117 | } |
| 118 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 119 | /// Executes the given task. Stdout of the task is piped into the vsock stream to the |
| 120 | /// virtualizationservice in the host side. |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 121 | fn exec_task(task: &Task) -> Result<()> { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 122 | const VMADDR_CID_HOST: u32 = 2; |
| 123 | const PORT_VIRT_SVC: u32 = 3000; |
| 124 | let stdout = match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, PORT_VIRT_SVC) { |
| 125 | Ok(stream) => { |
| 126 | // SAFETY: the ownership of the underlying file descriptor is transferred from stream |
| 127 | // to the file object, and then into the Command object. When the command is finished, |
| 128 | // the file descriptor is closed. |
| 129 | let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 130 | Stdio::from(f) |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 131 | } |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 132 | Err(e) => { |
| 133 | error!("failed to connect to virtualization service: {}", e); |
| 134 | // Don't fail hard here. Even if we failed to connect to the virtualizationservice, |
| 135 | // we keep executing the task. This can happen if the owner of the VM doesn't register |
| 136 | // callback to accept the stream. Use /dev/null as the stdout so that the task can |
| 137 | // make progress without waiting for someone to consume the output. |
| 138 | Stdio::null() |
| 139 | } |
| 140 | }; |
| 141 | info!("executing main task {:?}...", task); |
| 142 | // TODO(jiyong): consider piping the stream into stdio (and probably stderr) as well. |
| 143 | let mut child = build_command(task)?.stdout(stdout).spawn()?; |
| 144 | match child.wait()?.code() { |
| 145 | Some(0) => { |
| 146 | info!("task successfully finished"); |
| 147 | Ok(()) |
| 148 | } |
| 149 | Some(code) => bail!("task exited with exit code: {}", code), |
| 150 | None => bail!("task terminated by signal"), |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 151 | } |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 152 | } |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 153 | |
| 154 | fn build_command(task: &Task) -> Result<Command> { |
| 155 | Ok(match task.type_ { |
| 156 | TaskType::Executable => { |
| 157 | let mut command = Command::new(&task.command); |
| 158 | command.args(&task.args); |
| 159 | command |
| 160 | } |
| 161 | TaskType::MicrodroidLauncher => { |
| 162 | let mut command = Command::new("/system/bin/microdroid_launcher"); |
| 163 | command.arg(find_library_path(&task.command)?).args(&task.args); |
| 164 | command |
| 165 | } |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | fn find_library_path(name: &str) -> Result<String> { |
| 170 | let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?; |
| 171 | let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?; |
| 172 | let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?; |
| 173 | let path = format!("/mnt/apk/lib/{}/{}", abi, name); |
| 174 | |
| 175 | let metadata = fs::metadata(&path)?; |
| 176 | if !metadata.is_file() { |
| 177 | bail!("{} is not a file", &path); |
| 178 | } |
| 179 | |
| 180 | Ok(path) |
| 181 | } |