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 | }; |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 23 | use anyhow::{anyhow, Context, Result}; |
Andrew Walbran | 46999c9 | 2022-08-04 17:33:46 +0000 | [diff] [blame] | 24 | use binder::{LazyServiceGuard, ParcelFileDescriptor, Strong}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 25 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 26 | use compos_common::compos_client::{ComposClient, VmParameters}; |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 27 | use compos_common::{ |
| 28 | COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, IDSIG_MANIFEST_EXT_APK_FILE, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 29 | INSTANCE_ID_FILE, INSTANCE_IMAGE_FILE, |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 30 | }; |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 31 | use log::info; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 32 | use std::fs; |
| 33 | use std::path::{Path, PathBuf}; |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 34 | use std::sync::Arc; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 35 | |
| 36 | pub struct CompOsInstance { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 37 | service: Strong<dyn ICompOsService>, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 38 | #[allow(dead_code)] // Keeps VirtualizationService & the VM alive |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 39 | vm_instance: ComposClient, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 40 | #[allow(dead_code)] // Keeps composd process alive |
| 41 | lazy_service_guard: LazyServiceGuard, |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 42 | // Keep this alive as long as we are |
| 43 | instance_tracker: Arc<()>, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | impl CompOsInstance { |
| 47 | pub fn get_service(&self) -> Strong<dyn ICompOsService> { |
| 48 | self.service.clone() |
| 49 | } |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 50 | |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 51 | /// Returns an Arc that this instance holds a strong reference to as long as it exists. This |
| 52 | /// can be used to determine when the instance has been dropped. |
| 53 | pub fn get_instance_tracker(&self) -> &Arc<()> { |
| 54 | &self.instance_tracker |
| 55 | } |
| 56 | |
| 57 | /// Attempt to shut down the VM cleanly, giving time for any relevant logs to be written. |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 58 | pub fn shutdown(self) -> LazyServiceGuard { |
| 59 | self.vm_instance.shutdown(self.service); |
| 60 | // Return the guard to the caller, since we might be terminated at any point after it is |
| 61 | // dropped, and there might still be things to do. |
| 62 | self.lazy_service_guard |
| 63 | } |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | pub struct InstanceStarter { |
| 67 | instance_name: String, |
| 68 | instance_root: PathBuf, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 69 | instance_id_file: PathBuf, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 70 | instance_image: PathBuf, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 71 | idsig: PathBuf, |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 72 | idsig_manifest_apk: PathBuf, |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 73 | idsig_manifest_ext_apk: PathBuf, |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 74 | vm_parameters: VmParameters, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | impl InstanceStarter { |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 78 | pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self { |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 79 | let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name); |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 80 | let instance_root_path = instance_root.as_path(); |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 81 | let instance_id_file = instance_root_path.join(INSTANCE_ID_FILE); |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 82 | let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE); |
| 83 | let idsig = instance_root_path.join(IDSIG_FILE); |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 84 | let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE); |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 85 | let idsig_manifest_ext_apk = instance_root_path.join(IDSIG_MANIFEST_EXT_APK_FILE); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 86 | Self { |
| 87 | instance_name: instance_name.to_owned(), |
| 88 | instance_root, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 89 | instance_id_file, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 90 | instance_image, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 91 | idsig, |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 92 | idsig_manifest_apk, |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 93 | idsig_manifest_ext_apk, |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 94 | vm_parameters, |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 98 | pub fn start_new_instance( |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 99 | &self, |
| 100 | virtualization_service: &dyn IVirtualizationService, |
| 101 | ) -> Result<CompOsInstance> { |
| 102 | info!("Creating {} CompOs instance", self.instance_name); |
| 103 | |
Dan Albert | 04e5dbd | 2023-05-08 22:49:40 +0000 | [diff] [blame] | 104 | fs::create_dir_all(&self.instance_root)?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 105 | |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 106 | // Overwrite any existing instance - it's unlikely to be valid with the current set |
| 107 | // of APEXes, and finding out it isn't is much more expensive than creating a new one. |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 108 | self.create_instance_image(virtualization_service)?; |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 109 | // TODO(b/294177871): Ping VS to delete the old instance's secret. |
| 110 | if cfg!(llpvm_changes) { |
| 111 | self.allocate_instance_id(virtualization_service)?; |
| 112 | } |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 113 | // Delete existing idsig files. Ignore error in case idsig doesn't exist. |
Dan Albert | 04e5dbd | 2023-05-08 22:49:40 +0000 | [diff] [blame] | 114 | let _ignored1 = fs::remove_file(&self.idsig); |
| 115 | let _ignored2 = fs::remove_file(&self.idsig_manifest_apk); |
| 116 | let _ignored3 = fs::remove_file(&self.idsig_manifest_ext_apk); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 117 | |
Alan Stokes | 5430eca | 2022-03-21 14:02:09 +0000 | [diff] [blame] | 118 | let instance = self.start_vm(virtualization_service)?; |
| 119 | |
| 120 | // Retrieve the VM's attestation chain as a BCC and save it in the instance directory. |
| 121 | let bcc = instance.service.getAttestationChain().context("Getting attestation chain")?; |
| 122 | fs::write(self.instance_root.join("bcc"), bcc).context("Writing BCC")?; |
| 123 | |
| 124 | Ok(instance) |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 127 | fn start_vm( |
| 128 | &self, |
| 129 | virtualization_service: &dyn IVirtualizationService, |
| 130 | ) -> Result<CompOsInstance> { |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 131 | let instance_id: [u8; 64] = if cfg!(llpvm_changes) { |
| 132 | fs::read(&self.instance_id_file)? |
| 133 | .try_into() |
| 134 | .map_err(|_| anyhow!("Failed to get instance_id"))? |
| 135 | } else { |
| 136 | [0u8; 64] |
| 137 | }; |
| 138 | |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 139 | let instance_image = fs::OpenOptions::new() |
| 140 | .read(true) |
| 141 | .write(true) |
| 142 | .open(&self.instance_image) |
| 143 | .context("Failed to open instance image")?; |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 144 | let vm_instance = ComposClient::start( |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 145 | virtualization_service, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 146 | instance_id, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 147 | instance_image, |
| 148 | &self.idsig, |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 149 | &self.idsig_manifest_apk, |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 150 | &self.idsig_manifest_ext_apk, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 151 | &self.vm_parameters, |
| 152 | ) |
| 153 | .context("Starting VM")?; |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 154 | let service = vm_instance.connect_service().context("Connecting to CompOS")?; |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 155 | Ok(CompOsInstance { |
| 156 | vm_instance, |
| 157 | service, |
| 158 | lazy_service_guard: Default::default(), |
| 159 | instance_tracker: Default::default(), |
| 160 | }) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | fn create_instance_image( |
| 164 | &self, |
| 165 | virtualization_service: &dyn IVirtualizationService, |
| 166 | ) -> Result<()> { |
| 167 | let instance_image = fs::OpenOptions::new() |
| 168 | .create(true) |
Alan Stokes | 23b90ee | 2021-10-28 11:36:14 +0100 | [diff] [blame] | 169 | .truncate(true) |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 170 | .read(true) |
| 171 | .write(true) |
| 172 | .open(&self.instance_image) |
| 173 | .context("Creating instance image file")?; |
| 174 | let instance_image = ParcelFileDescriptor::new(instance_image); |
| 175 | // TODO: Where does this number come from? |
| 176 | let size = 10 * 1024 * 1024; |
| 177 | virtualization_service |
| 178 | .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE) |
| 179 | .context("Writing instance image file")?; |
| 180 | Ok(()) |
| 181 | } |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 182 | |
| 183 | fn allocate_instance_id( |
| 184 | &self, |
| 185 | virtualization_service: &dyn IVirtualizationService, |
| 186 | ) -> Result<()> { |
| 187 | let id = virtualization_service.allocateInstanceId().context("Allocating Instance Id")?; |
| 188 | fs::write(&self.instance_id_file, id)?; |
| 189 | Ok(()) |
| 190 | } |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 191 | } |