Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [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 | //! Starts and manages instances of the CompOS VM. At most one instance should be running at |
| 18 | //! a time. |
| 19 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 20 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 21 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 22 | }; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 23 | use anyhow::{bail, Context, Result}; |
| 24 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 25 | use compos_aidl_interface::binder::{ParcelFileDescriptor, Strong}; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 26 | use compos_common::compos_client::VmInstance; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 27 | use compos_common::{ |
| 28 | COMPOS_DATA_ROOT, CURRENT_DIR, INSTANCE_IMAGE_FILE, PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE, |
| 29 | }; |
| 30 | use log::{info, warn}; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 31 | use std::fs; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 32 | use std::path::{Path, PathBuf}; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 33 | use std::sync::{Arc, Mutex, Weak}; |
| 34 | |
| 35 | pub struct CompOsInstance { |
| 36 | #[allow(dead_code)] // Keeps VirtualizationService & the VM alive |
| 37 | vm_instance: VmInstance, |
| 38 | service: Strong<dyn ICompOsService>, |
| 39 | } |
| 40 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 41 | pub struct InstanceManager { |
| 42 | service: Strong<dyn IVirtualizationService>, |
| 43 | state: Mutex<State>, |
| 44 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 45 | |
| 46 | impl InstanceManager { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 47 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 48 | Self { service, state: Default::default() } |
| 49 | } |
| 50 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 51 | pub fn get_running_service(&self) -> Result<Strong<dyn ICompOsService>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 52 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 53 | let instance = state.get_running_instance().context("No running instance")?; |
| 54 | Ok(instance.service.clone()) |
| 55 | } |
| 56 | |
| 57 | pub fn start_current_instance(&self) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 58 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 59 | state.mark_starting()?; |
| 60 | // Don't hold the lock while we start the instance to avoid blocking other callers. |
| 61 | drop(state); |
| 62 | |
| 63 | let instance = self.try_start_current_instance(); |
| 64 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 65 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 66 | if let Ok(ref instance) = instance { |
| 67 | state.mark_started(instance)?; |
| 68 | } else { |
| 69 | state.mark_stopped(); |
| 70 | } |
| 71 | instance |
| 72 | } |
| 73 | |
| 74 | fn try_start_current_instance(&self) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 75 | let instance_files = InstanceFiles::new(CURRENT_DIR); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 76 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 77 | let compos_instance = instance_files.create_or_start_instance(&*self.service)?; |
| 78 | |
| 79 | Ok(Arc::new(compos_instance)) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | struct InstanceFiles { |
| 84 | instance_name: String, |
| 85 | instance_root: PathBuf, |
| 86 | instance_image: PathBuf, |
| 87 | key_blob: PathBuf, |
| 88 | public_key: PathBuf, |
| 89 | } |
| 90 | |
| 91 | impl InstanceFiles { |
| 92 | fn new(instance_name: &str) -> Self { |
| 93 | let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name); |
| 94 | let instant_root_path = instance_root.as_path(); |
| 95 | let instance_image = instant_root_path.join(INSTANCE_IMAGE_FILE); |
| 96 | let key_blob = instant_root_path.join(PRIVATE_KEY_BLOB_FILE); |
| 97 | let public_key = instant_root_path.join(PUBLIC_KEY_FILE); |
| 98 | Self { |
| 99 | instance_name: instance_name.to_owned(), |
| 100 | instance_root, |
| 101 | instance_image, |
| 102 | key_blob, |
| 103 | public_key, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fn create_or_start_instance( |
| 108 | &self, |
| 109 | service: &dyn IVirtualizationService, |
| 110 | ) -> Result<CompOsInstance> { |
| 111 | let compos_instance = self.start_instance(); |
| 112 | match compos_instance { |
| 113 | Ok(_) => return compos_instance, |
| 114 | Err(e) => warn!("Failed to start {}: {}", self.instance_name, e), |
| 115 | } |
| 116 | |
| 117 | self.start_new_instance(service) |
| 118 | } |
| 119 | |
| 120 | fn start_instance(&self) -> Result<CompOsInstance> { |
| 121 | // No point even trying if the files we need aren't there. |
| 122 | self.check_files_exist()?; |
| 123 | |
| 124 | let key_blob = fs::read(&self.key_blob).context("Reading private key blob")?; |
| 125 | let public_key = fs::read(&self.public_key).context("Reading public key")?; |
| 126 | |
| 127 | let vm_instance = VmInstance::start(&self.instance_image).context("Starting VM")?; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 128 | let service = vm_instance.get_service().context("Connecting to CompOS")?; |
| 129 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 130 | if !service.verifySigningKey(&key_blob, &public_key).context("Verifying key pair")? { |
| 131 | bail!("Key pair invalid"); |
| 132 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 133 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 134 | // If we get this far then the instance image is valid in the current context (e.g. the |
| 135 | // current set of APEXes) and the key blob can be successfully decrypted by the VM. So the |
| 136 | // files have not been tampered with and we're good to go. |
| 137 | |
| 138 | service.initializeSigningKey(&key_blob).context("Loading signing key")?; |
| 139 | |
| 140 | Ok(CompOsInstance { vm_instance, service }) |
| 141 | } |
| 142 | |
| 143 | fn start_new_instance( |
| 144 | &self, |
| 145 | virtualization_service: &dyn IVirtualizationService, |
| 146 | ) -> Result<CompOsInstance> { |
| 147 | info!("Creating {} CompOs instance", self.instance_name); |
| 148 | |
| 149 | // Ignore failure here - the directory may already exist. |
| 150 | let _ = fs::create_dir(&self.instance_root); |
| 151 | |
| 152 | self.create_instance_image(virtualization_service)?; |
| 153 | |
| 154 | let vm_instance = VmInstance::start(&self.instance_image).context("Starting VM")?; |
| 155 | let service = vm_instance.get_service().context("Connecting to CompOS")?; |
| 156 | |
| 157 | let key_data = service.generateSigningKey().context("Generating signing key")?; |
| 158 | fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?; |
| 159 | // TODO: Extract public key from cert |
| 160 | fs::write(&self.public_key, &key_data.certificate).context("Writing public key")?; |
| 161 | |
| 162 | // We don't need to verify the key, since we just generated it and have it in memory. |
| 163 | |
| 164 | service.initializeSigningKey(&key_data.keyBlob).context("Loading signing key")?; |
| 165 | |
| 166 | Ok(CompOsInstance { vm_instance, service }) |
| 167 | } |
| 168 | |
| 169 | fn create_instance_image( |
| 170 | &self, |
| 171 | virtualization_service: &dyn IVirtualizationService, |
| 172 | ) -> Result<()> { |
| 173 | let instance_image = fs::OpenOptions::new() |
| 174 | .create(true) |
| 175 | .read(true) |
| 176 | .write(true) |
| 177 | .open(&self.instance_image) |
| 178 | .context("Creating instance image file")?; |
| 179 | let instance_image = ParcelFileDescriptor::new(instance_image); |
| 180 | // TODO: Where does this number come from? |
| 181 | let size = 10 * 1024 * 1024; |
| 182 | virtualization_service |
| 183 | .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE) |
| 184 | .context("Writing instance image file")?; |
| 185 | Ok(()) |
| 186 | } |
| 187 | |
| 188 | fn check_files_exist(&self) -> Result<()> { |
| 189 | if !self.instance_root.is_dir() { |
| 190 | bail!("Directory {} not found", self.instance_root.display()) |
| 191 | }; |
| 192 | Self::check_file_exists(&self.instance_image)?; |
| 193 | Self::check_file_exists(&self.key_blob)?; |
| 194 | Self::check_file_exists(&self.public_key)?; |
| 195 | Ok(()) |
| 196 | } |
| 197 | |
| 198 | fn check_file_exists(file: &Path) -> Result<()> { |
| 199 | if !file.is_file() { |
| 200 | bail!("File {} not found", file.display()) |
| 201 | }; |
| 202 | Ok(()) |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | // Ensures we only run one instance at a time. |
| 207 | // Valid states: |
| 208 | // Starting: is_starting is true, running_instance is None. |
| 209 | // Started: is_starting is false, running_instance is Some(x) and there is a strong ref to x. |
| 210 | // Stopped: is_starting is false and running_instance is None or a weak ref to a dropped instance. |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame^] | 211 | // The panic calls here should never happen, unless the code above in InstanceManager is buggy. |
| 212 | // In particular nothing the client does should be able to trigger them. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 213 | #[derive(Default)] |
| 214 | struct State { |
| 215 | running_instance: Option<Weak<CompOsInstance>>, |
| 216 | is_starting: bool, |
| 217 | } |
| 218 | |
| 219 | impl State { |
| 220 | // Move to Starting iff we are Stopped. |
| 221 | fn mark_starting(&mut self) -> Result<()> { |
| 222 | if self.is_starting { |
| 223 | bail!("An instance is already starting"); |
| 224 | } |
| 225 | if let Some(weak) = &self.running_instance { |
| 226 | if weak.strong_count() != 0 { |
| 227 | bail!("An instance is already running"); |
| 228 | } |
| 229 | } |
| 230 | self.running_instance = None; |
| 231 | self.is_starting = true; |
| 232 | Ok(()) |
| 233 | } |
| 234 | |
| 235 | // Move from Starting to Stopped. |
| 236 | fn mark_stopped(&mut self) { |
| 237 | if !self.is_starting || self.running_instance.is_some() { |
| 238 | panic!("Tried to mark stopped when not starting"); |
| 239 | } |
| 240 | self.is_starting = false; |
| 241 | } |
| 242 | |
| 243 | // Move from Starting to Started. |
| 244 | fn mark_started(&mut self, instance: &Arc<CompOsInstance>) -> Result<()> { |
| 245 | if !self.is_starting { |
| 246 | panic!("Tried to mark started when not starting") |
| 247 | } |
| 248 | if self.running_instance.is_some() { |
| 249 | panic!("Attempted to mark started when already started"); |
| 250 | } |
| 251 | self.is_starting = false; |
| 252 | self.running_instance = Some(Arc::downgrade(instance)); |
| 253 | Ok(()) |
| 254 | } |
| 255 | |
| 256 | // Return the running instance if we are in the Started state. |
| 257 | fn get_running_instance(&mut self) -> Option<Arc<CompOsInstance>> { |
| 258 | if self.is_starting { |
| 259 | return None; |
| 260 | } |
| 261 | let instance = self.running_instance.as_ref()?.upgrade(); |
| 262 | if instance.is_none() { |
| 263 | // No point keeping an orphaned weak reference |
| 264 | self.running_instance = None; |
| 265 | } |
| 266 | instance |
| 267 | } |
| 268 | } |