blob: 85e4425fa79b48e4691caba0b62376e2a2f2349d [file] [log] [blame]
Alan Stokes16fb8552022-02-10 15:07:27 +00001/*
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//! A tool to verify a CompOS signature. It starts a CompOS VM as part of this to retrieve the
18//! public key. The tool is intended to be run by odsign during boot.
19
Alan Stokes98a964c2022-02-23 11:37:46 +000020use android_logger::LogId;
Shikha Panwar61a74b52024-02-16 13:17:01 +000021use anyhow::{anyhow, bail, Context, Result};
Alan Stokes0e82b502022-08-08 14:44:48 +010022use binder::ProcessState;
Victor Hsieh1732c0d2022-09-12 14:36:03 -070023use clap::{Parser, ValueEnum};
David Brazdil7d1e5ec2023-02-06 17:56:29 +000024use compos_common::compos_client::{ComposClient, VmCpuTopology, VmParameters};
Alan Stokes16fb8552022-02-10 15:07:27 +000025use compos_common::odrefresh::{
Alan Stokes32d8fa52022-03-14 14:59:42 +000026 CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR,
27 TEST_ARTIFACTS_SUBDIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000028};
29use compos_common::{
Alan Stokes6542fdd2022-02-17 15:21:46 +000030 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE,
Shikha Panwar61a74b52024-02-16 13:17:01 +000031 IDSIG_MANIFEST_EXT_APK_FILE, INSTANCE_ID_FILE, INSTANCE_IMAGE_FILE, TEST_INSTANCE_DIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000032};
33use log::error;
Shikha Panwar61a74b52024-02-16 13:17:01 +000034use std::fs;
Alan Stokes16fb8552022-02-10 15:07:27 +000035use std::fs::File;
36use std::io::Read;
Alan Stokes16fb8552022-02-10 15:07:27 +000037use std::path::Path;
38
39const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
40
Victor Hsieh1732c0d2022-09-12 14:36:03 -070041#[derive(Parser)]
42struct Args {
43 /// Type of the VM instance
44 #[clap(long, value_enum)]
45 instance: Instance,
46
47 /// Starts the VM in debug mode
48 #[clap(long, action)]
49 debug: bool,
Inseob Kim18408a72025-02-22 08:28:52 +090050
51 /// OS for the VM.
52 #[clap(long, default_value = "microdroid")]
53 os: String,
Victor Hsieh1732c0d2022-09-12 14:36:03 -070054}
55
56#[derive(ValueEnum, Clone)]
57enum Instance {
58 Current,
59 Pending,
60 Test,
61}
62
Alan Stokes16fb8552022-02-10 15:07:27 +000063fn main() {
64 android_logger::init_once(
65 android_logger::Config::default()
66 .with_tag("compos_verify")
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010067 .with_max_level(log::LevelFilter::Info)
68 .with_log_buffer(LogId::System), // Needed to log successfully early in boot
Alan Stokes16fb8552022-02-10 15:07:27 +000069 );
70
Alan Stokes16fb8552022-02-10 15:07:27 +000071 if let Err(e) = try_main() {
72 error!("{:?}", e);
73 std::process::exit(1)
74 }
75}
76
77fn try_main() -> Result<()> {
Victor Hsieh1732c0d2022-09-12 14:36:03 -070078 let args = Args::parse();
79 let (instance_dir, artifacts_dir) = match args.instance {
80 Instance::Current => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
81 Instance::Pending => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
82 Instance::Test => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
Alan Stokes16fb8552022-02-10 15:07:27 +000083 };
84
85 let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
86 let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir);
87
88 if !instance_dir.is_dir() {
89 bail!("{:?} is not a directory", instance_dir);
90 }
91
Shikha Panwar61a74b52024-02-16 13:17:01 +000092 let instance_id_file = instance_dir.join(INSTANCE_ID_FILE);
Alan Stokes16fb8552022-02-10 15:07:27 +000093 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
94 let idsig = instance_dir.join(IDSIG_FILE);
95 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
Victor Hsieha61ec2e2022-09-21 16:25:27 -070096 let idsig_manifest_ext_apk = instance_dir.join(IDSIG_MANIFEST_EXT_APK_FILE);
Alan Stokes16fb8552022-02-10 15:07:27 +000097
Shikha Panwar61a74b52024-02-16 13:17:01 +000098 let instance_id: [u8; 64] = if cfg!(llpvm_changes) {
99 fs::read(instance_id_file)?.try_into().map_err(|_| anyhow!("Failed to get instance_id"))?
100 } else {
101 [0u8; 64]
102 };
Alan Stokes16fb8552022-02-10 15:07:27 +0000103 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
104
105 let info = artifacts_dir.join("compos.info");
106 let signature = artifacts_dir.join("compos.info.signature");
107
Alan Stokesdd8dfe82022-03-10 15:30:49 +0000108 let info = read_small_file(&info).context("Failed to read compos.info")?;
109 let signature = read_small_file(&signature).context("Failed to read compos.info signature")?;
Alan Stokes16fb8552022-02-10 15:07:27 +0000110
111 // We need to start the thread pool to be able to receive Binder callbacks
112 ProcessState::start_thread_pool();
113
David Brazdil4b4c5102022-12-19 22:56:20 +0000114 let virtmgr = vmclient::VirtualizationService::new()?;
115 let virtualization_service = virtmgr.connect()?;
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000116 let vm_instance = ComposClient::start(
Alan Stokes16fb8552022-02-10 15:07:27 +0000117 &*virtualization_service,
Shikha Panwar61a74b52024-02-16 13:17:01 +0000118 instance_id,
Alan Stokes16fb8552022-02-10 15:07:27 +0000119 instance_image,
120 &idsig,
121 &idsig_manifest_apk,
Victor Hsieha61ec2e2022-09-21 16:25:27 -0700122 &idsig_manifest_ext_apk,
Victor Hsieh2805da92023-02-06 12:42:06 -0800123 &VmParameters {
Seungjae Yooa61a8672023-04-10 14:19:14 +0900124 name: String::from("ComposVerify"),
Inseob Kim18408a72025-02-22 08:28:52 +0900125 os: args.os,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000126 cpu_topology: VmCpuTopology::OneCpu, // This VM runs very little work at boot
Victor Hsieh2805da92023-02-06 12:42:06 -0800127 debug_mode: args.debug,
128 ..Default::default()
129 },
Alan Stokes16fb8552022-02-10 15:07:27 +0000130 )?;
Alan Stokes16fb8552022-02-10 15:07:27 +0000131
Alan Stokes71403772022-06-21 14:56:28 +0100132 let service = vm_instance.connect_service()?;
133 let public_key = service.getPublicKey().context("Getting public key");
Alan Stokes16fb8552022-02-10 15:07:27 +0000134
Alan Stokes0fc6ce52022-08-02 17:01:48 +0100135 vm_instance.shutdown(service);
Alan Stokes71403772022-06-21 14:56:28 +0100136
137 if !compos_verify_native::verify(&public_key?, &signature, &info) {
Alan Stokes16fb8552022-02-10 15:07:27 +0000138 bail!("Signature verification failed");
139 }
140
141 Ok(())
142}
143
144fn read_small_file(file: &Path) -> Result<Vec<u8>> {
145 let mut file = File::open(file)?;
146 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
147 bail!("File is too big");
148 }
149 let mut data = Vec::new();
150 file.read_to_end(&mut data)?;
151 Ok(data)
152}
Andrew Walbranda8786d2022-12-01 14:54:27 +0000153
154#[cfg(test)]
155mod tests {
156 use super::*;
157 use clap::CommandFactory;
158
159 #[test]
160 fn verify_args() {
161 // Check that the command parsing has been configured in a valid way.
162 Args::command().debug_assert();
163 }
164}