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 | |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 17 | mod instance; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 18 | mod ioutil; |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 19 | mod metadata; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 20 | |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 21 | use crate::instance::InstanceDisk; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 22 | use anyhow::{anyhow, bail, Context, Result}; |
| 23 | use apkverify::verify; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 24 | use binder::unstable_api::{new_spibinder, AIBinder}; |
| 25 | use binder::{FromIBinder, Strong}; |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 26 | use idsig::V4Signature; |
| 27 | use log::{debug, error, info, warn}; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 28 | use microdroid_payload_config::{Task, TaskType, VmPayloadConfig}; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 29 | use nix::ioctl_read_bad; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 30 | use rustutils::system_properties::PropertyWatcher; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 31 | use std::fs::{self, File, OpenOptions}; |
| 32 | use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 33 | use std::path::Path; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 34 | use std::process::{Command, Stdio}; |
| 35 | use std::str; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 36 | use std::time::Duration; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 37 | use vsock::VsockStream; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 38 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame^] | 39 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::{ |
| 40 | VM_BINDER_SERVICE_PORT, VM_STREAM_SERVICE_PORT, IVirtualMachineService, |
| 41 | }; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 42 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 43 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 44 | const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk"; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 45 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 46 | /// The CID representing the host VM |
| 47 | const VMADDR_CID_HOST: u32 = 2; |
| 48 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 49 | fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> { |
| 50 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be |
| 51 | // safely taken by new_spibinder. |
| 52 | let ibinder = unsafe { |
| 53 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient( |
| 54 | VMADDR_CID_HOST, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame^] | 55 | VM_BINDER_SERVICE_PORT as u32, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 56 | ) as *mut AIBinder) |
| 57 | }; |
| 58 | if let Some(ibinder) = ibinder { |
| 59 | <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service") |
| 60 | } else { |
| 61 | bail!("Invalid raw AIBinder") |
| 62 | } |
| 63 | } |
| 64 | |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 65 | const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9; |
| 66 | ioctl_read_bad!( |
| 67 | /// Gets local cid from /dev/vsock |
| 68 | vm_sockets_get_local_cid, |
| 69 | IOCTL_VM_SOCKETS_GET_LOCAL_CID, |
| 70 | u32 |
| 71 | ); |
| 72 | |
| 73 | // TODO: remove this after VS can check the peer addresses of binder clients |
| 74 | fn get_local_cid() -> Result<u32> { |
| 75 | let f = OpenOptions::new() |
| 76 | .read(true) |
| 77 | .write(false) |
| 78 | .open("/dev/vsock") |
| 79 | .context("failed to open /dev/vsock")?; |
| 80 | let mut ret = 0; |
| 81 | // SAFETY: the kernel only modifies the given u32 integer. |
| 82 | unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut ret) }?; |
| 83 | Ok(ret) |
| 84 | } |
| 85 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 86 | fn main() -> Result<()> { |
Jiyong Park | 79b8801 | 2021-06-25 13:06:25 +0900 | [diff] [blame] | 87 | kernlog::init()?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 88 | info!("started."); |
| 89 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 90 | let metadata = metadata::load()?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 91 | |
| 92 | if let Err(err) = verify_payloads() { |
Jooyung Han | d4e035e | 2021-08-18 21:19:41 +0900 | [diff] [blame] | 93 | error!("failed to verify payload: {:#?}", err); |
| 94 | return Err(err); |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 97 | let mut instance = InstanceDisk::new()?; |
| 98 | // TODO(jiyong): the data should have an internal structure |
| 99 | if let Some(data) = instance.read_microdroid_data().context("Failed to read identity data")? { |
| 100 | debug!("read apk root hash: {}", to_hex_string(&data)); |
| 101 | //TODO(jiyong) apkdmverity should use this root hash instead of the one read from the idsig |
| 102 | //file, if the root hash is found in the instance image. |
| 103 | } else { |
| 104 | let data = get_apk_roothash()?; |
| 105 | debug!("write apk root hash: {}", to_hex_string(&data)); |
| 106 | instance.write_microdroid_data(data.as_ref()).context("Failed to write identity data")?; |
| 107 | } |
| 108 | |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 109 | let service = get_vms_rpc_binder().expect("cannot connect to VirtualMachineService"); |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 110 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 111 | if !metadata.payload_config_path.is_empty() { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 112 | let config = load_config(Path::new(&metadata.payload_config_path))?; |
| 113 | |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 114 | 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] | 115 | 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] | 116 | warn!("failed to set ro.vmsecret.keymint: {}", err); |
| 117 | } |
| 118 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 119 | // TODO(jooyung): wait until sys.boot_completed? |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 120 | if let Some(main_task) = &config.task { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 121 | exec_task(main_task, &service).map_err(|e| { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 122 | error!("failed to execute task: {}", e); |
| 123 | e |
| 124 | })?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 131 | // TODO(jooyung): v2/v3 full verification can be slow. Consider multithreading. |
| 132 | fn verify_payloads() -> Result<()> { |
| 133 | // We don't verify APEXes since apexd does. |
| 134 | |
| 135 | // should wait APK to be dm-verity mounted by apkdmverity |
| 136 | ioutil::wait_for_file(DM_MOUNTED_APK_PATH, WAIT_TIMEOUT)?; |
| 137 | verify(DM_MOUNTED_APK_PATH).context(format!("failed to verify {}", DM_MOUNTED_APK_PATH))?; |
| 138 | |
| 139 | info!("payload verification succeeded."); |
| 140 | // TODO(jooyung): collect public keys and store them in instance.img |
| 141 | Ok(()) |
| 142 | } |
| 143 | |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 144 | fn get_apk_roothash() -> Result<Box<[u8]>> { |
| 145 | let mut idsig = File::open("/dev/block/by-name/microdroid-apk-idsig")?; |
| 146 | let idsig = V4Signature::from(&mut idsig)?; |
| 147 | Ok(idsig.hashing_info.raw_root_hash) |
| 148 | } |
| 149 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 150 | fn load_config(path: &Path) -> Result<VmPayloadConfig> { |
| 151 | info!("loading config from {:?}...", path); |
| 152 | let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?; |
| 153 | Ok(serde_json::from_reader(file)?) |
| 154 | } |
| 155 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 156 | /// Executes the given task. Stdout of the task is piped into the vsock stream to the |
| 157 | /// virtualizationservice in the host side. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 158 | fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<()> { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 159 | info!("executing main task {:?}...", task); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 160 | let mut child = build_command(task)?.spawn()?; |
| 161 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 162 | let local_cid = get_local_cid()?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 163 | info!("notifying payload started"); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 164 | service.notifyPayloadStarted(local_cid as i32)?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 165 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 166 | if let Some(code) = child.wait()?.code() { |
| 167 | info!("notifying payload finished"); |
| 168 | service.notifyPayloadFinished(local_cid as i32, code)?; |
| 169 | |
| 170 | if code == 0 { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 171 | info!("task successfully finished"); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 172 | } else { |
| 173 | error!("task exited with exit code: {}", code); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 174 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 175 | } else { |
| 176 | error!("task terminated by signal"); |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 177 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 178 | Ok(()) |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 179 | } |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 180 | |
| 181 | fn build_command(task: &Task) -> Result<Command> { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 182 | const VMADDR_CID_HOST: u32 = 2; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 183 | |
| 184 | let mut command = match task.type_ { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 185 | TaskType::Executable => { |
| 186 | let mut command = Command::new(&task.command); |
| 187 | command.args(&task.args); |
| 188 | command |
| 189 | } |
| 190 | TaskType::MicrodroidLauncher => { |
| 191 | let mut command = Command::new("/system/bin/microdroid_launcher"); |
| 192 | command.arg(find_library_path(&task.command)?).args(&task.args); |
| 193 | command |
| 194 | } |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 195 | }; |
| 196 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame^] | 197 | match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, VM_STREAM_SERVICE_PORT as u32) { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 198 | Ok(stream) => { |
| 199 | // SAFETY: the ownership of the underlying file descriptor is transferred from stream |
| 200 | // to the file object, and then into the Command object. When the command is finished, |
| 201 | // the file descriptor is closed. |
| 202 | let file = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 203 | command |
| 204 | .stdin(Stdio::from(file.try_clone()?)) |
| 205 | .stdout(Stdio::from(file.try_clone()?)) |
| 206 | .stderr(Stdio::from(file)); |
| 207 | } |
| 208 | Err(e) => { |
| 209 | error!("failed to connect to virtualization service: {}", e); |
| 210 | // Don't fail hard here. Even if we failed to connect to the virtualizationservice, |
| 211 | // we keep executing the task. This can happen if the owner of the VM doesn't register |
| 212 | // callback to accept the stream. Use /dev/null as the stream so that the task can |
| 213 | // make progress without waiting for someone to consume the output. |
| 214 | command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | Ok(command) |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | fn find_library_path(name: &str) -> Result<String> { |
| 222 | let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?; |
| 223 | let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?; |
| 224 | let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?; |
| 225 | let path = format!("/mnt/apk/lib/{}/{}", abi, name); |
| 226 | |
| 227 | let metadata = fs::metadata(&path)?; |
| 228 | if !metadata.is_file() { |
| 229 | bail!("{} is not a file", &path); |
| 230 | } |
| 231 | |
| 232 | Ok(path) |
| 233 | } |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 234 | |
| 235 | fn to_hex_string(buf: &[u8]) -> String { |
| 236 | buf.iter().map(|b| format!("{:02X}", b)).collect() |
| 237 | } |