Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +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 | //! Responsible for validating and starting an existing instance of the CompOS VM, or creating and |
| 18 | //! starting a new instance if necessary. |
| 19 | |
| 20 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 21 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 22 | }; |
| 23 | use anyhow::{bail, Context, Result}; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 24 | use binder_common::lazy_service::LazyServiceGuard; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 25 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
| 26 | use compos_aidl_interface::binder::{ParcelFileDescriptor, Strong}; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 27 | use compos_common::compos_client::{VmInstance, VmParameters}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 28 | use compos_common::{ |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 29 | COMPOS_DATA_ROOT, IDSIG_FILE, INSTANCE_IMAGE_FILE, PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 30 | }; |
| 31 | use log::{info, warn}; |
| 32 | use std::fs; |
| 33 | use std::path::{Path, PathBuf}; |
| 34 | |
| 35 | pub struct CompOsInstance { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 36 | service: Strong<dyn ICompOsService>, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 37 | #[allow(dead_code)] // Keeps VirtualizationService & the VM alive |
| 38 | vm_instance: VmInstance, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 39 | #[allow(dead_code)] // Keeps composd process alive |
| 40 | lazy_service_guard: LazyServiceGuard, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | impl CompOsInstance { |
| 44 | pub fn get_service(&self) -> Strong<dyn ICompOsService> { |
| 45 | self.service.clone() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub struct InstanceStarter { |
| 50 | instance_name: String, |
| 51 | instance_root: PathBuf, |
| 52 | instance_image: PathBuf, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 53 | idsig: PathBuf, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 54 | key_blob: PathBuf, |
| 55 | public_key: PathBuf, |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 56 | vm_parameters: VmParameters, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | impl InstanceStarter { |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 60 | pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self { |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 61 | let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name); |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 62 | let instance_root_path = instance_root.as_path(); |
| 63 | let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE); |
| 64 | let idsig = instance_root_path.join(IDSIG_FILE); |
| 65 | let key_blob = instance_root_path.join(PRIVATE_KEY_BLOB_FILE); |
| 66 | let public_key = instance_root_path.join(PUBLIC_KEY_FILE); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 67 | Self { |
| 68 | instance_name: instance_name.to_owned(), |
| 69 | instance_root, |
| 70 | instance_image, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 71 | idsig, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 72 | key_blob, |
| 73 | public_key, |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 74 | vm_parameters, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn create_or_start_instance( |
| 79 | &self, |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 80 | virtualization_service: &dyn IVirtualizationService, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 81 | ) -> Result<CompOsInstance> { |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 82 | let compos_instance = self.start_existing_instance(virtualization_service); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 83 | match compos_instance { |
| 84 | Ok(_) => return compos_instance, |
Alan Stokes | 14f0739 | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 85 | Err(e) => warn!("Failed to start: {}", e), |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 88 | self.start_new_instance(virtualization_service) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 91 | fn start_existing_instance( |
| 92 | &self, |
| 93 | virtualization_service: &dyn IVirtualizationService, |
| 94 | ) -> Result<CompOsInstance> { |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 95 | // No point even trying if the files we need aren't there. |
| 96 | self.check_files_exist()?; |
| 97 | |
Alan Stokes | 14f0739 | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 98 | info!("Starting {} CompOs instance", self.instance_name); |
| 99 | |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 100 | let key_blob = fs::read(&self.key_blob).context("Reading private key blob")?; |
| 101 | let public_key = fs::read(&self.public_key).context("Reading public key")?; |
| 102 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 103 | let compos_instance = self.start_vm(virtualization_service)?; |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 104 | let service = &compos_instance.service; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 105 | |
| 106 | if !service.verifySigningKey(&key_blob, &public_key).context("Verifying key pair")? { |
| 107 | bail!("Key pair invalid"); |
| 108 | } |
| 109 | |
| 110 | // If we get this far then the instance image is valid in the current context (e.g. the |
| 111 | // current set of APEXes) and the key blob can be successfully decrypted by the VM. So the |
| 112 | // files have not been tampered with and we're good to go. |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 113 | service.initializeSigningKey(&key_blob).context("Loading signing key")?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 114 | |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 115 | Ok(compos_instance) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | fn start_new_instance( |
| 119 | &self, |
| 120 | virtualization_service: &dyn IVirtualizationService, |
| 121 | ) -> Result<CompOsInstance> { |
| 122 | info!("Creating {} CompOs instance", self.instance_name); |
| 123 | |
| 124 | // Ignore failure here - the directory may already exist. |
| 125 | let _ = fs::create_dir(&self.instance_root); |
| 126 | |
| 127 | self.create_instance_image(virtualization_service)?; |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 128 | // Delete existing idsig file. Ignore error in case idsig doesn't exist. |
| 129 | let _ = fs::remove_file(&self.idsig); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 130 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 131 | let compos_instance = self.start_vm(virtualization_service)?; |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 132 | let service = &compos_instance.service; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 133 | |
| 134 | let key_data = service.generateSigningKey().context("Generating signing key")?; |
| 135 | fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?; |
Alan Stokes | 14f0739 | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 136 | |
| 137 | let key_result = composd_native::extract_rsa_public_key(&key_data.certificate); |
| 138 | let rsa_public_key = key_result.key; |
| 139 | if rsa_public_key.is_empty() { |
| 140 | bail!("Failed to extract public key from certificate: {}", key_result.error); |
| 141 | } |
| 142 | fs::write(&self.public_key, &rsa_public_key).context("Writing public key")?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 143 | |
Victor Hsieh | 18775b1 | 2021-10-12 17:42:48 -0700 | [diff] [blame] | 144 | // Unlike when starting an existing instance, we don't need to verify the key, since we |
| 145 | // just generated it and have it in memory. |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 146 | service.initializeSigningKey(&key_data.keyBlob).context("Loading signing key")?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 147 | |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 148 | Ok(compos_instance) |
| 149 | } |
| 150 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 151 | fn start_vm( |
| 152 | &self, |
| 153 | virtualization_service: &dyn IVirtualizationService, |
| 154 | ) -> Result<CompOsInstance> { |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 155 | let instance_image = fs::OpenOptions::new() |
| 156 | .read(true) |
| 157 | .write(true) |
| 158 | .open(&self.instance_image) |
| 159 | .context("Failed to open instance image")?; |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 160 | let vm_instance = VmInstance::start( |
| 161 | virtualization_service, |
| 162 | instance_image, |
| 163 | &self.idsig, |
| 164 | &self.vm_parameters, |
| 165 | ) |
| 166 | .context("Starting VM")?; |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 167 | let service = vm_instance.get_service().context("Connecting to CompOS")?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 168 | Ok(CompOsInstance { vm_instance, service, lazy_service_guard: Default::default() }) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | fn create_instance_image( |
| 172 | &self, |
| 173 | virtualization_service: &dyn IVirtualizationService, |
| 174 | ) -> Result<()> { |
| 175 | let instance_image = fs::OpenOptions::new() |
| 176 | .create(true) |
Alan Stokes | 23b90ee | 2021-10-28 11:36:14 +0100 | [diff] [blame] | 177 | .truncate(true) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 178 | .read(true) |
| 179 | .write(true) |
| 180 | .open(&self.instance_image) |
| 181 | .context("Creating instance image file")?; |
| 182 | let instance_image = ParcelFileDescriptor::new(instance_image); |
| 183 | // TODO: Where does this number come from? |
| 184 | let size = 10 * 1024 * 1024; |
| 185 | virtualization_service |
| 186 | .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE) |
| 187 | .context("Writing instance image file")?; |
| 188 | Ok(()) |
| 189 | } |
| 190 | |
| 191 | fn check_files_exist(&self) -> Result<()> { |
| 192 | if !self.instance_root.is_dir() { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 193 | bail!("Directory {:?} not found", self.instance_root) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 194 | }; |
| 195 | Self::check_file_exists(&self.instance_image)?; |
| 196 | Self::check_file_exists(&self.key_blob)?; |
| 197 | Self::check_file_exists(&self.public_key)?; |
| 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | fn check_file_exists(file: &Path) -> Result<()> { |
| 202 | if !file.is_file() { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 203 | bail!("File {:?} not found", file) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 204 | }; |
| 205 | Ok(()) |
| 206 | } |
| 207 | } |