blob: 53be583fdc70e1e2c7072bec0087961de8f737cc [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 Wangf7c0f942023-09-14 09:33:04 +000025use ciborium::value::Value;
David Brazdil66fc1202022-07-04 21:48:45 +010026use log::info;
Alice Wang9646fb32023-09-08 10:01:31 +000027use service_vm_comm::{
Alice Wang74eb78b2023-11-09 16:13:10 +000028 ClientVmAttestationParams, EcdsaP256KeyPair, GenerateCertificateRequestParams, Request,
29 RequestProcessingError, Response, VmType,
Alice Wang9646fb32023-09-08 10:01:31 +000030};
Alice Wang17dc76e2023-09-06 09:43:52 +000031use service_vm_manager::ServiceVm;
David Brazdil66fc1202022-07-04 21:48:45 +010032use std::fs::File;
Alice Wangf7c0f942023-09-14 09:33:04 +000033use std::io;
David Brazdil66fc1202022-07-04 21:48:45 +010034use std::panic;
Alice Wang17dc76e2023-09-06 09:43:52 +000035use std::path::PathBuf;
Alice Wang17dc76e2023-09-06 09:43:52 +000036use vmclient::VmInstance;
Alice Wang4e082c32023-07-11 07:41:50 +000037
Alice Wang9a8b39f2023-04-12 15:31:48 +000038const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
39const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
David Brazdil66fc1202022-07-04 21:48:45 +010040
Alice Wang9a8b39f2023-04-12 15:31:48 +000041#[test]
Alice Wange910b902023-09-07 10:35:12 +000042fn process_requests_in_protected_vm() -> Result<()> {
Alice Wang9646fb32023-09-08 10:01:31 +000043 check_processing_requests(VmType::ProtectedVm)
Alice Wang9a8b39f2023-04-12 15:31:48 +000044}
45
Alice Wange910b902023-09-07 10:35:12 +000046#[test]
47fn process_requests_in_non_protected_vm() -> Result<()> {
Alice Wang9646fb32023-09-08 10:01:31 +000048 check_processing_requests(VmType::NonProtectedVm)
49}
50
51fn check_processing_requests(vm_type: VmType) -> Result<()> {
52 let mut vm = start_service_vm(vm_type)?;
Alice Wange910b902023-09-07 10:35:12 +000053
54 check_processing_reverse_request(&mut vm)?;
Alice Wang74eb78b2023-11-09 16:13:10 +000055 let key_pair = check_processing_generating_key_pair_request(&mut vm)?;
56 check_processing_generating_certificate_request(&mut vm, &key_pair.maced_public_key)?;
57 check_attestation_request(&mut vm, &key_pair.key_blob)?;
Alice Wange910b902023-09-07 10:35:12 +000058 Ok(())
59}
60
61fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> {
Alice Wang0486e252023-10-06 14:30:49 +000062 let message = "abc".repeat(500);
Alice Wange910b902023-09-07 10:35:12 +000063 let request = Request::Reverse(message.as_bytes().to_vec());
64
Alice Wangfbdc85b2023-09-07 12:56:46 +000065 let response = vm.process_request(request)?;
66 info!("Received response: {response:?}.");
Alice Wange910b902023-09-07 10:35:12 +000067
68 let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect();
69 assert_eq!(Response::Reverse(expected_response), response);
70 Ok(())
71}
72
Alice Wang74eb78b2023-11-09 16:13:10 +000073fn check_processing_generating_key_pair_request(vm: &mut ServiceVm) -> Result<EcdsaP256KeyPair> {
Alice Wang9646fb32023-09-08 10:01:31 +000074 let request = Request::GenerateEcdsaP256KeyPair;
75
76 let response = vm.process_request(request)?;
77 info!("Received response: {response:?}.");
78
79 match response {
Alice Wang74eb78b2023-11-09 16:13:10 +000080 Response::GenerateEcdsaP256KeyPair(key_pair) => {
81 assert_array_has_nonzero(&key_pair.maced_public_key);
82 assert_array_has_nonzero(&key_pair.key_blob);
83 Ok(key_pair)
Alice Wanga78d3f02023-09-13 12:39:16 +000084 }
Alice Wangff5592d2023-09-13 15:27:39 +000085 _ => bail!("Incorrect response type: {response:?}"),
Alice Wang9646fb32023-09-08 10:01:31 +000086 }
87}
88
Alice Wanga78d3f02023-09-13 12:39:16 +000089fn assert_array_has_nonzero(v: &[u8]) {
90 assert!(v.iter().any(|&x| x != 0))
91}
92
Alice Wangff5592d2023-09-13 15:27:39 +000093fn check_processing_generating_certificate_request(
94 vm: &mut ServiceVm,
Alice Wang74eb78b2023-11-09 16:13:10 +000095 maced_public_key: &[u8],
Alice Wangff5592d2023-09-13 15:27:39 +000096) -> Result<()> {
97 let params = GenerateCertificateRequestParams {
Alice Wang74eb78b2023-11-09 16:13:10 +000098 keys_to_sign: vec![maced_public_key.to_vec()],
Alice Wangff5592d2023-09-13 15:27:39 +000099 challenge: vec![],
100 };
Alice Wang9646fb32023-09-08 10:01:31 +0000101 let request = Request::GenerateCertificateRequest(params);
102
103 let response = vm.process_request(request)?;
104 info!("Received response: {response:?}.");
105
106 match response {
Alice Wangf7c0f942023-09-14 09:33:04 +0000107 Response::GenerateCertificateRequest(csr) => check_csr(csr),
Alice Wangff5592d2023-09-13 15:27:39 +0000108 _ => bail!("Incorrect response type: {response:?}"),
Alice Wang9646fb32023-09-08 10:01:31 +0000109 }
110}
111
Alice Wang74eb78b2023-11-09 16:13:10 +0000112fn check_attestation_request(vm: &mut ServiceVm, key_blob: &[u8]) -> Result<()> {
113 let params =
114 ClientVmAttestationParams { csr: vec![], remotely_provisioned_key_blob: key_blob.to_vec() };
115 let request = Request::RequestClientVmAttestation(params);
116
117 let response = vm.process_request(request)?;
118 info!("Received response: {response:?}.");
119
120 match response {
121 // TODO(b/309441500): Check the certificate once it is implemented.
122 Response::Err(RequestProcessingError::OperationUnimplemented) => Ok(()),
123 _ => bail!("Incorrect response type: {response:?}"),
124 }
125}
126
Alice Wangf7c0f942023-09-14 09:33:04 +0000127/// TODO(b/300625792): Check the CSR with libhwtrust once the CSR is complete.
128fn check_csr(csr: Vec<u8>) -> Result<()> {
129 let mut reader = io::Cursor::new(csr);
130 let csr: Value = ciborium::from_reader(&mut reader)?;
131 match csr {
132 Value::Array(arr) => {
133 assert_eq!(4, arr.len());
134 }
135 _ => bail!("Incorrect CSR format: {csr:?}"),
136 }
137 Ok(())
138}
139
Alice Wange910b902023-09-07 10:35:12 +0000140fn start_service_vm(vm_type: VmType) -> Result<ServiceVm> {
David Brazdil66fc1202022-07-04 21:48:45 +0100141 android_logger::init_once(
142 android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug),
143 );
David Brazdil66fc1202022-07-04 21:48:45 +0100144 // Redirect panic messages to logcat.
145 panic::set_hook(Box::new(|panic_info| {
146 log::error!("{}", panic_info);
147 }));
David Brazdil66fc1202022-07-04 21:48:45 +0100148 // We need to start the thread pool for Binder to work properly, especially link_to_death.
149 ProcessState::start_thread_pool();
Alice Wange910b902023-09-07 10:35:12 +0000150 ServiceVm::start_vm(vm_instance(vm_type)?, vm_type)
Alice Wang17dc76e2023-09-06 09:43:52 +0000151}
152
Alice Wange910b902023-09-07 10:35:12 +0000153fn vm_instance(vm_type: VmType) -> Result<VmInstance> {
Alice Wanga6357692023-09-07 14:59:37 +0000154 match vm_type {
Alice Wang1d9a5872023-09-06 14:32:36 +0000155 VmType::ProtectedVm => {
Alice Wanga6357692023-09-07 14:59:37 +0000156 service_vm_manager::protected_vm_instance(PathBuf::from(INSTANCE_IMG_PATH))
Alice Wang1d9a5872023-09-06 14:32:36 +0000157 }
Alice Wanga6357692023-09-07 14:59:37 +0000158 VmType::NonProtectedVm => nonprotected_vm_instance(),
159 }
160}
161
162fn nonprotected_vm_instance() -> Result<VmInstance> {
163 let rialto = File::open(UNSIGNED_RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
David Brazdil66fc1202022-07-04 21:48:45 +0100164 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Alice Wanga6357692023-09-07 14:59:37 +0000165 name: String::from("Non protected rialto"),
David Brazdil66fc1202022-07-04 21:48:45 +0100166 bootloader: Some(ParcelFileDescriptor::new(rialto)),
Alice Wanga6357692023-09-07 14:59:37 +0000167 protectedVm: false,
David Brazdil66fc1202022-07-04 21:48:45 +0100168 memoryMib: 300,
David Brazdil66fc1202022-07-04 21:48:45 +0100169 platformVersion: "~1.0".to_string(),
Inseob Kim6ef80972023-07-20 17:23:36 +0900170 ..Default::default()
David Brazdil66fc1202022-07-04 21:48:45 +0100171 });
Alice Wanga6357692023-09-07 14:59:37 +0000172 let console = Some(service_vm_manager::android_log_fd()?);
173 let log = Some(service_vm_manager::android_log_fd()?);
174 let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
175 let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
176 info!("Connected to VirtMgr for service VM");
177 VmInstance::create(service.as_ref(), &config, console, /* consoleIn */ None, log, None)
178 .context("Failed to create VM")
David Brazdil66fc1202022-07-04 21:48:45 +0100179}