blob: 7c0d9dc5495fe5f449d48102f434e75aa6d51e66 [file] [log] [blame]
David Brazdil66fc1202022-07-04 21:48:45 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Integration test for Rialto.
16
17use android_system_virtualizationservice::{
18 aidl::android::system::virtualizationservice::{
Alice Wanga6357692023-09-07 14:59:37 +000019 VirtualMachineConfig::VirtualMachineConfig,
David Brazdil66fc1202022-07-04 21:48:45 +010020 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
Alice Wang9646fb32023-09-08 10:01:31 +000024use anyhow::{bail, Context, Result};
Alice Wangb76b66f2024-03-26 08:16:23 +000025use bssl_avf::{rand_bytes, sha256, EcKey, PKey};
Alice Wangde6bee52023-11-10 09:58:40 +000026use client_vm_csr::generate_attestation_key_and_csr;
Alice Wang20b8ebc2023-11-17 09:54:47 +000027use coset::{CborSerializable, CoseMac0, CoseSign};
Alice Wang68d11402024-01-02 13:59:44 +000028use hwtrust::{rkp, session::Session};
Nikita Ioffebd2e2e42024-07-05 15:04:49 +000029use log::{info, warn};
Alice Wang9646fb32023-09-08 10:01:31 +000030use service_vm_comm::{
Alice Wang20b8ebc2023-11-17 09:54:47 +000031 ClientVmAttestationParams, Csr, CsrPayload, EcdsaP256KeyPair, GenerateCertificateRequestParams,
Alice Wangd3a96402023-11-24 15:37:39 +000032 Request, RequestProcessingError, Response, VmType,
Alice Wang9646fb32023-09-08 10:01:31 +000033};
Alice Wang1cc13502023-12-05 11:05:34 +000034use service_vm_fake_chain::client_vm::{
35 fake_client_vm_dice_artifacts, fake_sub_components, SubComponent,
36};
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +010037use service_vm_manager::{ServiceVm, VM_MEMORY_MB};
Alice Wang20b8ebc2023-11-17 09:54:47 +000038use std::fs;
David Brazdil66fc1202022-07-04 21:48:45 +010039use std::fs::File;
David Brazdil66fc1202022-07-04 21:48:45 +010040use std::panic;
Alice Wang17dc76e2023-09-06 09:43:52 +000041use std::path::PathBuf;
Alice Wang6a504ef2023-12-21 15:37:55 +000042use std::str::FromStr;
Alice Wang17dc76e2023-09-06 09:43:52 +000043use vmclient::VmInstance;
Alice Wang6a504ef2023-12-21 15:37:55 +000044use x509_cert::{
45 certificate::{Certificate, Version},
46 der::{self, asn1, Decode, Encode},
47 name::Name,
48 spki::{AlgorithmIdentifier, ObjectIdentifier, SubjectPublicKeyInfo},
Alice Wang20b8ebc2023-11-17 09:54:47 +000049};
Alice Wang4e082c32023-07-11 07:41:50 +000050
Alice Wang9a8b39f2023-04-12 15:31:48 +000051const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
52const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
Alice Wang20b8ebc2023-11-17 09:54:47 +000053const TEST_CERT_CHAIN_PATH: &str = "testdata/rkp_cert_chain.der";
David Brazdil66fc1202022-07-04 21:48:45 +010054
Alice Wang0472f462024-02-06 08:53:19 +000055#[cfg(dice_changes)]
Alice Wang9a8b39f2023-04-12 15:31:48 +000056#[test]
Alice Wange910b902023-09-07 10:35:12 +000057fn process_requests_in_protected_vm() -> Result<()> {
Nikita Ioffebd2e2e42024-07-05 15:04:49 +000058 if hypervisor_props::is_protected_vm_supported()? {
59 // The test is skipped if the feature flag |dice_changes| is not enabled, because when
60 // the flag is off, the DICE chain is truncated in the pvmfw, and the service VM cannot
61 // verify the chain due to the missing entries in the chain.
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +010062 check_processing_requests(VmType::ProtectedVm, None)
Nikita Ioffebd2e2e42024-07-05 15:04:49 +000063 } else {
64 warn!("pVMs are not supported on device, skipping test");
65 Ok(())
66 }
Alice Wang9a8b39f2023-04-12 15:31:48 +000067}
68
Alice Wange910b902023-09-07 10:35:12 +000069#[test]
70fn process_requests_in_non_protected_vm() -> Result<()> {
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +010071 check_processing_requests(VmType::NonProtectedVm, None)
Alice Wang9646fb32023-09-08 10:01:31 +000072}
73
Pierre-Clément Tosida586152024-08-20 14:11:47 +010074#[ignore] // TODO(b/360077974): Figure out why this is flaky.
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +010075#[test]
76fn process_requests_in_non_protected_vm_with_extra_ram() -> Result<()> {
77 const MEMORY_MB: i32 = 300;
78 check_processing_requests(VmType::NonProtectedVm, Some(MEMORY_MB))
79}
80
81fn check_processing_requests(vm_type: VmType, vm_memory_mb: Option<i32>) -> Result<()> {
82 let mut vm = start_service_vm(vm_type, vm_memory_mb)?;
Alice Wange910b902023-09-07 10:35:12 +000083
84 check_processing_reverse_request(&mut vm)?;
Alice Wang74eb78b2023-11-09 16:13:10 +000085 let key_pair = check_processing_generating_key_pair_request(&mut vm)?;
Karuna Wadherac8fb3bf2024-07-01 12:54:25 +000086 check_processing_generating_certificate_request(&mut vm, &key_pair.maced_public_key)?;
Alice Wangd3a96402023-11-24 15:37:39 +000087 check_attestation_request(&mut vm, &key_pair, vm_type)?;
Alice Wange910b902023-09-07 10:35:12 +000088 Ok(())
89}
90
91fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> {
Alice Wang0486e252023-10-06 14:30:49 +000092 let message = "abc".repeat(500);
Alice Wange910b902023-09-07 10:35:12 +000093 let request = Request::Reverse(message.as_bytes().to_vec());
94
Alice Wangfbdc85b2023-09-07 12:56:46 +000095 let response = vm.process_request(request)?;
96 info!("Received response: {response:?}.");
Alice Wange910b902023-09-07 10:35:12 +000097
98 let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect();
99 assert_eq!(Response::Reverse(expected_response), response);
100 Ok(())
101}
102
Alice Wang74eb78b2023-11-09 16:13:10 +0000103fn check_processing_generating_key_pair_request(vm: &mut ServiceVm) -> Result<EcdsaP256KeyPair> {
Alice Wang9646fb32023-09-08 10:01:31 +0000104 let request = Request::GenerateEcdsaP256KeyPair;
105
106 let response = vm.process_request(request)?;
107 info!("Received response: {response:?}.");
108
109 match response {
Alice Wang74eb78b2023-11-09 16:13:10 +0000110 Response::GenerateEcdsaP256KeyPair(key_pair) => {
111 assert_array_has_nonzero(&key_pair.maced_public_key);
112 assert_array_has_nonzero(&key_pair.key_blob);
113 Ok(key_pair)
Alice Wanga78d3f02023-09-13 12:39:16 +0000114 }
Alice Wangff5592d2023-09-13 15:27:39 +0000115 _ => bail!("Incorrect response type: {response:?}"),
Alice Wang9646fb32023-09-08 10:01:31 +0000116 }
117}
118
Alice Wanga78d3f02023-09-13 12:39:16 +0000119fn assert_array_has_nonzero(v: &[u8]) {
120 assert!(v.iter().any(|&x| x != 0))
121}
122
Alice Wangff5592d2023-09-13 15:27:39 +0000123fn check_processing_generating_certificate_request(
124 vm: &mut ServiceVm,
Alice Wang74eb78b2023-11-09 16:13:10 +0000125 maced_public_key: &[u8],
Alice Wangff5592d2023-09-13 15:27:39 +0000126) -> Result<()> {
127 let params = GenerateCertificateRequestParams {
Alice Wang74eb78b2023-11-09 16:13:10 +0000128 keys_to_sign: vec![maced_public_key.to_vec()],
Alice Wangff5592d2023-09-13 15:27:39 +0000129 challenge: vec![],
130 };
Alice Wang9646fb32023-09-08 10:01:31 +0000131 let request = Request::GenerateCertificateRequest(params);
132
133 let response = vm.process_request(request)?;
134 info!("Received response: {response:?}.");
135
136 match response {
Karuna Wadherac8fb3bf2024-07-01 12:54:25 +0000137 Response::GenerateCertificateRequest(csr) => check_csr(csr),
Alice Wangff5592d2023-09-13 15:27:39 +0000138 _ => bail!("Incorrect response type: {response:?}"),
Alice Wang9646fb32023-09-08 10:01:31 +0000139 }
140}
141
Alice Wang20b8ebc2023-11-17 09:54:47 +0000142fn check_attestation_request(
143 vm: &mut ServiceVm,
144 remotely_provisioned_key_pair: &EcdsaP256KeyPair,
Alice Wangd3a96402023-11-24 15:37:39 +0000145 vm_type: VmType,
Alice Wang20b8ebc2023-11-17 09:54:47 +0000146) -> Result<()> {
Alice Wangde6bee52023-11-10 09:58:40 +0000147 /// The following data was generated randomly with urandom.
148 const CHALLENGE: [u8; 16] = [
149 0x7d, 0x86, 0x58, 0x79, 0x3a, 0x09, 0xdf, 0x1c, 0xa5, 0x80, 0x80, 0x15, 0x2b, 0x13, 0x17,
150 0x5c,
151 ];
Alice Wang4ac9c8b2023-12-05 16:23:14 +0000152 let dice_artifacts = fake_client_vm_dice_artifacts()?;
Alice Wangde6bee52023-11-10 09:58:40 +0000153 let attestation_data = generate_attestation_key_and_csr(&CHALLENGE, &dice_artifacts)?;
Alice Wang20b8ebc2023-11-17 09:54:47 +0000154 let cert_chain = fs::read(TEST_CERT_CHAIN_PATH)?;
Alice Wang6a504ef2023-12-21 15:37:55 +0000155 // The certificate chain contains several certificates, but we only need the first one.
156 // Parsing the data with trailing data always fails with a `TrailingData` error.
157 let cert_len: usize = match Certificate::from_der(&cert_chain).unwrap_err().kind() {
158 der::ErrorKind::TrailingData { decoded, .. } => decoded.try_into().unwrap(),
159 e => bail!("Unexpected error: {e}"),
160 };
Alice Wangde6bee52023-11-10 09:58:40 +0000161
Alice Wang20b8ebc2023-11-17 09:54:47 +0000162 // Builds the mock parameters for the client VM attestation.
163 // The `csr` and `remotely_provisioned_key_blob` parameters are extracted from the same
164 // libraries as in production.
165 // The `remotely_provisioned_cert` parameter is an RKP certificate extracted from a test
166 // certificate chain retrieved from RKPD.
Alice Wangde6bee52023-11-10 09:58:40 +0000167 let params = ClientVmAttestationParams {
Alice Wang20b8ebc2023-11-17 09:54:47 +0000168 csr: attestation_data.csr.clone().into_cbor_vec()?,
169 remotely_provisioned_key_blob: remotely_provisioned_key_pair.key_blob.to_vec(),
Alice Wang6a504ef2023-12-21 15:37:55 +0000170 remotely_provisioned_cert: cert_chain[..cert_len].to_vec(),
Alice Wangde6bee52023-11-10 09:58:40 +0000171 };
Alice Wang74eb78b2023-11-09 16:13:10 +0000172 let request = Request::RequestClientVmAttestation(params);
173
174 let response = vm.process_request(request)?;
175 info!("Received response: {response:?}.");
176
177 match response {
Alice Wang20b8ebc2023-11-17 09:54:47 +0000178 Response::RequestClientVmAttestation(certificate) => {
Alice Wangd3a96402023-11-24 15:37:39 +0000179 // The end-to-end test for non-protected VM attestation works because both the service
180 // VM and the client VM use the same fake DICE chain.
181 assert_eq!(vm_type, VmType::NonProtectedVm);
Alice Wang20b8ebc2023-11-17 09:54:47 +0000182 check_certificate_for_client_vm(
183 &certificate,
184 &remotely_provisioned_key_pair.maced_public_key,
185 &attestation_data.csr,
Alice Wang6a504ef2023-12-21 15:37:55 +0000186 &Certificate::from_der(&cert_chain[..cert_len]).unwrap(),
Alice Wang20b8ebc2023-11-17 09:54:47 +0000187 )?;
188 Ok(())
189 }
Alice Wangd3a96402023-11-24 15:37:39 +0000190 Response::Err(RequestProcessingError::InvalidDiceChain) => {
191 // The end-to-end test for protected VM attestation doesn't work because the service VM
192 // compares the fake DICE chain in the CSR with the real DICE chain.
193 // We cannot generate a valid DICE chain with the same payloads up to pvmfw.
194 assert_eq!(vm_type, VmType::ProtectedVm);
195 Ok(())
196 }
Alice Wang74eb78b2023-11-09 16:13:10 +0000197 _ => bail!("Incorrect response type: {response:?}"),
198 }
199}
200
Alice Wang6a504ef2023-12-21 15:37:55 +0000201fn check_vm_components(vm_components: &asn1::SequenceOf<asn1::Any, 4>) -> Result<()> {
Alice Wang1cc13502023-12-05 11:05:34 +0000202 let expected_components = fake_sub_components();
203 assert_eq!(expected_components.len(), vm_components.len());
Alice Wang6a504ef2023-12-21 15:37:55 +0000204 for (i, expected_component) in expected_components.iter().enumerate() {
205 check_vm_component(vm_components.get(i).unwrap(), expected_component)?;
Alice Wang1cc13502023-12-05 11:05:34 +0000206 }
207 Ok(())
208}
209
Alice Wang6a504ef2023-12-21 15:37:55 +0000210fn check_vm_component(vm_component: &asn1::Any, expected_component: &SubComponent) -> Result<()> {
211 let vm_component = vm_component.decode_as::<asn1::SequenceOf<asn1::Any, 4>>().unwrap();
Alice Wang1cc13502023-12-05 11:05:34 +0000212 assert_eq!(4, vm_component.len());
Alice Wang6a504ef2023-12-21 15:37:55 +0000213 let name = vm_component.get(0).unwrap().decode_as::<asn1::Utf8StringRef>().unwrap();
214 assert_eq!(expected_component.name, name.as_ref());
215 let version = vm_component.get(1).unwrap().decode_as::<u64>().unwrap();
216 assert_eq!(expected_component.version, version);
217 let code_hash = vm_component.get(2).unwrap().decode_as::<asn1::OctetString>().unwrap();
218 assert_eq!(expected_component.code_hash, code_hash.as_bytes());
219 let authority_hash = vm_component.get(3).unwrap().decode_as::<asn1::OctetString>().unwrap();
220 assert_eq!(expected_component.authority_hash, authority_hash.as_bytes());
Alice Wang1cc13502023-12-05 11:05:34 +0000221 Ok(())
222}
223
Alice Wang20b8ebc2023-11-17 09:54:47 +0000224fn check_certificate_for_client_vm(
225 certificate: &[u8],
226 maced_public_key: &[u8],
227 csr: &Csr,
Alice Wang6a504ef2023-12-21 15:37:55 +0000228 parent_certificate: &Certificate,
Alice Wang20b8ebc2023-11-17 09:54:47 +0000229) -> Result<()> {
230 let cose_mac = CoseMac0::from_slice(maced_public_key)?;
Alice Wangbe7a4b12023-12-01 11:53:36 +0000231 let authority_public_key =
232 EcKey::from_cose_public_key_slice(&cose_mac.payload.unwrap()).unwrap();
Alice Wang6a504ef2023-12-21 15:37:55 +0000233 let cert = Certificate::from_der(certificate).unwrap();
Alice Wang20b8ebc2023-11-17 09:54:47 +0000234
235 // Checks the certificate signature against the authority public key.
Alice Wang6a504ef2023-12-21 15:37:55 +0000236 const ECDSA_WITH_SHA_256: ObjectIdentifier =
237 ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2");
238 let expected_algorithm = AlgorithmIdentifier { oid: ECDSA_WITH_SHA_256, parameters: None };
Alice Wang20b8ebc2023-11-17 09:54:47 +0000239 assert_eq!(expected_algorithm, cert.signature_algorithm);
Alice Wang6a504ef2023-12-21 15:37:55 +0000240 let tbs_cert = cert.tbs_certificate;
241 let digest = sha256(&tbs_cert.to_der().unwrap()).unwrap();
Alice Wang20b8ebc2023-11-17 09:54:47 +0000242 authority_public_key
Alan Stokesb2f52fb2024-05-09 10:12:55 +0100243 .ecdsa_verify_der(cert.signature.raw_bytes(), &digest)
Alice Wang20b8ebc2023-11-17 09:54:47 +0000244 .expect("Failed to verify the certificate signature with the authority public key");
245
246 // Checks that the certificate's subject public key is equal to the key in the CSR.
247 let cose_sign = CoseSign::from_slice(&csr.signed_csr_payload)?;
248 let csr_payload =
249 cose_sign.payload.as_ref().and_then(|v| CsrPayload::from_cbor_slice(v).ok()).unwrap();
Alice Wangbe7a4b12023-12-01 11:53:36 +0000250 let subject_public_key = EcKey::from_cose_public_key_slice(&csr_payload.public_key).unwrap();
Alice Wang20b8ebc2023-11-17 09:54:47 +0000251 let expected_spki_data =
Alice Wangeb77f7d2023-12-01 09:13:58 +0000252 PKey::try_from(subject_public_key).unwrap().subject_public_key_info().unwrap();
Alice Wang6a504ef2023-12-21 15:37:55 +0000253 let expected_spki = SubjectPublicKeyInfo::from_der(&expected_spki_data).unwrap();
254 assert_eq!(expected_spki, tbs_cert.subject_public_key_info);
Alice Wang20b8ebc2023-11-17 09:54:47 +0000255
256 // Checks the certificate extension.
Alice Wang6a504ef2023-12-21 15:37:55 +0000257 const ATTESTATION_EXTENSION_OID: ObjectIdentifier =
258 ObjectIdentifier::new_unwrap("1.3.6.1.4.1.11129.2.1.29.1");
259 let extensions = tbs_cert.extensions.unwrap();
Alice Wang20b8ebc2023-11-17 09:54:47 +0000260 assert_eq!(1, extensions.len());
261 let extension = &extensions[0];
Alice Wang6a504ef2023-12-21 15:37:55 +0000262 assert_eq!(ATTESTATION_EXTENSION_OID, extension.extn_id);
Alice Wang20b8ebc2023-11-17 09:54:47 +0000263 assert!(!extension.critical);
Alice Wang6a504ef2023-12-21 15:37:55 +0000264 let attestation_ext =
265 asn1::SequenceOf::<asn1::Any, 3>::from_der(extension.extn_value.as_bytes()).unwrap();
Alice Wang1cc13502023-12-05 11:05:34 +0000266 assert_eq!(3, attestation_ext.len());
Alice Wang6a504ef2023-12-21 15:37:55 +0000267 let challenge = attestation_ext.get(0).unwrap().decode_as::<asn1::OctetString>().unwrap();
268 assert_eq!(csr_payload.challenge, challenge.as_bytes());
269 let is_vm_secure = attestation_ext.get(1).unwrap().decode_as::<bool>().unwrap();
Alice Wangd3a96402023-11-24 15:37:39 +0000270 assert!(
271 !is_vm_secure,
272 "The VM shouldn't be secure as the last payload added in the test is in Debug mode"
273 );
Alice Wang6a504ef2023-12-21 15:37:55 +0000274 let vm_components =
275 attestation_ext.get(2).unwrap().decode_as::<asn1::SequenceOf<asn1::Any, 4>>().unwrap();
276 check_vm_components(&vm_components)?;
Alice Wang20b8ebc2023-11-17 09:54:47 +0000277
278 // Checks other fields on the certificate
Alice Wang6a504ef2023-12-21 15:37:55 +0000279 assert_eq!(Version::V3, tbs_cert.version);
280 assert_eq!(parent_certificate.tbs_certificate.validity, tbs_cert.validity);
Alice Wang20b8ebc2023-11-17 09:54:47 +0000281 assert_eq!(
Alice Wang6a504ef2023-12-21 15:37:55 +0000282 Name::from_str("CN=Android Protected Virtual Machine Key").unwrap(),
283 tbs_cert.subject
Alice Wang20b8ebc2023-11-17 09:54:47 +0000284 );
Alice Wang6a504ef2023-12-21 15:37:55 +0000285 assert_eq!(parent_certificate.tbs_certificate.subject, tbs_cert.issuer);
Alice Wang20b8ebc2023-11-17 09:54:47 +0000286
287 Ok(())
288}
289
Karuna Wadherac8fb3bf2024-07-01 12:54:25 +0000290fn check_csr(csr: Vec<u8>) -> Result<()> {
291 let _csr = rkp::Csr::from_cbor(&Session::default(), &csr[..]).context("Failed to parse CSR")?;
Alice Wangf7c0f942023-09-14 09:33:04 +0000292 Ok(())
293}
294
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100295fn start_service_vm(vm_type: VmType, vm_memory_mb: Option<i32>) -> Result<ServiceVm> {
David Brazdil66fc1202022-07-04 21:48:45 +0100296 android_logger::init_once(
Jeff Vander Stoepd9dda0c2024-02-07 14:27:06 +0100297 android_logger::Config::default()
298 .with_tag("rialto")
299 .with_max_level(log::LevelFilter::Debug),
David Brazdil66fc1202022-07-04 21:48:45 +0100300 );
David Brazdil66fc1202022-07-04 21:48:45 +0100301 // Redirect panic messages to logcat.
302 panic::set_hook(Box::new(|panic_info| {
303 log::error!("{}", panic_info);
304 }));
David Brazdil66fc1202022-07-04 21:48:45 +0100305 // We need to start the thread pool for Binder to work properly, especially link_to_death.
306 ProcessState::start_thread_pool();
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100307 ServiceVm::start_vm(vm_instance(vm_type, vm_memory_mb)?, vm_type)
Alice Wang17dc76e2023-09-06 09:43:52 +0000308}
309
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100310fn vm_instance(vm_type: VmType, vm_memory_mb: Option<i32>) -> Result<VmInstance> {
Alice Wanga6357692023-09-07 14:59:37 +0000311 match vm_type {
Alice Wang1d9a5872023-09-06 14:32:36 +0000312 VmType::ProtectedVm => {
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100313 assert!(vm_memory_mb.is_none());
Alice Wanga6357692023-09-07 14:59:37 +0000314 service_vm_manager::protected_vm_instance(PathBuf::from(INSTANCE_IMG_PATH))
Alice Wang1d9a5872023-09-06 14:32:36 +0000315 }
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100316 VmType::NonProtectedVm => nonprotected_vm_instance(vm_memory_mb.unwrap_or(VM_MEMORY_MB)),
Alice Wanga6357692023-09-07 14:59:37 +0000317 }
318}
319
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100320fn nonprotected_vm_instance(memory_mib: i32) -> Result<VmInstance> {
Alice Wanga6357692023-09-07 14:59:37 +0000321 let rialto = File::open(UNSIGNED_RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
Alice Wangb76b66f2024-03-26 08:16:23 +0000322 // Do not use `#allocateInstanceId` to generate the instance ID because the method
323 // also adds an instance ID to the database it manages.
324 // This is not necessary for this test.
325 let mut instance_id = [0u8; 64];
326 rand_bytes(&mut instance_id).unwrap();
David Brazdil66fc1202022-07-04 21:48:45 +0100327 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100328 name: format!("Non protected rialto ({memory_mib}MiB)"),
Frederick Mayle75842402024-08-05 19:32:08 -0700329 kernel: Some(ParcelFileDescriptor::new(rialto)),
Alice Wanga6357692023-09-07 14:59:37 +0000330 protectedVm: false,
Pierre-Clément Tosi311de8e2024-08-14 10:10:49 +0100331 memoryMib: memory_mib,
David Brazdil66fc1202022-07-04 21:48:45 +0100332 platformVersion: "~1.0".to_string(),
Alice Wangb76b66f2024-03-26 08:16:23 +0000333 instanceId: instance_id,
Inseob Kim6ef80972023-07-20 17:23:36 +0900334 ..Default::default()
David Brazdil66fc1202022-07-04 21:48:45 +0100335 });
Alice Wanga6357692023-09-07 14:59:37 +0000336 let console = Some(service_vm_manager::android_log_fd()?);
337 let log = Some(service_vm_manager::android_log_fd()?);
338 let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
339 let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
340 info!("Connected to VirtMgr for service VM");
341 VmInstance::create(service.as_ref(), &config, console, /* consoleIn */ None, log, None)
342 .context("Failed to create VM")
David Brazdil66fc1202022-07-04 21:48:45 +0100343}