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 | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 19 | mod payload; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 20 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 21 | use crate::instance::{ApkData, InstanceDisk, MicrodroidData, RootHash}; |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 22 | use anyhow::{anyhow, bail, ensure, Context, Error, Result}; |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 23 | use apkverify::{get_public_key_der, 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; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 27 | use log::{error, info, warn}; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 28 | use microdroid_metadata::{write_metadata, Metadata}; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 29 | use microdroid_payload_config::{Task, TaskType, VmPayloadConfig}; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 30 | use payload::{get_apex_data_from_payload, load_metadata, to_metadata}; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 31 | use rustutils::system_properties; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 32 | use rustutils::system_properties::PropertyWatcher; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 33 | use std::fs::{self, File, OpenOptions}; |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 34 | use std::os::unix::io::{FromRawFd, IntoRawFd}; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 35 | use std::path::Path; |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 36 | use std::process::{Child, Command, Stdio}; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 37 | use std::str; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 38 | use std::time::{Duration, SystemTime}; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 39 | use vsock::VsockStream; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 40 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 41 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::{ |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 42 | ERROR_PAYLOAD_CHANGED, ERROR_PAYLOAD_VERIFICATION_FAILED, ERROR_UNKNOWN, VM_BINDER_SERVICE_PORT, VM_STREAM_SERVICE_PORT, IVirtualMachineService, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 43 | }; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 44 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 45 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 46 | const APK_DM_VERITY_ARGUMENT: ApkDmverityArgument = { |
| 47 | ApkDmverityArgument { |
| 48 | apk: "/dev/block/by-name/microdroid-apk", |
| 49 | idsig: "/dev/block/by-name/microdroid-apk-idsig", |
| 50 | name: "microdroid-apk", |
| 51 | } |
| 52 | }; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 53 | const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk"; |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 54 | const APKDMVERITY_BIN: &str = "/system/bin/apkdmverity"; |
| 55 | const ZIPFUSE_BIN: &str = "/system/bin/zipfuse"; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 56 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 57 | /// The CID representing the host VM |
| 58 | const VMADDR_CID_HOST: u32 = 2; |
| 59 | |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 60 | const APEX_CONFIG_DONE_PROP: &str = "apex_config.done"; |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 61 | const LOGD_ENABLED_PROP: &str = "ro.boot.logd.enabled"; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 62 | |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 63 | #[derive(thiserror::Error, Debug)] |
| 64 | enum MicrodroidError { |
| 65 | #[error("Payload has changed: {0}")] |
| 66 | PayloadChanged(String), |
| 67 | #[error("Payload verification has failed: {0}")] |
| 68 | PayloadVerificationFailed(String), |
| 69 | } |
| 70 | |
| 71 | fn translate_error(err: &Error) -> (i32, String) { |
| 72 | if let Some(e) = err.downcast_ref::<MicrodroidError>() { |
| 73 | match e { |
| 74 | MicrodroidError::PayloadChanged(msg) => (ERROR_PAYLOAD_CHANGED, msg.to_string()), |
| 75 | MicrodroidError::PayloadVerificationFailed(msg) => { |
| 76 | (ERROR_PAYLOAD_VERIFICATION_FAILED, msg.to_string()) |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | (ERROR_UNKNOWN, err.to_string()) |
| 81 | } |
| 82 | } |
| 83 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 84 | fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> { |
| 85 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be |
| 86 | // safely taken by new_spibinder. |
| 87 | let ibinder = unsafe { |
| 88 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient( |
| 89 | VMADDR_CID_HOST, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 90 | VM_BINDER_SERVICE_PORT as u32, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 91 | ) as *mut AIBinder) |
| 92 | }; |
| 93 | if let Some(ibinder) = ibinder { |
| 94 | <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service") |
| 95 | } else { |
| 96 | bail!("Invalid raw AIBinder") |
| 97 | } |
| 98 | } |
| 99 | |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 100 | fn main() { |
| 101 | if let Err(e) = try_main() { |
Jooyung Han | bfe086f | 2021-10-28 10:15:45 +0900 | [diff] [blame] | 102 | error!("Failed with {:?}. Shutting down...", e); |
| 103 | if let Err(e) = system_properties::write("sys.powerctl", "shutdown") { |
| 104 | error!("failed to shutdown {:?}", e); |
| 105 | } |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 106 | std::process::exit(1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | fn try_main() -> Result<()> { |
Jiyong Park | 79b8801 | 2021-06-25 13:06:25 +0900 | [diff] [blame] | 111 | kernlog::init()?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 112 | info!("started."); |
| 113 | |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 114 | let service = get_vms_rpc_binder().context("cannot connect to VirtualMachineService")?; |
| 115 | if let Err(err) = try_start_payload(&service) { |
| 116 | let (error_code, message) = translate_error(&err); |
| 117 | service.notifyError(error_code, &message)?; |
| 118 | Err(err) |
| 119 | } else { |
| 120 | Ok(()) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn try_start_payload(service: &Strong<dyn IVirtualMachineService>) -> Result<()> { |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 125 | let metadata = load_metadata().context("Failed to load payload metadata")?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 126 | |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 127 | let mut instance = InstanceDisk::new().context("Failed to load instance.img")?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 128 | let saved_data = instance.read_microdroid_data().context("Failed to read identity data")?; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 129 | |
| 130 | // Verify the payload before using it. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 131 | let verified_data = |
| 132 | verify_payload(&metadata, saved_data.as_ref()).context("Payload verification failed")?; |
| 133 | if let Some(saved_data) = saved_data { |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 134 | ensure!( |
| 135 | saved_data == verified_data, |
| 136 | MicrodroidError::PayloadChanged(String::from( |
| 137 | "Detected an update of the payload which isn't supported yet." |
| 138 | )) |
| 139 | ); |
| 140 | info!("Saved data is verified."); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 141 | } else { |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 142 | info!("Saving verified data."); |
| 143 | instance.write_microdroid_data(&verified_data).context("Failed to write identity data")?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 146 | // Before reading a file from the APK, start zipfuse |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 147 | run_zipfuse( |
| 148 | "fscontext=u:object_r:zipfusefs:s0,context=u:object_r:system_file:s0", |
| 149 | Path::new("/dev/block/mapper/microdroid-apk"), |
| 150 | Path::new("/mnt/apk"), |
| 151 | ) |
| 152 | .context("Failed to run zipfuse")?; |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 153 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 154 | if !metadata.payload_config_path.is_empty() { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 155 | let config = load_config(Path::new(&metadata.payload_config_path))?; |
| 156 | |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 157 | 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] | 158 | 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] | 159 | warn!("failed to set ro.vmsecret.keymint: {}", err); |
| 160 | } |
| 161 | |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 162 | // Wait until apex config is done. (e.g. linker configuration for apexes) |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 163 | // TODO(jooyung): wait until sys.boot_completed? |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 164 | wait_for_apex_config_done()?; |
| 165 | |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 166 | if let Some(main_task) = &config.task { |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 167 | exec_task(main_task, service).map_err(|e| { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 168 | error!("failed to execute task: {}", e); |
| 169 | e |
| 170 | })?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | Ok(()) |
| 175 | } |
| 176 | |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 177 | struct ApkDmverityArgument<'a> { |
| 178 | apk: &'a str, |
| 179 | idsig: &'a str, |
| 180 | name: &'a str, |
| 181 | } |
| 182 | |
| 183 | fn run_apkdmverity(args: &[ApkDmverityArgument]) -> Result<Child> { |
| 184 | let mut cmd = Command::new(APKDMVERITY_BIN); |
| 185 | |
| 186 | cmd.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); |
| 187 | |
| 188 | for argument in args { |
| 189 | cmd.arg("--apk").arg(argument.apk).arg(argument.idsig).arg(argument.name); |
| 190 | } |
| 191 | |
| 192 | cmd.spawn().context("Spawn apkdmverity") |
| 193 | } |
| 194 | |
| 195 | fn run_zipfuse(option: &str, zip_path: &Path, mount_dir: &Path) -> Result<Child> { |
| 196 | Command::new(ZIPFUSE_BIN) |
| 197 | .arg("-o") |
| 198 | .arg(option) |
| 199 | .arg(zip_path) |
| 200 | .arg(mount_dir) |
| 201 | .stdin(Stdio::null()) |
| 202 | .stdout(Stdio::null()) |
| 203 | .stderr(Stdio::null()) |
| 204 | .spawn() |
| 205 | .context("Spawn zipfuse") |
| 206 | } |
| 207 | |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 208 | // Verify payload before executing it. For APK payload, Full verification (which is slow) is done |
| 209 | // when the root_hash values from the idsig file and the instance disk are different. This function |
| 210 | // returns the verified root hash (for APK payload) and pubkeys (for APEX payloads) that can be |
| 211 | // saved to the instance disk. |
| 212 | fn verify_payload( |
| 213 | metadata: &Metadata, |
| 214 | saved_data: Option<&MicrodroidData>, |
| 215 | ) -> Result<MicrodroidData> { |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 216 | let start_time = SystemTime::now(); |
| 217 | |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 218 | let root_hash = saved_data.map(|d| &d.apk_data.root_hash); |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 219 | let root_hash_from_idsig = get_apk_root_hash_from_idsig()?; |
| 220 | let root_hash_trustful = root_hash == Some(&root_hash_from_idsig); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 221 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 222 | // If root_hash can be trusted, pass it to apkdmverity so that it uses the passed root_hash |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 223 | // instead of the value read from the idsig file. |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 224 | if root_hash_trustful { |
| 225 | let root_hash = to_hex_string(root_hash.unwrap()); |
| 226 | system_properties::write("microdroid_manager.apk_root_hash", &root_hash)?; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Start apkdmverity and wait for the dm-verify block |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 230 | let mut apkdmverity_child = run_apkdmverity(&[APK_DM_VERITY_ARGUMENT])?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 231 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 232 | // While waiting for apkdmverity to mount APK, gathers public keys and root digests from |
| 233 | // APEX payload. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 234 | let apex_data_from_payload = get_apex_data_from_payload(metadata)?; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 235 | if let Some(saved_data) = saved_data.map(|d| &d.apex_data) { |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 236 | // We don't support APEX updates. (assuming that update will change root digest) |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 237 | ensure!( |
| 238 | saved_data == &apex_data_from_payload, |
| 239 | MicrodroidError::PayloadChanged(String::from("APEXes have changed.")) |
| 240 | ); |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 241 | let apex_metadata = to_metadata(&apex_data_from_payload); |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 242 | // Pass metadata(with public keys and root digests) to apexd so that it uses the passed |
| 243 | // metadata instead of the default one (/dev/block/by-name/payload-metadata) |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 244 | OpenOptions::new() |
| 245 | .create_new(true) |
| 246 | .write(true) |
| 247 | .open("/apex/vm-payload-metadata") |
| 248 | .context("Failed to open /apex/vm-payload-metadata") |
| 249 | .and_then(|f| write_metadata(&apex_metadata, f))?; |
| 250 | } |
| 251 | // Start apexd to activate APEXes |
| 252 | system_properties::write("ctl.start", "apexd-vm")?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 253 | |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame^] | 254 | // TODO(inseob): add timeout |
| 255 | apkdmverity_child.wait()?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 256 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 257 | // Do the full verification if the root_hash is un-trustful. This requires the full scanning of |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 258 | // the APK file and therefore can be very slow if the APK is large. Note that this step is |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 259 | // taken only when the root_hash is un-trustful which can be either when this is the first boot |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 260 | // of the VM or APK was updated in the host. |
| 261 | // TODO(jooyung): consider multithreading to make this faster |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 262 | let apk_pubkey = if !root_hash_trustful { |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 263 | verify(DM_MOUNTED_APK_PATH).context(MicrodroidError::PayloadVerificationFailed(format!( |
| 264 | "failed to verify {}", |
| 265 | DM_MOUNTED_APK_PATH |
| 266 | )))? |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 267 | } else { |
| 268 | get_public_key_der(DM_MOUNTED_APK_PATH)? |
| 269 | }; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 270 | |
| 271 | info!("payload verification successful. took {:#?}", start_time.elapsed().unwrap()); |
| 272 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 273 | // At this point, we can ensure that the root_hash from the idsig file is trusted, either by |
| 274 | // fully verifying the APK or by comparing it with the saved root_hash. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 275 | Ok(MicrodroidData { |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 276 | apk_data: ApkData { root_hash: root_hash_from_idsig, pubkey: apk_pubkey }, |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 277 | apex_data: apex_data_from_payload, |
| 278 | }) |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Waits until linker config is generated |
| 282 | fn wait_for_apex_config_done() -> Result<()> { |
| 283 | let mut prop = PropertyWatcher::new(APEX_CONFIG_DONE_PROP)?; |
| 284 | loop { |
| 285 | prop.wait()?; |
| 286 | let val = system_properties::read(APEX_CONFIG_DONE_PROP)?; |
| 287 | if val == "true" { |
| 288 | break; |
| 289 | } |
| 290 | } |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 291 | Ok(()) |
| 292 | } |
| 293 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 294 | fn get_apk_root_hash_from_idsig() -> Result<Box<RootHash>> { |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 295 | let mut idsig = File::open("/dev/block/by-name/microdroid-apk-idsig")?; |
| 296 | let idsig = V4Signature::from(&mut idsig)?; |
| 297 | Ok(idsig.hashing_info.raw_root_hash) |
| 298 | } |
| 299 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 300 | fn load_config(path: &Path) -> Result<VmPayloadConfig> { |
| 301 | info!("loading config from {:?}...", path); |
| 302 | let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?; |
| 303 | Ok(serde_json::from_reader(file)?) |
| 304 | } |
| 305 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 306 | /// Executes the given task. Stdout of the task is piped into the vsock stream to the |
| 307 | /// virtualizationservice in the host side. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 308 | fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<()> { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 309 | info!("executing main task {:?}...", task); |
Inseob Kim | 86ca016 | 2021-10-20 02:21:02 +0000 | [diff] [blame] | 310 | let mut command = build_command(task)?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 311 | |
| 312 | info!("notifying payload started"); |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 313 | service.notifyPayloadStarted()?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 314 | |
Jiyong Park | fa91d70 | 2021-10-18 23:51:39 +0900 | [diff] [blame] | 315 | // Start logging if enabled |
| 316 | // TODO(b/200914564) set filterspec if debug_level is app_only |
| 317 | if system_properties::read(LOGD_ENABLED_PROP)? == "1" { |
| 318 | system_properties::write("ctl.start", "seriallogging")?; |
| 319 | } |
| 320 | |
Inseob Kim | 86ca016 | 2021-10-20 02:21:02 +0000 | [diff] [blame] | 321 | let exit_status = command.spawn()?.wait()?; |
Alan Stokes | 4eea5c7 | 2021-09-20 17:40:28 +0100 | [diff] [blame] | 322 | if let Some(code) = exit_status.code() { |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 323 | info!("notifying payload finished"); |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 324 | service.notifyPayloadFinished(code)?; |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 325 | |
| 326 | if code == 0 { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 327 | info!("task successfully finished"); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 328 | } else { |
| 329 | error!("task exited with exit code: {}", code); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 330 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 331 | } else { |
Alan Stokes | 4eea5c7 | 2021-09-20 17:40:28 +0100 | [diff] [blame] | 332 | error!("task terminated: {}", exit_status); |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 333 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 334 | Ok(()) |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 335 | } |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 336 | |
| 337 | fn build_command(task: &Task) -> Result<Command> { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 338 | const VMADDR_CID_HOST: u32 = 2; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 339 | |
| 340 | let mut command = match task.type_ { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 341 | TaskType::Executable => { |
| 342 | let mut command = Command::new(&task.command); |
| 343 | command.args(&task.args); |
| 344 | command |
| 345 | } |
| 346 | TaskType::MicrodroidLauncher => { |
| 347 | let mut command = Command::new("/system/bin/microdroid_launcher"); |
| 348 | command.arg(find_library_path(&task.command)?).args(&task.args); |
| 349 | command |
| 350 | } |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 351 | }; |
| 352 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 353 | 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] | 354 | Ok(stream) => { |
| 355 | // SAFETY: the ownership of the underlying file descriptor is transferred from stream |
| 356 | // to the file object, and then into the Command object. When the command is finished, |
| 357 | // the file descriptor is closed. |
| 358 | let file = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 359 | command |
| 360 | .stdin(Stdio::from(file.try_clone()?)) |
| 361 | .stdout(Stdio::from(file.try_clone()?)) |
| 362 | .stderr(Stdio::from(file)); |
| 363 | } |
| 364 | Err(e) => { |
| 365 | error!("failed to connect to virtualization service: {}", e); |
| 366 | // Don't fail hard here. Even if we failed to connect to the virtualizationservice, |
| 367 | // we keep executing the task. This can happen if the owner of the VM doesn't register |
| 368 | // callback to accept the stream. Use /dev/null as the stream so that the task can |
| 369 | // make progress without waiting for someone to consume the output. |
| 370 | command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | Ok(command) |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | fn find_library_path(name: &str) -> Result<String> { |
| 378 | let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?; |
| 379 | let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?; |
| 380 | let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?; |
| 381 | let path = format!("/mnt/apk/lib/{}/{}", abi, name); |
| 382 | |
| 383 | let metadata = fs::metadata(&path)?; |
| 384 | if !metadata.is_file() { |
| 385 | bail!("{} is not a file", &path); |
| 386 | } |
| 387 | |
| 388 | Ok(path) |
| 389 | } |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 390 | |
| 391 | fn to_hex_string(buf: &[u8]) -> String { |
| 392 | buf.iter().map(|b| format!("{:02X}", b)).collect() |
| 393 | } |