Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Stokes | 98a964c | 2022-02-23 11:37:46 +0000 | [diff] [blame] | 20 | use android_logger::LogId; |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 21 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 22 | use binder::ProcessState; |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 23 | use clap::{Parser, ValueEnum}; |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 24 | use compos_common::compos_client::{ComposClient, VmCpuTopology, VmParameters}; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 25 | use compos_common::odrefresh::{ |
Alan Stokes | 32d8fa5 | 2022-03-14 14:59:42 +0000 | [diff] [blame] | 26 | CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR, |
| 27 | TEST_ARTIFACTS_SUBDIR, |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 28 | }; |
| 29 | use compos_common::{ |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 30 | COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 31 | IDSIG_MANIFEST_EXT_APK_FILE, INSTANCE_ID_FILE, INSTANCE_IMAGE_FILE, TEST_INSTANCE_DIR, |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 32 | }; |
| 33 | use log::error; |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 34 | use std::fs; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 35 | use std::fs::File; |
| 36 | use std::io::Read; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 37 | use std::path::Path; |
| 38 | |
| 39 | const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024; |
| 40 | |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 41 | #[derive(Parser)] |
| 42 | struct 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, |
| 50 | } |
| 51 | |
| 52 | #[derive(ValueEnum, Clone)] |
| 53 | enum Instance { |
| 54 | Current, |
| 55 | Pending, |
| 56 | Test, |
| 57 | } |
| 58 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 59 | fn main() { |
| 60 | android_logger::init_once( |
| 61 | android_logger::Config::default() |
| 62 | .with_tag("compos_verify") |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 63 | .with_max_level(log::LevelFilter::Info) |
| 64 | .with_log_buffer(LogId::System), // Needed to log successfully early in boot |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 65 | ); |
| 66 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 67 | if let Err(e) = try_main() { |
| 68 | error!("{:?}", e); |
| 69 | std::process::exit(1) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn try_main() -> Result<()> { |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 74 | let args = Args::parse(); |
| 75 | let (instance_dir, artifacts_dir) = match args.instance { |
| 76 | Instance::Current => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR), |
| 77 | Instance::Pending => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR), |
| 78 | Instance::Test => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR), |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir); |
| 82 | let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir); |
| 83 | |
| 84 | if !instance_dir.is_dir() { |
| 85 | bail!("{:?} is not a directory", instance_dir); |
| 86 | } |
| 87 | |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 88 | let instance_id_file = instance_dir.join(INSTANCE_ID_FILE); |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 89 | let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE); |
| 90 | let idsig = instance_dir.join(IDSIG_FILE); |
| 91 | let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE); |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 92 | let idsig_manifest_ext_apk = instance_dir.join(IDSIG_MANIFEST_EXT_APK_FILE); |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 93 | |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 94 | let instance_id: [u8; 64] = if cfg!(llpvm_changes) { |
| 95 | fs::read(instance_id_file)?.try_into().map_err(|_| anyhow!("Failed to get instance_id"))? |
| 96 | } else { |
| 97 | [0u8; 64] |
| 98 | }; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 99 | let instance_image = File::open(instance_image).context("Failed to open instance image")?; |
| 100 | |
| 101 | let info = artifacts_dir.join("compos.info"); |
| 102 | let signature = artifacts_dir.join("compos.info.signature"); |
| 103 | |
Alan Stokes | dd8dfe8 | 2022-03-10 15:30:49 +0000 | [diff] [blame] | 104 | let info = read_small_file(&info).context("Failed to read compos.info")?; |
| 105 | let signature = read_small_file(&signature).context("Failed to read compos.info signature")?; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 106 | |
| 107 | // We need to start the thread pool to be able to receive Binder callbacks |
| 108 | ProcessState::start_thread_pool(); |
| 109 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 110 | let virtmgr = vmclient::VirtualizationService::new()?; |
| 111 | let virtualization_service = virtmgr.connect()?; |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 112 | let vm_instance = ComposClient::start( |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 113 | &*virtualization_service, |
Shikha Panwar | 61a74b5 | 2024-02-16 13:17:01 +0000 | [diff] [blame] | 114 | instance_id, |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 115 | instance_image, |
| 116 | &idsig, |
| 117 | &idsig_manifest_apk, |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 118 | &idsig_manifest_ext_apk, |
Victor Hsieh | 2805da9 | 2023-02-06 12:42:06 -0800 | [diff] [blame] | 119 | &VmParameters { |
Seungjae Yoo | a61a867 | 2023-04-10 14:19:14 +0900 | [diff] [blame] | 120 | name: String::from("ComposVerify"), |
Inseob Kim | 4657d0e | 2024-11-28 13:34:10 +0900 | [diff] [blame] | 121 | os: String::from("microdroid"), |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 122 | cpu_topology: VmCpuTopology::OneCpu, // This VM runs very little work at boot |
Victor Hsieh | 2805da9 | 2023-02-06 12:42:06 -0800 | [diff] [blame] | 123 | debug_mode: args.debug, |
| 124 | ..Default::default() |
| 125 | }, |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 126 | )?; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 127 | |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 128 | let service = vm_instance.connect_service()?; |
| 129 | let public_key = service.getPublicKey().context("Getting public key"); |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 130 | |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 131 | vm_instance.shutdown(service); |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 132 | |
| 133 | if !compos_verify_native::verify(&public_key?, &signature, &info) { |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 134 | bail!("Signature verification failed"); |
| 135 | } |
| 136 | |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | fn read_small_file(file: &Path) -> Result<Vec<u8>> { |
| 141 | let mut file = File::open(file)?; |
| 142 | if file.metadata()?.len() > MAX_FILE_SIZE_BYTES { |
| 143 | bail!("File is too big"); |
| 144 | } |
| 145 | let mut data = Vec::new(); |
| 146 | file.read_to_end(&mut data)?; |
| 147 | Ok(data) |
| 148 | } |
Andrew Walbran | da8786d | 2022-12-01 14:54:27 +0000 | [diff] [blame] | 149 | |
| 150 | #[cfg(test)] |
| 151 | mod tests { |
| 152 | use super::*; |
| 153 | use clap::CommandFactory; |
| 154 | |
| 155 | #[test] |
| 156 | fn verify_args() { |
| 157 | // Check that the command parsing has been configured in a valid way. |
| 158 | Args::command().debug_assert(); |
| 159 | } |
| 160 | } |