blob: 4fed98a2a1f1b89b27c006baee2357f894fbbbe3 [file] [log] [blame]
Alan Stokes6b2d0a82021-09-29 11:30:39 +01001/*
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
20use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
21 IVirtualizationService::IVirtualizationService, PartitionType::PartitionType,
22};
23use anyhow::{bail, Context, Result};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010024use binder_common::lazy_service::LazyServiceGuard;
Alan Stokes6b2d0a82021-09-29 11:30:39 +010025use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService;
26use compos_aidl_interface::binder::{ParcelFileDescriptor, Strong};
Alan Stokesd21764c2021-10-25 15:33:40 +010027use compos_common::compos_client::{VmInstance, VmParameters};
Alan Stokes6b2d0a82021-09-29 11:30:39 +010028use compos_common::{
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080029 COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, INSTANCE_IMAGE_FILE,
30 PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010031};
32use log::{info, warn};
33use std::fs;
34use std::path::{Path, PathBuf};
35
36pub struct CompOsInstance {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010037 service: Strong<dyn ICompOsService>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010038 #[allow(dead_code)] // Keeps VirtualizationService & the VM alive
39 vm_instance: VmInstance,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010040 #[allow(dead_code)] // Keeps composd process alive
41 lazy_service_guard: LazyServiceGuard,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010042}
43
44impl CompOsInstance {
45 pub fn get_service(&self) -> Strong<dyn ICompOsService> {
46 self.service.clone()
47 }
48}
49
50pub struct InstanceStarter {
51 instance_name: String,
52 instance_root: PathBuf,
53 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090054 idsig: PathBuf,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080055 idsig_manifest_apk: PathBuf,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010056 key_blob: PathBuf,
57 public_key: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010058 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010059}
60
61impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010062 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010063 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090064 let instance_root_path = instance_root.as_path();
65 let instance_image = instance_root_path.join(INSTANCE_IMAGE_FILE);
66 let idsig = instance_root_path.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080067 let idsig_manifest_apk = instance_root_path.join(IDSIG_MANIFEST_APK_FILE);
Jooyung Hanccae66d2021-12-20 11:54:36 +090068 let key_blob = instance_root_path.join(PRIVATE_KEY_BLOB_FILE);
69 let public_key = instance_root_path.join(PUBLIC_KEY_FILE);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010070 Self {
71 instance_name: instance_name.to_owned(),
72 instance_root,
73 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090074 idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080075 idsig_manifest_apk,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010076 key_blob,
77 public_key,
Alan Stokesd21764c2021-10-25 15:33:40 +010078 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010079 }
80 }
81
82 pub fn create_or_start_instance(
83 &self,
Alan Stokesd21764c2021-10-25 15:33:40 +010084 virtualization_service: &dyn IVirtualizationService,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010085 ) -> Result<CompOsInstance> {
Alan Stokesd21764c2021-10-25 15:33:40 +010086 let compos_instance = self.start_existing_instance(virtualization_service);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010087 match compos_instance {
88 Ok(_) => return compos_instance,
Alan Stokes14f07392021-09-27 14:03:31 +010089 Err(e) => warn!("Failed to start: {}", e),
Alan Stokes6b2d0a82021-09-29 11:30:39 +010090 }
91
Alan Stokesd21764c2021-10-25 15:33:40 +010092 self.start_new_instance(virtualization_service)
Alan Stokes6b2d0a82021-09-29 11:30:39 +010093 }
94
Alan Stokesd21764c2021-10-25 15:33:40 +010095 fn start_existing_instance(
96 &self,
97 virtualization_service: &dyn IVirtualizationService,
98 ) -> Result<CompOsInstance> {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010099 // No point even trying if the files we need aren't there.
100 self.check_files_exist()?;
101
Alan Stokes14f07392021-09-27 14:03:31 +0100102 info!("Starting {} CompOs instance", self.instance_name);
103
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100104 let key_blob = fs::read(&self.key_blob).context("Reading private key blob")?;
105 let public_key = fs::read(&self.public_key).context("Reading public key")?;
106
Alan Stokesd21764c2021-10-25 15:33:40 +0100107 let compos_instance = self.start_vm(virtualization_service)?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100108 let service = &compos_instance.service;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100109
110 if !service.verifySigningKey(&key_blob, &public_key).context("Verifying key pair")? {
111 bail!("Key pair invalid");
112 }
113
114 // If we get this far then the instance image is valid in the current context (e.g. the
115 // current set of APEXes) and the key blob can be successfully decrypted by the VM. So the
116 // files have not been tampered with and we're good to go.
Alan Stokes92472512022-01-04 11:48:38 +0000117 service.initializeSigningKey(&key_blob).context("Loading signing key")?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100118
Alan Stokes16e027f2021-10-04 17:57:31 +0100119 Ok(compos_instance)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100120 }
121
122 fn start_new_instance(
123 &self,
124 virtualization_service: &dyn IVirtualizationService,
125 ) -> Result<CompOsInstance> {
126 info!("Creating {} CompOs instance", self.instance_name);
127
128 // Ignore failure here - the directory may already exist.
129 let _ = fs::create_dir(&self.instance_root);
130
131 self.create_instance_image(virtualization_service)?;
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800132 // Delete existing idsig files. Ignore error in case idsig doesn't exist.
Jooyung Hanccae66d2021-12-20 11:54:36 +0900133 let _ = fs::remove_file(&self.idsig);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800134 let _ = fs::remove_file(&self.idsig_manifest_apk);
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100135
Alan Stokesd21764c2021-10-25 15:33:40 +0100136 let compos_instance = self.start_vm(virtualization_service)?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100137 let service = &compos_instance.service;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100138
139 let key_data = service.generateSigningKey().context("Generating signing key")?;
140 fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?;
Alan Stokes14f07392021-09-27 14:03:31 +0100141
142 let key_result = composd_native::extract_rsa_public_key(&key_data.certificate);
143 let rsa_public_key = key_result.key;
144 if rsa_public_key.is_empty() {
145 bail!("Failed to extract public key from certificate: {}", key_result.error);
146 }
147 fs::write(&self.public_key, &rsa_public_key).context("Writing public key")?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100148
Victor Hsieh18775b12021-10-12 17:42:48 -0700149 // Unlike when starting an existing instance, we don't need to verify the key, since we
150 // just generated it and have it in memory.
Alan Stokes92472512022-01-04 11:48:38 +0000151 service.initializeSigningKey(&key_data.keyBlob).context("Loading signing key")?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100152
Alan Stokes16e027f2021-10-04 17:57:31 +0100153 Ok(compos_instance)
154 }
155
Alan Stokesd21764c2021-10-25 15:33:40 +0100156 fn start_vm(
157 &self,
158 virtualization_service: &dyn IVirtualizationService,
159 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +0100160 let instance_image = fs::OpenOptions::new()
161 .read(true)
162 .write(true)
163 .open(&self.instance_image)
164 .context("Failed to open instance image")?;
Jooyung Hanccae66d2021-12-20 11:54:36 +0900165 let vm_instance = VmInstance::start(
166 virtualization_service,
167 instance_image,
168 &self.idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800169 &self.idsig_manifest_apk,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900170 &self.vm_parameters,
171 )
172 .context("Starting VM")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100173 let service = vm_instance.get_service().context("Connecting to CompOS")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100174 Ok(CompOsInstance { vm_instance, service, lazy_service_guard: Default::default() })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100175 }
176
177 fn create_instance_image(
178 &self,
179 virtualization_service: &dyn IVirtualizationService,
180 ) -> Result<()> {
181 let instance_image = fs::OpenOptions::new()
182 .create(true)
Alan Stokes23b90ee2021-10-28 11:36:14 +0100183 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100184 .read(true)
185 .write(true)
186 .open(&self.instance_image)
187 .context("Creating instance image file")?;
188 let instance_image = ParcelFileDescriptor::new(instance_image);
189 // TODO: Where does this number come from?
190 let size = 10 * 1024 * 1024;
191 virtualization_service
192 .initializeWritablePartition(&instance_image, size, PartitionType::ANDROID_VM_INSTANCE)
193 .context("Writing instance image file")?;
194 Ok(())
195 }
196
197 fn check_files_exist(&self) -> Result<()> {
198 if !self.instance_root.is_dir() {
Alan Stokes6fc18372021-11-25 17:50:27 +0000199 bail!("Directory {:?} not found", self.instance_root)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100200 };
201 Self::check_file_exists(&self.instance_image)?;
202 Self::check_file_exists(&self.key_blob)?;
203 Self::check_file_exists(&self.public_key)?;
204 Ok(())
205 }
206
207 fn check_file_exists(file: &Path) -> Result<()> {
208 if !file.is_file() {
Alan Stokes6fc18372021-11-25 17:50:27 +0000209 bail!("File {:?} not found", file)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100210 };
211 Ok(())
212 }
213}