blob: 6946c114e0c96c8505a023532d06414468e469d6 [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::{
Jooyung Hanccae66d2021-12-20 11:54:36 +090029 COMPOS_DATA_ROOT, IDSIG_FILE, INSTANCE_IMAGE_FILE, PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010030};
31use log::{info, warn};
32use std::fs;
33use std::path::{Path, PathBuf};
34
35pub struct CompOsInstance {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010036 service: Strong<dyn ICompOsService>,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010037 #[allow(dead_code)] // Keeps VirtualizationService & the VM alive
38 vm_instance: VmInstance,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010039 #[allow(dead_code)] // Keeps composd process alive
40 lazy_service_guard: LazyServiceGuard,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010041}
42
43impl CompOsInstance {
44 pub fn get_service(&self) -> Strong<dyn ICompOsService> {
45 self.service.clone()
46 }
47}
48
49pub struct InstanceStarter {
50 instance_name: String,
51 instance_root: PathBuf,
52 instance_image: PathBuf,
Jooyung Hanccae66d2021-12-20 11:54:36 +090053 idsig: PathBuf,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010054 key_blob: PathBuf,
55 public_key: PathBuf,
Alan Stokesd21764c2021-10-25 15:33:40 +010056 vm_parameters: VmParameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010057}
58
59impl InstanceStarter {
Alan Stokesd21764c2021-10-25 15:33:40 +010060 pub fn new(instance_name: &str, vm_parameters: VmParameters) -> Self {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010061 let instance_root = Path::new(COMPOS_DATA_ROOT).join(instance_name);
Jooyung Hanccae66d2021-12-20 11:54:36 +090062 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 Stokes6b2d0a82021-09-29 11:30:39 +010067 Self {
68 instance_name: instance_name.to_owned(),
69 instance_root,
70 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +090071 idsig,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010072 key_blob,
73 public_key,
Alan Stokesd21764c2021-10-25 15:33:40 +010074 vm_parameters,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010075 }
76 }
77
78 pub fn create_or_start_instance(
79 &self,
Alan Stokesd21764c2021-10-25 15:33:40 +010080 virtualization_service: &dyn IVirtualizationService,
Alan Stokes6b2d0a82021-09-29 11:30:39 +010081 ) -> Result<CompOsInstance> {
Alan Stokesd21764c2021-10-25 15:33:40 +010082 let compos_instance = self.start_existing_instance(virtualization_service);
Alan Stokes6b2d0a82021-09-29 11:30:39 +010083 match compos_instance {
84 Ok(_) => return compos_instance,
Alan Stokes14f07392021-09-27 14:03:31 +010085 Err(e) => warn!("Failed to start: {}", e),
Alan Stokes6b2d0a82021-09-29 11:30:39 +010086 }
87
Alan Stokesd21764c2021-10-25 15:33:40 +010088 self.start_new_instance(virtualization_service)
Alan Stokes6b2d0a82021-09-29 11:30:39 +010089 }
90
Alan Stokesd21764c2021-10-25 15:33:40 +010091 fn start_existing_instance(
92 &self,
93 virtualization_service: &dyn IVirtualizationService,
94 ) -> Result<CompOsInstance> {
Alan Stokes6b2d0a82021-09-29 11:30:39 +010095 // No point even trying if the files we need aren't there.
96 self.check_files_exist()?;
97
Alan Stokes14f07392021-09-27 14:03:31 +010098 info!("Starting {} CompOs instance", self.instance_name);
99
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100100 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 Stokesd21764c2021-10-25 15:33:40 +0100103 let compos_instance = self.start_vm(virtualization_service)?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100104 let service = &compos_instance.service;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100105
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 Stokes92472512022-01-04 11:48:38 +0000113 service.initializeSigningKey(&key_blob).context("Loading signing key")?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100114
Alan Stokes16e027f2021-10-04 17:57:31 +0100115 Ok(compos_instance)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100116 }
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 Hanccae66d2021-12-20 11:54:36 +0900128 // Delete existing idsig file. Ignore error in case idsig doesn't exist.
129 let _ = fs::remove_file(&self.idsig);
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100130
Alan Stokesd21764c2021-10-25 15:33:40 +0100131 let compos_instance = self.start_vm(virtualization_service)?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100132 let service = &compos_instance.service;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100133
134 let key_data = service.generateSigningKey().context("Generating signing key")?;
135 fs::write(&self.key_blob, &key_data.keyBlob).context("Writing key blob")?;
Alan Stokes14f07392021-09-27 14:03:31 +0100136
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 Stokes6b2d0a82021-09-29 11:30:39 +0100143
Victor Hsieh18775b12021-10-12 17:42:48 -0700144 // 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 Stokes92472512022-01-04 11:48:38 +0000146 service.initializeSigningKey(&key_data.keyBlob).context("Loading signing key")?;
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100147
Alan Stokes16e027f2021-10-04 17:57:31 +0100148 Ok(compos_instance)
149 }
150
Alan Stokesd21764c2021-10-25 15:33:40 +0100151 fn start_vm(
152 &self,
153 virtualization_service: &dyn IVirtualizationService,
154 ) -> Result<CompOsInstance> {
Alan Stokes16e027f2021-10-04 17:57:31 +0100155 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 Hanccae66d2021-12-20 11:54:36 +0900160 let vm_instance = VmInstance::start(
161 virtualization_service,
162 instance_image,
163 &self.idsig,
164 &self.vm_parameters,
165 )
166 .context("Starting VM")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100167 let service = vm_instance.get_service().context("Connecting to CompOS")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100168 Ok(CompOsInstance { vm_instance, service, lazy_service_guard: Default::default() })
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100169 }
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 Stokes23b90ee2021-10-28 11:36:14 +0100177 .truncate(true)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100178 .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 Stokes6fc18372021-11-25 17:50:27 +0000193 bail!("Directory {:?} not found", self.instance_root)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100194 };
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 Stokes6fc18372021-11-25 17:50:27 +0000203 bail!("File {:?} not found", file)
Alan Stokes6b2d0a82021-09-29 11:30:39 +0100204 };
205 Ok(())
206 }
207}